Merge pull request #10486 from vrothberg/tag-digest-support

support tag@digest notation
This commit is contained in:
OpenShift Merge Robot
2021-06-01 22:38:03 +02:00
committed by GitHub
17 changed files with 161 additions and 30 deletions

2
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/containernetworking/cni v0.8.1 github.com/containernetworking/cni v0.8.1
github.com/containernetworking/plugins v0.9.1 github.com/containernetworking/plugins v0.9.1
github.com/containers/buildah v1.21.0 github.com/containers/buildah v1.21.0
github.com/containers/common v0.39.0 github.com/containers/common v0.39.1-0.20210527140106-e5800a20386a
github.com/containers/conmon v2.0.20+incompatible github.com/containers/conmon v2.0.20+incompatible
github.com/containers/image/v5 v5.12.0 github.com/containers/image/v5 v5.12.0
github.com/containers/ocicrypt v1.1.1 github.com/containers/ocicrypt v1.1.1

4
go.sum
View File

@ -219,8 +219,8 @@ github.com/containernetworking/plugins v0.9.1/go.mod h1:xP/idU2ldlzN6m4p5LmGiwRD
github.com/containers/buildah v1.21.0 h1:LuwuqRPjan3X3AIdGwfkEkqMgmrDMNpQznFqNdHgCz8= github.com/containers/buildah v1.21.0 h1:LuwuqRPjan3X3AIdGwfkEkqMgmrDMNpQznFqNdHgCz8=
github.com/containers/buildah v1.21.0/go.mod h1:yPdlpVd93T+i91yGxrJbW1YOWrqN64j5ZhHOZmHUejs= github.com/containers/buildah v1.21.0/go.mod h1:yPdlpVd93T+i91yGxrJbW1YOWrqN64j5ZhHOZmHUejs=
github.com/containers/common v0.38.4/go.mod h1:egfpX/Y3+19Dz4Wa1eRZDdgzoEOeneieF9CQppKzLBg= github.com/containers/common v0.38.4/go.mod h1:egfpX/Y3+19Dz4Wa1eRZDdgzoEOeneieF9CQppKzLBg=
github.com/containers/common v0.39.0 h1:MrvpFa/bM4UmUILACv2IhOif4oLmWAiD4C+CpOc/MUo= github.com/containers/common v0.39.1-0.20210527140106-e5800a20386a h1:XzYOUf7qjgVJ59YGqAzehlbT63EgjUJhMnfhsPSSJV0=
github.com/containers/common v0.39.0/go.mod h1:vPUHCg/dHoiyqIyLN+EdbjUaGrVEhs/hAvsqsxuYepk= github.com/containers/common v0.39.1-0.20210527140106-e5800a20386a/go.mod h1:CxHAf4iQOZZ8nASIjMdYHHRyA8dMR4tINSS7WQWlv90=
github.com/containers/conmon v2.0.20+incompatible h1:YbCVSFSCqFjjVwHTPINGdMX1F6JXHGTUje2ZYobNrkg= github.com/containers/conmon v2.0.20+incompatible h1:YbCVSFSCqFjjVwHTPINGdMX1F6JXHGTUje2ZYobNrkg=
github.com/containers/conmon v2.0.20+incompatible/go.mod h1:hgwZ2mtuDrppv78a/cOBNiCm6O0UMWGx1mu7P00nu5I= github.com/containers/conmon v2.0.20+incompatible/go.mod h1:hgwZ2mtuDrppv78a/cOBNiCm6O0UMWGx1mu7P00nu5I=
github.com/containers/image/v5 v5.12.0 h1:1hNS2QkzFQ4lH3GYQLyAXB0acRMhS1Ubm6oV++8vw4w= github.com/containers/image/v5 v5.12.0 h1:1hNS2QkzFQ4lH3GYQLyAXB0acRMhS1Ubm6oV++8vw4w=

View File

@ -71,13 +71,12 @@ func CreateContainer(w http.ResponseWriter, r *http.Request) {
imgNameOrID := newImage.ID() imgNameOrID := newImage.ID()
// if the img had multi names with the same sha256 ID, should use the InputName, not the ID // if the img had multi names with the same sha256 ID, should use the InputName, not the ID
if len(newImage.Names()) > 1 { if len(newImage.Names()) > 1 {
imageRef, err := utils.ParseDockerReference(resolvedName) if err := utils.IsRegistryReference(resolvedName); err != nil {
if err != nil {
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, err) utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, err)
return return
} }
// maybe the InputName has no tag, so use full name to display // maybe the InputName has no tag, so use full name to display
imgNameOrID = imageRef.DockerReference().String() imgNameOrID = resolvedName
} }
sg := specgen.NewSpecGenerator(imgNameOrID, cliOpts.RootFS) sg := specgen.NewSpecGenerator(imgNameOrID, cliOpts.RootFS)

