cleanup: use errors.As() in journal.Connection methods

See-also: https://github.com/golang/go/wiki/ErrorValueFAQ#how-should-i-change-my-error-handling-code-to-work-with-the-new-features
Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-06-25 10:35:19 +02:00 committed by mergify[bot]
parent ad04e0d8c4
commit 91ade42a32

View File

@ -281,13 +281,14 @@ func (conn *Connection) CheckReservation(ctx context.Context,
values, err := getOMapValues( values, err := getOMapValues(
ctx, conn, journalPool, cj.namespace, cj.csiDirectory, ctx, conn, journalPool, cj.namespace, cj.csiDirectory,
cj.commonPrefix, fetchKeys) cj.commonPrefix, fetchKeys)
switch err.(type) { if err != nil {
case nil: var eknf util.ErrKeyNotFound
case util.ErrKeyNotFound, util.ErrPoolNotFound: var epnf util.ErrPoolNotFound
// pool or omap (oid) was not present if errors.As(err, &eknf) || errors.As(err, &epnf) {
// stop processing but without an error for no reservation exists // pool or omap (oid) was not present
return nil, nil // stop processing but without an error for no reservation exists
default: return nil, nil
}
return nil, err return nil, err
} }
objUUIDAndPool, found := values[cj.csiNameKeyPrefix+reqName] objUUIDAndPool, found := values[cj.csiNameKeyPrefix+reqName]
@ -315,7 +316,8 @@ func (conn *Connection) CheckReservation(ctx context.Context,
savedImagePool, err = util.GetPoolName(conn.monitors, conn.cr, savedImagePoolID) savedImagePool, err = util.GetPoolName(conn.monitors, conn.cr, savedImagePoolID)
if err != nil { if err != nil {
if _, ok := err.(util.ErrPoolNotFound); ok { var epnf util.ErrPoolNotFound
if errors.As(err, &epnf) {
err = conn.UndoReservation(ctx, journalPool, "", "", reqName) err = conn.UndoReservation(ctx, journalPool, "", "", reqName)
} }
return nil, err return nil, err
@ -327,7 +329,8 @@ func (conn *Connection) CheckReservation(ctx context.Context,
if err != nil { if err != nil {
// error should specifically be not found, for image to be absent, any other error // error should specifically be not found, for image to be absent, any other error
// is not conclusive, and we should not proceed // is not conclusive, and we should not proceed
if _, ok := err.(util.ErrKeyNotFound); ok { var eknf util.ErrKeyNotFound
if errors.As(err, &eknf) {
err = conn.UndoReservation(ctx, journalPool, savedImagePool, err = conn.UndoReservation(ctx, journalPool, savedImagePool,
cj.GetNameForUUID(namePrefix, objUUID, snapSource), reqName) cj.GetNameForUUID(namePrefix, objUUID, snapSource), reqName)
} }
@ -409,7 +412,8 @@ func (conn *Connection) UndoReservation(ctx context.Context,
err := util.RemoveObject(ctx, conn.monitors, conn.cr, volJournalPool, cj.namespace, cj.cephUUIDDirectoryPrefix+imageUUID) err := util.RemoveObject(ctx, conn.monitors, conn.cr, volJournalPool, cj.namespace, cj.cephUUIDDirectoryPrefix+imageUUID)
if err != nil { if err != nil {
if _, ok := err.(util.ErrObjectNotFound); !ok { var eonf util.ErrObjectNotFound
if !errors.As(err, &eonf) {
klog.Errorf(util.Log(ctx, "failed removing oMap %s (%s)"), cj.cephUUIDDirectoryPrefix+imageUUID, err) klog.Errorf(util.Log(ctx, "failed removing oMap %s (%s)"), cj.cephUUIDDirectoryPrefix+imageUUID, err)
return err return err
} }
@ -441,7 +445,8 @@ func reserveOMapName(ctx context.Context, monitors string, cr *util.Credentials,
err := util.CreateObject(ctx, monitors, cr, pool, namespace, oMapNamePrefix+iterUUID) err := util.CreateObject(ctx, monitors, cr, pool, namespace, oMapNamePrefix+iterUUID)
if err != nil { if err != nil {
if _, ok := err.(util.ErrObjectExists); ok { var eoe util.ErrObjectExists
if errors.As(err, &eoe) {
attempt++ attempt++
// try again with a different uuid, for maxAttempts tries // try again with a different uuid, for maxAttempts tries
klog.V(4).Infof(util.Log(ctx, "uuid (%s) conflict detected, retrying (attempt %d of %d)"), klog.V(4).Infof(util.Log(ctx, "uuid (%s) conflict detected, retrying (attempt %d of %d)"),
@ -608,12 +613,13 @@ func (conn *Connection) GetImageAttributes(ctx context.Context, pool, objectUUID
values, err := getOMapValues( values, err := getOMapValues(
ctx, conn, pool, cj.namespace, cj.cephUUIDDirectoryPrefix+objectUUID, ctx, conn, pool, cj.namespace, cj.cephUUIDDirectoryPrefix+objectUUID,
cj.commonPrefix, fetchKeys) cj.commonPrefix, fetchKeys)
switch err.(type) { if err != nil {
case nil: var eknf util.ErrKeyNotFound
case util.ErrPoolNotFound, util.ErrKeyNotFound: var epnf util.ErrPoolNotFound
if !errors.As(err, &eknf) && !errors.As(err, &epnf) {
return nil, err
}
klog.Warningf(util.Log(ctx, "unable to read omap keys: pool or key missing: %v"), err) klog.Warningf(util.Log(ctx, "unable to read omap keys: pool or key missing: %v"), err)
default:
return nil, err
} }
var found bool var found bool