diff --git a/internal/rbd/controllerserver.go b/internal/rbd/controllerserver.go index da464ad53e3e..87819ab79701 100644 --- a/internal/rbd/controllerserver.go +++ b/internal/rbd/controllerserver.go @@ -1447,9 +1447,7 @@ func (cs *ControllerServer) ControllerExpandVolume( return nil, status.Error(codes.InvalidArgument, err.Error()) } defer cr.DeleteCredentials() - - rbdVol, err := GenVolFromVolID(ctx, volID, cr, req.GetSecrets()) - defer rbdVol.Destroy() + rbdVol, err := genVolFromVolIDWithMigration(ctx, volID, cr, req.GetSecrets()) if err != nil { switch { case errors.Is(err, ErrImageNotFound): @@ -1463,6 +1461,7 @@ func (cs *ControllerServer) ControllerExpandVolume( return nil, err } + defer rbdVol.Destroy() // NodeExpansion is needed for PersistentVolumes with, // 1. Filesystem VolumeMode with & without Encryption and diff --git a/internal/rbd/migration.go b/internal/rbd/migration.go index ca8c48e4c5ca..ec401737863d 100644 --- a/internal/rbd/migration.go +++ b/internal/rbd/migration.go @@ -77,17 +77,8 @@ func parseMigrationVolID(vh string) (*migrationVolID, error) { // deleteMigratedVolume get rbd volume details from the migration volID // and delete the volume from the cluster, return err if there was an error on the process. func deleteMigratedVolume(ctx context.Context, parsedMigHandle *migrationVolID, cr *util.Credentials) error { - var err error - rv := &rbdVolume{} - - // fill details to rv struct from parsed migration handle - rv.RbdImageName = parsedMigHandle.imageName - rv.Pool = parsedMigHandle.poolName - rv.ClusterID = parsedMigHandle.clusterID - rv.Monitors, err = util.Mons(util.CsiConfigFile, rv.ClusterID) + rv, err := genVolFromMigVolID(ctx, parsedMigHandle) if err != nil { - log.ErrorLog(ctx, "failed to fetch monitors using clusterID: %s, err: %v", rv.ClusterID, err) - return err } @@ -99,7 +90,7 @@ func deleteMigratedVolume(ctx context.Context, parsedMigHandle *migrationVolID, return err } defer rv.Destroy() - // if connected , delete it + // if connected , delete it. err = deleteImage(ctx, rv, cr) if err != nil { log.ErrorLog(ctx, "failed to delete rbd image : %s, err: %v", rv.RbdImageName, err) @@ -109,3 +100,22 @@ func deleteMigratedVolume(ctx context.Context, parsedMigHandle *migrationVolID, return nil } + +// genVolFromMigVolID populate rbdVol struct from the migration volID. +func genVolFromMigVolID(ctx context.Context, migVolID *migrationVolID) (*rbdVolume, error) { + var err error + rv := &rbdVolume{} + + // fill details to rv struct from parsed migration handle + rv.RbdImageName = migVolID.imageName + rv.Pool = migVolID.poolName + rv.ClusterID = migVolID.clusterID + rv.Monitors, err = util.Mons(util.CsiConfigFile, rv.ClusterID) + if err != nil { + log.ErrorLog(ctx, "failed to fetch monitors using clusterID: %s, err: %v", rv.ClusterID, err) + + return nil, err + } + + return rv, nil +} diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index ef3a15416328..660bd4794568 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -2071,3 +2071,28 @@ func strategicActionOnLogFile(ctx context.Context, logStrategy, logFile string) log.ErrorLog(ctx, "unknown cephLogStrategy option %q: hint: 'remove'|'compress'|'preserve'", logStrategy) } } + +// genVolFromVolIDWithMigration populate a rbdVol structure based on the volID format. +func genVolFromVolIDWithMigration( + ctx context.Context, volID string, cr *util.Credentials, secrets map[string]string) (*rbdVolume, error) { + var err error + var rv *rbdVolume + if isMigrationVolID(volID) { + pmVolID, pErr := parseMigrationVolID(volID) + if pErr != nil { + return nil, pErr + } + rv, pErr = genVolFromMigVolID(ctx, pmVolID) + if pErr != nil { + return nil, pErr + } + } else { + rv, err = GenVolFromVolID(ctx, volID, cr, secrets) + if err != nil { + rv.Destroy() + return nil, err + } + } + + return rv, nil +}