cleanup: pass context to rbdImage.Destroy()

In the future we'll introduce a more standard interface for objects like
Volumes and Snapshots. It is useful to have the context passed as 1st
argument to all functions of those objects, including their Destroy()
function.

Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
Niels de Vos 2024-03-21 15:01:56 +01:00 committed by mergify[bot]
parent d5849a4801
commit 3aece2f38e
8 changed files with 37 additions and 37 deletions

View File

@ -68,7 +68,7 @@ func (rscs *ReclaimSpaceControllerServer) ControllerReclaimSpace(
if err != nil { if err != nil {
return nil, status.Errorf(codes.Aborted, "failed to find volume with ID %q: %s", volumeID, err.Error()) return nil, status.Errorf(codes.Aborted, "failed to find volume with ID %q: %s", volumeID, err.Error())
} }
defer rbdVol.Destroy() defer rbdVol.Destroy(ctx)
err = rbdVol.Sparsify() err = rbdVol.Sparsify()
if errors.Is(err, rbdutil.ErrImageInUse) { if errors.Is(err, rbdutil.ErrImageInUse) {

View File

@ -273,7 +273,7 @@ func (rs *ReplicationServer) EnableVolumeReplication(ctx context.Context,
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
defer func() { defer func() {
if rbdVol != nil { if rbdVol != nil {
rbdVol.Destroy() rbdVol.Destroy(ctx)
} }
}() }()
if err != nil { if err != nil {
@ -350,7 +350,7 @@ func (rs *ReplicationServer) DisableVolumeReplication(ctx context.Context,
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
defer func() { defer func() {
if rbdVol != nil { if rbdVol != nil {
rbdVol.Destroy() rbdVol.Destroy(ctx)
} }
}() }()
if err != nil { if err != nil {
@ -425,7 +425,7 @@ func (rs *ReplicationServer) PromoteVolume(ctx context.Context,
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
defer func() { defer func() {
if rbdVol != nil { if rbdVol != nil {
rbdVol.Destroy() rbdVol.Destroy(ctx)
} }
}() }()
if err != nil { if err != nil {
@ -525,7 +525,7 @@ func (rs *ReplicationServer) DemoteVolume(ctx context.Context,
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
defer func() { defer func() {
if rbdVol != nil { if rbdVol != nil {
rbdVol.Destroy() rbdVol.Destroy(ctx)
} }
}() }()
if err != nil { if err != nil {
@ -642,7 +642,7 @@ func (rs *ReplicationServer) ResyncVolume(ctx context.Context,
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
defer func() { defer func() {
if rbdVol != nil { if rbdVol != nil {
rbdVol.Destroy() rbdVol.Destroy(ctx)
} }
}() }()
if err != nil { if err != nil {
@ -856,7 +856,7 @@ func (rs *ReplicationServer) GetVolumeReplicationInfo(ctx context.Context,
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
defer func() { defer func() {
if rbdVol != nil { if rbdVol != nil {
rbdVol.Destroy() rbdVol.Destroy(ctx)
} }
}() }()
if err != nil { if err != nil {

View File

@ -46,10 +46,10 @@ import (
func (rv *rbdVolume) checkCloneImage(ctx context.Context, parentVol *rbdVolume) (bool, error) { func (rv *rbdVolume) checkCloneImage(ctx context.Context, parentVol *rbdVolume) (bool, error) {
// generate temp cloned volume // generate temp cloned volume
tempClone := rv.generateTempClone() tempClone := rv.generateTempClone()
defer tempClone.Destroy() defer tempClone.Destroy(ctx)
snap := &rbdSnapshot{} snap := &rbdSnapshot{}
defer snap.Destroy() defer snap.Destroy(ctx)
snap.RbdSnapName = rv.RbdImageName snap.RbdSnapName = rv.RbdImageName
snap.Pool = rv.Pool snap.Pool = rv.Pool
@ -183,7 +183,7 @@ func (rv *rbdVolume) doSnapClone(ctx context.Context, parentVol *rbdVolume) erro
// generate temp cloned volume // generate temp cloned volume
tempClone := rv.generateTempClone() tempClone := rv.generateTempClone()
defer tempClone.Destroy() defer tempClone.Destroy(ctx)
// snapshot name is same as temporary cloned image, This helps to // snapshot name is same as temporary cloned image, This helps to
// flatten the temporary cloned images as we cannot have more than 510 // flatten the temporary cloned images as we cannot have more than 510

View File

@ -335,7 +335,7 @@ func (cs *ControllerServer) CreateVolume(
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rbdVol.Destroy() defer rbdVol.Destroy(ctx)
// Existence and conflict checks // Existence and conflict checks
if acquired := cs.VolumeLocks.TryAcquire(req.GetName()); !acquired { if acquired := cs.VolumeLocks.TryAcquire(req.GetName()); !acquired {
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, req.GetName()) log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, req.GetName())
@ -349,10 +349,10 @@ func (cs *ControllerServer) CreateVolume(
return nil, err return nil, err
} }
if parentVol != nil { if parentVol != nil {
defer parentVol.Destroy() defer parentVol.Destroy(ctx)
} }
if rbdSnap != nil { if rbdSnap != nil {
defer rbdSnap.Destroy() defer rbdSnap.Destroy(ctx)
} }
err = updateTopologyConstraints(rbdVol, rbdSnap) err = updateTopologyConstraints(rbdVol, rbdSnap)
@ -465,7 +465,7 @@ func flattenParentImage(
// in case of any error call Destroy for cleanup. // in case of any error call Destroy for cleanup.
defer func() { defer func() {
if err != nil { if err != nil {
rbdSnap.Destroy() rbdSnap.Destroy(ctx)
} }
}() }()
@ -661,14 +661,14 @@ func (cs *ControllerServer) createVolumeFromSnapshot(
return status.Error(codes.Internal, err.Error()) return status.Error(codes.Internal, err.Error())
} }
defer rbdSnap.Destroy() defer rbdSnap.Destroy(ctx)
// update parent name(rbd image name in snapshot) // update parent name(rbd image name in snapshot)
rbdSnap.RbdImageName = rbdSnap.RbdSnapName rbdSnap.RbdImageName = rbdSnap.RbdSnapName
parentVol := rbdSnap.toVolume() parentVol := rbdSnap.toVolume()
// as we are operating on single cluster reuse the connection // as we are operating on single cluster reuse the connection
parentVol.conn = rbdVol.conn.Copy() parentVol.conn = rbdVol.conn.Copy()
defer parentVol.Destroy() defer parentVol.Destroy(ctx)
// create clone image and delete snapshot // create clone image and delete snapshot
err = rbdVol.cloneRbdImageFromSnapshot(ctx, rbdSnap, parentVol) err = rbdVol.cloneRbdImageFromSnapshot(ctx, rbdSnap, parentVol)
@ -944,7 +944,7 @@ func (cs *ControllerServer) DeleteVolume(
rbdVol, err := GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) rbdVol, err := GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
defer func() { defer func() {
if rbdVol != nil { if rbdVol != nil {
rbdVol.Destroy() rbdVol.Destroy(ctx)
} }
}() }()
if err != nil { if err != nil {
@ -1094,7 +1094,7 @@ func (cs *ControllerServer) CreateSnapshot(
rbdVol, err := GenVolFromVolID(ctx, req.GetSourceVolumeId(), cr, req.GetSecrets()) rbdVol, err := GenVolFromVolID(ctx, req.GetSourceVolumeId(), cr, req.GetSecrets())
defer func() { defer func() {
if rbdVol != nil { if rbdVol != nil {
rbdVol.Destroy() rbdVol.Destroy(ctx)
} }
}() }()
if err != nil { if err != nil {
@ -1237,7 +1237,7 @@ func cloneFromSnapshot(
return nil, status.Errorf(codes.Internal, err.Error()) return nil, status.Errorf(codes.Internal, err.Error())
} }
defer vol.Destroy() defer vol.Destroy(ctx)
err = rbdVol.copyEncryptionConfig(ctx, &vol.rbdImage, false) err = rbdVol.copyEncryptionConfig(ctx, &vol.rbdImage, false)
if err != nil { if err != nil {
@ -1313,7 +1313,7 @@ func (cs *ControllerServer) doSnapshotClone(
) (*rbdVolume, error) { ) (*rbdVolume, error) {
// generate cloned volume details from snapshot // generate cloned volume details from snapshot
cloneRbd := rbdSnap.toVolume() cloneRbd := rbdSnap.toVolume()
defer cloneRbd.Destroy() defer cloneRbd.Destroy(ctx)
// add image feature for cloneRbd // add image feature for cloneRbd
f := []string{librbd.FeatureNameLayering, librbd.FeatureNameDeepFlatten} f := []string{librbd.FeatureNameLayering, librbd.FeatureNameDeepFlatten}
cloneRbd.ImageFeatureSet = librbd.FeatureSetFromNames(f) cloneRbd.ImageFeatureSet = librbd.FeatureSetFromNames(f)
@ -1460,7 +1460,7 @@ func (cs *ControllerServer) DeleteSnapshot(
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }
defer rbdSnap.Destroy() defer rbdSnap.Destroy(ctx)
// safeguard against parallel create or delete requests against the same // safeguard against parallel create or delete requests against the same
// name // name
@ -1480,7 +1480,7 @@ func (cs *ControllerServer) DeleteSnapshot(
if err != nil { if err != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }
defer rbdVol.Destroy() defer rbdVol.Destroy(ctx)
rbdVol.ImageID = rbdSnap.ImageID rbdVol.ImageID = rbdSnap.ImageID
// update parent name to delete the snapshot // update parent name to delete the snapshot
@ -1510,7 +1510,7 @@ func cleanUpImageAndSnapReservation(ctx context.Context, rbdSnap *rbdSnapshot, c
if err != nil { if err != nil {
return status.Error(codes.Internal, err.Error()) return status.Error(codes.Internal, err.Error())
} }
defer rbdVol.Destroy() defer rbdVol.Destroy(ctx)
err = rbdVol.openIoctx() err = rbdVol.openIoctx()
if err != nil { if err != nil {
@ -1584,7 +1584,7 @@ func (cs *ControllerServer) ControllerExpandVolume(
return nil, err return nil, err
} }
defer rbdVol.Destroy() defer rbdVol.Destroy(ctx)
// NodeExpansion is needed for PersistentVolumes with, // NodeExpansion is needed for PersistentVolumes with,
// 1. Filesystem VolumeMode with & without Encryption and // 1. Filesystem VolumeMode with & without Encryption and

View File

@ -81,7 +81,7 @@ func deleteMigratedVolume(ctx context.Context, parsedMigHandle *migrationVolID,
if err != nil { if err != nil {
return err return err
} }
defer rv.Destroy() defer rv.Destroy(ctx)
err = rv.deleteImage(ctx) err = rv.deleteImage(ctx)
if err != nil { if err != nil {
log.ErrorLog(ctx, "failed to delete rbd image: %s, err: %v", rv, err) log.ErrorLog(ctx, "failed to delete rbd image: %s, err: %v", rv, err)

View File

@ -198,7 +198,7 @@ func (ns *NodeServer) populateRbdVol(
} else { } else {
rv, err = GenVolFromVolID(ctx, volID, cr, req.GetSecrets()) rv, err = GenVolFromVolID(ctx, volID, cr, req.GetSecrets())
if err != nil { if err != nil {
rv.Destroy() rv.Destroy(ctx)
log.ErrorLog(ctx, "error generating volume %s: %v", volID, err) log.ErrorLog(ctx, "error generating volume %s: %v", volID, err)
return nil, status.Errorf(codes.Internal, "error generating volume %s: %v", volID, err) return nil, status.Errorf(codes.Internal, "error generating volume %s: %v", volID, err)
@ -221,7 +221,7 @@ func (ns *NodeServer) populateRbdVol(
// in case of any error call Destroy for cleanup. // in case of any error call Destroy for cleanup.
defer func() { defer func() {
if err != nil { if err != nil {
rv.Destroy() rv.Destroy(ctx)
} }
}() }()
// get the image details from the ceph cluster. // get the image details from the ceph cluster.
@ -345,7 +345,7 @@ func (ns *NodeServer) NodeStageVolume(
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rv.Destroy() defer rv.Destroy(ctx)
rv.NetNamespaceFilePath, err = util.GetRBDNetNamespaceFilePath(util.CsiConfigFile, rv.ClusterID) rv.NetNamespaceFilePath, err = util.GetRBDNetNamespaceFilePath(util.CsiConfigFile, rv.ClusterID)
if err != nil { if err != nil {

View File

@ -163,7 +163,7 @@ func checkSnapCloneExists(
} }
vol := rbdSnap.toVolume() vol := rbdSnap.toVolume()
defer vol.Destroy() defer vol.Destroy(ctx)
err = vol.Connect(cr) err = vol.Connect(cr)
if err != nil { if err != nil {
return false, err return false, err

View File

@ -385,7 +385,7 @@ func (ri *rbdImage) Connect(cr *util.Credentials) error {
// Destroy cleans up the rbdVolume and closes the connection to the Ceph // Destroy cleans up the rbdVolume and closes the connection to the Ceph
// cluster in case one was setup. // cluster in case one was setup.
func (ri *rbdImage) Destroy() { func (ri *rbdImage) Destroy(ctx context.Context) {
if ri.ioctx != nil { if ri.ioctx != nil {
ri.ioctx.Destroy() ri.ioctx.Destroy()
} }
@ -777,7 +777,7 @@ func flattenClonedRbdImages(
rv.Pool = pool rv.Pool = pool
rv.RbdImageName = rbdImageName rv.RbdImageName = rbdImageName
defer rv.Destroy() defer rv.Destroy(ctx)
err := rv.Connect(cr) err := rv.Connect(cr)
if err != nil { if err != nil {
log.ErrorLog(ctx, "failed to open connection %s; err %v", rv, err) log.ErrorLog(ctx, "failed to open connection %s; err %v", rv, err)
@ -1045,7 +1045,7 @@ func genSnapFromSnapID(
} }
defer func() { defer func() {
if err != nil { if err != nil {
rbdSnap.Destroy() rbdSnap.Destroy(ctx)
} }
}() }()
@ -1064,7 +1064,7 @@ func genSnapFromSnapID(
} }
} }
err = updateSnapshotDetails(rbdSnap) err = updateSnapshotDetails(ctx, rbdSnap)
if err != nil { if err != nil {
return rbdSnap, fmt.Errorf("failed to update snapshot details for %q: %w", rbdSnap, err) return rbdSnap, fmt.Errorf("failed to update snapshot details for %q: %w", rbdSnap, err)
} }
@ -1074,13 +1074,13 @@ func genSnapFromSnapID(
// updateSnapshotDetails will copy the details from the rbdVolume to the // updateSnapshotDetails will copy the details from the rbdVolume to the
// rbdSnapshot. example copying size from rbdVolume to rbdSnapshot. // rbdSnapshot. example copying size from rbdVolume to rbdSnapshot.
func updateSnapshotDetails(rbdSnap *rbdSnapshot) error { func updateSnapshotDetails(ctx context.Context, rbdSnap *rbdSnapshot) error {
vol := rbdSnap.toVolume() vol := rbdSnap.toVolume()
err := vol.Connect(rbdSnap.conn.Creds) err := vol.Connect(rbdSnap.conn.Creds)
if err != nil { if err != nil {
return err return err
} }
defer vol.Destroy() defer vol.Destroy(ctx)
err = vol.getImageInfo() err = vol.getImageInfo()
if err != nil { if err != nil {
@ -1685,7 +1685,7 @@ func (ri *rbdImage) flattenParent(ctx context.Context, hardLimit, softLimit uint
if parentImage == nil { if parentImage == nil {
return nil return nil
} }
defer parentImage.Destroy() defer parentImage.Destroy(ctx)
return parentImage.flattenRbdImage(ctx, false, hardLimit, softLimit) return parentImage.flattenRbdImage(ctx, false, hardLimit, softLimit)
} }
@ -2128,7 +2128,7 @@ func genVolFromVolIDWithMigration(
} }
rv, err := GenVolFromVolID(ctx, volID, cr, secrets) rv, err := GenVolFromVolID(ctx, volID, cr, secrets)
if err != nil { if err != nil {
rv.Destroy() rv.Destroy(ctx)
} }
return rv, err return rv, err