View File

@ -482,7 +482,7 @@ func PushImage(w http.ResponseWriter, r *http.Request) {
destination = source destination = source
} }
if _, err := utils.ParseDockerReference(destination); err != nil { if err := utils.IsRegistryReference(destination); err != nil {
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, err) utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, err)
return return
} }

View File

@ -48,7 +48,7 @@ func ImagesPull(w http.ResponseWriter, r *http.Request) {
} }
// Make sure that the reference has no transport or the docker one. // Make sure that the reference has no transport or the docker one.
if _, err := utils.ParseDockerReference(query.Reference); err != nil { if err := utils.IsRegistryReference(query.Reference); err != nil {
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, err) utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, err)
return return
} }

View File

@ -169,7 +169,7 @@ func ManifestPush(w http.ResponseWriter, r *http.Request) {
errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
return return
} }
if _, err := utils.ParseDockerReference(query.Destination); err != nil { if err := utils.IsRegistryReference(query.Destination); err != nil {
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, err) utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, err)
return return
} }

View File

@ -15,22 +15,19 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// ParseDockerReference parses the specified image name to a // IsRegistryReference checks if the specified name points to the "docker://"
// `types.ImageReference` and enforces it to refer to a docker-transport // transport. If it points to no supported transport, we'll assume a
// reference. // non-transport reference pointing to an image (e.g., "fedora:latest").
func ParseDockerReference(name string) (types.ImageReference, error) { func IsRegistryReference(name string) error {
dockerPrefix := fmt.Sprintf("%s://", docker.Transport.Name())
imageRef, err := alltransports.ParseImageName(name) imageRef, err := alltransports.ParseImageName(name)
if err == nil && imageRef.Transport().Name() != docker.Transport.Name() { if err != nil {
return nil, errors.Errorf("reference %q must be a docker reference", name) // No supported transport -> assume a docker-stype reference.
} else if err != nil { return nil
origErr := err
imageRef, err = alltransports.ParseImageName(fmt.Sprintf("%s%s", dockerPrefix, name))
if err != nil {
return nil, errors.Wrapf(origErr, "reference %q must be a docker reference", name)
}
} }
return imageRef, nil if imageRef.Transport().Name() == docker.Transport.Name() {
return nil
}
return errors.Errorf("unsupport transport %s in %q: only docker transport is supported", imageRef.Transport().Name(), name)
} }
// ParseStorageReference parses the specified image name to a // ParseStorageReference parses the specified image name to a

View File

