ceph-csi/pkg/cephfs/util.go

132 lines
3.0 KiB
Go
Raw Normal View History

2018-03-05 11:59:47 +00:00
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cephfs
import (
2018-04-13 12:34:48 +00:00
"bytes"
"encoding/json"
"fmt"
2018-03-05 11:59:47 +00:00
"os/exec"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/kubernetes/pkg/util/keymutex"
"k8s.io/kubernetes/pkg/util/mount"
)
func execCommand(command string, args ...string) ([]byte, error) {
cmd := exec.Command(command, args...)
return cmd.CombinedOutput()
}
2018-04-13 12:34:48 +00:00
func execCommandAndValidate(program string, args ...string) error {
out, err := execCommand(program, args...)
if err != nil {
return fmt.Errorf("cephfs: %s failed with following error: %s\ncephfs: %s output: %s", program, err, program, out)
}
return nil
}
func execCommandJson(v interface{}, program string, args ...string) error {
cmd := exec.Command(program, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return err
}
return json.NewDecoder(bytes.NewReader(out)).Decode(v)
}
2018-03-05 11:59:47 +00:00
func isMountPoint(p string) (bool, error) {
notMnt, err := mount.New("").IsLikelyNotMountPoint(p)
if err != nil {
return false, status.Error(codes.Internal, err.Error())
}
return !notMnt, nil
}
func tryLock(id string, mtx keymutex.KeyMutex, name string) error {
// TODO uncomment this once TryLockKey gets into Kubernetes
/*
if !mtx.TryLockKey(id) {
msg := fmt.Sprintf("%s has a pending operation on %s", name, req.GetVolumeId())
glog.Infoln(msg)
return status.Error(codes.Aborted, msg)
}
*/
return nil
}
2018-04-13 12:34:48 +00:00
func storeCephUserCredentials(volUuid string, cr *credentials, volOptions *volumeOptions) error {
keyringData := cephKeyringData{
UserId: cr.id,
Key: cr.key,
RootPath: volOptions.RootPath,
}
2018-04-13 12:34:48 +00:00
if volOptions.ProvisionVolume {
keyringData.Pool = volOptions.Pool
keyringData.Namespace = getVolumeNamespace(volUuid)
}
return storeCephCredentials(cr, &keyringData)
}
2018-03-26 13:00:28 +00:00
2018-04-13 12:34:48 +00:00
func storeCephAdminCredentials(cr *credentials) error {
return storeCephCredentials(cr, &cephFullCapsKeyringData{UserId: cr.id, Key: cr.key})
}
2018-03-26 13:00:28 +00:00
2018-04-13 12:34:48 +00:00
func storeCephCredentials(cr *credentials, keyringData cephConfigWriter) error {
if err := keyringData.writeToFile(); err != nil {
return err
}
2018-03-26 13:00:28 +00:00
2018-04-13 12:34:48 +00:00
secret := cephSecretData{
UserId: cr.id,
Key: cr.key,
}
2018-03-26 13:00:28 +00:00
2018-04-13 12:34:48 +00:00
if err := secret.writeToFile(); err != nil {
return err
}
2018-03-26 13:00:28 +00:00
2018-04-13 12:34:48 +00:00
return nil
}
func newMounter(volOptions *volumeOptions) volumeMounter {
mounter := volOptions.Mounter
2018-03-26 13:00:28 +00:00
2018-04-13 12:34:48 +00:00
if mounter == "" {
mounter = DefaultVolumeMounter
2018-03-26 13:00:28 +00:00
}
2018-04-13 12:34:48 +00:00
switch mounter {
case volumeMounter_fuse:
return &fuseMounter{}
case volumeMounter_kernel:
return &kernelMounter{}
}
return nil
2018-03-26 13:00:28 +00:00
}