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-24 09:14:15 +00:00
|
|
|
"context"
|
2020-06-25 06:41:35 +00:00
|
|
|
"errors"
|
2020-08-04 04:25:28 +00:00
|
|
|
"fmt"
|
2023-11-22 06:16:50 +00:00
|
|
|
"syscall"
|
2019-08-24 09:14:15 +00:00
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/cephfs/core"
|
2021-08-25 06:46:03 +00:00
|
|
|
cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors"
|
2022-02-15 12:11:09 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/cephfs/store"
|
2021-09-16 13:47:57 +00:00
|
|
|
fsutil "github.com/ceph/ceph-csi/internal/cephfs/util"
|
2020-04-17 09:23:49 +00:00
|
|
|
csicommon "github.com/ceph/ceph-csi/internal/csi-common"
|
2022-08-12 14:31:08 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/kms"
|
2020-04-17 09:23:49 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2022-03-15 11:04:28 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/k8s"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2022-04-06 13:26:07 +00:00
|
|
|
rterrors "github.com/ceph/ceph-csi/internal/util/reftracker/errors"
|
2020-04-15 03:38:16 +00:00
|
|
|
|
2019-02-18 11:30:28 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2022-11-11 10:40:24 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
|
2018-03-05 11:59:47 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// ControllerServer struct of CEPH CSI driver with supported methods of CSI
|
|
|
|
// controller server spec.
|
2019-01-17 07:51:06 +00:00
|
|
|
type ControllerServer struct {
|
2018-03-05 11:59:47 +00:00
|
|
|
*csicommon.DefaultControllerServer
|
2019-09-12 04:53:37 +00:00
|
|
|
// A map storing all volumes with ongoing operations so that additional operations
|
|
|
|
// for that same volume (as defined by VolumeID/volume name) return an Aborted error
|
|
|
|
VolumeLocks *util.VolumeLocks
|
2020-08-03 18:32:34 +00:00
|
|
|
|
|
|
|
// A map storing all snapshots with ongoing operations so that additional operations
|
|
|
|
// for that same snapshot (as defined by SnapshotID/snapshot name) return an Aborted error
|
|
|
|
SnapshotLocks *util.VolumeLocks
|
|
|
|
|
|
|
|
// A map storing all volumes/snapshots with ongoing operations.
|
|
|
|
OperationLocks *util.OperationLock
|
2022-06-14 13:23:29 +00:00
|
|
|
|
2024-02-13 13:24:45 +00:00
|
|
|
// A map storing all volumes with ongoing operations so that additional operations
|
|
|
|
// for that same volume (as defined by volumegroup ID/volumegroup name) return an Aborted error
|
|
|
|
VolumeGroupLocks *util.VolumeLocks
|
|
|
|
|
2022-06-14 13:23:29 +00:00
|
|
|
// Cluster name
|
|
|
|
ClusterName string
|
2022-07-28 12:05:33 +00:00
|
|
|
|
|
|
|
// Set metadata on volume
|
|
|
|
SetMetadata bool
|
2018-12-19 14:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// createBackingVolume creates the backing subvolume and on any error cleans up any created entities.
|
2020-08-03 18:34:28 +00:00
|
|
|
func (cs *ControllerServer) createBackingVolume(
|
|
|
|
ctx context.Context,
|
|
|
|
volOptions,
|
2022-02-15 12:11:09 +00:00
|
|
|
parentVolOpt *store.VolumeOptions,
|
2022-08-12 14:31:08 +00:00
|
|
|
vID, pvID *store.VolumeIdentifier,
|
2022-06-01 10:17:19 +00:00
|
|
|
sID *store.SnapshotIdentifier,
|
2022-08-12 14:31:08 +00:00
|
|
|
secrets map[string]string,
|
2022-06-01 10:17:19 +00:00
|
|
|
) error {
|
2020-08-03 18:34:28 +00:00
|
|
|
var err error
|
2022-06-14 13:23:29 +00:00
|
|
|
volClient := core.NewSubVolume(volOptions.GetConnection(),
|
2022-07-28 12:05:33 +00:00
|
|
|
&volOptions.SubVolume, volOptions.ClusterID, cs.ClusterName, cs.SetMetadata)
|
2022-02-15 12:11:09 +00:00
|
|
|
|
2020-08-03 18:34:28 +00:00
|
|
|
if sID != nil {
|
2024-03-04 15:13:31 +00:00
|
|
|
err = parentVolOpt.CopyEncryptionConfig(ctx, volOptions, sID.SnapshotID, vID.VolumeID)
|
2022-08-12 14:31:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return cs.createBackingVolumeFromSnapshotSource(ctx, volOptions, parentVolOpt, volClient, sID, secrets)
|
2022-04-06 13:26:07 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if parentVolOpt != nil {
|
2024-03-04 15:13:31 +00:00
|
|
|
err = parentVolOpt.CopyEncryptionConfig(ctx, volOptions, pvID.VolumeID, vID.VolumeID)
|
2022-08-12 14:31:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
return cs.createBackingVolumeFromVolumeSource(ctx, parentVolOpt, volClient, pvID)
|
|
|
|
}
|
2020-08-03 18:34:28 +00:00
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if err = volClient.CreateVolume(ctx); err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to create volume %s: %v", volOptions.RequestName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ControllerServer) createBackingVolumeFromSnapshotSource(
|
|
|
|
ctx context.Context,
|
|
|
|
volOptions *store.VolumeOptions,
|
|
|
|
parentVolOpt *store.VolumeOptions,
|
|
|
|
volClient core.SubVolumeClient,
|
|
|
|
sID *store.SnapshotIdentifier,
|
2022-08-12 14:31:08 +00:00
|
|
|
secrets map[string]string,
|
2022-04-06 13:26:07 +00:00
|
|
|
) error {
|
|
|
|
if err := cs.OperationLocks.GetRestoreLock(sID.SnapshotID); err != nil {
|
|
|
|
log.ErrorLog(ctx, err.Error())
|
|
|
|
|
|
|
|
return status.Error(codes.Aborted, err.Error())
|
2020-08-03 18:34:28 +00:00
|
|
|
}
|
2022-04-06 13:26:07 +00:00
|
|
|
defer cs.OperationLocks.ReleaseRestoreLock(sID.SnapshotID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if volOptions.BackingSnapshot {
|
2022-08-12 14:31:08 +00:00
|
|
|
if err := store.AddSnapshotBackedVolumeRef(ctx, volOptions, cs.ClusterName, cs.SetMetadata, secrets); err != nil {
|
2022-04-06 13:26:07 +00:00
|
|
|
log.ErrorLog(ctx, "failed to create snapshot-backed volume from snapshot %s: %v",
|
|
|
|
sID.FsSnapshotName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-03 18:34:28 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-03 18:34:28 +00:00
|
|
|
return nil
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
err := volClient.CreateCloneFromSnapshot(ctx, core.Snapshot{
|
|
|
|
SnapshotID: sID.FsSnapshotName,
|
|
|
|
SubVolume: &parentVolOpt.SubVolume,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to create clone from snapshot %s: %v", sID.FsSnapshotName, err)
|
2023-11-22 06:16:50 +00:00
|
|
|
// TODO: Add error handle for EAGAIN in go-ceph and replace the
|
|
|
|
// syscall.EAGAIN check with the go-ceph compatible error.
|
|
|
|
if errors.Is(err, syscall.EAGAIN) {
|
|
|
|
return status.Error(codes.ResourceExhausted, err.Error())
|
|
|
|
}
|
2022-04-06 13:26:07 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ControllerServer) createBackingVolumeFromVolumeSource(
|
|
|
|
ctx context.Context,
|
|
|
|
parentVolOpt *store.VolumeOptions,
|
|
|
|
volClient core.SubVolumeClient,
|
|
|
|
pvID *store.VolumeIdentifier,
|
|
|
|
) error {
|
|
|
|
if err := cs.OperationLocks.GetCloneLock(pvID.VolumeID); err != nil {
|
|
|
|
log.ErrorLog(ctx, err.Error())
|
|
|
|
|
|
|
|
return status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
|
|
|
defer cs.OperationLocks.ReleaseCloneLock(pvID.VolumeID)
|
|
|
|
|
|
|
|
if err := volClient.CreateCloneFromSubvolume(ctx, &parentVolOpt.SubVolume); err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to create clone from subvolume %s: %v", fsutil.VolumeID(pvID.FsSubvolName), err)
|
2023-11-22 06:16:50 +00:00
|
|
|
// TODO: Add error handle for EAGAIN in go-ceph and replace the
|
|
|
|
// syscall.EAGAIN check with the go-ceph compatible error.
|
|
|
|
if errors.Is(err, syscall.EAGAIN) {
|
|
|
|
return status.Error(codes.ResourceExhausted, err.Error())
|
|
|
|
}
|
2022-04-06 13:26:07 +00:00
|
|
|
|
|
|
|
return err
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:05:33 +00:00
|
|
|
func (cs *ControllerServer) checkContentSource(
|
2021-06-25 10:18:59 +00:00
|
|
|
ctx context.Context,
|
|
|
|
req *csi.CreateVolumeRequest,
|
2022-06-01 10:17:19 +00:00
|
|
|
cr *util.Credentials,
|
|
|
|
) (*store.VolumeOptions, *store.VolumeIdentifier, *store.SnapshotIdentifier, error) {
|
2024-04-04 08:50:20 +00:00
|
|
|
if req.GetVolumeContentSource() == nil {
|
2020-08-03 18:37:44 +00:00
|
|
|
return nil, nil, nil, nil
|
|
|
|
}
|
2024-04-04 08:50:20 +00:00
|
|
|
volumeSource := req.GetVolumeContentSource()
|
|
|
|
switch volumeSource.GetType().(type) {
|
2020-08-03 18:37:44 +00:00
|
|
|
case *csi.VolumeContentSource_Snapshot:
|
2024-04-04 08:50:20 +00:00
|
|
|
snapshotID := req.GetVolumeContentSource().GetSnapshot().GetSnapshotId()
|
2022-08-12 14:31:08 +00:00
|
|
|
volOpt, _, sid, err := store.NewSnapshotOptionsFromID(ctx, snapshotID, cr,
|
|
|
|
req.GetSecrets(), cs.ClusterName, cs.SetMetadata)
|
2020-08-03 18:37:44 +00:00
|
|
|
if err != nil {
|
2021-08-25 06:46:03 +00:00
|
|
|
if errors.Is(err, cerrors.ErrSnapNotFound) {
|
2020-08-03 18:37:44 +00:00
|
|
|
return nil, nil, nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-03 18:37:44 +00:00
|
|
|
return nil, nil, nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-03 18:37:44 +00:00
|
|
|
return volOpt, nil, sid, nil
|
|
|
|
case *csi.VolumeContentSource_Volume:
|
|
|
|
// Find the volume using the provided VolumeID
|
2024-04-04 08:50:20 +00:00
|
|
|
volID := req.GetVolumeContentSource().GetVolume().GetVolumeId()
|
2022-07-28 12:05:33 +00:00
|
|
|
parentVol, pvID, err := store.NewVolumeOptionsFromVolID(ctx,
|
2024-04-04 08:50:20 +00:00
|
|
|
volID, nil, req.GetSecrets(), cs.ClusterName, cs.SetMetadata)
|
2020-08-03 18:37:44 +00:00
|
|
|
if err != nil {
|
2021-08-25 06:46:03 +00:00
|
|
|
if !errors.Is(err, cerrors.ErrVolumeNotFound) {
|
2020-08-03 18:37:44 +00:00
|
|
|
return nil, nil, nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-03 18:37:44 +00:00
|
|
|
return nil, nil, nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return parentVol, pvID, nil, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-03 18:37:44 +00:00
|
|
|
return nil, nil, nil, status.Errorf(codes.InvalidArgument, "not a proper volume source %v", volumeSource)
|
|
|
|
}
|
|
|
|
|
2022-01-17 07:14:39 +00:00
|
|
|
// checkValidCreateVolumeRequest checks if the request is valid
|
|
|
|
// CreateVolumeRequest by inspecting the request parameters.
|
|
|
|
func checkValidCreateVolumeRequest(
|
|
|
|
vol,
|
|
|
|
parentVol *store.VolumeOptions,
|
|
|
|
pvID *store.VolumeIdentifier,
|
2022-06-01 10:17:19 +00:00
|
|
|
sID *store.SnapshotIdentifier,
|
2022-04-06 13:26:07 +00:00
|
|
|
req *csi.CreateVolumeRequest,
|
2022-06-01 10:17:19 +00:00
|
|
|
) error {
|
2023-08-30 07:46:01 +00:00
|
|
|
volCaps := req.GetVolumeCapabilities()
|
2022-01-17 07:14:39 +00:00
|
|
|
switch {
|
|
|
|
case pvID != nil:
|
|
|
|
if vol.Size < parentVol.Size {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"cannot clone from volume %s: volume size %d is smaller than source volume size %d",
|
|
|
|
pvID.VolumeID,
|
|
|
|
parentVol.Size,
|
|
|
|
vol.Size)
|
|
|
|
}
|
2022-04-06 13:26:07 +00:00
|
|
|
|
2023-08-30 07:46:01 +00:00
|
|
|
if parentVol.BackingSnapshot && store.IsVolumeCreateRO(volCaps) {
|
|
|
|
return errors.New("creating read-only clone from a snapshot-backed volume is not supported")
|
2022-04-06 13:26:07 +00:00
|
|
|
}
|
2023-08-30 07:46:01 +00:00
|
|
|
|
2022-01-17 07:14:39 +00:00
|
|
|
case sID != nil:
|
2022-04-06 13:26:07 +00:00
|
|
|
if vol.BackingSnapshot {
|
2023-02-07 09:06:36 +00:00
|
|
|
isRO := store.IsVolumeCreateRO(volCaps)
|
|
|
|
if !isRO {
|
|
|
|
return errors.New("backingSnapshot may be used only with read-only access modes")
|
2022-04-06 13:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 07:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:18:56 +00:00
|
|
|
func buildCreateVolumeResponse(
|
|
|
|
req *csi.CreateVolumeRequest,
|
|
|
|
volOptions *store.VolumeOptions,
|
|
|
|
vID *store.VolumeIdentifier,
|
|
|
|
) *csi.CreateVolumeResponse {
|
|
|
|
volumeContext := util.GetVolumeContext(req.GetParameters())
|
|
|
|
volumeContext["subvolumeName"] = vID.FsSubvolName
|
|
|
|
volumeContext["subvolumePath"] = volOptions.RootPath
|
|
|
|
volume := &csi.Volume{
|
|
|
|
VolumeId: vID.VolumeID,
|
|
|
|
CapacityBytes: volOptions.Size,
|
|
|
|
ContentSource: req.GetVolumeContentSource(),
|
|
|
|
VolumeContext: volumeContext,
|
|
|
|
}
|
|
|
|
if volOptions.Topology != nil {
|
|
|
|
volume.AccessibleTopology = []*csi.Topology{
|
|
|
|
{
|
|
|
|
Segments: volOptions.Topology,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &csi.CreateVolumeResponse{Volume: volume}
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// CreateVolume creates a reservation and the volume in backend, if it is not already present.
|
2023-06-02 09:49:22 +00:00
|
|
|
//
|
2023-06-02 08:59:52 +00:00
|
|
|
//nolint:gocognit,gocyclo,nestif,cyclop // TODO: reduce complexity
|
2021-06-25 10:18:59 +00:00
|
|
|
func (cs *ControllerServer) CreateVolume(
|
|
|
|
ctx context.Context,
|
2022-06-01 10:17:19 +00:00
|
|
|
req *csi.CreateVolumeRequest,
|
|
|
|
) (*csi.CreateVolumeResponse, error) {
|
2018-03-20 15:15:19 +00:00
|
|
|
if err := cs.validateCreateVolumeRequest(req); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "CreateVolumeRequest validation failed: %v", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2018-03-05 11:59:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-13 12:57:16 +00:00
|
|
|
|
2018-04-13 12:54:40 +00:00
|
|
|
// Configuration
|
2019-01-21 14:21:03 +00:00
|
|
|
secret := req.GetSecrets()
|
2019-05-28 19:03:18 +00:00
|
|
|
requestName := req.GetName()
|
2019-09-12 04:53:37 +00:00
|
|
|
|
2020-08-04 03:56:43 +00:00
|
|
|
cr, err := util.NewAdminCredentials(secret)
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to retrieve admin credentials: %v", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 03:56:43 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
defer cr.DeleteCredentials()
|
|
|
|
|
2019-09-12 04:53:37 +00:00
|
|
|
// Existence and conflict checks
|
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(requestName); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, requestName)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-09-12 04:53:37 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, requestName)
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(requestName)
|
|
|
|
|
2022-07-28 12:05:33 +00:00
|
|
|
volOptions, err := store.NewVolumeOptions(ctx, requestName, cs.ClusterName, cs.SetMetadata, req, cr)
|
2018-03-05 11:59:47 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "validation and extraction of volume options failed: %v", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2018-03-20 15:15:19 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
2020-08-05 10:10:04 +00:00
|
|
|
defer volOptions.Destroy()
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2019-09-25 08:35:33 +00:00
|
|
|
if req.GetCapacityRange() != nil {
|
2022-07-12 08:49:25 +00:00
|
|
|
volOptions.Size = util.RoundOffCephFSVolSize(req.GetCapacityRange().GetRequiredBytes())
|
2019-09-25 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 12:05:33 +00:00
|
|
|
parentVol, pvID, sID, err := cs.checkContentSource(ctx, req, cr)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
2020-08-04 03:56:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-05 10:10:04 +00:00
|
|
|
if parentVol != nil {
|
|
|
|
defer parentVol.Destroy()
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
err = checkValidCreateVolumeRequest(volOptions, parentVol, pvID, sID, req)
|
2022-01-17 07:14:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-08-30 09:13:03 +00:00
|
|
|
// As we are trying to create RWX volume from backing snapshot, we need to
|
|
|
|
// retrieve the snapshot details from the backing snapshot and create a
|
|
|
|
// subvolume clone from the snapshot.
|
2024-04-04 08:50:20 +00:00
|
|
|
if parentVol != nil && parentVol.BackingSnapshot && !store.IsVolumeCreateRO(req.GetVolumeCapabilities()) {
|
2023-08-30 09:13:03 +00:00
|
|
|
// unset pvID as we dont have real subvolume for the parent volumeID as its a backing snapshot
|
|
|
|
pvID = nil
|
|
|
|
parentVol, _, sID, err = store.NewSnapshotOptionsFromID(ctx, parentVol.BackingSnapshotID, cr,
|
|
|
|
req.GetSecrets(), cs.ClusterName, cs.SetMetadata)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, cerrors.ErrSnapNotFound) {
|
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:05:33 +00:00
|
|
|
vID, err := store.CheckVolExists(ctx, volOptions, parentVol, pvID, sID, cr, cs.ClusterName, cs.SetMetadata)
|
2020-08-04 03:56:43 +00:00
|
|
|
if err != nil {
|
2021-09-16 13:47:57 +00:00
|
|
|
if cerrors.IsCloneRetryError(err) {
|
2020-08-04 03:56:43 +00:00
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2022-08-12 14:31:08 +00:00
|
|
|
|
2019-09-25 08:35:33 +00:00
|
|
|
// TODO return error message if requested vol size greater than found volume return error
|
|
|
|
|
2022-05-24 14:06:52 +00:00
|
|
|
metadata := k8s.GetVolumeMetadata(req.GetParameters())
|
2019-05-28 19:03:18 +00:00
|
|
|
if vID != nil {
|
2022-06-14 13:23:29 +00:00
|
|
|
volClient := core.NewSubVolume(volOptions.GetConnection(), &volOptions.SubVolume,
|
2022-07-28 12:05:33 +00:00
|
|
|
volOptions.ClusterID, cs.ClusterName, cs.SetMetadata)
|
2022-12-21 09:27:34 +00:00
|
|
|
if (sID != nil || pvID != nil) && !volOptions.BackingSnapshot {
|
2022-02-15 12:11:09 +00:00
|
|
|
err = volClient.ExpandVolume(ctx, volOptions.Size)
|
2021-12-21 05:03:41 +00:00
|
|
|
if err != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
purgeErr := volClient.PurgeVolume(ctx, false)
|
2021-12-21 05:03:41 +00:00
|
|
|
if purgeErr != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to delete volume %s: %v", requestName, purgeErr)
|
|
|
|
// All errors other than ErrVolumeNotFound should return an error back to the caller
|
|
|
|
if !errors.Is(purgeErr, cerrors.ErrVolumeNotFound) {
|
|
|
|
return nil, status.Error(codes.Internal, purgeErr.Error())
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 12:11:09 +00:00
|
|
|
errUndo := store.UndoVolReservation(ctx, volOptions, *vID, secret)
|
2021-12-21 05:03:41 +00:00
|
|
|
if errUndo != nil {
|
|
|
|
log.WarningLog(ctx, "failed undoing reservation of volume: %s (%s)",
|
|
|
|
requestName, errUndo)
|
|
|
|
}
|
|
|
|
log.ErrorLog(ctx, "failed to expand volume %s: %v", fsutil.VolumeID(vID.FsSubvolName), err)
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 04:56:28 +00:00
|
|
|
|
2022-05-24 14:06:52 +00:00
|
|
|
if !volOptions.BackingSnapshot {
|
|
|
|
// Set metadata on restart of provisioner pod when subvolume exist
|
|
|
|
err = volClient.SetAllMetadata(metadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:18:56 +00:00
|
|
|
return buildCreateVolumeResponse(req, volOptions, vID), nil
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2018-04-13 12:54:40 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// Reservation
|
2022-02-15 12:11:09 +00:00
|
|
|
vID, err = store.ReserveVol(ctx, volOptions, secret)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2020-08-04 03:56:43 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
defer func() {
|
2018-04-13 12:54:40 +00:00
|
|
|
if err != nil {
|
2021-09-16 13:47:57 +00:00
|
|
|
if !cerrors.IsCloneRetryError(err) {
|
2022-02-15 12:11:09 +00:00
|
|
|
errDefer := store.UndoVolReservation(ctx, volOptions, *vID, secret)
|
2020-08-04 03:56:43 +00:00
|
|
|
if errDefer != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.WarningLog(ctx, "failed undoing reservation of volume: %s (%s)",
|
2020-08-04 03:56:43 +00:00
|
|
|
requestName, errDefer)
|
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
}()
|
2018-04-13 12:54:40 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// Create a volume
|
2022-08-12 14:31:08 +00:00
|
|
|
err = cs.createBackingVolume(ctx, volOptions, parentVol, vID, pvID, sID, req.GetSecrets())
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
2021-09-16 13:47:57 +00:00
|
|
|
if cerrors.IsCloneRetryError(err) {
|
2020-08-04 03:56:43 +00:00
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, err
|
2018-04-13 12:54:40 +00:00
|
|
|
}
|
2021-02-08 07:15:23 +00:00
|
|
|
|
2022-06-14 13:23:29 +00:00
|
|
|
volClient := core.NewSubVolume(volOptions.GetConnection(),
|
2022-07-28 12:05:33 +00:00
|
|
|
&volOptions.SubVolume, volOptions.ClusterID, cs.ClusterName, cs.SetMetadata)
|
2022-04-06 13:26:07 +00:00
|
|
|
if !volOptions.BackingSnapshot {
|
|
|
|
// Get root path for the created subvolume.
|
|
|
|
// Note that root path for snapshot-backed volumes has been already set when
|
|
|
|
// building VolumeOptions.
|
|
|
|
|
|
|
|
volOptions.RootPath, err = volClient.GetVolumeRootPathCeph(ctx)
|
|
|
|
if err != nil {
|
|
|
|
purgeErr := volClient.PurgeVolume(ctx, true)
|
|
|
|
if purgeErr != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to delete volume %s: %v", vID.FsSubvolName, purgeErr)
|
|
|
|
// All errors other than ErrVolumeNotFound should return an error back to the caller
|
|
|
|
if !errors.Is(purgeErr, cerrors.ErrVolumeNotFound) {
|
|
|
|
// If the subvolume deletion is failed, we should not cleanup
|
|
|
|
// the OMAP entry it will stale subvolume in cluster.
|
|
|
|
// set err=nil so that when we get the request again we can get
|
|
|
|
// the subvolume info.
|
|
|
|
err = nil
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Internal, purgeErr.Error())
|
|
|
|
}
|
2021-02-08 07:15:23 +00:00
|
|
|
}
|
2022-04-06 13:26:07 +00:00
|
|
|
log.ErrorLog(ctx, "failed to get subvolume path %s: %v", vID.FsSubvolName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2022-05-24 14:05:23 +00:00
|
|
|
|
|
|
|
// Set Metadata on PV Create
|
|
|
|
err = volClient.SetAllMetadata(metadata)
|
|
|
|
if err != nil {
|
2022-10-14 12:24:51 +00:00
|
|
|
purgeErr := volClient.PurgeVolume(ctx, true)
|
|
|
|
if purgeErr != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to delete volume %s: %v", vID.FsSubvolName, purgeErr)
|
|
|
|
}
|
|
|
|
|
2022-05-24 14:05:23 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-02-08 07:15:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLog(ctx, "cephfs: successfully created backing volume named %s for request name %s",
|
2019-05-28 19:03:18 +00:00
|
|
|
vID.FsSubvolName, requestName)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2024-03-27 11:18:56 +00:00
|
|
|
return buildCreateVolumeResponse(req, volOptions, vID), nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// DeleteVolume deletes the volume in backend and its reservation.
|
2021-06-25 10:18:59 +00:00
|
|
|
func (cs *ControllerServer) DeleteVolume(
|
|
|
|
ctx context.Context,
|
2022-06-01 10:17:19 +00:00
|
|
|
req *csi.DeleteVolumeRequest,
|
|
|
|
) (*csi.DeleteVolumeResponse, error) {
|
2019-05-28 19:03:18 +00:00
|
|
|
if err := cs.validateDeleteVolumeRequest(); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "DeleteVolumeRequest validation failed: %v", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
volID := fsutil.VolumeID(req.GetVolumeId())
|
2019-05-28 19:03:18 +00:00
|
|
|
secrets := req.GetSecrets()
|
|
|
|
|
2019-09-12 04:53:37 +00:00
|
|
|
// lock out parallel delete operations
|
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(string(volID)); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, volID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-09-12 04:53:37 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, string(volID))
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(string(volID))
|
|
|
|
|
2020-08-04 03:53:28 +00:00
|
|
|
// lock out volumeID for clone and expand operation
|
|
|
|
if err := cs.OperationLocks.GetDeleteLock(req.GetVolumeId()); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 03:53:28 +00:00
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
|
|
|
defer cs.OperationLocks.ReleaseDeleteLock(req.GetVolumeId())
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// Find the volume using the provided VolumeID
|
2022-07-28 12:05:33 +00:00
|
|
|
volOptions, vID, err := store.NewVolumeOptionsFromVolID(ctx, string(volID), nil, secrets,
|
|
|
|
cs.ClusterName, cs.SetMetadata)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
2020-01-31 08:49:11 +00:00
|
|
|
// if error is ErrPoolNotFound, the pool is already deleted we dont
|
|
|
|
// need to worry about deleting subvolume or omap data, return success
|
2020-07-08 23:00:23 +00:00
|
|
|
if errors.Is(err, util.ErrPoolNotFound) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.WarningLog(ctx, "failed to get backend volume for %s: %v", string(volID), err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-01-31 08:49:11 +00:00
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
// if error is ErrKeyNotFound, then a previous attempt at deletion was complete
|
|
|
|
// or partially complete (subvolume and imageOMap are garbage collected already), hence
|
|
|
|
// return success as deletion is complete
|
2020-07-08 23:00:23 +00:00
|
|
|
if errors.Is(err, util.ErrKeyNotFound) {
|
2019-05-28 19:03:18 +00:00
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "Error returned from newVolumeOptionsFromVolID: %v", err)
|
2020-08-04 03:53:28 +00:00
|
|
|
|
2020-01-23 02:48:46 +00:00
|
|
|
// All errors other than ErrVolumeNotFound should return an error back to the caller
|
2021-08-25 06:46:03 +00:00
|
|
|
if !errors.Is(err, cerrors.ErrVolumeNotFound) {
|
2020-01-23 02:48:46 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If error is ErrImageNotFound then we failed to find the subvolume, but found the imageOMap
|
|
|
|
// to lead us to the image, hence the imageOMap needs to be garbage collected, by calling
|
|
|
|
// unreserve for the same
|
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(volOptions.RequestName); !acquired {
|
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volOptions.RequestName)
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(volOptions.RequestName)
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
if err = store.UndoVolReservation(ctx, volOptions, *vID, secrets); err != nil {
|
2020-01-23 02:48:46 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-01-23 02:48:46 +00:00
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2020-08-05 10:10:04 +00:00
|
|
|
defer volOptions.Destroy()
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2019-09-12 04:53:37 +00:00
|
|
|
// lock out parallel delete and create requests against the same volume name as we
|
|
|
|
// cleanup the subvolume and associated omaps for the same
|
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(volOptions.RequestName); !acquired {
|
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volOptions.RequestName)
|
|
|
|
}
|
2020-08-18 04:52:36 +00:00
|
|
|
defer cs.VolumeLocks.Release(volOptions.RequestName)
|
2019-09-12 04:53:37 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// Deleting a volume requires admin credentials
|
2019-06-25 19:29:17 +00:00
|
|
|
cr, err := util.NewAdminCredentials(secrets)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to retrieve admin credentials: %v", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
2019-06-25 19:29:17 +00:00
|
|
|
defer cr.DeleteCredentials()
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
if err := cs.cleanUpBackingVolume(ctx, volOptions, vID, cr, secrets); err != nil {
|
2022-04-06 13:26:07 +00:00
|
|
|
return nil, err
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
if err := store.UndoVolReservation(ctx, volOptions, *vID, secrets); err != nil {
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLog(ctx, "cephfs: successfully deleted volume %s", volID)
|
2019-05-28 19:03:18 +00:00
|
|
|
|
|
|
|
return &csi.DeleteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:05:33 +00:00
|
|
|
func (cs *ControllerServer) cleanUpBackingVolume(
|
2022-04-06 13:26:07 +00:00
|
|
|
ctx context.Context,
|
|
|
|
volOptions *store.VolumeOptions,
|
|
|
|
volID *store.VolumeIdentifier,
|
|
|
|
cr *util.Credentials,
|
2022-08-12 14:31:08 +00:00
|
|
|
secrets map[string]string,
|
2022-04-06 13:26:07 +00:00
|
|
|
) error {
|
2022-08-12 14:31:08 +00:00
|
|
|
if volOptions.IsEncrypted() && volOptions.Encryption.KMS.RequiresDEKStore() == kms.DEKStoreIntegrated {
|
|
|
|
// Only remove DEK when the KMS stores it itself. On
|
|
|
|
// GetSecret enabled KMS the DEKs are stored by
|
|
|
|
// fscrypt on the volume that is going to be deleted anyway.
|
|
|
|
log.DebugLog(ctx, "going to remove DEK for integrated store %q (fscrypt)", volOptions.Encryption.GetID())
|
2024-03-04 15:13:31 +00:00
|
|
|
if err := volOptions.Encryption.RemoveDEK(ctx, volID.VolumeID); err != nil {
|
2022-08-12 14:31:08 +00:00
|
|
|
log.WarningLog(ctx, "failed to clean the passphrase for volume %q (file encryption): %s",
|
|
|
|
volOptions.VolID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if !volOptions.BackingSnapshot {
|
|
|
|
// Regular volumes need to be purged.
|
|
|
|
|
2022-06-14 13:23:29 +00:00
|
|
|
volClient := core.NewSubVolume(volOptions.GetConnection(),
|
2022-07-28 12:05:33 +00:00
|
|
|
&volOptions.SubVolume, volOptions.ClusterID, cs.ClusterName, cs.SetMetadata)
|
2022-04-06 13:26:07 +00:00
|
|
|
if err := volClient.PurgeVolume(ctx, false); err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to delete volume %s: %v", volID, err)
|
|
|
|
if errors.Is(err, cerrors.ErrVolumeHasSnapshots) {
|
|
|
|
return status.Error(codes.FailedPrecondition, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errors.Is(err, cerrors.ErrVolumeNotFound) {
|
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot-backed volumes need to un-reference the backing snapshot, and
|
|
|
|
// the snapshot itself may need to be deleted if its reftracker doesn't
|
|
|
|
// hold any references anymore.
|
|
|
|
|
|
|
|
backingSnapNeedsDelete, err := store.UnrefSnapshotBackedVolume(ctx, volOptions)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, rterrors.ErrObjectOutOfDate) {
|
|
|
|
return status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !backingSnapNeedsDelete {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 13:23:29 +00:00
|
|
|
snapParentVolOptions, _, snapID, err := store.NewSnapshotOptionsFromID(ctx,
|
2022-08-12 14:31:08 +00:00
|
|
|
volOptions.BackingSnapshotID, cr, secrets, cs.ClusterName, cs.SetMetadata)
|
2022-04-06 13:26:07 +00:00
|
|
|
if err != nil {
|
|
|
|
absorbErrs := []error{
|
|
|
|
util.ErrPoolNotFound,
|
|
|
|
util.ErrKeyNotFound,
|
|
|
|
cerrors.ErrSnapNotFound,
|
|
|
|
cerrors.ErrVolumeNotFound,
|
|
|
|
}
|
|
|
|
|
|
|
|
fatalErr := true
|
|
|
|
for i := range absorbErrs {
|
|
|
|
if errors.Is(err, absorbErrs[i]) {
|
|
|
|
fatalErr = false
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fatalErr {
|
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-28 12:26:55 +00:00
|
|
|
snapClient := core.NewSnapshot(snapParentVolOptions.GetConnection(), snapID.FsSnapshotName,
|
|
|
|
volOptions.ClusterID, cs.ClusterName, cs.SetMetadata, &snapParentVolOptions.SubVolume)
|
2022-04-06 13:26:07 +00:00
|
|
|
|
|
|
|
err = deleteSnapshotAndUndoReservation(ctx, snapClient, snapParentVolOptions, snapID, cr)
|
|
|
|
if err != nil {
|
|
|
|
return status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-28 11:47:06 +00:00
|
|
|
// ValidateVolumeCapabilities checks whether the volume capabilities requested
|
|
|
|
// are supported.
|
2019-01-17 07:51:06 +00:00
|
|
|
func (cs *ControllerServer) ValidateVolumeCapabilities(
|
2018-04-13 12:54:40 +00:00
|
|
|
ctx context.Context,
|
2022-06-01 10:17:19 +00:00
|
|
|
req *csi.ValidateVolumeCapabilitiesRequest,
|
|
|
|
) (*csi.ValidateVolumeCapabilitiesResponse, error) {
|
2018-07-10 16:48:55 +00:00
|
|
|
// Cephfs doesn't support Block volume
|
2024-04-04 08:50:20 +00:00
|
|
|
for _, capability := range req.GetVolumeCapabilities() {
|
2020-10-19 07:31:11 +00:00
|
|
|
if capability.GetBlock() != nil {
|
2018-11-24 18:48:36 +00:00
|
|
|
return &csi.ValidateVolumeCapabilitiesResponse{Message: ""}, nil
|
2018-07-10 16:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2018-11-24 18:48:36 +00:00
|
|
|
return &csi.ValidateVolumeCapabilitiesResponse{
|
|
|
|
Confirmed: &csi.ValidateVolumeCapabilitiesResponse_Confirmed{
|
2024-04-04 08:50:20 +00:00
|
|
|
VolumeCapabilities: req.GetVolumeCapabilities(),
|
2018-11-24 18:48:36 +00:00
|
|
|
},
|
|
|
|
}, nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
2019-10-07 06:41:33 +00:00
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// ControllerExpandVolume expands CephFS Volumes on demand based on resizer request.
|
2021-06-25 10:18:59 +00:00
|
|
|
func (cs *ControllerServer) ControllerExpandVolume(
|
|
|
|
ctx context.Context,
|
2022-06-01 10:17:19 +00:00
|
|
|
req *csi.ControllerExpandVolumeRequest,
|
|
|
|
) (*csi.ControllerExpandVolumeResponse, error) {
|
2019-10-07 06:41:33 +00:00
|
|
|
if err := cs.validateExpandVolumeRequest(req); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "ControllerExpandVolumeRequest validation failed: %v", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-10-07 06:41:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
volID := req.GetVolumeId()
|
|
|
|
secret := req.GetSecrets()
|
|
|
|
|
2019-11-25 11:09:24 +00:00
|
|
|
// lock out parallel delete operations
|
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(volID); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, volID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-11-25 11:09:24 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volID)
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(volID)
|
|
|
|
|
2020-08-03 18:28:52 +00:00
|
|
|
// lock out volumeID for clone and delete operation
|
|
|
|
if err := cs.OperationLocks.GetExpandLock(volID); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-03 18:28:52 +00:00
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
|
|
|
defer cs.OperationLocks.ReleaseExpandLock(volID)
|
|
|
|
|
2019-10-07 06:41:33 +00:00
|
|
|
cr, err := util.NewAdminCredentials(secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
defer cr.DeleteCredentials()
|
|
|
|
|
2022-07-28 12:05:33 +00:00
|
|
|
volOptions, volIdentifier, err := store.NewVolumeOptionsFromVolID(ctx, volID, nil, secret,
|
|
|
|
cs.ClusterName, cs.SetMetadata)
|
2019-10-07 06:41:33 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "validation and extraction of volume options failed: %v", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-10-07 06:41:33 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
2020-08-05 10:10:04 +00:00
|
|
|
defer volOptions.Destroy()
|
2019-10-07 06:41:33 +00:00
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if volOptions.BackingSnapshot {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "cannot expand snapshot-backed volume")
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:49:25 +00:00
|
|
|
RoundOffSize := util.RoundOffCephFSVolSize(req.GetCapacityRange().GetRequiredBytes())
|
|
|
|
|
2022-06-14 13:23:29 +00:00
|
|
|
volClient := core.NewSubVolume(volOptions.GetConnection(),
|
2022-07-28 12:05:33 +00:00
|
|
|
&volOptions.SubVolume, volOptions.ClusterID, cs.ClusterName, cs.SetMetadata)
|
2022-02-15 12:11:09 +00:00
|
|
|
if err = volClient.ResizeVolume(ctx, RoundOffSize); err != nil {
|
2021-09-16 13:47:57 +00:00
|
|
|
log.ErrorLog(ctx, "failed to expand volume %s: %v", fsutil.VolumeID(volIdentifier.FsSubvolName), err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-10-07 06:41:33 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &csi.ControllerExpandVolumeResponse{
|
2019-11-26 08:29:28 +00:00
|
|
|
CapacityBytes: RoundOffSize,
|
2019-10-07 06:41:33 +00:00
|
|
|
NodeExpansionRequired: false,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-08-04 04:25:28 +00:00
|
|
|
|
|
|
|
// CreateSnapshot creates the snapshot in backend and stores metadata
|
|
|
|
// in store
|
2023-06-02 09:49:22 +00:00
|
|
|
//
|
2023-10-18 08:15:53 +00:00
|
|
|
//nolint:gocyclo,cyclop // golangci-lint did not catch this earlier, needs to get fixed late
|
2021-06-25 10:18:59 +00:00
|
|
|
func (cs *ControllerServer) CreateSnapshot(
|
|
|
|
ctx context.Context,
|
2022-06-01 10:17:19 +00:00
|
|
|
req *csi.CreateSnapshotRequest,
|
|
|
|
) (*csi.CreateSnapshotResponse, error) {
|
2020-08-04 04:25:28 +00:00
|
|
|
if err := cs.validateSnapshotReq(ctx, req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cr, err := util.NewAdminCredentials(req.GetSecrets())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer cr.DeleteCredentials()
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
clusterData, err := store.GetClusterInformation(req.GetParameters())
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
requestName := req.GetName()
|
|
|
|
sourceVolID := req.GetSourceVolumeId()
|
|
|
|
// Existence and conflict checks
|
|
|
|
if acquired := cs.SnapshotLocks.TryAcquire(requestName); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.SnapshotOperationAlreadyExistsFmt, requestName)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.SnapshotOperationAlreadyExistsFmt, requestName)
|
|
|
|
}
|
|
|
|
defer cs.SnapshotLocks.Release(requestName)
|
|
|
|
|
|
|
|
if err = cs.OperationLocks.GetSnapshotCreateLock(sourceVolID); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cs.OperationLocks.ReleaseSnapshotCreateLock(sourceVolID)
|
|
|
|
|
|
|
|
// Find the volume using the provided VolumeID
|
2022-06-14 13:23:29 +00:00
|
|
|
parentVolOptions, vid, err := store.NewVolumeOptionsFromVolID(ctx,
|
2022-07-28 12:05:33 +00:00
|
|
|
sourceVolID, nil, req.GetSecrets(), cs.ClusterName, cs.SetMetadata)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, util.ErrPoolNotFound) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.WarningLog(ctx, "failed to get backend volume for %s: %v", sourceVolID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2021-08-25 06:46:03 +00:00
|
|
|
if errors.Is(err, cerrors.ErrVolumeNotFound) {
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2020-08-05 10:10:04 +00:00
|
|
|
defer parentVolOptions.Destroy()
|
2020-08-04 04:25:28 +00:00
|
|
|
|
|
|
|
if clusterData.ClusterID != parentVolOptions.ClusterID {
|
2021-06-25 10:18:59 +00:00
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
"requested cluster id %s not matching subvolume cluster id %s",
|
|
|
|
clusterData.ClusterID,
|
|
|
|
parentVolOptions.ClusterID)
|
2020-08-04 04:25:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if parentVolOptions.BackingSnapshot {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "cannot snapshot a snapshot-backed volume")
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
cephfsSnap, genSnapErr := store.GenSnapFromOptions(ctx, req)
|
2020-08-04 04:25:28 +00:00
|
|
|
if genSnapErr != nil {
|
|
|
|
return nil, status.Error(codes.Internal, genSnapErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock out parallel snapshot create operations
|
|
|
|
if acquired := cs.VolumeLocks.TryAcquire(sourceVolID); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, sourceVolID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, sourceVolID)
|
|
|
|
}
|
|
|
|
defer cs.VolumeLocks.Release(sourceVolID)
|
|
|
|
snapName := req.GetName()
|
2023-10-30 08:45:18 +00:00
|
|
|
sid, err := store.CheckSnapExists(ctx, parentVolOptions, cephfsSnap, cs.ClusterName, cs.SetMetadata, cr)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:05:33 +00:00
|
|
|
volClient := core.NewSubVolume(parentVolOptions.GetConnection(), &parentVolOptions.SubVolume,
|
|
|
|
parentVolOptions.ClusterID, cs.ClusterName, cs.SetMetadata)
|
2022-02-15 12:11:09 +00:00
|
|
|
info, err := volClient.GetSubVolumeInfo(ctx)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
|
|
|
if sid != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
errDefer := store.UndoSnapReservation(ctx, parentVolOptions, *sid, snapName, cr)
|
2020-08-04 04:25:28 +00:00
|
|
|
if errDefer != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.WarningLog(ctx, "failed undoing reservation of snapshot: %s (%s)",
|
2020-08-04 04:25:28 +00:00
|
|
|
requestName, errDefer)
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2022-06-08 14:22:55 +00:00
|
|
|
metadata := k8s.GetSnapshotMetadata(req.GetParameters())
|
2020-08-04 04:25:28 +00:00
|
|
|
if sid != nil {
|
2022-06-08 14:22:55 +00:00
|
|
|
// Update snapshot-name/snapshot-namespace/snapshotcontent-name details on
|
|
|
|
// subvolume snapshot as metadata in case snapshot already exist
|
|
|
|
if len(metadata) != 0 {
|
2023-10-18 08:15:53 +00:00
|
|
|
snapClient := core.NewSnapshot(parentVolOptions.GetConnection(), sid.FsSnapshotName,
|
|
|
|
parentVolOptions.ClusterID, cs.ClusterName, cs.SetMetadata, &parentVolOptions.SubVolume)
|
2022-06-08 14:22:55 +00:00
|
|
|
err = snapClient.SetAllSnapshotMetadata(metadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return &csi.CreateSnapshotResponse{
|
|
|
|
Snapshot: &csi.Snapshot{
|
2020-10-10 16:17:08 +00:00
|
|
|
SizeBytes: info.BytesQuota,
|
2020-08-04 04:25:28 +00:00
|
|
|
SnapshotId: sid.SnapshotID,
|
|
|
|
SourceVolumeId: req.GetSourceVolumeId(),
|
|
|
|
CreationTime: sid.CreationTime,
|
2023-10-18 08:15:53 +00:00
|
|
|
ReadyToUse: true,
|
2020-08-04 04:25:28 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reservation
|
2022-02-15 12:11:09 +00:00
|
|
|
sID, err := store.ReserveSnap(ctx, parentVolOptions, vid.FsSubvolName, cephfsSnap, cr)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
errDefer := store.UndoSnapReservation(ctx, parentVolOptions, *sID, snapName, cr)
|
2020-08-04 04:25:28 +00:00
|
|
|
if errDefer != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.WarningLog(ctx, "failed undoing reservation of snapshot: %s (%s)",
|
2020-08-04 04:25:28 +00:00
|
|
|
requestName, errDefer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2022-07-28 12:26:55 +00:00
|
|
|
snap, err := cs.doSnapshot(ctx, parentVolOptions, sID.FsSnapshotName, metadata)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
// Use same encryption KMS than source volume and copy the passphrase. The passphrase becomes
|
|
|
|
// available under the snapshot id for CreateVolume to use this snap as a backing volume
|
|
|
|
snapVolOptions := store.VolumeOptions{}
|
2024-03-04 15:13:31 +00:00
|
|
|
err = parentVolOptions.CopyEncryptionConfig(ctx, &snapVolOptions, sourceVolID, sID.SnapshotID)
|
2022-08-12 14:31:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return &csi.CreateSnapshotResponse{
|
|
|
|
Snapshot: &csi.Snapshot{
|
2020-10-10 16:17:08 +00:00
|
|
|
SizeBytes: info.BytesQuota,
|
2020-08-04 04:25:28 +00:00
|
|
|
SnapshotId: sID.SnapshotID,
|
|
|
|
SourceVolumeId: req.GetSourceVolumeId(),
|
|
|
|
CreationTime: snap.CreationTime,
|
|
|
|
ReadyToUse: true,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:26:55 +00:00
|
|
|
func (cs *ControllerServer) doSnapshot(
|
2021-09-16 13:47:57 +00:00
|
|
|
ctx context.Context,
|
2022-02-15 12:11:09 +00:00
|
|
|
volOpt *store.VolumeOptions,
|
2022-07-28 12:26:55 +00:00
|
|
|
snapshotName string,
|
2022-06-07 07:59:22 +00:00
|
|
|
metadata map[string]string,
|
2022-06-01 10:17:19 +00:00
|
|
|
) (core.SnapshotInfo, error) {
|
2021-09-16 13:47:57 +00:00
|
|
|
snapID := fsutil.VolumeID(snapshotName)
|
|
|
|
snap := core.SnapshotInfo{}
|
2022-07-28 08:44:52 +00:00
|
|
|
snapClient := core.NewSnapshot(volOpt.GetConnection(), snapshotName,
|
2022-07-28 12:26:55 +00:00
|
|
|
volOpt.ClusterID, cs.ClusterName, cs.SetMetadata, &volOpt.SubVolume)
|
2022-02-15 12:11:09 +00:00
|
|
|
err := snapClient.CreateSnapshot(ctx)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to create snapshot %s %v", snapID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
dErr := snapClient.DeleteSnapshot(ctx)
|
2020-08-04 04:25:28 +00:00
|
|
|
if dErr != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapID, err)
|
2020-08-04 04:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2022-02-15 12:11:09 +00:00
|
|
|
snap, err = snapClient.GetSnapshotInfo(ctx)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to get snapshot info %s %v", snapID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return snap, fmt.Errorf("failed to get snapshot info for snapshot:%s", snapID)
|
|
|
|
}
|
2022-11-11 10:40:24 +00:00
|
|
|
snap.CreationTime = timestamppb.New(snap.CreatedAt)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-06-07 07:59:22 +00:00
|
|
|
// Set snapshot-name/snapshot-namespace/snapshotcontent-name details
|
|
|
|
// on subvolume snapshot as metadata on create
|
|
|
|
if len(metadata) != 0 {
|
|
|
|
err = snapClient.SetAllSnapshotMetadata(metadata)
|
|
|
|
if err != nil {
|
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return snap, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ControllerServer) validateSnapshotReq(ctx context.Context, req *csi.CreateSnapshotRequest) error {
|
2021-06-25 10:18:59 +00:00
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(
|
|
|
|
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "invalid create snapshot req: %v", protosanitizer.StripSecrets(req))
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check sanity of request Snapshot Name, Source Volume Id
|
2024-04-04 08:50:20 +00:00
|
|
|
if req.GetName() == "" {
|
2020-08-04 04:25:28 +00:00
|
|
|
return status.Error(codes.NotFound, "snapshot Name cannot be empty")
|
|
|
|
}
|
2024-04-04 08:50:20 +00:00
|
|
|
if req.GetSourceVolumeId() == "" {
|
2020-08-04 04:25:28 +00:00
|
|
|
return status.Error(codes.NotFound, "source Volume ID cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSnapshot deletes the snapshot in backend and removes the
|
|
|
|
// snapshot metadata from store.
|
2021-06-25 10:18:59 +00:00
|
|
|
func (cs *ControllerServer) DeleteSnapshot(
|
|
|
|
ctx context.Context,
|
2022-06-01 10:17:19 +00:00
|
|
|
req *csi.DeleteSnapshotRequest,
|
|
|
|
) (*csi.DeleteSnapshotResponse, error) {
|
2021-06-25 10:18:59 +00:00
|
|
|
if err := cs.Driver.ValidateControllerServiceRequest(
|
|
|
|
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "invalid delete snapshot req: %v", protosanitizer.StripSecrets(req))
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cr, err := util.NewAdminCredentials(req.GetSecrets())
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
defer cr.DeleteCredentials()
|
|
|
|
snapshotID := req.GetSnapshotId()
|
|
|
|
if snapshotID == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "snapshot ID cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if acquired := cs.SnapshotLocks.TryAcquire(snapshotID); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.SnapshotOperationAlreadyExistsFmt, snapshotID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.SnapshotOperationAlreadyExistsFmt, snapshotID)
|
|
|
|
}
|
|
|
|
defer cs.SnapshotLocks.Release(snapshotID)
|
|
|
|
|
|
|
|
// lock out snapshotID for restore operation
|
|
|
|
if err = cs.OperationLocks.GetDeleteLock(snapshotID); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
|
|
|
defer cs.OperationLocks.ReleaseDeleteLock(snapshotID)
|
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
volOpt, snapInfo, sid, err := store.NewSnapshotOptionsFromID(ctx, snapshotID, cr,
|
|
|
|
req.GetSecrets(), cs.ClusterName, cs.SetMetadata)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
2021-04-04 07:14:33 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, util.ErrPoolNotFound):
|
|
|
|
// if error is ErrPoolNotFound, the pool is already deleted we dont
|
|
|
|
// need to worry about deleting snapshot or omap data, return success
|
2021-08-24 15:03:25 +00:00
|
|
|
log.WarningLog(ctx, "failed to get backend snapshot for %s: %v", snapshotID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return &csi.DeleteSnapshotResponse{}, nil
|
2021-04-04 07:14:33 +00:00
|
|
|
case errors.Is(err, util.ErrKeyNotFound):
|
|
|
|
// if error is ErrKeyNotFound, then a previous attempt at deletion was complete
|
|
|
|
// or partially complete (snap and snapOMap are garbage collected already), hence return
|
|
|
|
// success as deletion is complete
|
2020-08-04 04:25:28 +00:00
|
|
|
return &csi.DeleteSnapshotResponse{}, nil
|
2021-08-25 06:46:03 +00:00
|
|
|
case errors.Is(err, cerrors.ErrSnapNotFound):
|
2022-03-29 16:19:31 +00:00
|
|
|
err = store.UndoSnapReservation(ctx, volOpt, *sid, sid.RequestName, cr)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to remove reservation for snapname (%s) with backing snap (%s) (%s)",
|
2022-03-29 16:19:31 +00:00
|
|
|
sid.RequestName, sid.FsSnapshotName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return &csi.DeleteSnapshotResponse{}, nil
|
2021-08-25 06:46:03 +00:00
|
|
|
case errors.Is(err, cerrors.ErrVolumeNotFound):
|
2021-04-04 07:14:33 +00:00
|
|
|
// if the error is ErrVolumeNotFound, the subvolume is already deleted
|
|
|
|
// from backend, Hence undo the omap entries and return success
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "Volume not present")
|
2022-03-29 16:19:31 +00:00
|
|
|
err = store.UndoSnapReservation(ctx, volOpt, *sid, sid.RequestName, cr)
|
2021-02-10 08:19:40 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to remove reservation for snapname (%s) with backing snap (%s) (%s)",
|
2022-03-29 16:19:31 +00:00
|
|
|
sid.RequestName, sid.FsSnapshotName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-10 08:19:40 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-10 08:19:40 +00:00
|
|
|
return &csi.DeleteSnapshotResponse{}, nil
|
2021-04-04 07:14:33 +00:00
|
|
|
default:
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2021-02-10 08:19:40 +00:00
|
|
|
}
|
2020-08-04 04:25:28 +00:00
|
|
|
}
|
2020-10-19 07:08:57 +00:00
|
|
|
defer volOpt.Destroy()
|
2020-08-04 04:25:28 +00:00
|
|
|
|
|
|
|
// safeguard against parallel create or delete requests against the same
|
|
|
|
// name
|
|
|
|
if acquired := cs.SnapshotLocks.TryAcquire(sid.RequestName); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.SnapshotOperationAlreadyExistsFmt, sid.RequestName)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, sid.RequestName)
|
|
|
|
}
|
|
|
|
defer cs.SnapshotLocks.Release(sid.RequestName)
|
|
|
|
|
|
|
|
if snapInfo.HasPendingClones == "yes" {
|
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, "snapshot %s has pending clones", snapshotID)
|
|
|
|
}
|
2022-04-06 13:26:07 +00:00
|
|
|
|
|
|
|
needsDelete, err := store.UnrefSelfInSnapshotBackedVolumes(ctx, volOpt, sid.SnapshotID)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
2022-04-06 13:26:07 +00:00
|
|
|
if errors.Is(err, rterrors.ErrObjectOutOfDate) {
|
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
|
|
|
|
2020-08-04 04:25:28 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2022-04-06 13:26:07 +00:00
|
|
|
|
|
|
|
if needsDelete {
|
2023-10-18 08:15:53 +00:00
|
|
|
snapClient := core.NewSnapshot(volOpt.GetConnection(), sid.FsSnapshotName,
|
|
|
|
volOpt.ClusterID, cs.ClusterName, cs.SetMetadata, &volOpt.SubVolume)
|
2022-04-06 13:26:07 +00:00
|
|
|
err = deleteSnapshotAndUndoReservation(
|
|
|
|
ctx,
|
|
|
|
snapClient,
|
|
|
|
volOpt,
|
|
|
|
sid,
|
|
|
|
cr,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &csi.DeleteSnapshotResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteSnapshotAndUndoReservation(
|
|
|
|
ctx context.Context,
|
|
|
|
snapClient core.SnapshotClient,
|
|
|
|
parentVolOptions *store.VolumeOptions,
|
|
|
|
snapID *store.SnapshotIdentifier,
|
|
|
|
cr *util.Credentials,
|
|
|
|
) error {
|
|
|
|
err := snapClient.DeleteSnapshot(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.UndoSnapReservation(ctx, parentVolOptions, *snapID, snapID.RequestName, cr)
|
2020-08-04 04:25:28 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to remove reservation for snapname (%s) with backing snap (%s) (%s)",
|
2022-04-06 13:26:07 +00:00
|
|
|
snapID.RequestName, snapID.RequestName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
return err
|
2020-08-04 04:25:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
return nil
|
2020-08-04 04:25:28 +00:00
|
|
|
}
|