Use errors.Is() to check for storage errors

The recent update to the storage library switched its error wrapping
from using github.com/pkg/errors to the standard library's fmt.Errorf()
%w specifier, which errors.Cause() can't unwrap.  Use errors.Is() to
check for specific errors from the storage library.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai 2022-08-01 16:34:20 -04:00
parent 0da5f30211
commit 635de90f9d
11 changed files with 14 additions and 13 deletions

View File

@ -154,7 +154,7 @@ func addAndCopyCmd(c *cobra.Command, args []string, verb string, iopts addCopyRe
}
if iopts.from != "" {
if from, err = openBuilder(getContext(), store, iopts.from); err != nil && errors.Cause(err) == storage.ErrContainerUnknown {
if from, err = openBuilder(getContext(), store, iopts.from); err != nil && errors.Is(err, storage.ErrContainerUnknown) {
systemContext, err2 := parse.SystemContextFromOptions(c)
if err2 != nil {
return errors.Wrap(err2, "error building system context")

View File

@ -129,7 +129,7 @@ func setXDGRuntimeDir() error {
func openBuilder(ctx context.Context, store storage.Store, name string) (builder *buildah.Builder, err error) {
if name != "" {
builder, err = buildah.OpenBuilder(store, name)
if os.IsNotExist(errors.Cause(err)) {
if errors.Is(errors.Cause(err), os.ErrNotExist) {
options := buildah.ImportOptions{
Container: name,
}

View File

@ -189,7 +189,7 @@ func shutdownStore(cmd *cobra.Command) error {
logrus.Debugf("shutting down the store")
needToShutdownStore = false
if _, err = store.Shutdown(false); err != nil {
if errors.Cause(err) == storage.ErrLayerUsedByContainer {
if errors.Is(err, storage.ErrLayerUsedByContainer) {
logrus.Infof("failed to shutdown storage: %q", err)
} else {
logrus.Warnf("failed to shutdown storage: %q", err)

View File

@ -657,10 +657,11 @@ func manifestInspect(ctx context.Context, store storage.Store, systemContext *ty
// Before doing a remote lookup, attempt to resolve the manifest list
// locally.
manifestList, err := runtime.LookupManifestList(imageSpec)
switch errors.Cause(err) {
case storage.ErrImageUnknown, libimage.ErrNotAManifestList:
switch {
case errors.Is(errors.Cause(err), storage.ErrImageUnknown), errors.Is(errors.Cause(err), libimage.ErrNotAManifestList):
// We need to do the remote inspection below.
case nil:
case err == nil, errors.Cause(err) == nil:
schema2List, err := manifestList.Inspect()
if err != nil {
return err

View File

@ -215,7 +215,7 @@ func pushCmd(c *cobra.Command, args []string, iopts pushOptions) error {
ref, digest, err := buildah.Push(getContext(), src, dest, options)
if err != nil {
if errors.Cause(err) != storage.ErrImageUnknown {
if !errors.Is(errors.Cause(err), storage.ErrImageUnknown) {
// Image might be a manifest so attempt a manifest push
if manifestsErr := manifestPush(systemContext, store, src, destSpec, iopts); manifestsErr == nil {
return nil

View File

@ -364,7 +364,7 @@ func (b *Builder) Commit(ctx context.Context, dest types.ImageReference, options
}
img, err := is.Transport.GetStoreImage(b.store, dest)
if err != nil && errors.Cause(err) != storage.ErrImageUnknown {
if err != nil && !errors.Is(errors.Cause(err), storage.ErrImageUnknown) {
return imgID, nil, "", errors.Wrapf(err, "error locating image %q in local storage", transports.ImageName(dest))
}
if err == nil {

View File

@ -280,7 +280,7 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B
// partially-populated state at any point if we're creating it
// fresh.
list, err := rt.LookupManifestList(manifestList)
if err != nil && errors.Cause(err) == storage.ErrImageUnknown {
if err != nil && errors.Is(errors.Cause(err), storage.ErrImageUnknown) {
list, err = rt.CreateManifestList(manifestList)
}
if err != nil {

View File

@ -564,7 +564,7 @@ func (b *Executor) Build(ctx context.Context, stages imagebuilder.Stages) (image
}
if _, err := b.store.DeleteImage(removeID, true); err != nil {
logrus.Debugf("failed to remove intermediate image %q: %v", removeID, err)
if b.forceRmIntermediateCtrs || errors.Cause(err) != storage.ErrImageUsedByContainer {
if b.forceRmIntermediateCtrs || !errors.Is(err, storage.ErrImageUsedByContainer) {
lastErr = err
}
}

2
new.go
View File

@ -257,7 +257,7 @@ func newBuilder(ctx context.Context, store storage.Store, options BuilderOptions
name = tmpName
break
}
if errors.Cause(err) != storage.ErrDuplicateName || options.Container != "" {
if !errors.Is(err, storage.ErrDuplicateName) || options.Container != "" {
return nil, errors.Wrapf(err, "error creating container")
}
tmpName = fmt.Sprintf("%s-%d", name, rand.Int()%conflict)

View File

@ -2898,7 +2898,7 @@ func (b *Builder) cleanupRunMounts(context *imagetypes.SystemContext, mountpoint
// if image is being used by something else
_ = i.Unmount(false)
}
if errors.Cause(err) == storagetypes.ErrImageUnknown {
if errors.Is(errors.Cause(err), storagetypes.ErrImageUnknown) {
// Ignore only if ErrImageUnknown
// Reason: Image is already unmounted do nothing
continue

View File

@ -159,7 +159,7 @@ func ReserveSELinuxLabels(store storage.Store, id string) error {
} else {
b, err := OpenBuilder(store, c.ID)
if err != nil {
if os.IsNotExist(errors.Cause(err)) {
if errors.Is(errors.Cause(err), os.ErrNotExist) {
// Ignore not exist errors since containers probably created by other tool
// TODO, we need to read other containers json data to reserve their SELinux labels
continue