@ -2119,7 +2119,7 @@ MemoryReservation: {{ .HostConfig.MemoryReservation }}`})
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml}) kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
kube.WaitWithDefaultTimeout() kube.WaitWithDefaultTimeout()
Expect(kube.ExitCode()).To(Equal(125)) Expect(kube.ExitCode()).To(Equal(125))
Expect(kube.ErrorToString()).To(ContainSubstring(invalidImageName)) Expect(kube.ErrorToString()).To(ContainSubstring("invalid reference format"))
}) })
It("podman play kube applies log driver to containers", func() { It("podman play kube applies log driver to containers", func() {

View File

@ -49,6 +49,14 @@ function setup() {
@test "podman can pull an image" { @test "podman can pull an image" {
run_podman pull $IMAGE run_podman pull $IMAGE
# Also make sure that the tag@digest syntax is supported.
run_podman inspect --format "{{ .Digest }}" $IMAGE
digest=$output
run_podman pull $IMAGE@$digest
# Now untag the digest reference again.
run_podman untag $IMAGE $IMAGE@$digest
} }
# PR #7212: allow --remote anywhere before subcommand, not just as 1st flag # PR #7212: allow --remote anywhere before subcommand, not just as 1st flag

View File

@ -5,6 +5,7 @@ import (
"github.com/containers/image/v5/docker/reference" "github.com/containers/image/v5/docker/reference"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus"
) )
// NormalizeName normalizes the provided name according to the conventions by // NormalizeName normalizes the provided name according to the conventions by
@ -40,6 +41,11 @@ func NormalizeName(name string) (reference.Named, error) {
} }
if _, hasTag := named.(reference.NamedTagged); hasTag { if _, hasTag := named.(reference.NamedTagged); hasTag {
// Strip off the tag of a tagged and digested reference.
named, err = normalizeTaggedDigestedNamed(named)
if err != nil {
return nil, err
}
return named, nil return named, nil
} }
if _, hasDigest := named.(reference.Digested); hasDigest { if _, hasDigest := named.(reference.Digested); hasDigest {
@ -90,3 +96,48 @@ func ToNameTagPairs(repoTags []reference.Named) ([]NameTagPair, error) {
} }
return pairs, nil return pairs, nil
} }
// normalizeTaggedDigestedString strips the tag off the specified string iff it
// is tagged and digested. Note that the tag is entirely ignored to match
// Docker behavior.
func normalizeTaggedDigestedString(s string) (string, error) {
// Note that the input string is not expected to be parseable, so we
// return it verbatim in error cases.
ref, err := reference.Parse(s)
if err != nil {
return "", err
}
named, ok := ref.(reference.Named)
if !ok {
return s, nil
}
named, err = normalizeTaggedDigestedNamed(named)
if err != nil {
return "", err
}
return named.String(), nil
}
// normalizeTaggedDigestedNamed strips the tag off the specified named
// reference iff it is tagged and digested. Note that the tag is entirely
// ignored to match Docker behavior.
func normalizeTaggedDigestedNamed(named reference.Named) (reference.Named, error) {
_, isTagged := named.(reference.NamedTagged)
if !isTagged {
return named, nil
}
digested, isDigested := named.(reference.Digested)
if !isDigested {
return named, nil
}
// Now strip off the tag.
newNamed := reference.TrimNamed(named)
// And re-add the digest.
newNamed, err := reference.WithDigest(newNamed, digested.Digest())
if err != nil {
return named, err
}
logrus.Debugf("Stripped off tag from tagged and digested reference %q", named.String())
return newNamed, nil
}

View File

@ -52,6 +52,7 @@ func (r *Runtime) Pull(ctx context.Context, name string, pullPolicy config.PullP
options = &PullOptions{} options = &PullOptions{}
} }
var possiblyUnqualifiedName string // used for short-name resolution
ref, err := alltransports.ParseImageName(name) ref, err := alltransports.ParseImageName(name)
if err != nil { if err != nil {
// If the image clearly refers to a local one, we can look it up directly. // If the image clearly refers to a local one, we can look it up directly.
@ -67,6 +68,15 @@ func (r *Runtime) Pull(ctx context.Context, name string, pullPolicy config.PullP
return []*Image{local}, err return []*Image{local}, err
} }
// Docker compat: strip off the tag iff name is tagged and digested
// (e.g., fedora:latest@sha256...). In that case, the tag is stripped
// off and entirely ignored. The digest is the sole source of truth.
normalizedName, normalizeError := normalizeTaggedDigestedString(name)
if normalizeError != nil {
return nil, normalizeError
}
name = normalizedName
// If the input does not include a transport assume it refers // If the input does not include a transport assume it refers
// to a registry. // to a registry.
dockerRef, dockerErr := alltransports.ParseImageName("docker://" + name) dockerRef, dockerErr := alltransports.ParseImageName("docker://" + name)
@ -74,6 +84,17 @@ func (r *Runtime) Pull(ctx context.Context, name string, pullPolicy config.PullP
return nil, err return nil, err
} }
ref = dockerRef ref = dockerRef
possiblyUnqualifiedName = name
} else if ref.Transport().Name() == registryTransport.Transport.Name() {
// Normalize the input if we're referring to the docker
// transport directly. That makes sure that a `docker://fedora`
// will resolve directly to `docker.io/library/fedora:latest`
// and not be subject to short-name resolution.
named := ref.DockerReference()
if named == nil {
return nil, errors.New("internal error: unexpected nil reference")
}
possiblyUnqualifiedName = named.String()
} }
if options.AllTags && ref.Transport().Name() != registryTransport.Transport.Name() { if options.AllTags && ref.Transport().Name() != registryTransport.Transport.Name() {
@ -94,7 +115,7 @@ func (r *Runtime) Pull(ctx context.Context, name string, pullPolicy config.PullP
// DOCKER REGISTRY // DOCKER REGISTRY
case registryTransport.Transport.Name(): case registryTransport.Transport.Name():
pulledImages, pullError = r.copyFromRegistry(ctx, ref, strings.TrimPrefix(name, "docker://"), pullPolicy, options) pulledImages, pullError = r.copyFromRegistry(ctx, ref, possiblyUnqualifiedName, pullPolicy, options)
// DOCKER ARCHIVE // DOCKER ARCHIVE
case dockerArchiveTransport.Transport.Name(): case dockerArchiveTransport.Transport.Name():

View File

@ -180,6 +180,15 @@ func (r *Runtime) LookupImage(name string, options *LookupImageOptions) (*Image,
} }
logrus.Debugf("Found image %q in local containers storage (%s)", name, storageRef.StringWithinTransport()) logrus.Debugf("Found image %q in local containers storage (%s)", name, storageRef.StringWithinTransport())
return r.storageToImage(img, storageRef), "", nil return r.storageToImage(img, storageRef), "", nil
} else {
// Docker compat: strip off the tag iff name is tagged and digested
// (e.g., fedora:latest@sha256...). In that case, the tag is stripped
// off and entirely ignored. The digest is the sole source of truth.
normalizedName, err := normalizeTaggedDigestedString(name)
if err != nil {
return nil, "", err
}
name = normalizedName
} }
originalName := name originalName := name

