Skip to content

Commit 3a02290

Browse files
committed
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: ceph#3416 Signed-off-by: ruslanloman <[email protected]>
1 parent 0f2daca commit 3a02290

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

internal/rbd/rbd_util.go

+31
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,13 @@ func (ri *rbdImage) deleteImage(ctx context.Context) error {
652652
return err
653653
}
654654

655+
// Make sure there are no snapshots for the image before removing, otherwise it's stuck in the trash.
656+
err = ri.purgeSnapshots(ctx)
657+
if err != nil {
658+
log.ErrorLog(ctx, "failed to purge image snapshots: %s, error: %v", ri, err)
659+
return err
660+
}
661+
655662
rbdImage := librbd.GetImage(ri.ioctx, image)
656663
err = rbdImage.Trash(0)
657664
if err != nil {
@@ -1442,6 +1449,30 @@ func (ri *rbdImage) deleteSnapshot(ctx context.Context, pOpts *rbdSnapshot) erro
14421449
return err
14431450
}
14441451

1452+
func (ri *rbdImage) purgeSnapshots(ctx context.Context) error {
1453+
rbdSnap := &rbdSnapshot{}
1454+
1455+
snaps, err := ri.listSnapshots()
1456+
if err != nil {
1457+
log.DebugLog(ctx, "rbd: failed to get list of snapshots %v", err)
1458+
}
1459+
1460+
for _, snap := range snaps {
1461+
log.DebugLog(ctx, "rbd: found image snapshot %s", snap.Name)
1462+
rbdSnap.RbdSnapName = snap.Name
1463+
rbdSnap.Monitors = ri.Monitors
1464+
1465+
err = ri.deleteSnapshot(ctx, rbdSnap)
1466+
if err != nil {
1467+
log.ErrorLog(ctx, "failed to delete rbd image snapshot: %s, error: %v", ri, err)
1468+
1469+
return err
1470+
}
1471+
}
1472+
1473+
return nil
1474+
}
1475+
14451476
func (rv *rbdVolume) cloneRbdImageFromSnapshot(
14461477
ctx context.Context,
14471478
pSnapOpts *rbdSnapshot,

0 commit comments

Comments
 (0)