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 Oct 19, 2022
1 parent 0f2daca commit df98fe7
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions internal/rbd/rbd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,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 @@ -1442,6 +1449,30 @@ 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.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 df98fe7

Please sign in to comment.