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"
|
2018-03-20 15:14:14 +00:00
|
|
|
"fmt"
|
2018-03-05 11:59:47 +00:00
|
|
|
"os/exec"
|
|
|
|
|
2018-07-31 06:31:11 +00:00
|
|
|
"github.com/golang/glog"
|
2018-03-05 11:59:47 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2018-11-24 18:48:36 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2018-03-05 11:59:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
|
|
|
)
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
type volumeID string
|
|
|
|
|
2018-12-05 02:39:00 +00:00
|
|
|
func makeVolumeID(volName string) volumeID {
|
|
|
|
return volumeID("csi-cephfs-" + volName)
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 11:59:47 +00:00
|
|
|
func execCommand(command string, args ...string) ([]byte, error) {
|
2018-07-28 08:24:07 +00:00
|
|
|
glog.V(4).Infof("cephfs: EXEC %s %s", command, args)
|
|
|
|
|
2018-03-05 11:59:47 +00:00
|
|
|
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 {
|
2018-08-28 08:19:28 +00:00
|
|
|
out, err := execCommand(program, args...)
|
2018-04-13 12:34:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-08-08 15:39:19 +00:00
|
|
|
return fmt.Errorf("cephfs: %s failed with following error: %s\ncephfs: %s output: %s", program, err, program, out)
|
2018-04-13 12:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewDecoder(bytes.NewReader(out)).Decode(v)
|
|
|
|
}
|
|
|
|
|
2018-08-28 08:19:28 +00:00
|
|
|
// Used in isMountPoint()
|
|
|
|
var dummyMount = mount.New("")
|
|
|
|
|
2018-03-05 11:59:47 +00:00
|
|
|
func isMountPoint(p string) (bool, error) {
|
2018-08-28 08:19:28 +00:00
|
|
|
notMnt, err := dummyMount.IsLikelyNotMountPoint(p)
|
2018-03-05 11:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return !notMnt, nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
func storeCephCredentials(volId volumeID, cr *credentials) error {
|
2018-04-13 12:34:48 +00:00
|
|
|
keyringData := cephKeyringData{
|
2018-07-28 08:24:07 +00:00
|
|
|
UserId: cr.id,
|
|
|
|
Key: cr.key,
|
|
|
|
VolumeID: volId,
|
2018-03-20 15:14:14 +00:00
|
|
|
}
|
2018-04-13 12:34:48 +00:00
|
|
|
|
|
|
|
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{
|
2018-07-28 08:24:07 +00:00
|
|
|
UserId: cr.id,
|
|
|
|
Key: cr.key,
|
|
|
|
VolumeID: volId,
|
2018-04-13 12:34:48 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
//
|
|
|
|
// Controller service request validation
|
|
|
|
//
|
|
|
|
|
|
|
|
func (cs *controllerServer) validateCreateVolumeRequest(req *csi.CreateVolumeRequest) error {
|
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
|
|
|
|
return fmt.Errorf("invalid CreateVolumeRequest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetName() == "" {
|
|
|
|
return status.Error(codes.InvalidArgument, "Volume Name cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetVolumeCapabilities() == nil {
|
|
|
|
return status.Error(codes.InvalidArgument, "Volume Capabilities cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *controllerServer) validateDeleteVolumeRequest(req *csi.DeleteVolumeRequest) error {
|
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
|
|
|
|
return fmt.Errorf("invalid DeleteVolumeRequest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Node service request validation
|
|
|
|
//
|
|
|
|
|
|
|
|
func validateNodeStageVolumeRequest(req *csi.NodeStageVolumeRequest) error {
|
|
|
|
if req.GetVolumeCapability() == nil {
|
|
|
|
return fmt.Errorf("volume capability missing in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetVolumeId() == "" {
|
|
|
|
return fmt.Errorf("volume ID missing in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetStagingTargetPath() == "" {
|
|
|
|
return fmt.Errorf("staging target path missing in request")
|
|
|
|
}
|
|
|
|
|
2018-11-24 18:48:36 +00:00
|
|
|
if req.GetSecrets() == nil || len(req.GetSecrets()) == 0 {
|
2018-07-28 08:24:07 +00:00
|
|
|
return fmt.Errorf("stage secrets cannot be nil or empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNodeUnstageVolumeRequest(req *csi.NodeUnstageVolumeRequest) error {
|
|
|
|
if req.GetVolumeId() == "" {
|
|
|
|
return fmt.Errorf("volume ID missing in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetStagingTargetPath() == "" {
|
|
|
|
return fmt.Errorf("staging target path missing in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNodePublishVolumeRequest(req *csi.NodePublishVolumeRequest) error {
|
|
|
|
if req.GetVolumeCapability() == nil {
|
|
|
|
return fmt.Errorf("volume capability missing in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetVolumeId() == "" {
|
|
|
|
return fmt.Errorf("volume ID missing in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetTargetPath() == "" {
|
|
|
|
return fmt.Errorf("varget path missing in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNodeUnpublishVolumeRequest(req *csi.NodeUnpublishVolumeRequest) error {
|
|
|
|
if req.GetVolumeId() == "" {
|
|
|
|
return fmt.Errorf("volume ID missing in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetTargetPath() == "" {
|
|
|
|
return fmt.Errorf("target path missing in request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|