cleanup: refractor checkCloneImage to reducing nesting if

This commit refractors checkCloneImage function to
address nestif linter issue.

Signed-off-by: Rakshith R <rar@redhat.com>
(cherry picked from commit 859d696279)
This commit is contained in:
Rakshith R 2021-07-14 14:41:50 +05:30 committed by mergify[bot]
parent 32faed322a
commit 33234c1b51

View File

@ -75,50 +75,52 @@ func (rv *rbdVolume) checkCloneImage(ctx context.Context, parentVol *rbdVolume)
} }
return true, nil return true, nil
case !errors.Is(err, ErrImageNotFound):
// any error other than image not found return error case errors.Is(err, ErrImageNotFound):
return false, err // as the temp clone does not exist,check snapshot exists on parent volume
} // snapshot name is same as temporary clone image
} else { snap.RbdImageName = tempClone.RbdImageName
// snap will be create after we flatten the temporary cloned image,no err = parentVol.checkSnapExists(snap)
// need to check for flatten here. if err == nil {
// as the snap exists,create clone image and delete temporary snapshot // the temp clone exists, delete it lets reserve a new ID and
// and add task to flatten temporary cloned image // create new resources for a cleaner approach
err = rv.cloneRbdImageFromSnapshot(ctx, snap, parentVol) err = parentVol.deleteSnapshot(ctx, snap)
if err != nil { }
util.ErrorLog(ctx, "failed to clone rbd image %s from snapshot %s: %v", rv.RbdImageName, snap.RbdSnapName, err) if errors.Is(err, ErrSnapNotFound) {
err = fmt.Errorf("failed to clone rbd image %s from snapshot %s: %w", rv.RbdImageName, snap.RbdSnapName, err) return false, nil
}
return false, err return false, err
}
err = tempClone.deleteSnapshot(ctx, snap)
if err != nil {
util.ErrorLog(ctx, "failed to delete snapshot: %v", err)
default:
// any error other than the above return error
return false, err return false, err
} }
// check image needs flatten, if yes add task to flatten the clone
err = rv.flattenRbdImage(ctx, rv.conn.Creds, false, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
if err != nil {
return false, err
}
return true, nil
} }
// as the temp clone does not exist,check snapshot exists on parent volume // snap will be create after we flatten the temporary cloned image,no
// snapshot name is same as temporary clone image // need to check for flatten here.
snap.RbdImageName = tempClone.RbdImageName // as the snap exists,create clone image and delete temporary snapshot
err = parentVol.checkSnapExists(snap) // and add task to flatten temporary cloned image
if err == nil { err = rv.cloneRbdImageFromSnapshot(ctx, snap, parentVol)
// the temp clone exists, delete it lets reserve a new ID and if err != nil {
// create new resources for a cleaner approach util.ErrorLog(ctx, "failed to clone rbd image %s from snapshot %s: %v", rv.RbdImageName, snap.RbdSnapName, err)
err = parentVol.deleteSnapshot(ctx, snap) err = fmt.Errorf("failed to clone rbd image %s from snapshot %s: %w", rv.RbdImageName, snap.RbdSnapName, err)
return false, err
} }
if errors.Is(err, ErrSnapNotFound) { err = tempClone.deleteSnapshot(ctx, snap)
return false, nil if err != nil {
util.ErrorLog(ctx, "failed to delete snapshot: %v", err)
return false, err
}
// check image needs flatten, if yes add task to flatten the clone
err = rv.flattenRbdImage(ctx, rv.conn.Creds, false, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
if err != nil {
return false, err
} }
return false, err return true, nil
} }
func (rv *rbdVolume) generateTempClone() *rbdVolume { func (rv *rbdVolume) generateTempClone() *rbdVolume {