From ba05c0f5f18bb09a07fe1a2aebf198289498669a Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Tue, 26 Mar 2024 09:51:19 +0100 Subject: [PATCH] cleanup: reformat generateVolFromSnap() to rbdSnapshot.toVolume() Signed-off-by: Niels de Vos --- internal/rbd/controllerserver.go | 10 ++++----- internal/rbd/rbd_journal.go | 2 +- internal/rbd/rbd_util.go | 2 +- internal/rbd/snapshot.go | 35 ++++++++++++++++---------------- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/internal/rbd/controllerserver.go b/internal/rbd/controllerserver.go index 89f0191ec71..01d4b6c583b 100644 --- a/internal/rbd/controllerserver.go +++ b/internal/rbd/controllerserver.go @@ -666,7 +666,7 @@ func (cs *ControllerServer) createVolumeFromSnapshot( // update parent name(rbd image name in snapshot) rbdSnap.RbdImageName = rbdSnap.RbdSnapName - parentVol := generateVolFromSnap(rbdSnap) + parentVol := rbdSnap.toVolume() // as we are operating on single cluster reuse the connection parentVol.conn = rbdVol.conn.Copy() @@ -1237,7 +1237,7 @@ func cloneFromSnapshot( cr *util.Credentials, parameters map[string]string, ) (*csi.CreateSnapshotResponse, error) { - vol := generateVolFromSnap(rbdSnap) + vol := rbdSnap.toVolume() err := vol.Connect(cr) if err != nil { uErr := undoSnapshotCloning(ctx, rbdVol, rbdSnap, vol, cr) @@ -1322,7 +1322,7 @@ func (cs *ControllerServer) doSnapshotClone( cr *util.Credentials, ) (*rbdVolume, error) { // generate cloned volume details from snapshot - cloneRbd := generateVolFromSnap(rbdSnap) + cloneRbd := rbdSnap.toVolume() defer cloneRbd.Destroy() // add image feature for cloneRbd f := []string{librbd.FeatureNameLayering, librbd.FeatureNameDeepFlatten} @@ -1480,7 +1480,7 @@ func (cs *ControllerServer) DeleteSnapshot( // Deleting snapshot and cloned volume log.DebugLog(ctx, "deleting cloned rbd volume %s", rbdSnap.RbdSnapName) - rbdVol := generateVolFromSnap(rbdSnap) + rbdVol := rbdSnap.toVolume() err = rbdVol.Connect(cr) if err != nil { @@ -1511,7 +1511,7 @@ func (cs *ControllerServer) DeleteSnapshot( // cleanUpImageAndSnapReservation cleans up the image from the trash and // snapshot reservation in rados OMAP. func cleanUpImageAndSnapReservation(ctx context.Context, rbdSnap *rbdSnapshot, cr *util.Credentials) error { - rbdVol := generateVolFromSnap(rbdSnap) + rbdVol := rbdSnap.toVolume() err := rbdVol.Connect(cr) if err != nil { return status.Error(codes.Internal, err.Error()) diff --git a/internal/rbd/rbd_journal.go b/internal/rbd/rbd_journal.go index f7c2f86c7ce..04ad9138e17 100644 --- a/internal/rbd/rbd_journal.go +++ b/internal/rbd/rbd_journal.go @@ -162,7 +162,7 @@ func checkSnapCloneExists( snapData.ImagePool, rbdSnap.Pool) } - vol := generateVolFromSnap(rbdSnap) + vol := rbdSnap.toVolume() defer vol.Destroy() err = vol.Connect(cr) if err != nil { diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index 13e226bf4de..31b330b4d37 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -1053,7 +1053,7 @@ func genSnapFromSnapID( // updateSnapshotDetails will copy the details from the rbdVolume to the // rbdSnapshot. example copying size from rbdVolume to rbdSnapshot. func updateSnapshotDetails(rbdSnap *rbdSnapshot) error { - vol := generateVolFromSnap(rbdSnap) + vol := rbdSnap.toVolume() err := vol.Connect(rbdSnap.conn.Creds) if err != nil { return err diff --git a/internal/rbd/snapshot.go b/internal/rbd/snapshot.go index bb420475c3d..d6dcd48c20d 100644 --- a/internal/rbd/snapshot.go +++ b/internal/rbd/snapshot.go @@ -98,23 +98,24 @@ func cleanUpSnapshot( return nil } -func generateVolFromSnap(rbdSnap *rbdSnapshot) *rbdVolume { - vol := new(rbdVolume) - vol.ClusterID = rbdSnap.ClusterID - vol.VolID = rbdSnap.VolID - vol.Monitors = rbdSnap.Monitors - vol.Pool = rbdSnap.Pool - vol.JournalPool = rbdSnap.JournalPool - vol.RadosNamespace = rbdSnap.RadosNamespace - vol.RbdImageName = rbdSnap.RbdSnapName - vol.ImageID = rbdSnap.ImageID - // copyEncryptionConfig cannot be used here because the volume and the - // snapshot will have the same volumeID which cases the panic in - // copyEncryptionConfig function. - vol.blockEncryption = rbdSnap.blockEncryption - vol.fileEncryption = rbdSnap.fileEncryption - - return vol +func (rbdSnap *rbdSnapshot) toVolume() *rbdVolume { + return &rbdVolume{ + rbdImage: rbdImage{ + ClusterID: rbdSnap.ClusterID, + VolID: rbdSnap.VolID, + Monitors: rbdSnap.Monitors, + Pool: rbdSnap.Pool, + JournalPool: rbdSnap.JournalPool, + RadosNamespace: rbdSnap.RadosNamespace, + RbdImageName: rbdSnap.RbdSnapName, + ImageID: rbdSnap.ImageID, + // copyEncryptionConfig cannot be used here because the volume and the + // snapshot will have the same volumeID which cases the panic in + // copyEncryptionConfig function. + blockEncryption: rbdSnap.blockEncryption, + fileEncryption: rbdSnap.fileEncryption, + }, + } } func undoSnapshotCloning(