diff --git a/internal/rbd/controllerserver.go b/internal/rbd/controllerserver.go index 3ec067c3ecf..cc0fa1f53de 100644 --- a/internal/rbd/controllerserver.go +++ b/internal/rbd/controllerserver.go @@ -1471,23 +1471,13 @@ func (cs *ControllerServer) DeleteSnapshot( // Deleting snapshot and cloned volume log.DebugLog(ctx, "deleting cloned rbd volume %s", rbdSnap.RbdSnapName) - rbdVol := rbdSnap.toVolume() - - err = rbdVol.Connect(cr) - if err != nil { - return nil, status.Error(codes.Internal, err.Error()) - } - defer rbdVol.Destroy(ctx) - - rbdVol.ImageID = rbdSnap.ImageID - // update parent name to delete the snapshot - rbdSnap.RbdImageName = rbdVol.RbdImageName - err = cleanUpSnapshot(ctx, rbdVol, rbdSnap, rbdVol) + err = rbdSnap.Delete(ctx) if err != nil { - log.ErrorLog(ctx, "failed to delete image: %v", err) + log.ErrorLog(ctx, "failed to delete rbd snapshot: %s with error: %v", rbdSnap, err) return nil, status.Error(codes.Internal, err.Error()) } + err = undoSnapReservation(ctx, rbdSnap, cr) if err != nil { log.ErrorLog(ctx, "failed to remove reservation for snapname (%s) with backing snap (%s) on image (%s) (%s)", diff --git a/internal/rbd/snapshot.go b/internal/rbd/snapshot.go index 6a06cefc288..92e5fe4ce6d 100644 --- a/internal/rbd/snapshot.go +++ b/internal/rbd/snapshot.go @@ -162,6 +162,30 @@ func (rbdSnap *rbdSnapshot) ToCSI(ctx context.Context) (*csi.Snapshot, error) { }, nil } +// Delete removes the snapshot from the RBD image and then +// the RBD image itself. +func (rbdSnap *rbdSnapshot) Delete(ctx context.Context) error { + rbdVol := rbdSnap.toVolume() + + err := rbdVol.Connect(rbdSnap.conn.Creds) + if err != nil { + return err + } + defer rbdVol.Destroy(ctx) + + rbdVol.ImageID = rbdSnap.ImageID + // update parent name to delete the snapshot + rbdSnap.RbdImageName = rbdVol.RbdImageName + err = cleanUpSnapshot(ctx, rbdVol, rbdSnap, rbdVol) + if err != nil { + log.ErrorLog(ctx, "failed to delete image: %v", err) + + return err + } + + return nil +} + func undoSnapshotCloning( ctx context.Context, parentVol *rbdVolume,