Skip to content

Commit

Permalink
Add --image-refs to crane push. (#1217)
Browse files Browse the repository at this point in the history
This adds an `--image-refs` flag to `crane push` similar to the flag
I recently added to `ko`, which emits a file containing the digests
of the various images published by the command.

The intention of this flag is to work well with things like Tekton
Chains' `IMAGES` result for attesting to how a bunch of images were
produced.
  • Loading branch information
mattmoor authored Dec 28, 2021
1 parent 2874338 commit 969d436
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ jobs:
dst=localhost:1338/roundtrip-test
./app/crane pull --format=oci $img $layout
./app/crane push $layout $dst
diff <(./app/crane manifest $img) <(./app/crane manifest $dst)
./app/crane push --image-refs=foo.images $layout $dst
diff <(./app/crane manifest $img) <(./app/crane manifest $(cat foo.images))
36 changes: 29 additions & 7 deletions cmd/crane/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"

"github.com/google/go-containerregistry/pkg/crane"
Expand All @@ -30,6 +31,7 @@ import (
// NewCmdPush creates a new cobra.Command for the push subcommand.
func NewCmdPush(options *[]crane.Option) *cobra.Command {
index := false
imageRefs := ""
cmd := &cobra.Command{
Use: "push PATH IMAGE",
Short: "Push local image contents to a remote registry",
Expand All @@ -43,23 +45,43 @@ func NewCmdPush(options *[]crane.Option) *cobra.Command {
return err
}

// TODO(generics): Make crane.Push support index.
o := crane.GetOptions(*options...)
ref, err := name.ParseReference(tag, o.Name...)
if err != nil {
return err
}
var h v1.Hash
switch t := img.(type) {
case v1.Image:
return crane.Push(t, tag, *options...)
if err := remote.Write(ref, t, o.Remote...); err != nil {
return err
}
if h, err = t.Digest(); err != nil {
return err
}
case v1.ImageIndex:
o := crane.GetOptions(*options...)
ref, err := name.ParseReference(tag, o.Name...)
if err != nil {
if err := remote.WriteIndex(ref, t, o.Remote...); err != nil {
return err
}
if h, err = t.Digest(); err != nil {
return err
}
return remote.WriteIndex(ref, t, o.Remote...)
default:
return fmt.Errorf("cannot push type (%T) to registry", img)
}

digest := ref.Context().Digest(h.String())
if imageRefs != "" {
return ioutil.WriteFile(imageRefs, []byte(digest.String()), 0600)
}
// TODO(mattmoor): think about printing the digest to standard out
// to facilitate command composition similar to ko build.

return fmt.Errorf("cannot push type (%T) to registry", img)
return nil
},
}
cmd.Flags().BoolVar(&index, "index", false, "push a collection of images as a single index, currently required if PATH contains multiple images")
cmd.Flags().StringVar(&imageRefs, "image-refs", "", "path to file where a list of the published image references will be written")
return cmd
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/crane/doc/crane_push.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 969d436

Please sign in to comment.