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 <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2021-05-26 10:33:14 +02:00 committed by mergify[bot]
parent 2e978e4211
commit 96a8ea3e88

View File

@ -257,23 +257,8 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
found, err := rbdVol.Exists(ctx, parentVol) found, err := rbdVol.Exists(ctx, parentVol)
if err != nil { if err != nil {
return nil, getGRPCErrorForCreateVolume(err) return nil, getGRPCErrorForCreateVolume(err)
} } else if found {
return cs.repairExistingVolume(ctx, req, cr, rbdVol, rbdSnap)
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
} }
err = validateRequestedVolumeSize(rbdVol, parentVol, rbdSnap, cr) err = validateRequestedVolumeSize(rbdVol, parentVol, rbdSnap, cr)
@ -332,6 +317,27 @@ func flattenParentImage(ctx context.Context, rbdVol *rbdVolume, cr *util.Credent
return nil 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 // 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 // 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 // snapshots are more than the `maxSnapshotsOnImage` Add a task to flatten all