From de74c36d8c3db81f8d207665b74cfba42eabbfe3 Mon Sep 17 00:00:00 2001 From: Sven Anderson Date: Thu, 2 Jul 2020 23:43:40 +0200 Subject: [PATCH] cleanup: address err113 warnings about direct error comparisons Several places in the code compared errors directly with the go-ceph sentinel errors. This change uses the errors.Is() function of go 1.13 instead. The err113 linter reported this issue as: err113: do not compare errors directly, use errors.Is() instead Signed-off-by: Sven Anderson --- internal/journal/omap.go | 46 ++++++++++++++++++------------------- internal/rbd/rbd_util.go | 4 ++-- internal/util/cephcmds.go | 6 ++--- internal/util/connection.go | 2 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/internal/journal/omap.go b/internal/journal/omap.go index 0e2209001..70768a5ec 100644 --- a/internal/journal/omap.go +++ b/internal/journal/omap.go @@ -18,6 +18,7 @@ package journal import ( "context" + "errors" "github.com/ceph/ceph-csi/internal/util" @@ -60,14 +61,13 @@ func getOMapValues( } }, ) - switch err { - case nil: - case rados.ErrNotFound: - klog.Errorf( - util.Log(ctx, "omap not found (pool=%q, namespace=%q, name=%q): %v"), - poolName, namespace, oid, err) - return nil, util.NewErrKeyNotFound(oid, err) - default: + if err != nil { + if errors.Is(err, rados.ErrNotFound) { + klog.Errorf( + util.Log(ctx, "omap not found (pool=%q, namespace=%q, name=%q): %v"), + poolName, namespace, oid, err) + return nil, util.NewErrKeyNotFound(oid, err) + } return nil, err } @@ -93,20 +93,20 @@ func removeMapKeys( } err = ioctx.RmOmapKeys(oid, keys) - switch err { - case nil: - case rados.ErrNotFound: - // the previous implementation of removing omap keys (via the cli) - // treated failure to find the omap as a non-error. Do so here to - // mimic the previous behavior. - klog.V(4).Infof( - util.Log(ctx, "when removing omap keys, omap not found (pool=%q, namespace=%q, name=%q): %+v"), - poolName, namespace, oid, keys) - default: - klog.Errorf( - util.Log(ctx, "failed removing omap keys (pool=%q, namespace=%q, name=%q): %v"), - poolName, namespace, oid, err) - return err + if err != nil { + if errors.Is(err, rados.ErrNotFound) { + // the previous implementation of removing omap keys (via the cli) + // treated failure to find the omap as a non-error. Do so here to + // mimic the previous behavior. + klog.V(4).Infof( + util.Log(ctx, "when removing omap keys, omap not found (pool=%q, namespace=%q, name=%q): %+v"), + poolName, namespace, oid, keys) + } else { + klog.Errorf( + util.Log(ctx, "failed removing omap keys (pool=%q, namespace=%q, name=%q): %v"), + poolName, namespace, oid, err) + return err + } } klog.V(4).Infof( util.Log(ctx, "removed omap keys (pool=%q, namespace=%q, name=%q): %+v"), @@ -147,7 +147,7 @@ func setOMapKeys( } func omapPoolError(poolName string, err error) error { - if err == rados.ErrNotFound { + if errors.Is(err, rados.ErrNotFound) { return util.NewErrPoolNotFound(poolName, err) } return err diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index ac8ae8f5a..b626e61a5 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -269,7 +269,7 @@ func (rv *rbdVolume) open() (*librbd.Image, error) { image, err := librbd.OpenImage(rv.ioctx, rv.RbdImageName, librbd.NoSnapshot) if err != nil { - if err == librbd.ErrNotFound { + if errors.Is(err, librbd.ErrNotFound) { err = ErrImageNotFound{rv.RbdImageName, err} } return nil, err @@ -851,7 +851,7 @@ func (rv *rbdVolume) deleteSnapshot(ctx context.Context, pOpts *rbdSnapshot) err return fmt.Errorf("snapshot value is nil for %s", pOpts.RbdSnapName) } err = snap.Remove() - if err == librbd.ErrNotFound { + if errors.Is(err, librbd.ErrNotFound) { return ErrSnapNotFound{snapName: pOpts.RbdSnapName, err: err} } return err diff --git a/internal/util/cephcmds.go b/internal/util/cephcmds.go index d2274776a..e5075b6e0 100644 --- a/internal/util/cephcmds.go +++ b/internal/util/cephcmds.go @@ -63,7 +63,7 @@ func GetPoolID(monitors string, cr *Credentials, poolName string) (int64, error) defer connPool.Put(conn) id, err := conn.GetPoolByName(poolName) - if err == rados.ErrNotFound { + if errors.Is(err, rados.ErrNotFound) { return InvalidPoolID, ErrPoolNotFound{poolName, fmt.Errorf("pool (%s) not found in Ceph cluster", poolName)} } else if err != nil { return InvalidPoolID, err @@ -247,7 +247,7 @@ func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolNam } err = ioctx.Create(objectName, rados.CreateExclusive) - if err == rados.ErrObjectExists { + if errors.Is(err, rados.ErrObjectExists) { return ErrObjectExists{objectName, err} } else if err != nil { klog.Errorf(Log(ctx, "failed creating omap (%s) in pool (%s): (%v)"), objectName, poolName, err) @@ -282,7 +282,7 @@ func RemoveObject(ctx context.Context, monitors string, cr *Credentials, poolNam } err = ioctx.Delete(oMapName) - if err == rados.ErrNotFound { + if errors.Is(err, rados.ErrNotFound) { return ErrObjectNotFound{oMapName, err} } else if err != nil { klog.Errorf(Log(ctx, "failed removing omap (%s) in pool (%s): (%v)"), oMapName, poolName, err) diff --git a/internal/util/connection.go b/internal/util/connection.go index 28c81bafd..1426ddeb7 100644 --- a/internal/util/connection.go +++ b/internal/util/connection.go @@ -73,7 +73,7 @@ func (cc *ClusterConnection) GetIoctx(pool string) (*rados.IOContext, error) { ioctx, err := cc.conn.OpenIOContext(pool) if err != nil { // ErrNotFound indicates the Pool was not found - if err == rados.ErrNotFound { + if errors.Is(err, rados.ErrNotFound) { err = ErrPoolNotFound{pool, err} } else { err = fmt.Errorf("failed to open IOContext for pool %s: %w", pool, err)