From 13f291dfc67eb2c31d18b4775aca19725d24ae33 Mon Sep 17 00:00:00 2001 From: Sven Anderson Date: Mon, 6 Jul 2020 19:51:04 +0200 Subject: [PATCH] cleanup: use errors.As() for error type checks Replaces some remaining old-style error type checks with errors.As() Signed-off-by: Sven Anderson --- internal/rbd/controllerserver.go | 27 ++++++++++++++++++--------- internal/rbd/rbd_journal.go | 6 ++++-- internal/rbd/snapshot.go | 11 +++++++---- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/internal/rbd/controllerserver.go b/internal/rbd/controllerserver.go index 89f9b46aa..82f53f249 100644 --- a/internal/rbd/controllerserver.go +++ b/internal/rbd/controllerserver.go @@ -223,7 +223,8 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol } defer func() { if err != nil { - if _, ok := err.(ErrFlattenInProgress); !ok { + var efip ErrFlattenInProgress + if !errors.As(err, &efip) { errDefer := undoVolReservation(ctx, rbdVol, cr) if errDefer != nil { klog.Warningf(util.Log(ctx, "failed undoing reservation of volume: %s (%s)"), req.GetName(), errDefer) @@ -234,7 +235,8 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol err = cs.createBackingImage(ctx, cr, rbdVol, rbdSnap) if err != nil { - if _, ok := err.(ErrFlattenInProgress); ok { + var efip ErrFlattenInProgress + if errors.As(err, &efip) { return nil, status.Error(codes.Aborted, err.Error()) } return nil, err @@ -268,7 +270,8 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol func checkFlatten(ctx context.Context, rbdVol *rbdVolume, cr *util.Credentials) error { err := rbdVol.flattenRbdImage(ctx, cr, false) if err != nil { - if _, ok := err.(ErrFlattenInProgress); ok { + var efip ErrFlattenInProgress + if errors.As(err, &efip) { return status.Error(codes.Aborted, err.Error()) } if errDefer := deleteImage(ctx, rbdVol, cr); errDefer != nil { @@ -294,7 +297,8 @@ func (cs *ControllerServer) createVolumeFromSnapshot(ctx context.Context, cr *ut err := genSnapFromSnapID(ctx, rbdSnap, snapshotID, cr) if err != nil { - if _, ok := err.(util.ErrPoolNotFound); ok { + var epnf util.ErrPoolNotFound + if errors.As(err, &epnf) { klog.Errorf(util.Log(ctx, "failed to get backend snapshot for %s: %v"), snapshotID, err) return status.Error(codes.InvalidArgument, err.Error()) } @@ -342,7 +346,8 @@ func (cs *ControllerServer) createBackingImage(ctx context.Context, cr *util.Cre defer func() { if err != nil { - if _, ok := err.(ErrFlattenInProgress); !ok { + var efip ErrFlattenInProgress + if !errors.As(err, &efip) { if deleteErr := deleteImage(ctx, rbdVol, cr); deleteErr != nil { klog.Errorf(util.Log(ctx, "failed to delete rbd image: %s with error: %v"), rbdVol, deleteErr) } @@ -701,7 +706,8 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS defer vol.Destroy() err = vol.flattenRbdImage(ctx, cr, false) - if _, ok := err.(ErrFlattenInProgress); ok { + var efip ErrFlattenInProgress + if errors.As(err, &efip) { return &csi.CreateSnapshotResponse{ Snapshot: &csi.Snapshot{ SizeBytes: rbdSnap.SizeBytes, @@ -824,7 +830,8 @@ func (cs *ControllerServer) doSnapshotClone(ctx context.Context, parentVol *rbdV defer func() { if err != nil { - if _, ok := err.(ErrFlattenInProgress); !ok { + var efip ErrFlattenInProgress + if !errors.As(err, &efip) { // cleanup clone and snapshot errCleanUp := cleanUpSnapshot(ctx, cloneRbd, rbdSnap, cloneRbd, cr) if errCleanUp != nil { @@ -864,7 +871,8 @@ func (cs *ControllerServer) doSnapshotClone(ctx context.Context, parentVol *rbdV err = cloneRbd.flattenRbdImage(ctx, cr, false) if err != nil { - if _, ok := err.(ErrFlattenInProgress); ok { + var efip ErrFlattenInProgress + if errors.As(err, &efip) { return ready, cloneRbd, nil } return ready, cloneRbd, err @@ -940,7 +948,8 @@ func (cs *ControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS err = rbdVol.getImageInfo() if err != nil { - if _, ok := err.(ErrImageNotFound); !ok { + var einf ErrImageNotFound + if !errors.As(err, &einf) { 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()) } diff --git a/internal/rbd/rbd_journal.go b/internal/rbd/rbd_journal.go index 8852cbc80..70874c108 100644 --- a/internal/rbd/rbd_journal.go +++ b/internal/rbd/rbd_journal.go @@ -155,7 +155,8 @@ func checkSnapCloneExists(ctx context.Context, parentVol *rbdVolume, rbdSnap *rb if errors.As(err, &einf) { err = parentVol.deleteSnapshot(ctx, rbdSnap) if err != nil { - if _, ok := err.(ErrSnapNotFound); !ok { + var esnf ErrSnapNotFound + if !errors.As(err, &esnf) { klog.Errorf(util.Log(ctx, "failed to delete snapshot %s: %v"), rbdSnap, err) return false, err } @@ -181,7 +182,8 @@ func checkSnapCloneExists(ctx context.Context, parentVol *rbdVolume, rbdSnap *rb // check snapshot exists if not create it _, err = vol.getSnapInfo(rbdSnap) - if _, ok := err.(ErrSnapNotFound); ok { + var esnf ErrSnapNotFound + if errors.As(err, &esnf) { // create snapshot sErr := vol.createSnapshot(ctx, rbdSnap) if sErr != nil { diff --git a/internal/rbd/snapshot.go b/internal/rbd/snapshot.go index 038853122..addcf4cc4 100644 --- a/internal/rbd/snapshot.go +++ b/internal/rbd/snapshot.go @@ -17,10 +17,11 @@ package rbd import ( "context" + "errors" + "fmt" "github.com/ceph/ceph-csi/internal/util" - "github.com/pkg/errors" "k8s.io/klog" ) @@ -36,7 +37,7 @@ func createRBDClone(ctx context.Context, parentVol, cloneRbdVol *rbdVolume, snap err = cloneRbdVol.cloneRbdImageFromSnapshot(ctx, snap) if err != nil { klog.Errorf(util.Log(ctx, "failed to clone rbd image %s from snapshot %s: %v"), cloneRbdVol.RbdImageName, snap.RbdSnapName, err) - err = errors.Errorf("failed to clone rbd image %s from snapshot %s: %v", cloneRbdVol.RbdImageName, snap.RbdSnapName, err) + err = fmt.Errorf("failed to clone rbd image %s from snapshot %s: %w", cloneRbdVol.RbdImageName, snap.RbdSnapName, err) } errSnap := parentVol.deleteSnapshot(ctx, snap) if errSnap != nil { @@ -64,14 +65,16 @@ func createRBDClone(ctx context.Context, parentVol, cloneRbdVol *rbdVolume, snap func cleanUpSnapshot(ctx context.Context, parentVol *rbdVolume, rbdSnap *rbdSnapshot, rbdVol *rbdVolume, cr *util.Credentials) error { err := parentVol.deleteSnapshot(ctx, rbdSnap) if err != nil { - if _, ok := err.(ErrSnapNotFound); !ok { + var esnf ErrSnapNotFound + if !errors.As(err, &esnf) { klog.Errorf(util.Log(ctx, "failed to delete snapshot: %v"), err) return err } } err = deleteImage(ctx, rbdVol, cr) if err != nil { - if _, ok := err.(ErrImageNotFound); !ok { + var einf ErrImageNotFound + if !errors.As(err, &einf) { klog.Errorf(util.Log(ctx, "failed to delete rbd image: %s/%s with error: %v"), rbdVol.Pool, rbdVol.VolName, err) return err }