Skip to content

Commit

Permalink
Cleanup snapshots before removing rbd image
Browse files Browse the repository at this point in the history
  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]>
  • Loading branch information
ruslanloman committed Jan 4, 2023
1 parent 47b59ee commit 656ac2c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions internal/rbd/rbd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 656ac2c

Please sign in to comment.