mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 10:33:35 +00:00
rbd: Add support for smart cloning
Added support for RBD PVC to PVC cloning, below commands are executed to create a PVC-PVC clone from RBD side. * Check the depth(n) of the cloned image if n>=(hard limit -2) or ((soft limit-2) Add a task to flatten the image and return about (to avoid image leak) **Note** will try to flatten the temp clone image in the chain if available * Reserve the key and values in omap (this will help us to avoid the leak as it's not reserved earlier as we have returned ABORT (the request may not come back)) * Create a snapshot of rbd image * Clone the snapshot (temp clone) * Delete the snapshot * Snapshot the temp clone * Clone the snapshot (final clone) * Delete the snapshot ```bash 1) check the image depth of the parent image if flatten required add a task to flatten image and return ABORT to avoid leak (hardlimit-2 and softlimit-2 check will be done) 2) Reserve omap keys 2) rbd snap create <RBD image for src k8s volume>@<random snap name> 3) rbd clone --rbd-default-clone-format 2 --image-feature layering,deep-flatten <RBD image for src k8s volume>@<random snap> <RBD image for temporary snap image> 4) rbd snap rm <RBD image for src k8s volume>@<random snap name> 5) rbd snap create <cloned RBD image created in snapshot process>@<random snap name> 6) rbd clone --rbd-default-clone-format 2 --image-feature <k8s dst vol config> <RBD image for temporary snap image>@<random snap name> <RBD image for k8s dst vol> 7)rbd snap rm <RBD image for src k8s volume>@<random snap name> ``` * Delete temporary clone image created as part of clone(delete if present) * Delete rbd image Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
committed by
mergify[bot]
parent
9077c25c15
commit
a0fd805a8b
@ -161,6 +161,21 @@ func buildCreateVolumeResponse(ctx context.Context, req *csi.CreateVolumeRequest
|
||||
return &csi.CreateVolumeResponse{Volume: volume}, nil
|
||||
}
|
||||
|
||||
// getGRPCErrorForCreateVolume converts the returns the GRPC errors based on
|
||||
// the input error types it expected to use only for CreateVolume as we need to
|
||||
// return different GRPC codes for different functions based on the input.
|
||||
func getGRPCErrorForCreateVolume(err error) error {
|
||||
var evnc ErrVolNameConflict
|
||||
if errors.As(err, &evnc) {
|
||||
return status.Error(codes.AlreadyExists, err.Error())
|
||||
}
|
||||
var efip ErrFlattenInProgress
|
||||
if errors.As(err, &efip) {
|
||||
return status.Error(codes.Aborted, err.Error())
|
||||
}
|
||||
return status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
// CreateVolume creates the volume in backend
|
||||
func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
|
||||
if err := cs.validateVolumeReq(ctx, req); err != nil {
|
||||
@ -193,18 +208,14 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
rbdSnap, err := cs.checkSnapshotSource(ctx, req, cr)
|
||||
parentVol, rbdSnap, err := checkContentSource(ctx, req, cr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
found, err := rbdVol.Exists(ctx)
|
||||
found, err := rbdVol.Exists(ctx, parentVol)
|
||||
if err != nil {
|
||||
var evnc ErrVolNameConflict
|
||||
if errors.As(err, &evnc) {
|
||||
return nil, status.Error(codes.AlreadyExists, err.Error())
|
||||
}
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
return nil, getGRPCErrorForCreateVolume(err)
|
||||
}
|
||||
if found {
|
||||
if rbdSnap != nil {
|
||||
@ -217,6 +228,15 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
|
||||
return buildCreateVolumeResponse(ctx, req, rbdVol)
|
||||
}
|
||||
|
||||
if parentVol != nil {
|
||||
// flatten the image or its parent before the reservation to avoid
|
||||
// stale entries in post creation if we return ABORT error and the
|
||||
// delete volume is not called
|
||||
err = parentVol.flattenCloneImage(ctx)
|
||||
if err != nil {
|
||||
return nil, getGRPCErrorForCreateVolume(err)
|
||||
}
|
||||
}
|
||||
err = reserveVol(ctx, rbdVol, rbdSnap, cr)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
@ -233,7 +253,7 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
|
||||
}
|
||||
}()
|
||||
|
||||
err = cs.createBackingImage(ctx, cr, rbdVol, rbdSnap)
|
||||
err = cs.createBackingImage(ctx, cr, rbdVol, parentVol, rbdSnap)
|
||||
if err != nil {
|
||||
var efip ErrFlattenInProgress
|
||||
if errors.As(err, &efip) {
|
||||
@ -268,7 +288,7 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
|
||||
// return success,the hardlimit is reached it starts a task to flatten the
|
||||
// image and return Aborted
|
||||
func checkFlatten(ctx context.Context, rbdVol *rbdVolume, cr *util.Credentials) error {
|
||||
err := rbdVol.flattenRbdImage(ctx, cr, false)
|
||||
err := rbdVol.flattenRbdImage(ctx, cr, false, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
|
||||
if err != nil {
|
||||
var efip ErrFlattenInProgress
|
||||
if errors.As(err, &efip) {
|
||||
@ -318,7 +338,7 @@ func (cs *ControllerServer) createVolumeFromSnapshot(ctx context.Context, cr *ut
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *ControllerServer) createBackingImage(ctx context.Context, cr *util.Credentials, rbdVol *rbdVolume, rbdSnap *rbdSnapshot) error {
|
||||
func (cs *ControllerServer) createBackingImage(ctx context.Context, cr *util.Credentials, rbdVol, parentVol *rbdVolume, rbdSnap *rbdSnapshot) error {
|
||||
var err error
|
||||
|
||||
var j = &journal.Connection{}
|
||||
@ -328,12 +348,20 @@ func (cs *ControllerServer) createBackingImage(ctx context.Context, cr *util.Cre
|
||||
}
|
||||
defer j.Destroy()
|
||||
|
||||
// nolint:gocritic // this ifElseChain can not be rewritten to a switch statement
|
||||
if rbdSnap != nil {
|
||||
err = cs.createVolumeFromSnapshot(ctx, cr, rbdVol, rbdSnap.SnapID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
util.DebugLog(ctx, "created volume %s from snapshot %s", rbdVol.RequestName, rbdSnap.RbdSnapName)
|
||||
} else if parentVol != nil {
|
||||
if acquired := cs.VolumeLocks.TryAcquire(parentVol.VolID); !acquired {
|
||||
klog.Infof(util.Log(ctx, util.VolumeOperationAlreadyExistsFmt), parentVol.VolID)
|
||||
return status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, parentVol.VolID)
|
||||
}
|
||||
defer cs.VolumeLocks.Release(parentVol.VolID)
|
||||
return rbdVol.createCloneFromImage(ctx, parentVol)
|
||||
} else {
|
||||
err = createImage(ctx, rbdVol, cr)
|
||||
if err != nil {
|
||||
@ -367,7 +395,7 @@ func (cs *ControllerServer) createBackingImage(ctx context.Context, cr *util.Cre
|
||||
}
|
||||
|
||||
if rbdSnap != nil {
|
||||
err = rbdVol.flattenRbdImage(ctx, cr, false)
|
||||
err = rbdVol.flattenRbdImage(ctx, cr, false, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
|
||||
if err != nil {
|
||||
klog.Errorf(util.Log(ctx, "failed to flatten image %s: %v"), rbdVol, err)
|
||||
return err
|
||||
@ -384,38 +412,53 @@ func (cs *ControllerServer) createBackingImage(ctx context.Context, cr *util.Cre
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *ControllerServer) checkSnapshotSource(ctx context.Context, req *csi.CreateVolumeRequest,
|
||||
cr *util.Credentials) (*rbdSnapshot, error) {
|
||||
func checkContentSource(ctx context.Context, req *csi.CreateVolumeRequest, cr *util.Credentials) (*rbdVolume, *rbdSnapshot, error) {
|
||||
if req.VolumeContentSource == nil {
|
||||
return nil, nil
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
snapshot := req.VolumeContentSource.GetSnapshot()
|
||||
if snapshot == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "volume Snapshot cannot be empty")
|
||||
}
|
||||
|
||||
snapshotID := snapshot.GetSnapshotId()
|
||||
if snapshotID == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "volume Snapshot ID cannot be empty")
|
||||
}
|
||||
|
||||
rbdSnap := &rbdSnapshot{}
|
||||
if err := genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr); err != nil {
|
||||
var esnf ErrSnapNotFound
|
||||
if !errors.As(err, &esnf) {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
volumeSource := req.VolumeContentSource
|
||||
switch volumeSource.Type.(type) {
|
||||
case *csi.VolumeContentSource_Snapshot:
|
||||
snapshot := req.VolumeContentSource.GetSnapshot()
|
||||
if snapshot == nil {
|
||||
return nil, nil, status.Error(codes.NotFound, "volume Snapshot cannot be empty")
|
||||
}
|
||||
|
||||
var epnf util.ErrPoolNotFound
|
||||
if errors.As(err, &epnf) {
|
||||
snapshotID := snapshot.GetSnapshotId()
|
||||
if snapshotID == "" {
|
||||
return nil, nil, status.Errorf(codes.NotFound, "volume Snapshot ID cannot be empty")
|
||||
}
|
||||
rbdSnap := &rbdSnapshot{}
|
||||
if err := genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr); err != nil {
|
||||
klog.Errorf(util.Log(ctx, "failed to get backend snapshot for %s: %v"), snapshotID, err)
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
var esnf ErrSnapNotFound
|
||||
if !errors.As(err, &esnf) {
|
||||
return nil, nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
return nil, nil, status.Errorf(codes.NotFound, "%s snapshot doesnot exists", snapshotID)
|
||||
}
|
||||
|
||||
return nil, status.Error(codes.InvalidArgument, "missing requested Snapshot ID")
|
||||
return nil, rbdSnap, nil
|
||||
case *csi.VolumeContentSource_Volume:
|
||||
vol := req.VolumeContentSource.GetVolume()
|
||||
if vol == nil {
|
||||
return nil, nil, status.Error(codes.NotFound, "volume cannot be empty")
|
||||
}
|
||||
volID := vol.GetVolumeId()
|
||||
if volID == "" {
|
||||
return nil, nil, status.Errorf(codes.NotFound, "volume ID cannot be empty")
|
||||
}
|
||||
// TODO need to support cloning for encrypted volume
|
||||
rbdvol, err := genVolFromVolID(ctx, volID, cr, nil)
|
||||
if err != nil {
|
||||
klog.Errorf(util.Log(ctx, "failed to get backend image for %s: %v"), volID, err)
|
||||
var esnf ErrImageNotFound
|
||||
if !errors.As(err, &esnf) {
|
||||
return nil, nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
return nil, nil, status.Errorf(codes.NotFound, "%s image doesnot exists", volID)
|
||||
}
|
||||
return rbdvol, nil, nil
|
||||
}
|
||||
return rbdSnap, nil
|
||||
return nil, nil, status.Errorf(codes.InvalidArgument, "not a proper volume source")
|
||||
}
|
||||
|
||||
// DeleteLegacyVolume deletes a volume provisioned using version 1.0.0 of the plugin
|
||||
@ -573,6 +616,20 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
|
||||
return nil, status.Errorf(codes.Internal, "rbd %s is still being used", rbdVol.RbdImageName)
|
||||
}
|
||||
|
||||
// delete the temporary rbd image created as part of volume clone during
|
||||
// create volume
|
||||
var einf ErrImageNotFound
|
||||
tempClone := rbdVol.generateTempClone()
|
||||
err = deleteImage(ctx, tempClone, cr)
|
||||
if err != nil {
|
||||
// return error if it is not ErrImageNotFound
|
||||
if !errors.As(err, &einf) {
|
||||
klog.Errorf(util.Log(ctx, "failed to delete rbd image: %s with error: %v"),
|
||||
tempClone, err)
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Deleting rbd image
|
||||
util.DebugLog(ctx, "deleting image %s", rbdVol.RbdImageName)
|
||||
if err = deleteImage(ctx, rbdVol, cr); err != nil {
|
||||
@ -705,7 +762,7 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
|
||||
}
|
||||
defer vol.Destroy()
|
||||
|
||||
err = vol.flattenRbdImage(ctx, cr, false)
|
||||
err = vol.flattenRbdImage(ctx, cr, false, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
|
||||
var efip ErrFlattenInProgress
|
||||
if errors.As(err, &efip) {
|
||||
return &csi.CreateSnapshotResponse{
|
||||
@ -869,7 +926,7 @@ func (cs *ControllerServer) doSnapshotClone(ctx context.Context, parentVol *rbdV
|
||||
return ready, cloneRbd, err
|
||||
}
|
||||
|
||||
err = cloneRbd.flattenRbdImage(ctx, cr, false)
|
||||
err = cloneRbd.flattenRbdImage(ctx, cr, false, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
|
||||
if err != nil {
|
||||
var efip ErrFlattenInProgress
|
||||
if errors.As(err, &efip) {
|
||||
|
Reference in New Issue
Block a user