Skip to content

Commit

Permalink
crane: add --flatten for index append (#1566)
Browse files Browse the repository at this point in the history
* crane: add --flatten for index append

While it's possible that folks might want to append an index to an
index, it's more likely that they would want to merge them. This flag
gives them an option to do either.

* Update cmd/crane/cmd/index.go

Co-authored-by: Jason Hall <jason@chainguard.dev>

* ./hack/update-codegen.sh

---------

Co-authored-by: Jason Hall <jason@chainguard.dev>
  • Loading branch information
jonjohnsonjr and imjasonh committed Feb 23, 2023
1 parent 4a0e0af commit 4e95ae2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
40 changes: 36 additions & 4 deletions cmd/crane/cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func NewCmdIndexFilter(options *[]crane.Option) *cobra.Command {
func NewCmdIndexAppend(options *[]crane.Option) *cobra.Command {
var baseRef, newTag string
var newManifests []string
var dockerEmptyBase bool
var dockerEmptyBase, flatten bool

cmd := &cobra.Command{
Use: "append",
Expand Down Expand Up @@ -194,9 +194,40 @@ The platform for appended manifests is inferred from the config file or omitted
if err != nil {
return err
}
adds = append(adds, mutate.IndexAddendum{
Add: idx,
})
if flatten {
im, err := idx.IndexManifest()
if err != nil {
return err
}
for _, child := range im.Manifests {
switch {
case child.MediaType.IsImage():
img, err := idx.Image(child.Digest)
if err != nil {
return err
}
adds = append(adds, mutate.IndexAddendum{
Add: img,
Descriptor: child,
})
case child.MediaType.IsIndex():
idx, err := idx.ImageIndex(child.Digest)
if err != nil {
return err
}
adds = append(adds, mutate.IndexAddendum{
Add: idx,
Descriptor: child,
})
default:
return fmt.Errorf("unexpected child %q with media type %q", child.Digest, child.MediaType)
}
}
} else {
adds = append(adds, mutate.IndexAddendum{
Add: idx,
})
}
} else {
return fmt.Errorf("saw unexpected MediaType %q for %q", desc.MediaType, m)
}
Expand Down Expand Up @@ -229,6 +260,7 @@ The platform for appended manifests is inferred from the config file or omitted
cmd.Flags().StringVarP(&newTag, "tag", "t", "", "Tag to apply to resulting image")
cmd.Flags().StringSliceVarP(&newManifests, "manifest", "m", []string{}, "References to manifests to append to the base index")
cmd.Flags().BoolVar(&dockerEmptyBase, "docker-empty-base", false, "If true, empty base index will have Docker media types instead of OCI")
cmd.Flags().BoolVar(&flatten, "flatten", true, "If true, appending an index will append each of its children rather than the index itself")

return cmd
}
Expand Down
1 change: 1 addition & 0 deletions cmd/crane/doc/crane_index_append.md

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

5 changes: 5 additions & 0 deletions pkg/v1/zz_deepcopy_generated.go

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

0 comments on commit 4e95ae2

Please sign in to comment.