Skip to content

Commit

Permalink
Add --set-base-image-annotations flag to crane append (#1098)
Browse files Browse the repository at this point in the history
* Add --annotate flag to crane append

This flag instructs crane append to annotate the resulting image with
information about the base image (name and current digest). This flag is
false by default.

* update codegen

* change flag name, don't set base name if it's a digest

* use ref.Name() which canonicalizes e.g., ubuntu -> index.docker.io/library/ubuntu

* don't set empty annotation
  • Loading branch information
imjasonh authored Aug 4, 2021
1 parent a65a0a6 commit 1830951
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 9 deletions.
23 changes: 23 additions & 0 deletions cmd/crane/cmd/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ import (
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
specsv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
)

// NewCmdAppend creates a new cobra.Command for the append subcommand.
func NewCmdAppend(options *[]crane.Option) *cobra.Command {
var baseRef, newTag, outFile string
var newLayers []string
var annotate bool

appendCmd := &cobra.Command{
Use: "append",
Expand All @@ -53,6 +56,25 @@ func NewCmdAppend(options *[]crane.Option) *cobra.Command {
return fmt.Errorf("appending %v: %v", newLayers, err)
}

if baseRef != "" && annotate {
ref, err := name.ParseReference(baseRef)
if err != nil {
return fmt.Errorf("parsing ref %q: %v", baseRef, err)
}

baseDigest, err := base.Digest()
if err != nil {
return err
}
anns := map[string]string{
specsv1.AnnotationBaseImageDigest: baseDigest.String(),
}
if _, ok := ref.(name.Tag); ok {
anns[specsv1.AnnotationBaseImageName] = ref.Name()
}
img = mutate.Annotations(img, anns).(v1.Image)
}

if outFile != "" {
if err := crane.Save(img, newTag, outFile); err != nil {
return fmt.Errorf("writing output %q: %v", outFile, err)
Expand All @@ -78,6 +100,7 @@ func NewCmdAppend(options *[]crane.Option) *cobra.Command {
appendCmd.Flags().StringVarP(&newTag, "new_tag", "t", "", "Tag to apply to resulting image")
appendCmd.Flags().StringSliceVarP(&newLayers, "new_layer", "f", []string{}, "Path to tarball to append to image")
appendCmd.Flags().StringVarP(&outFile, "output", "o", "", "Path to new tarball of resulting image")
appendCmd.Flags().BoolVar(&annotate, "set-base-image-annotations", false, "If true, annotate the resulting image as being based on the base image")

appendCmd.MarkFlagRequired("new_tag")
appendCmd.MarkFlagRequired("new_layer")
Expand Down
11 changes: 6 additions & 5 deletions cmd/crane/doc/crane_append.md

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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/opencontainers/image-spec v1.0.1
github.com/opencontainers/image-spec v1.0.2-0.20210730191737-8e42a01fb1b7
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/cobra v1.2.1
Expand Down
3 changes: 2 additions & 1 deletion go.sum

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

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

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

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

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ github.com/klauspost/compress/zstd/internal/xxhash
## explicit
# github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/go-digest
# github.com/opencontainers/image-spec v1.0.1
# github.com/opencontainers/image-spec v1.0.2-0.20210730191737-8e42a01fb1b7
## explicit
github.com/opencontainers/image-spec/specs-go
github.com/opencontainers/image-spec/specs-go/v1
Expand Down

0 comments on commit 1830951

Please sign in to comment.