View File

@ -195,7 +195,7 @@ func DefaultConfig() (*Config, error) {
Init: false, Init: false,
InitPath: "", InitPath: "",
IPCNS: "private", IPCNS: "private",
LogDriver: DefaultLogDriver, LogDriver: defaultLogDriver(),
LogSizeMax: DefaultLogSizeMax, LogSizeMax: DefaultLogSizeMax,
NetNS: netns, NetNS: netns,
NoHosts: false, NoHosts: false,

View File

@ -3,9 +3,17 @@
package config package config
func defaultCgroupManager() string { func defaultCgroupManager() string {
return "cgroupfs" return CgroupfsCgroupsManager
} }
func defaultEventsLogger() string { func defaultEventsLogger() string {
return "file" return "file"
} }
func defaultLogDriver() string {
return DefaultLogDriver
}
func useSystemd() bool {
return false
}

View File

@ -3,11 +3,23 @@
package config package config
import ( import (
"io/ioutil"
"strings"
"sync"
"github.com/containers/common/pkg/cgroupv2" "github.com/containers/common/pkg/cgroupv2"
"github.com/containers/storage/pkg/unshare" "github.com/containers/storage/pkg/unshare"
) )
var (
systemdOnce sync.Once
usesSystemd bool
)
func defaultCgroupManager() string { func defaultCgroupManager() string {
if !useSystemd() {
return CgroupfsCgroupsManager
}
enabled, err := cgroupv2.Enabled() enabled, err := cgroupv2.Enabled()
if err == nil && !enabled && unshare.IsRootless() { if err == nil && !enabled && unshare.IsRootless() {
return CgroupfsCgroupsManager return CgroupfsCgroupsManager
@ -15,6 +27,32 @@ func defaultCgroupManager() string {
return SystemdCgroupsManager return SystemdCgroupsManager
} }
func defaultEventsLogger() string { func defaultEventsLogger() string {
return "journald" if useSystemd() {
return "journald"
}
return "file"
}
func defaultLogDriver() string {
// If we decide to change the default for logdriver, it should be done here.
if useSystemd() {
return DefaultLogDriver
}
return DefaultLogDriver
}
func useSystemd() bool {
systemdOnce.Do(func() {
dat, err := ioutil.ReadFile("/proc/1/comm")
if err == nil {
val := strings.TrimSuffix(string(dat), "\n")
usesSystemd = (val == "systemd")
}
return
})
return usesSystemd
} }

View File

@ -1,4 +1,4 @@
package version package version
// Version is the version of the build. // Version is the version of the build.
const Version = "0.39.0" const Version = "0.39.1-dev"

2
vendor/modules.txt vendored
View File

@ -91,7 +91,7 @@ github.com/containers/buildah/pkg/overlay
github.com/containers/buildah/pkg/parse github.com/containers/buildah/pkg/parse
github.com/containers/buildah/pkg/rusage github.com/containers/buildah/pkg/rusage
github.com/containers/buildah/util github.com/containers/buildah/util
# github.com/containers/common v0.39.0 # github.com/containers/common v0.39.1-0.20210527140106-e5800a20386a
github.com/containers/common/libimage github.com/containers/common/libimage
github.com/containers/common/libimage/manifests github.com/containers/common/libimage/manifests
github.com/containers/common/pkg/apparmor github.com/containers/common/pkg/apparmor