cleanup: move copyEncryptionConfig() from CreateVolume to Exists()

The rbdVolume that needs its encryption configured is constructed in the
Exists() method. It is suitable to move the copyEncryptionConfig() call
there as well, so that the object is completely constructed in a single
place.

Golang-ci:gocyclo complained about the increased complexity of the
Exists() function. Moving the repairing of the ImageID into its own
helper function makes the code a little easier to understand.

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2021-04-08 15:04:32 +02:00 committed by mergify[bot]
parent 596410ae60
commit 52433841b4
2 changed files with 33 additions and 19 deletions

View File

@ -269,14 +269,6 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
}
}
if parentVol != nil && parentVol.isEncrypted() {
err = parentVol.copyEncryptionConfig(&rbdVol.rbdImage)
if err != nil {
util.ErrorLog(ctx, err.Error())
return nil, status.Error(codes.Internal, err.Error())
}
}
return buildCreateVolumeResponse(req, rbdVol), nil
}

View File

@ -300,17 +300,9 @@ func (rv *rbdVolume) Exists(ctx context.Context, parentVol *rbdVolume) (bool, er
return false, err
}
if rv.ImageID == "" {
err = rv.getImageID()
if err != nil {
util.ErrorLog(ctx, "failed to get image id %s: %v", rv, err)
return false, err
}
err = j.StoreImageID(ctx, rv.JournalPool, rv.ReservedID, rv.ImageID)
if err != nil {
util.ErrorLog(ctx, "failed to store volume id %s: %v", rv, err)
return false, err
}
err = rv.repairImageID(ctx, j)
if err != nil {
return false, err
}
// size checks
@ -327,12 +319,42 @@ func (rv *rbdVolume) Exists(ctx context.Context, parentVol *rbdVolume) (bool, er
return false, err
}
if parentVol != nil && parentVol.isEncrypted() {
err = parentVol.copyEncryptionConfig(&rv.rbdImage)
if err != nil {
util.ErrorLog(ctx, err.Error())
return false, err
}
}
util.DebugLog(ctx, "found existing volume (%s) with image name (%s) for request (%s)",
rv.VolID, rv.RbdImageName, rv.RequestName)
return true, nil
}
// repairImageID checks if rv.ImageID is already available (if so, it was
// fetched from the journal), in case it is missing, the imageID is obtained
// and stored in the journal.
func (rv *rbdVolume) repairImageID(ctx context.Context, j *journal.Connection) error {
if rv.ImageID != "" {
return nil
}
err := rv.getImageID()
if err != nil {
util.ErrorLog(ctx, "failed to get image id %s: %v", rv, err)
return err
}
err = j.StoreImageID(ctx, rv.JournalPool, rv.ReservedID, rv.ImageID)
if err != nil {
util.ErrorLog(ctx, "failed to store volume id %s: %v", rv, err)
return err
}
return nil
}
// reserveSnap is a helper routine to request a rbdSnapshot name reservation and generate the
// volume ID for the generated name.
func reserveSnap(ctx context.Context, rbdSnap *rbdSnapshot, rbdVol *rbdVolume, cr *util.Credentials) error {