Skip to content

Commit 2b2a37d

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 47b59ee commit 2b2a37d

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
@@ -636,6 +636,13 @@ func (ri *rbdImage) deleteImage(ctx context.Context) error {
636636
return err
637637
}
638638

639+
// Make sure there are no snapshots for the image before removing, otherwise it's stuck in the trash.
640+
err = ri.purgeSnapshots(ctx)
641+
if err != nil {
642+
log.ErrorLog(ctx, "failed to purge image snapshots: %s, error: %v", ri, err)
643+
return err
644+
}
645+
639646
rbdImage := librbd.GetImage(ri.ioctx, image)
640647
err = rbdImage.Trash(0)
641648
if err != nil {
@@ -1413,6 +1420,30 @@ func (ri *rbdImage) deleteSnapshot(ctx context.Context, pOpts *rbdSnapshot) erro
14131420
return err
14141421
}
14151422

1423+
func (ri *rbdImage) purgeSnapshots(ctx context.Context) error {
1424+
rbdSnap := &rbdSnapshot{}
1425+
1426+
snaps, err := ri.listSnapshots()
1427+
if err != nil {
1428+
log.DebugLog(ctx, "rbd: failed to get list of snapshots %v", err)
1429+
}
1430+
1431+
for _, snap := range snaps {
1432+
log.DebugLog(ctx, "rbd: found image snapshot %s", snap.Name)
1433+
rbdSnap.RbdSnapName = snap.Name
1434+
rbdSnap.Monitors = ri.Monitors
1435+
1436+
err = ri.deleteSnapshot(ctx, rbdSnap)
1437+
if err != nil {
1438+
log.ErrorLog(ctx, "failed to delete rbd image snapshot: %s, error: %v", ri, err)
1439+
1440+
return err
1441+
}
1442+
}
1443+
1444+
return nil
1445+
}
1446+
14161447
func (rv *rbdVolume) cloneRbdImageFromSnapshot(
14171448
ctx context.Context,
14181449
pSnapOpts *rbdSnapshot,

0 commit comments

Comments
 (0)