From 51a4c3b3ebcf416d73b6678f803d36f643245a4c Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Fri, 1 Mar 2024 13:41:59 +0200 Subject: [PATCH] push arch-specific tags optional Signed-off-by: Avi Deitcher --- src/cmd/linuxkit/cache/push.go | 73 +++++++++++++++++++--------------- src/cmd/linuxkit/cache_push.go | 9 +++-- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/cmd/linuxkit/cache/push.go b/src/cmd/linuxkit/cache/push.go index 3462619d1..1c8099b27 100644 --- a/src/cmd/linuxkit/cache/push.go +++ b/src/cmd/linuxkit/cache/push.go @@ -15,7 +15,10 @@ import ( // Push push an image along with a multi-arch index. // name is the name as referenced in the local cache, remoteName is the name to give it remotely. // If remoteName is empty, it is the same as name. -func (p *Provider) Push(name, remoteName string, withManifest bool) error { +// If withArchSpecificTags is true, it will push all arch-specific images in the index, each as +// their own tag with the same name as the index, but with the architecture appended, e.g. +// image:foo will have image:foo-amd64, image:foo-arm64, etc. +func (p *Provider) Push(name, remoteName string, withArchSpecificTags bool) error { var ( err error options []remote.Option @@ -89,42 +92,46 @@ func (p *Provider) Push(name, remoteName string, withManifest bool) error { return err } fmt.Printf("Pushed index %s\n", name) - log.Debugf("pushing individual images in the index %s", name) - for _, m := range manifest.Manifests { - if m.Platform == nil || m.Platform.Architecture == "" { - continue - } - archTag := fmt.Sprintf("%s-%s", remoteName, m.Platform.Architecture) - tag, err := namepkg.NewTag(archTag) - if err != nil { - return fmt.Errorf("could not create a valid arch-specific tag %s: %v", archTag, err) - } - img, err := p.cache.Image(m.Digest) - if err != nil { - // it might not have existed, so we can add it locally - // use the original image name in the annotation - desc := m.DeepCopy() - if desc.Annotations == nil { - desc.Annotations = map[string]string{} + if withArchSpecificTags { + fmt.Printf("pushing individual images in the index %s", name) + for _, m := range manifest.Manifests { + if m.Platform == nil || m.Platform.Architecture == "" { + continue } - desc.Annotations[imagespec.AnnotationRefName] = archTag - if err := p.cache.AppendDescriptor(*desc); err != nil { - return fmt.Errorf("error appending descriptor for %s to layout index: %v", archTag, err) - } - img, err = p.cache.Image(m.Digest) + archTag := fmt.Sprintf("%s-%s", remoteName, m.Platform.Architecture) + tag, err := namepkg.NewTag(archTag) if err != nil { - return fmt.Errorf("could not find or create arch-specific image for %s: %v", archTag, err) + return fmt.Errorf("could not create a valid arch-specific tag %s: %v", archTag, err) + } + img, err := p.cache.Image(m.Digest) + if err != nil { + // it might not have existed, so we can add it locally + // use the original image name in the annotation + desc := m.DeepCopy() + if desc.Annotations == nil { + desc.Annotations = map[string]string{} + } + desc.Annotations[imagespec.AnnotationRefName] = archTag + if err := p.cache.AppendDescriptor(*desc); err != nil { + return fmt.Errorf("error appending descriptor for %s to layout index: %v", archTag, err) + } + img, err = p.cache.Image(m.Digest) + if err != nil { + return fmt.Errorf("could not find or create arch-specific image for %s: %v", archTag, err) + } + } + if err := validate.Image(img); err != nil { + // skip arch we did not build/pull locally + log.Debugf("could not validate arch-specific image for %s: %v", archTag, err) + continue + } + log.Debugf("pushing image %s", tag) + if err := remote.Tag(tag, img, options...); err != nil { + return fmt.Errorf("error creating tag %s: %v", archTag, err) } } - if err := validate.Image(img); err != nil { - // skip arch we did not build/pull locally - log.Debugf("could not validate arch-specific image for %s: %v", archTag, err) - continue - } - log.Debugf("pushing image %s", tag) - if err := remote.Tag(tag, img, options...); err != nil { - return fmt.Errorf("error creating tag %s: %v", archTag, err) - } + } else { + fmt.Printf("Skipping push of individual images in the index %s\n", name) } default: return fmt.Errorf("name %s unknown in cache", name) diff --git a/src/cmd/linuxkit/cache_push.go b/src/cmd/linuxkit/cache_push.go index 9229495d1..1f0dbfbbd 100644 --- a/src/cmd/linuxkit/cache_push.go +++ b/src/cmd/linuxkit/cache_push.go @@ -8,7 +8,10 @@ import ( ) func cachePushCmd() *cobra.Command { - var remoteName string + var ( + remoteName string + pushArchSpecificTags bool + ) cmd := &cobra.Command{ Use: "push", Short: "push images from the linuxkit cache", @@ -26,7 +29,7 @@ func cachePushCmd() *cobra.Command { log.Fatalf("unable to read a local cache: %v", err) } - if err := p.Push(fullname, remoteName, true); err != nil { + if err := p.Push(fullname, remoteName, pushArchSpecificTags); err != nil { log.Fatalf("unable to push image named %s: %v", name, err) } } @@ -34,6 +37,6 @@ func cachePushCmd() *cobra.Command { }, } cmd.Flags().StringVar(&remoteName, "remote-name", "", "Push it under a different name, e.g. push local image foo/bar:mine as baz/bee:yours. If blank, uses same local name.") - + cmd.Flags().BoolVar(&pushArchSpecificTags, "with-arch-tags", false, "When the local reference is an index, add to the remote arch-specific tags for each arch in the index, each as their own tag with the same name as the index, but with the architecture appended, e.g. image:foo will have image:foo-amd64, image:foo-arm64, etc.") return cmd }