Merge pull request #4030 from deitch/canonicalize-pull

use canonical ref when looking in cache
This commit is contained in:
Avi Deitcher 2024-04-25 11:50:19 +03:00 committed by GitHub
commit e6b0ae05eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 2 deletions

View File

@ -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)}

View File

@ -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, ":") {