Skip to content

Commit

Permalink
inspect function support resolving shortname
Browse files Browse the repository at this point in the history
Signed-off-by: 24sama <[email protected]>
  • Loading branch information
24sama committed Feb 10, 2023
1 parent d68fa93 commit 763079c
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions pkg/buildah/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"text/template"

"github.com/containers/common/pkg/retry"
"github.com/containers/image/v5/docker/reference"
"github.com/containers/image/v5/manifest"
"github.com/containers/image/v5/pkg/shortnames"
"github.com/opencontainers/go-digest"

"github.com/containers/buildah"
Expand Down Expand Up @@ -250,16 +252,42 @@ func inspectImage(ctx context.Context, sc *types.SystemContext, store storage.St
if st, ok := transport.(imagestorage.StoreTransport); ok {
st.SetStore(store)
}
ref, err := transport.ParseReference(imgName)

// try out the candidates as resolved by shortnames. This takes
// "localhost/" prefixed images into account as well.
candidates, err := shortnames.ResolveLocally(sc, imgName)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("error resolve shortname %s: %w", imgName, storage.ErrImageUnknown)
}
// also try out the "docker.io/" prefixed image
if dockerNamed, err := reference.ParseDockerRef(imgName); err == nil {
candidates = append(candidates, dockerNamed)
}

if err = retry.IfNecessary(ctx, func() error {
src, err = ref.NewImageSource(ctx, sc)
return err
}, &retry.Options{}); err != nil {
return nil, nil, fmt.Errorf("error parsing image name %q: %w", imgName, err)
var errs []error
for _, candidate := range candidates {
f := func() (types.ImageSource, error) {
ref, err := transport.ParseReference(candidate.String())
if err != nil {
return nil, err
}
innerSrc, err := ref.NewImageSource(ctx, sc)
if err != nil {
return nil, err
}
return innerSrc, nil
}
res, err := f()
if err != nil {
errs = append(errs, err)
} else {
logger.Debug("using image name %q", candidate.String())
src = res
break
}
}
if src == nil {
return nil, nil, fmt.Errorf("error parsing image name %q: %w", imgName, errors.Join(errs...))
}

img, err := image.FromUnparsedImage(ctx, sc, image.UnparsedInstance(src, nil))
Expand Down

0 comments on commit 763079c

Please sign in to comment.