Skip to content

Commit

Permalink
allow annotations for OCI image index
Browse files Browse the repository at this point in the history
Signed-off-by: Qasim Sarfraz <qasimsarfraz@microsoft.com>
  • Loading branch information
mqasimsarfraz committed Jul 21, 2023
1 parent b8739d7 commit b071d65
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
}
}

dt, desc, err := itpull.Combine(ctx, srcs)
dt, desc, err := itpull.Combine(ctx, srcs, nil)
if err != nil {
return err
}
Expand Down
21 changes: 20 additions & 1 deletion commands/imagetools/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type createOptions struct {
builder string
files []string
tags []string
annotations []string
dryrun bool
actionAppend bool
progress string
Expand Down Expand Up @@ -82,6 +83,11 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
return errors.Errorf("no repositories specified, please set a reference in tag or source")
}

ann, err := parseAnnotations(in.annotations)
if err != nil {
return err
}

var defaultRepo *string
if len(repos) == 1 {
for repo := range repos {
Expand Down Expand Up @@ -154,7 +160,7 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
}
}

dt, desc, err := r.Combine(ctx, srcs)
dt, desc, err := r.Combine(ctx, srcs, ann)
if err != nil {
return err
}
Expand Down Expand Up @@ -264,6 +270,18 @@ func parseSource(in string) (*imagetools.Source, error) {
return &s, nil
}

func parseAnnotations(in []string) (map[string]string, error) {
out := make(map[string]string)
for _, i := range in {
kv := strings.SplitN(i, "=", 2)
if len(kv) != 2 {
return nil, errors.Errorf("invalid annotation %q, expected key=value", in)
}
out[kv[0]] = kv[1]
}
return out, nil
}

func createCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command {
var options createOptions

Expand All @@ -283,6 +301,7 @@ func createCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command {
flags.BoolVar(&options.dryrun, "dry-run", false, "Show final image instead of pushing")
flags.BoolVar(&options.actionAppend, "append", false, "Append to existing manifest")
flags.StringVar(&options.progress, "progress", "auto", `Set type of progress output ("auto", "plain", "tty"). Use plain to show container output`)
flags.StringArrayVarP(&options.annotations, "annotations", "", []string{}, "Add annotations to OCI image index")

return cmd
}
Expand Down
11 changes: 9 additions & 2 deletions util/imagetools/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Source struct {
Ref reference.Named
}

func (r *Resolver) Combine(ctx context.Context, srcs []*Source) ([]byte, ocispec.Descriptor, error) {
func (r *Resolver) Combine(ctx context.Context, srcs []*Source, ann map[string]string) ([]byte, ocispec.Descriptor, error) {
eg, ctx := errgroup.WithContext(ctx)

dts := make([][]byte, len(srcs))
Expand Down Expand Up @@ -138,12 +138,19 @@ func (r *Resolver) Combine(ctx context.Context, srcs []*Source) ([]byte, ocispec
mt = ocispec.MediaTypeImageIndex
}

// annotations are only allowed on OCI indexes
annotations := make(map[string]string)
if mt == ocispec.MediaTypeImageIndex {
annotations = ann
}

idxBytes, err := json.MarshalIndent(ocispec.Index{
MediaType: mt,
Versioned: specs.Versioned{
SchemaVersion: 2,
},
Manifests: newDescs,
Manifests: newDescs,
Annotations: annotations,
}, "", " ")
if err != nil {
return nil, ocispec.Descriptor{}, errors.Wrap(err, "failed to marshal index")
Expand Down

0 comments on commit b071d65

Please sign in to comment.