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 committed 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.

11 changes: 11 additions & 0 deletions vendor/github.com/opencontainers/image-spec/specs-go/v1/config.go

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.