From 96a8ea3e88c24e7835166f6e3c1183173fae9838 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Wed, 26 May 2021 10:33:14 +0200 Subject: [PATCH] cleanup: split repairExistingVolume() from CreateVolume() Move the repairing of a volume/snapshot from CreateVolume to its own function. This reduces the complexity of the code, and makes the procedure easier to understand. Further enhancements to repairing an exsiting volume can be done in the new function. Signed-off-by: Niels de Vos --- internal/rbd/controllerserver.go | 40 ++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/internal/rbd/controllerserver.go b/internal/rbd/controllerserver.go index 5eb283868..fda9fce24 100644 --- a/internal/rbd/controllerserver.go +++ b/internal/rbd/controllerserver.go @@ -257,23 +257,8 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol found, err := rbdVol.Exists(ctx, parentVol) if err != nil { return nil, getGRPCErrorForCreateVolume(err) - } - - if found { - if rbdSnap != nil { - // check if image depth is reached limit and requires flatten - err = checkFlatten(ctx, rbdVol, cr) - if err != nil { - return nil, err - } - - err = rbdSnap.repairEncryptionConfig(&rbdVol.rbdImage) - if err != nil { - return nil, err - } - } - - return buildCreateVolumeResponse(req, rbdVol), nil + } else if found { + return cs.repairExistingVolume(ctx, req, cr, rbdVol, rbdSnap) } err = validateRequestedVolumeSize(rbdVol, parentVol, rbdSnap, cr) @@ -332,6 +317,27 @@ func flattenParentImage(ctx context.Context, rbdVol *rbdVolume, cr *util.Credent return nil } +// repairExistingVolume checks the existing volume or snapshot and makes sure +// that the state is corrected to what was requested. It is needed to call this +// when the process of creating a volume was interrupted. +func (cs *ControllerServer) repairExistingVolume(ctx context.Context, req *csi.CreateVolumeRequest, + cr *util.Credentials, rbdVol *rbdVolume, rbdSnap *rbdSnapshot) (*csi.CreateVolumeResponse, error) { + if rbdSnap != nil { + // check if image depth is reached limit and requires flatten + err := checkFlatten(ctx, rbdVol, cr) + if err != nil { + return nil, err + } + + err = rbdSnap.repairEncryptionConfig(&rbdVol.rbdImage) + if err != nil { + return nil, err + } + } + + return buildCreateVolumeResponse(req, rbdVol), nil +} + // check snapshots on the rbd image, as we have limit from krbd that an image // cannot have more than 510 snapshot at a given point of time. If the // snapshots are more than the `maxSnapshotsOnImage` Add a task to flatten all