Skip to content

Commit 4e95ae2

Browse files
crane: add --flatten for index append (#1566)
* 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>
1 parent 4a0e0af commit 4e95ae2

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

cmd/crane/cmd/index.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func NewCmdIndexFilter(options *[]crane.Option) *cobra.Command {
112112
func NewCmdIndexAppend(options *[]crane.Option) *cobra.Command {
113113
var baseRef, newTag string
114114
var newManifests []string
115-
var dockerEmptyBase bool
115+
var dockerEmptyBase, flatten bool
116116

117117
cmd := &cobra.Command{
118118
Use: "append",
@@ -194,9 +194,40 @@ The platform for appended manifests is inferred from the config file or omitted
194194
if err != nil {
195195
return err
196196
}
197-
adds = append(adds, mutate.IndexAddendum{
198-
Add: idx,
199-
})
197+
if flatten {
198+
im, err := idx.IndexManifest()
199+
if err != nil {
200+
return err
201+
}
202+
for _, child := range im.Manifests {
203+
switch {
204+
case child.MediaType.IsImage():
205+
img, err := idx.Image(child.Digest)
206+
if err != nil {
207+
return err
208+
}
209+
adds = append(adds, mutate.IndexAddendum{
210+
Add: img,
211+
Descriptor: child,
212+
})
213+
case child.MediaType.IsIndex():
214+
idx, err := idx.ImageIndex(child.Digest)
215+
if err != nil {
216+
return err
217+
}
218+
adds = append(adds, mutate.IndexAddendum{
219+
Add: idx,
220+
Descriptor: child,
221+
})
222+
default:
223+
return fmt.Errorf("unexpected child %q with media type %q", child.Digest, child.MediaType)
224+
}
225+
}
226+
} else {
227+
adds = append(adds, mutate.IndexAddendum{
228+
Add: idx,
229+
})
230+
}
200231
} else {
201232
return fmt.Errorf("saw unexpected MediaType %q for %q", desc.MediaType, m)
202233
}
@@ -229,6 +260,7 @@ The platform for appended manifests is inferred from the config file or omitted
229260
cmd.Flags().StringVarP(&newTag, "tag", "t", "", "Tag to apply to resulting image")
230261
cmd.Flags().StringSliceVarP(&newManifests, "manifest", "m", []string{}, "References to manifests to append to the base index")
231262
cmd.Flags().BoolVar(&dockerEmptyBase, "docker-empty-base", false, "If true, empty base index will have Docker media types instead of OCI")
263+
cmd.Flags().BoolVar(&flatten, "flatten", true, "If true, appending an index will append each of its children rather than the index itself")
232264

233265
return cmd
234266
}

cmd/crane/doc/crane_index_append.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/v1/zz_deepcopy_generated.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)