2018-03-05 11:59:47 +00:00
|
|
|
/*
|
2019-04-03 08:46:15 +00:00
|
|
|
Copyright 2018 The Ceph-CSI Authors.
|
2018-03-05 11:59:47 +00:00
|
|
|
|
|
|
|
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 (
|
2019-02-26 10:06:16 +00:00
|
|
|
"bytes"
|
2018-04-13 12:34:48 +00:00
|
|
|
"encoding/json"
|
2019-01-16 13:17:29 +00:00
|
|
|
"errors"
|
2018-03-20 15:14:14 +00:00
|
|
|
"fmt"
|
2019-02-26 10:06:16 +00:00
|
|
|
"os"
|
2018-03-05 11:59:47 +00:00
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-02-04 13:04:11 +00:00
|
|
|
"k8s.io/klog"
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2019-02-14 10:39:07 +00:00
|
|
|
"github.com/ceph/ceph-csi/pkg/util"
|
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
|
|
|
|
|
2019-02-14 10:39:07 +00:00
|
|
|
func execCommand(program string, args ...string) (stdout, stderr []byte, err error) {
|
2019-02-26 10:06:16 +00:00
|
|
|
var (
|
|
|
|
cmd = exec.Command(program, args...) // nolint: gosec
|
|
|
|
sanitizedArgs = util.StripSecretInArgs(args)
|
|
|
|
stdoutBuf bytes.Buffer
|
|
|
|
stderrBuf bytes.Buffer
|
|
|
|
)
|
2019-02-14 10:39:07 +00:00
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
cmd.Stdout = &stdoutBuf
|
|
|
|
cmd.Stderr = &stderrBuf
|
2018-04-13 12:34:48 +00:00
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
klog.V(4).Infof("cephfs: EXEC %s %s", program, sanitizedArgs)
|
2018-04-13 12:34:48 +00:00
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
2019-07-03 03:53:11 +00:00
|
|
|
if cmd.Process == nil {
|
|
|
|
return nil, nil, fmt.Errorf("cannot get process pid while running %s %v: %v: %s",
|
|
|
|
program, sanitizedArgs, err, stderrBuf.Bytes())
|
|
|
|
}
|
2019-02-26 10:06:16 +00:00
|
|
|
return nil, nil, fmt.Errorf("an error occurred while running (%d) %s %v: %v: %s",
|
|
|
|
cmd.Process.Pid, program, sanitizedArgs, err, stderrBuf.Bytes())
|
2018-04-13 12:34:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
return stdoutBuf.Bytes(), stderrBuf.Bytes(), nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 10:39:07 +00:00
|
|
|
func execCommandErr(program string, args ...string) error {
|
2019-02-18 11:46:59 +00:00
|
|
|
_, _, err := execCommand(program, args...)
|
|
|
|
return err
|
2019-02-14 10:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
//nolint: unparam
|
2019-02-14 10:39:07 +00:00
|
|
|
func execCommandJSON(v interface{}, program string, args ...string) error {
|
|
|
|
stdout, _, err := execCommand(program, args...)
|
|
|
|
if err != nil {
|
2018-04-13 12:34:48 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-26 13:00:28 +00:00
|
|
|
|
2019-02-14 10:39:07 +00:00
|
|
|
if err = json.Unmarshal(stdout, v); err != nil {
|
2019-02-21 11:35:24 +00:00
|
|
|
return fmt.Errorf("failed to unmarshal JSON for %s %v: %s: %v", program, util.StripSecretInArgs(args), stdout, err)
|
2018-04-13 12:34:48 +00:00
|
|
|
}
|
2018-03-26 13:00:28 +00:00
|
|
|
|
2019-02-14 10:39:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used in isMountPoint()
|
|
|
|
var dummyMount = mount.New("")
|
|
|
|
|
|
|
|
func isMountPoint(p string) (bool, error) {
|
|
|
|
notMnt, err := dummyMount.IsLikelyNotMountPoint(p)
|
|
|
|
if err != nil {
|
|
|
|
return false, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return !notMnt, nil
|
2018-04-13 12:34:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
func pathExists(p string) bool {
|
|
|
|
_, err := os.Stat(p)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
// Controller service request validation
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) validateCreateVolumeRequest(req *csi.CreateVolumeRequest) error {
|
2018-07-28 08:24:07 +00:00
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
|
|
|
|
return fmt.Errorf("invalid CreateVolumeRequest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetName() == "" {
|
2019-05-13 04:47:17 +00:00
|
|
|
return status.Error(codes.InvalidArgument, "volume Name cannot be empty")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 12:47:14 +00:00
|
|
|
reqCaps := req.GetVolumeCapabilities()
|
|
|
|
if reqCaps == nil {
|
2019-05-13 04:47:17 +00:00
|
|
|
return status.Error(codes.InvalidArgument, "volume Capabilities cannot be empty")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 12:47:14 +00:00
|
|
|
for _, cap := range reqCaps {
|
|
|
|
if cap.GetBlock() != nil {
|
|
|
|
return status.Error(codes.Unimplemented, "block volume not supported")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-17 07:59:29 +00:00
|
|
|
func (cs *ControllerServer) validateDeleteVolumeRequest() error {
|
2018-07-28 08:24:07 +00:00
|
|
|
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 {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("volume capability missing in request")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetVolumeId() == "" {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("volume ID missing in request")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetStagingTargetPath() == "" {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("staging target path missing in request")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 18:48:36 +00:00
|
|
|
if req.GetSecrets() == nil || len(req.GetSecrets()) == 0 {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("stage secrets cannot be nil or empty")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNodeUnstageVolumeRequest(req *csi.NodeUnstageVolumeRequest) error {
|
|
|
|
if req.GetVolumeId() == "" {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("volume ID missing in request")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetStagingTargetPath() == "" {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("staging target path missing in request")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNodePublishVolumeRequest(req *csi.NodePublishVolumeRequest) error {
|
|
|
|
if req.GetVolumeCapability() == nil {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("volume capability missing in request")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetVolumeId() == "" {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("volume ID missing in request")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetTargetPath() == "" {
|
2019-02-18 11:46:59 +00:00
|
|
|
return errors.New("target path missing in request")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 15:06:41 +00:00
|
|
|
if req.GetStagingTargetPath() == "" {
|
|
|
|
return errors.New("staging target path missing in request")
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNodeUnpublishVolumeRequest(req *csi.NodeUnpublishVolumeRequest) error {
|
|
|
|
if req.GetVolumeId() == "" {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("volume ID missing in request")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetTargetPath() == "" {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("target path missing in request")
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|