mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 10:33:35 +00:00
rbd: simplify error handling
This change replaces the sentinel errors in rbd module with standard errors created with errors.New(). Related: #1203 Signed-off-by: Sven Anderson <sven@redhat.com>
This commit is contained in:
committed by
mergify[bot]
parent
dba2c27bcb
commit
92884f56f4
@ -167,12 +167,10 @@ func buildCreateVolumeResponse(ctx context.Context, req *csi.CreateVolumeRequest
|
||||
// the input error types it expected to use only for CreateVolume as we need to
|
||||
// return different GRPC codes for different functions based on the input.
|
||||
func getGRPCErrorForCreateVolume(err error) error {
|
||||
var evnc ErrVolNameConflict
|
||||
if errors.As(err, &evnc) {
|
||||
if errors.Is(err, ErrVolNameConflict) {
|
||||
return status.Error(codes.AlreadyExists, err.Error())
|
||||
}
|
||||
var efip ErrFlattenInProgress
|
||||
if errors.As(err, &efip) {
|
||||
if errors.Is(err, ErrFlattenInProgress) {
|
||||
return status.Error(codes.Aborted, err.Error())
|
||||
}
|
||||
return status.Error(codes.Internal, err.Error())
|
||||
@ -273,8 +271,7 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
var efip ErrFlattenInProgress
|
||||
if !errors.As(err, &efip) {
|
||||
if !errors.Is(err, ErrFlattenInProgress) {
|
||||
errDefer := undoVolReservation(ctx, rbdVol, cr)
|
||||
if errDefer != nil {
|
||||
klog.Warningf(util.Log(ctx, "failed undoing reservation of volume: %s (%s)"), req.GetName(), errDefer)
|
||||
@ -285,8 +282,7 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
|
||||
|
||||
err = cs.createBackingImage(ctx, cr, rbdVol, parentVol, rbdSnap)
|
||||
if err != nil {
|
||||
var efip ErrFlattenInProgress
|
||||
if errors.As(err, &efip) {
|
||||
if errors.Is(err, ErrFlattenInProgress) {
|
||||
return nil, status.Error(codes.Aborted, err.Error())
|
||||
}
|
||||
return nil, err
|
||||
@ -340,8 +336,7 @@ func flattenParentImage(ctx context.Context, rbdVol *rbdVolume, cr *util.Credent
|
||||
func flattenTemporaryClonedImages(ctx context.Context, rbdVol *rbdVolume, cr *util.Credentials) error {
|
||||
snaps, err := rbdVol.listSnapshots(ctx, cr)
|
||||
if err != nil {
|
||||
var einf ErrImageNotFound
|
||||
if errors.As(err, &einf) {
|
||||
if errors.Is(err, ErrImageNotFound) {
|
||||
return status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
return status.Error(codes.Internal, err.Error())
|
||||
@ -364,8 +359,7 @@ func flattenTemporaryClonedImages(ctx context.Context, rbdVol *rbdVolume, cr *ut
|
||||
func checkFlatten(ctx context.Context, rbdVol *rbdVolume, cr *util.Credentials) error {
|
||||
err := rbdVol.flattenRbdImage(ctx, cr, false, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
|
||||
if err != nil {
|
||||
var efip ErrFlattenInProgress
|
||||
if errors.As(err, &efip) {
|
||||
if errors.Is(err, ErrFlattenInProgress) {
|
||||
return status.Error(codes.Aborted, err.Error())
|
||||
}
|
||||
if errDefer := deleteImage(ctx, rbdVol, cr); errDefer != nil {
|
||||
@ -453,8 +447,7 @@ func (cs *ControllerServer) createBackingImage(ctx context.Context, cr *util.Cre
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
var efip ErrFlattenInProgress
|
||||
if !errors.As(err, &efip) {
|
||||
if !errors.Is(err, ErrFlattenInProgress) {
|
||||
if deleteErr := deleteImage(ctx, rbdVol, cr); deleteErr != nil {
|
||||
klog.Errorf(util.Log(ctx, "failed to delete rbd image: %s with error: %v"), rbdVol, deleteErr)
|
||||
}
|
||||
@ -509,8 +502,7 @@ func checkContentSource(ctx context.Context, req *csi.CreateVolumeRequest, cr *u
|
||||
rbdSnap := &rbdSnapshot{}
|
||||
if err := genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr); err != nil {
|
||||
klog.Errorf(util.Log(ctx, "failed to get backend snapshot for %s: %v"), snapshotID, err)
|
||||
var esnf ErrSnapNotFound
|
||||
if !errors.As(err, &esnf) {
|
||||
if !errors.Is(err, ErrSnapNotFound) {
|
||||
return nil, nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
return nil, nil, status.Errorf(codes.NotFound, "%s snapshot doesnot exists", snapshotID)
|
||||
@ -529,8 +521,7 @@ func checkContentSource(ctx context.Context, req *csi.CreateVolumeRequest, cr *u
|
||||
rbdvol, err := genVolFromVolID(ctx, volID, cr, nil)
|
||||
if err != nil {
|
||||
klog.Errorf(util.Log(ctx, "failed to get backend image for %s: %v"), volID, err)
|
||||
var esnf ErrImageNotFound
|
||||
if !errors.As(err, &esnf) {
|
||||
if !errors.Is(err, ErrImageNotFound) {
|
||||
return nil, nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
return nil, nil, status.Errorf(codes.NotFound, "%s image doesnot exists", volID)
|
||||
@ -594,8 +585,7 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
|
||||
}
|
||||
|
||||
// All errors other than ErrImageNotFound should return an error back to the caller
|
||||
var einf ErrImageNotFound
|
||||
if !errors.As(err, &einf) {
|
||||
if !errors.Is(err, ErrImageNotFound) {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
@ -635,12 +625,11 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
|
||||
|
||||
// delete the temporary rbd image created as part of volume clone during
|
||||
// create volume
|
||||
var einf ErrImageNotFound
|
||||
tempClone := rbdVol.generateTempClone()
|
||||
err = deleteImage(ctx, tempClone, cr)
|
||||
if err != nil {
|
||||
// return error if it is not ErrImageNotFound
|
||||
if !errors.As(err, &einf) {
|
||||
if !errors.Is(err, ErrImageNotFound) {
|
||||
klog.Errorf(util.Log(ctx, "failed to delete rbd image: %s with error: %v"),
|
||||
tempClone, err)
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
@ -713,9 +702,8 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
|
||||
// Fetch source volume information
|
||||
rbdVol, err = genVolFromVolID(ctx, req.GetSourceVolumeId(), cr, req.GetSecrets())
|
||||
if err != nil {
|
||||
var einf ErrImageNotFound
|
||||
// nolint:gocritic // this ifElseChain can not be rewritten to a switch statement
|
||||
if errors.As(err, &einf) {
|
||||
if errors.Is(err, ErrImageNotFound) {
|
||||
err = status.Errorf(codes.NotFound, "source Volume ID %s not found", req.GetSourceVolumeId())
|
||||
} else if errors.Is(err, util.ErrPoolNotFound) {
|
||||
klog.Errorf(util.Log(ctx, "failed to get backend volume for %s: %v"), req.GetSourceVolumeId(), err)
|
||||
@ -779,8 +767,7 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
|
||||
defer vol.Destroy()
|
||||
|
||||
err = vol.flattenRbdImage(ctx, cr, false, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
|
||||
var efip ErrFlattenInProgress
|
||||
if errors.As(err, &efip) {
|
||||
if errors.Is(err, ErrFlattenInProgress) {
|
||||
return &csi.CreateSnapshotResponse{
|
||||
Snapshot: &csi.Snapshot{
|
||||
SizeBytes: rbdSnap.SizeBytes,
|
||||
@ -891,8 +878,7 @@ func (cs *ControllerServer) doSnapshotClone(ctx context.Context, parentVol *rbdV
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
var efip ErrFlattenInProgress
|
||||
if !errors.As(err, &efip) {
|
||||
if !errors.Is(err, ErrFlattenInProgress) {
|
||||
// cleanup clone and snapshot
|
||||
errCleanUp := cleanUpSnapshot(ctx, cloneRbd, rbdSnap, cloneRbd, cr)
|
||||
if errCleanUp != nil {
|
||||
@ -932,8 +918,7 @@ func (cs *ControllerServer) doSnapshotClone(ctx context.Context, parentVol *rbdV
|
||||
|
||||
err = cloneRbd.flattenRbdImage(ctx, cr, false, rbdHardMaxCloneDepth, rbdSoftMaxCloneDepth)
|
||||
if err != nil {
|
||||
var efip ErrFlattenInProgress
|
||||
if errors.As(err, &efip) {
|
||||
if errors.Is(err, ErrFlattenInProgress) {
|
||||
return ready, cloneRbd, nil
|
||||
}
|
||||
return ready, cloneRbd, err
|
||||
@ -1014,8 +999,7 @@ func (cs *ControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
|
||||
|
||||
err = rbdVol.getImageInfo()
|
||||
if err != nil {
|
||||
var einf ErrImageNotFound
|
||||
if !errors.As(err, &einf) {
|
||||
if !errors.Is(err, ErrImageNotFound) {
|
||||
klog.Errorf(util.Log(ctx, "failed to delete rbd image: %s/%s with error: %v"), rbdVol.Pool, rbdVol.VolName, err)
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
@ -1081,9 +1065,8 @@ func (cs *ControllerServer) ControllerExpandVolume(ctx context.Context, req *csi
|
||||
|
||||
rbdVol, err = genVolFromVolID(ctx, volID, cr, req.GetSecrets())
|
||||
if err != nil {
|
||||
var einf ErrImageNotFound
|
||||
// nolint:gocritic // this ifElseChain can not be rewritten to a switch statement
|
||||
if errors.As(err, &einf) {
|
||||
if errors.Is(err, ErrImageNotFound) {
|
||||
err = status.Errorf(codes.NotFound, "volume ID %s not found", volID)
|
||||
} else if errors.Is(err, util.ErrPoolNotFound) {
|
||||
klog.Errorf(util.Log(ctx, "failed to get backend volume for %s: %v"), volID, err)
|
||||
|
Reference in New Issue
Block a user