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-08-22 17:19:06 +00:00
|
|
|
"context"
|
2018-04-13 12:34:48 +00:00
|
|
|
"encoding/json"
|
2018-03-20 15:14:14 +00:00
|
|
|
"fmt"
|
2020-08-07 06:10:17 +00:00
|
|
|
"time"
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2020-04-17 09:23:49 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2020-04-15 03:38:16 +00:00
|
|
|
|
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2020-08-07 06:10:17 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2018-03-05 11:59:47 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
type volumeID string
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
func execCommandErr(ctx context.Context, program string, args ...string) error {
|
2020-07-22 12:11:41 +00:00
|
|
|
_, _, err := util.ExecCommand(ctx, program, args...)
|
2019-02-18 11:46:59 +00:00
|
|
|
return err
|
2019-02-14 10:39:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 04:03:11 +00:00
|
|
|
// nolint:unparam // todo:program values has to be revisited later
|
2020-11-06 02:57:37 +00:00
|
|
|
func execCommandWithStdErr(ctx context.Context, program string, args ...string) (string, error) {
|
|
|
|
_, stdErr, err := util.ExecCommand(ctx, program, args...)
|
|
|
|
return stdErr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint:unparam // todo:program values has to be revisited later
|
|
|
|
func execCommandJSON(ctx context.Context, v interface{}, program string, args ...string) (string, error) {
|
|
|
|
stdout, stderr, err := util.ExecCommand(ctx, program, args...)
|
2019-02-14 10:39:07 +00:00
|
|
|
if err != nil {
|
2020-11-06 02:57:37 +00:00
|
|
|
return stderr, err
|
2018-04-13 12:34:48 +00:00
|
|
|
}
|
2018-03-26 13:00:28 +00:00
|
|
|
|
2020-07-22 12:53:22 +00:00
|
|
|
if err = json.Unmarshal([]byte(stdout), v); err != nil {
|
2020-11-06 02:57:37 +00:00
|
|
|
return "", fmt.Errorf("failed to unmarshal JSON for %s %v: %s: %w", program, util.StripSecretInArgs(args), stdout, err)
|
2018-04-13 12:34:48 +00:00
|
|
|
}
|
2018-03-26 13:00:28 +00:00
|
|
|
|
2020-11-06 02:57:37 +00:00
|
|
|
return "", nil
|
2019-02-14 10:39:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +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 {
|
2020-07-13 03:56:51 +00:00
|
|
|
return fmt.Errorf("invalid CreateVolumeRequest: %w", err)
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 08:07:14 +00:00
|
|
|
// Allow readonly access mode for volume with content source
|
|
|
|
err := util.CheckReadOnlyManyIsSupported(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-04 04:03:11 +00:00
|
|
|
if req.VolumeContentSource != nil {
|
|
|
|
volumeSource := req.VolumeContentSource
|
|
|
|
switch volumeSource.Type.(type) {
|
|
|
|
case *csi.VolumeContentSource_Snapshot:
|
|
|
|
snapshot := req.VolumeContentSource.GetSnapshot()
|
|
|
|
// CSI spec requires returning NOT_FOUND when the volumeSource is missing/incorrect.
|
|
|
|
if snapshot == nil {
|
|
|
|
return status.Error(codes.NotFound, "volume Snapshot cannot be empty")
|
|
|
|
}
|
|
|
|
if snapshot.GetSnapshotId() == "" {
|
|
|
|
return status.Error(codes.NotFound, "volume Snapshot ID cannot be empty")
|
|
|
|
}
|
|
|
|
case *csi.VolumeContentSource_Volume:
|
|
|
|
// CSI spec requires returning NOT_FOUND when the volumeSource is missing/incorrect.
|
|
|
|
vol := req.VolumeContentSource.GetVolume()
|
|
|
|
if vol == nil {
|
|
|
|
return status.Error(codes.NotFound, "volume cannot be empty")
|
|
|
|
}
|
|
|
|
if vol.GetVolumeId() == "" {
|
|
|
|
return status.Error(codes.NotFound, "volume ID cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return status.Error(codes.InvalidArgument, "unsupported volume data source")
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2020-07-13 03:56:51 +00:00
|
|
|
return fmt.Errorf("invalid DeleteVolumeRequest: %w", err)
|
2018-07-28 08:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-07 06:41:33 +00:00
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// Controller expand volume request validation.
|
2019-10-07 06:41:33 +00:00
|
|
|
func (cs *ControllerServer) validateExpandVolumeRequest(req *csi.ControllerExpandVolumeRequest) error {
|
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_EXPAND_VOLUME); err != nil {
|
2020-07-13 03:56:51 +00:00
|
|
|
return fmt.Errorf("invalid ExpandVolumeRequest: %w", err)
|
2019-10-07 06:41:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetVolumeId() == "" {
|
|
|
|
return status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
capRange := req.GetCapacityRange()
|
|
|
|
if capRange == nil {
|
|
|
|
return status.Error(codes.InvalidArgument, "CapacityRange cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-04 04:03:11 +00:00
|
|
|
|
|
|
|
func genSnapFromOptions(ctx context.Context, req *csi.CreateSnapshotRequest) (snap *cephfsSnapshot, err error) {
|
|
|
|
cephfsSnap := &cephfsSnapshot{}
|
|
|
|
cephfsSnap.RequestName = req.GetName()
|
|
|
|
snapOptions := req.GetParameters()
|
|
|
|
|
2020-08-10 06:27:28 +00:00
|
|
|
cephfsSnap.Monitors, cephfsSnap.ClusterID, err = util.GetMonsAndClusterID(snapOptions)
|
2020-08-04 04:03:11 +00:00
|
|
|
if err != nil {
|
2020-08-11 12:22:23 +00:00
|
|
|
util.ErrorLog(ctx, "failed getting mons (%s)", err)
|
2020-08-04 04:03:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if namePrefix, ok := snapOptions["snapshotNamePrefix"]; ok {
|
|
|
|
cephfsSnap.NamePrefix = namePrefix
|
|
|
|
}
|
|
|
|
return cephfsSnap, nil
|
|
|
|
}
|
|
|
|
|
2020-08-07 06:10:17 +00:00
|
|
|
func parseTime(ctx context.Context, createTime string) (*timestamp.Timestamp, error) {
|
|
|
|
tm := ×tamp.Timestamp{}
|
|
|
|
layout := "2006-01-02 15:04:05.000000"
|
|
|
|
// TODO currently parsing of timestamp to time.ANSIC generate from ceph fs is failng
|
|
|
|
var t time.Time
|
|
|
|
t, err := time.Parse(layout, createTime)
|
|
|
|
if err != nil {
|
2020-08-11 12:21:44 +00:00
|
|
|
util.ErrorLog(ctx, "failed to parse time %s %v", createTime, err)
|
2020-08-07 06:10:17 +00:00
|
|
|
return tm, err
|
|
|
|
}
|
|
|
|
tm, err = ptypes.TimestampProto(t)
|
|
|
|
if err != nil {
|
2020-08-11 12:21:44 +00:00
|
|
|
util.ErrorLog(ctx, "failed to convert time %s %v", createTime, err)
|
2020-08-07 06:10:17 +00:00
|
|
|
return tm, err
|
|
|
|
}
|
|
|
|
return tm, nil
|
|
|
|
}
|