diff --git a/commit.go b/commit.go index 1268181d82b..4edb12f69e3 100644 --- a/commit.go +++ b/commit.go @@ -3,7 +3,6 @@ package buildah import ( "context" "encoding/json" - "errors" "fmt" "io" "os" @@ -22,7 +21,6 @@ import ( "github.com/containers/image/v5/transports" "github.com/containers/image/v5/types" encconfig "github.com/containers/ocicrypt/config" - "github.com/containers/storage" "github.com/containers/storage/pkg/archive" "github.com/containers/storage/pkg/stringid" digest "github.com/opencontainers/go-digest" @@ -358,7 +356,7 @@ func (b *Builder) Commit(ctx context.Context, dest types.ImageReference, options if len(options.AdditionalTags) > 0 { switch dest.Transport().Name() { case is.Transport.Name(): - img, err := is.Transport.GetStoreImage(b.store, dest) + _, img, err := is.ResolveReference(dest) if err != nil { return imgID, nil, "", fmt.Errorf("locating just-written image %q: %w", transports.ImageName(dest), err) } @@ -371,32 +369,33 @@ func (b *Builder) Commit(ctx context.Context, dest types.ImageReference, options } } - img, err := is.Transport.GetStoreImage(b.store, dest) - if err != nil && !errors.Is(err, storage.ErrImageUnknown) { - return imgID, nil, "", fmt.Errorf("locating image %q in local storage: %w", transports.ImageName(dest), err) - } - if err == nil { - imgID = img.ID - toPruneNames := make([]string, 0, len(img.Names)) - for _, name := range img.Names { - if nameToRemove != "" && strings.Contains(name, nameToRemove) { - toPruneNames = append(toPruneNames, name) + dref := dest.DockerReference() + if dref != nil { + // Check if reference is on the storage + if _, err := b.store.Image(dref.String()); err == nil { + // resolve reference + dest2, img, err := is.ResolveReference(dest) + if err != nil { + return imgID, nil, "", fmt.Errorf("locating image %q in local storage: %w", transports.ImageName(dest), err) } - } - if len(toPruneNames) > 0 { - if err = b.store.RemoveNames(imgID, toPruneNames); err != nil { - return imgID, nil, "", fmt.Errorf("failed to remove temporary name from image %q: %w", imgID, err) + imgID = img.ID + toPruneNames := make([]string, 0, len(img.Names)) + for _, name := range img.Names { + if nameToRemove != "" && strings.Contains(name, nameToRemove) { + toPruneNames = append(toPruneNames, name) + } } - logrus.Debugf("removing %v from assigned names to image %q", nameToRemove, img.ID) - dest2, err := is.Transport.ParseStoreReference(b.store, "@"+imgID) - if err != nil { - return imgID, nil, "", fmt.Errorf("creating unnamed destination reference for image: %w", err) + if len(toPruneNames) > 0 { + if err = b.store.RemoveNames(imgID, toPruneNames); err != nil { + return imgID, nil, "", fmt.Errorf("failed to remove temporary name from image %q: %w", imgID, err) + } + logrus.Debugf("removing %v from assigned names to image %q", nameToRemove, img.ID) + dest = dest2 } - dest = dest2 - } - if options.IIDFile != "" { - if err = os.WriteFile(options.IIDFile, []byte("sha256:"+img.ID), 0644); err != nil { - return imgID, nil, "", err + if options.IIDFile != "" { + if err = os.WriteFile(options.IIDFile, []byte("sha256:"+img.ID), 0644); err != nil { + return imgID, nil, "", err + } } } } diff --git a/imagebuildah/executor.go b/imagebuildah/executor.go index 7cc7876daff..a3dd3cf5654 100644 --- a/imagebuildah/executor.go +++ b/imagebuildah/executor.go @@ -1018,7 +1018,7 @@ func (b *Executor) Build(ctx context.Context, stages imagebuilder.Stages) (image if dest, err := b.resolveNameToImageRef(b.output); err == nil { switch dest.Transport().Name() { case storageTransport.Transport.Name(): - img, err := storageTransport.Transport.GetStoreImage(b.store, dest) + _, img, err := storageTransport.ResolveReference(dest) if err != nil { return imageID, ref, fmt.Errorf("locating just-written image %q: %w", transports.ImageName(dest), err) } @@ -1029,7 +1029,7 @@ func (b *Executor) Build(ctx context.Context, stages imagebuilder.Stages) (image logrus.Debugf("assigned names %v to image %q", img.Names, img.ID) } // Report back the caller the tags applied, if any. - img, err = storageTransport.Transport.GetStoreImage(b.store, dest) + _, img, err = storageTransport.ResolveReference(dest) if err != nil { return imageID, ref, fmt.Errorf("locating just-written image %q: %w", transports.ImageName(dest), err) } diff --git a/imagebuildah/stage_executor.go b/imagebuildah/stage_executor.go index d4d88699f77..f7f7e6467cc 100644 --- a/imagebuildah/stage_executor.go +++ b/imagebuildah/stage_executor.go @@ -1752,7 +1752,7 @@ func (s *StageExecutor) tagExistingImage(ctx context.Context, cacheID, output st if err != nil { return "", nil, fmt.Errorf("computing digest of manifest for image %q: %w", cacheID, err) } - img, err := is.Transport.GetStoreImage(s.executor.store, dest) + _, img, err := is.ResolveReference(dest) if err != nil { return "", nil, fmt.Errorf("locating new copy of image %q (i.e., %q): %w", cacheID, transports.ImageName(dest), err) }