cache push only overrides remote if explicit

Signed-off-by: Avi Deitcher <avi@deitcher.net>
This commit is contained in:
Avi Deitcher 2024-03-05 20:05:01 +02:00
parent 60da9b92e4
commit 71096c4301
5 changed files with 20 additions and 7 deletions

View File

@ -18,7 +18,7 @@ import (
// If withArchSpecificTags is true, it will push all arch-specific images in the index, each as // 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. // 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. // image:foo will have image:foo-amd64, image:foo-arm64, etc.
func (p *Provider) Push(name, remoteName string, withArchSpecificTags bool) error { func (p *Provider) Push(name, remoteName string, withArchSpecificTags, override bool) error {
var ( var (
err error err error
options []remote.Option options []remote.Option
@ -30,14 +30,25 @@ func (p *Provider) Push(name, remoteName string, withArchSpecificTags bool) erro
if err != nil { if err != nil {
return err return err
} }
options = append(options, remote.WithAuthFromKeychain(authn.DefaultKeychain))
fmt.Printf("Pushing local %s as %s\n", name, remoteName) fmt.Printf("Pushing local %s as %s\n", name, remoteName)
// check if it already exists, unless override is explicit
if !override {
if _, err := remote.Get(ref, options...); err == nil {
log.Infof("image %s already exists in the registry, skipping", remoteName)
return nil
}
}
// if we made it this far, either we had a specific override, or we do not have the image remotely
// do we even have the given one? // do we even have the given one?
root, err := p.FindRoot(name) root, err := p.FindRoot(name)
if err != nil { if err != nil {
return err return err
} }
options = append(options, remote.WithAuthFromKeychain(authn.DefaultKeychain))
img, err1 := root.Image() img, err1 := root.Image()
ii, err2 := root.ImageIndex() ii, err2 := root.ImageIndex()

View File

@ -11,6 +11,7 @@ func cachePushCmd() *cobra.Command {
var ( var (
remoteName string remoteName string
pushArchSpecificTags bool pushArchSpecificTags bool
override bool
) )
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "push", Use: "push",
@ -29,7 +30,7 @@ func cachePushCmd() *cobra.Command {
log.Fatalf("unable to read a local cache: %v", err) log.Fatalf("unable to read a local cache: %v", err)
} }
if err := p.Push(fullname, remoteName, pushArchSpecificTags); err != nil { if err := p.Push(fullname, remoteName, pushArchSpecificTags, override); err != nil {
log.Fatalf("unable to push image named %s: %v", name, err) log.Fatalf("unable to push image named %s: %v", name, err)
} }
} }
@ -38,5 +39,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().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.") 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.")
cmd.Flags().BoolVar(&override, "override", false, "Even if the image already exists in the registry, push it again, overwriting the existing image.")
return cmd return cmd
} }

View File

@ -524,7 +524,7 @@ func (p Pkg) Build(bos ...BuildOpt) error {
} }
// push the manifest // push the manifest
if err := c.Push(p.FullTag(), "", bo.manifest); err != nil { if err := c.Push(p.FullTag(), "", bo.manifest, true); err != nil {
return err return err
} }
@ -546,7 +546,7 @@ func (p Pkg) Build(bos ...BuildOpt) error {
if _, err := c.DescriptorWrite(&ref, *desc); err != nil { if _, err := c.DescriptorWrite(&ref, *desc); err != nil {
return err return err
} }
if err := c.Push(fullRelTag, "", bo.manifest); err != nil { if err := c.Push(fullRelTag, "", bo.manifest, true); err != nil {
return err return err
} }

View File

@ -392,7 +392,7 @@ func (c *cacheMocker) IndexWrite(ref *reference.Spec, descriptors ...registry.De
return c.NewSource(ref, "", &desc), nil return c.NewSource(ref, "", &desc), nil
} }
func (c *cacheMocker) Push(name, remoteName string, withManifest bool) error { func (c *cacheMocker) Push(name, remoteName string, withManifest, override bool) error {
if !c.enablePush { if !c.enablePush {
return errors.New("push disabled") return errors.New("push disabled")
} }

View File

@ -41,7 +41,7 @@ type CacheProvider interface {
// name is the name as referenced in the local cache, remoteName is the name to give it remotely. // 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. // If remoteName is empty, it is the same as name.
// if withManifest defined will push a multi-arch manifest // if withManifest defined will push a multi-arch manifest
Push(name, remoteName string, withManifest bool) error Push(name, remoteName string, withManifest, override bool) error
// NewSource return an ImageSource for a specific ref and architecture in the cache. // NewSource return an ImageSource for a specific ref and architecture in the cache.
NewSource(ref *reference.Spec, architecture string, descriptor *v1.Descriptor) ImageSource NewSource(ref *reference.Spec, architecture string, descriptor *v1.Descriptor) ImageSource
// GetContent returns an io.Reader to the provided content as is, given a specific digest. It is // GetContent returns an io.Reader to the provided content as is, given a specific digest. It is