Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

local image lookup by digest #2934

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions new.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ func resolveImage(ctx context.Context, systemContext *types.SystemContext, store
return nil, "", nil, err
}

// If we could resolve the image locally, check if it was referenced by
// ID. In that case, we don't need to bother any further and can
// prevent prompting the user.
if localImage != nil && strings.HasPrefix(localImage.ID, options.FromImage) {
// If we could resolve the image locally, check if it was clearly
// referring to a local image, either by ID or digest. In that case,
// we don't need to perform a remote lookup.
if localImage != nil && (strings.HasPrefix(localImage.ID, options.FromImage) || strings.HasPrefix(options.FromImage, "sha256:")) {
return localImageRef, localImageRef.Transport().Name(), localImage, nil
}

Expand Down
12 changes: 12 additions & 0 deletions tests/from.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ load helpers
check_options_flag_err "--cred=fake fake"
}

@test "from-with-digest" {
run_buildah pull alpine
run_buildah inspect --format "{{.FromImageID}}" alpine
digest=$output

run_buildah from "sha256:$digest"
run_buildah rm $output

run_buildah 125 from sha256:1111111111111111111111111111111111111111111111111111111111111111
expect_output --substring "error locating image with ID \"1111111111111111111111111111111111111111111111111111111111111111\""
}

@test "commit-to-from-elsewhere" {
elsewhere=${TESTDIR}/elsewhere-img
mkdir -p ${elsewhere}
Expand Down
14 changes: 14 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/containers/image/v5/types"
"github.com/containers/storage"
"github.com/docker/distribution/registry/api/errcode"
"github.com/opencontainers/go-digest"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -68,6 +69,19 @@ func ResolveName(name string, firstRegistry string, sc *types.SystemContext, sto
return []string{img.ID}, "", false, nil
}
}
// If we're referring to an image by digest, it *must* be local and we
// should not have any fall through/back logic.
if strings.HasPrefix(name, "sha256:") {
d, err := digest.Parse(name)
if err != nil {
return nil, "", false, err
}
img, err := store.Image(d.Encoded())
if err != nil {
return nil, "", false, err
}
return []string{img.ID}, "", false, nil
}

// Transports are not supported for local image look ups.
srcRef, err := alltransports.ParseImageName(name)
Expand Down