From 656ac2cce9d74d03896a5b10200aafee0b625197 Mon Sep 17 00:00:00 2001 From: ruslanloman Date: Thu, 6 Oct 2022 16:11:31 +0300 Subject: [PATCH] Cleanup snapshots before removing rbd image Ceph csi couldn't remove pvc with snapshots that was created directly in ceph using rbd or other clients. As a result, the rbd image remains permanently in the trash. Fixes: #3416 Signed-off-by: ruslanloman --- internal/rbd/rbd_util.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index d32c4d39136..17b0bfb6ad7 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -636,6 +636,13 @@ func (ri *rbdImage) deleteImage(ctx context.Context) error { return err } + // Make sure there are no snapshots for the image before removing, otherwise it's stuck in the trash. + err = ri.purgeSnapshots(ctx) + if err != nil { + log.ErrorLog(ctx, "failed to purge image snapshots: %s, error: %v", ri, err) + return err + } + rbdImage := librbd.GetImage(ri.ioctx, image) err = rbdImage.Trash(0) if err != nil { @@ -1413,6 +1420,32 @@ func (ri *rbdImage) deleteSnapshot(ctx context.Context, pOpts *rbdSnapshot) erro return err } +func (ri *rbdImage) purgeSnapshots(ctx context.Context) error { + rbdSnap := &rbdSnapshot{} + + snaps, err := ri.listSnapshots() + if err != nil { + log.DebugLog(ctx, "rbd: failed to get list of snapshots %v", err) + } + + for _, snap := range snaps { + log.DebugLog(ctx, "rbd: found image snapshot %s", snap.Name) + rbdSnap.RbdImageName = ri.RbdImageName + rbdSnap.Pool = ri.Pool + rbdSnap.RbdSnapName = snap.Name + rbdSnap.Monitors = ri.Monitors + + err = ri.deleteSnapshot(ctx, rbdSnap) + if err != nil { + log.ErrorLog(ctx, "failed to delete rbd image snapshot: %s, error: %v", ri, err) + + return err + } + } + + return nil +} + func (rv *rbdVolume) cloneRbdImageFromSnapshot( ctx context.Context, pSnapOpts *rbdSnapshot,