From c836e54d225b408d8d0987071aa97f188a15b4e8 Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Wed, 24 Apr 2024 15:00:39 +0300 Subject: [PATCH] use canonical ref when looking in cache Signed-off-by: Avi Deitcher --- src/cmd/linuxkit/cache/write.go | 7 +++++++ src/cmd/linuxkit/util/reference.go | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cmd/linuxkit/cache/write.go b/src/cmd/linuxkit/cache/write.go index ab3284b8e..18131a573 100644 --- a/src/cmd/linuxkit/cache/write.go +++ b/src/cmd/linuxkit/cache/write.go @@ -20,6 +20,7 @@ import ( "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/google/go-containerregistry/pkg/v1/types" lktspec "github.com/linuxkit/linuxkit/src/cmd/linuxkit/spec" + "github.com/linuxkit/linuxkit/src/cmd/linuxkit/util" lktutil "github.com/linuxkit/linuxkit/src/cmd/linuxkit/util" imagespec "github.com/opencontainers/image-spec/specs-go/v1" log "github.com/sirupsen/logrus" @@ -41,6 +42,12 @@ const ( // Note that ImagePull does try ValidateImage first, so if the image is already in the cache, it will not // do any network activity at all. func (p *Provider) ImagePull(ref *reference.Spec, trustedRef, architecture string, alwaysPull bool) (lktspec.ImageSource, error) { + imageName := util.ReferenceExpand(ref.String()) + canonicalRef, err := reference.Parse(imageName) + if err != nil { + return ImageSource{}, fmt.Errorf("invalid image name %s: %v", imageName, err) + } + ref = &canonicalRef image := ref.String() pullImageName := image remoteOptions := []remote.Option{remote.WithAuthFromKeychain(authn.DefaultKeychain)} diff --git a/src/cmd/linuxkit/util/reference.go b/src/cmd/linuxkit/util/reference.go index 862ce1663..7ed89c349 100644 --- a/src/cmd/linuxkit/util/reference.go +++ b/src/cmd/linuxkit/util/reference.go @@ -17,7 +17,9 @@ func ReferenceWithTag() ReferenceOption { } } -// ReferenceExpand expands "redis" to "docker.io/library/redis" so all images have a full domain +// ReferenceExpand expands "redis" to "docker.io/library/redis" so all images have a full domain, +// and similarly foo/bar to docker.io/foo/bar. +// If the image does not have a tag, ":latest" is added. func ReferenceExpand(ref string, options ...ReferenceOption) string { var opts refOpts for _, opt := range options { @@ -26,8 +28,11 @@ func ReferenceExpand(ref string, options ...ReferenceOption) string { ret := ref parts := strings.Split(ref, "/") - if len(parts) == 1 { + switch len(parts) { + case 1: ret = "docker.io/library/" + ref + case 2: + ret = "docker.io/" + ref } if opts.withTag && !strings.Contains(ret, ":") {