From 5a4f3e1868fd8db2f13e00ddff6df5b1be77003e Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Thu, 2 Nov 2023 11:41:10 +0100 Subject: [PATCH] cephfs: remove subvolume during clone If any operations like Resize, Deleting snapshot fails, we need to remove both snapshot and the clone to avoid resource leak. closes: #4218 Signed-off-by: Madhu Rajanna --- internal/cephfs/core/clone.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/internal/cephfs/core/clone.go b/internal/cephfs/core/clone.go index b36951439dae..182e961310e2 100644 --- a/internal/cephfs/core/clone.go +++ b/internal/cephfs/core/clone.go @@ -68,11 +68,10 @@ func (s *subVolumeClient) CreateCloneFromSubvolume( return err } - // if cloneErr is not nil we will delete the snapshot - var cloneErr error - defer func() { - if cloneErr != nil { + // if any error occurs while cloning, resizing or deleting the snapshot + // fails then we need to delete the clone and snapshot. + if err != nil { if err = s.PurgeVolume(ctx, true); err != nil { log.ErrorLog(ctx, "failed to delete volume %s: %v", s.VolID, err) } @@ -81,18 +80,19 @@ func (s *subVolumeClient) CreateCloneFromSubvolume( } } }() - cloneErr = snapClient.CloneSnapshot(ctx, s.SubVolume) - if cloneErr != nil { - log.ErrorLog(ctx, "failed to clone snapshot %s %s to %s %v", parentvolOpt.VolID, snapshotID, s.VolID, cloneErr) + err = snapClient.CloneSnapshot(ctx, s.SubVolume) + if err != nil { + log.ErrorLog(ctx, "failed to clone snapshot %s %s to %s %v", parentvolOpt.VolID, snapshotID, s.VolID, err) - return cloneErr + return err } - cloneState, cloneErr := s.GetCloneState(ctx) - if cloneErr != nil { - log.ErrorLog(ctx, "failed to get clone state: %v", cloneErr) + cloneState := cephFSCloneState{} + cloneState, err = s.GetCloneState(ctx) + if err != nil { + log.ErrorLog(ctx, "failed to get clone state: %v", err) - return cloneErr + return err } err = cloneState.ToError() @@ -157,8 +157,9 @@ func (s *subVolumeClient) CreateCloneFromSnapshot( } } }() - - cloneState, err := s.GetCloneState(ctx) + cloneState := cephFSCloneState{} + // avoid err variable shadowing + cloneState, err = s.GetCloneState(ctx) if err != nil { log.ErrorLog(ctx, "failed to get clone state: %v", err)