cleanup: refactor deeply nested if statements in internal/cephfs

Refactored deeply nested if statement in internal/cephfs to
reduce cognitive complexity.

Signed-off-by: Rakshith R <rar@redhat.com>
This commit is contained in:
Rakshith R 2021-04-04 12:44:33 +05:30 committed by mergify[bot]
parent 0f7b653b4e
commit 2d1a572d11

View File

@ -689,20 +689,18 @@ func (cs *ControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
volOpt, snapInfo, sid, err := newSnapshotOptionsFromID(ctx, snapshotID, cr) volOpt, snapInfo, sid, err := newSnapshotOptionsFromID(ctx, snapshotID, cr)
if err != nil { if err != nil {
// if error is ErrPoolNotFound, the pool is already deleted we dont switch {
// need to worry about deleting snapshot or omap data, return success case errors.Is(err, util.ErrPoolNotFound):
if errors.Is(err, util.ErrPoolNotFound) { // if error is ErrPoolNotFound, the pool is already deleted we dont
// need to worry about deleting snapshot or omap data, return success
util.WarningLog(ctx, "failed to get backend snapshot for %s: %v", snapshotID, err) util.WarningLog(ctx, "failed to get backend snapshot for %s: %v", snapshotID, err)
return &csi.DeleteSnapshotResponse{}, nil return &csi.DeleteSnapshotResponse{}, nil
} case errors.Is(err, util.ErrKeyNotFound):
// if error is ErrKeyNotFound, then a previous attempt at deletion was complete
// if error is ErrKeyNotFound, then a previous attempt at deletion was complete // or partially complete (snap and snapOMap are garbage collected already), hence return
// or partially complete (snap and snapOMap are garbage collected already), hence return // success as deletion is complete
// success as deletion is complete
if errors.Is(err, util.ErrKeyNotFound) {
return &csi.DeleteSnapshotResponse{}, nil return &csi.DeleteSnapshotResponse{}, nil
} case errors.Is(err, ErrSnapNotFound):
if errors.Is(err, ErrSnapNotFound) {
err = undoSnapReservation(ctx, volOpt, *sid, sid.FsSnapshotName, cr) err = undoSnapReservation(ctx, volOpt, *sid, sid.FsSnapshotName, cr)
if err != nil { if err != nil {
util.ErrorLog(ctx, "failed to remove reservation for snapname (%s) with backing snap (%s) (%s)", util.ErrorLog(ctx, "failed to remove reservation for snapname (%s) with backing snap (%s) (%s)",
@ -710,10 +708,9 @@ func (cs *ControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }
return &csi.DeleteSnapshotResponse{}, nil return &csi.DeleteSnapshotResponse{}, nil
} case errors.Is(err, ErrVolumeNotFound):
// if the error is ErrVolumeNotFound, the subvolume is already deleted // if the error is ErrVolumeNotFound, the subvolume is already deleted
// from backend, Hence undo the omap entries and return success // from backend, Hence undo the omap entries and return success
if errors.Is(err, ErrVolumeNotFound) {
util.ErrorLog(ctx, "Volume not present") util.ErrorLog(ctx, "Volume not present")
err = undoSnapReservation(ctx, volOpt, *sid, sid.FsSnapshotName, cr) err = undoSnapReservation(ctx, volOpt, *sid, sid.FsSnapshotName, cr)
if err != nil { if err != nil {
@ -722,8 +719,9 @@ func (cs *ControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }
return &csi.DeleteSnapshotResponse{}, nil return &csi.DeleteSnapshotResponse{}, nil
default:
return nil, status.Error(codes.Internal, err.Error())
} }
return nil, status.Error(codes.Internal, err.Error())
} }
defer volOpt.Destroy() defer volOpt.Destroy()