-
Notifications
You must be signed in to change notification settings - Fork 685
imagetools: Allow annotations for OCI image index #1965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "context" | ||
| "encoding/json" | ||
| "net/url" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/containerd/containerd/content" | ||
|
|
@@ -13,6 +14,7 @@ import ( | |
| "github.com/containerd/containerd/platforms" | ||
| "github.com/containerd/containerd/remotes" | ||
| "github.com/docker/distribution/reference" | ||
| "github.com/moby/buildkit/exporter/containerimage/exptypes" | ||
| "github.com/moby/buildkit/util/contentutil" | ||
| "github.com/opencontainers/go-digest" | ||
| "github.com/opencontainers/image-spec/specs-go" | ||
|
|
@@ -26,7 +28,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)) | ||
|
|
@@ -75,7 +77,7 @@ func (r *Resolver) Combine(ctx context.Context, srcs []*Source) ([]byte, ocispec | |
| } | ||
|
|
||
| // on single source, return original bytes | ||
| if len(srcs) == 1 { | ||
| if len(srcs) == 1 && len(ann) == 0 { | ||
| if mt := srcs[0].Desc.MediaType; mt == images.MediaTypeDockerSchema2ManifestList || mt == ocispec.MediaTypeImageIndex { | ||
| return dts[0], srcs[0].Desc, nil | ||
| } | ||
|
|
@@ -138,12 +140,39 @@ func (r *Resolver) Combine(ctx context.Context, srcs []*Source) ([]byte, ocispec | |
| mt = ocispec.MediaTypeImageIndex | ||
| } | ||
|
|
||
| // annotations are only allowed on OCI indexes | ||
| indexAnnotation := make(map[string]string) | ||
| if mt == ocispec.MediaTypeImageIndex { | ||
| annotations, err := parseAnnotations(ann) | ||
| if err != nil { | ||
| return nil, ocispec.Descriptor{}, err | ||
| } | ||
| if len(annotations[exptypes.AnnotationIndex]) > 0 { | ||
| for k, v := range annotations[exptypes.AnnotationIndex] { | ||
| indexAnnotation[k.Key] = v | ||
| } | ||
| } | ||
| if len(annotations[exptypes.AnnotationManifestDescriptor]) > 0 { | ||
| for i := 0; i < len(newDescs); i++ { | ||
| if newDescs[i].Annotations == nil { | ||
| newDescs[i].Annotations = map[string]string{} | ||
| } | ||
| for k, v := range annotations[exptypes.AnnotationManifestDescriptor] { | ||
| if k.Platform == nil || k.PlatformString() == platforms.Format(*newDescs[i].Platform) { | ||
| newDescs[i].Annotations[k.Key] = v | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| idxBytes, err := json.MarshalIndent(ocispec.Index{ | ||
| MediaType: mt, | ||
| Versioned: specs.Versioned{ | ||
| SchemaVersion: 2, | ||
| }, | ||
| Manifests: newDescs, | ||
| Manifests: newDescs, | ||
| Annotations: indexAnnotation, | ||
| }, "", " ") | ||
| if err != nil { | ||
| return nil, ocispec.Descriptor{}, errors.Wrap(err, "failed to marshal index") | ||
|
|
@@ -266,3 +295,52 @@ func detectMediaType(dt []byte) (string, error) { | |
|
|
||
| return images.MediaTypeDockerSchema2ManifestList, nil | ||
| } | ||
|
|
||
| func parseAnnotations(ann map[string]string) (map[string]map[exptypes.AnnotationKey]string, error) { | ||
| // TODO: use buildkit's annotation parser once it supports setting custom prefix and ":" separator | ||
| annotationRegexp := regexp.MustCompile(`^([a-z-]+)(?:\[([A-Za-z0-9_/-]+)\])?:(\S+)$`) | ||
| indexAnnotations := make(map[exptypes.AnnotationKey]string) | ||
| manifestDescriptorAnnotations := make(map[exptypes.AnnotationKey]string) | ||
| for k, v := range ann { | ||
| groups := annotationRegexp.FindStringSubmatch(k) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using a regexp here, wdyt about using https://github.com/moby/buildkit/blob/dd0053cdce470b1355fdb0bd5a8f2b0fc506d842/exporter/containerimage/exptypes/annotations.go#L85 instead? Obviously, it's not perfect, since we'd need to add the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Initially I had the same idea but we are using
Does it make sense to make buildkit parser to also handle
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I'd prefer the conversion to buildkit's form for now, so that we only have one source of truth for parsing these keys - I'm happy to follow up in buildkit to rework the logic to be a bit more reusable for this case. I also just noticed (sorry), that we aren't handling platforms?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Agreed. That should be the way moving forward. I added a comment regrading using buildkit once it supports our use-case here.
Great Catch. Done. |
||
| if groups == nil { | ||
| return nil, errors.Errorf("invalid annotation format, expected <type>:<key>=<value>, got %q", k) | ||
| } | ||
|
|
||
| typ, platform, key := groups[1], groups[2], groups[3] | ||
| var ociPlatform *ocispec.Platform | ||
| if platform != "" { | ||
| p, err := platforms.Parse(platform) | ||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "invalid platform %q", platform) | ||
| } | ||
| ociPlatform = &p | ||
| } | ||
| switch typ { | ||
| case exptypes.AnnotationIndex: | ||
| ak := exptypes.AnnotationKey{ | ||
| Type: typ, | ||
| Platform: ociPlatform, | ||
| Key: key, | ||
| } | ||
| indexAnnotations[ak] = v | ||
| case exptypes.AnnotationManifestDescriptor: | ||
| ak := exptypes.AnnotationKey{ | ||
| Type: typ, | ||
| Platform: ociPlatform, | ||
| Key: key, | ||
| } | ||
| manifestDescriptorAnnotations[ak] = v | ||
| case exptypes.AnnotationManifest: | ||
| return nil, errors.Errorf("%q annotations are not supported yet", typ) | ||
| case exptypes.AnnotationIndexDescriptor: | ||
| return nil, errors.Errorf("%q annotations are invalid while creating an image", typ) | ||
| default: | ||
| return nil, errors.Errorf("unknown annotation type %q", typ) | ||
| } | ||
| } | ||
| return map[string]map[exptypes.AnnotationKey]string{ | ||
| exptypes.AnnotationIndex: indexAnnotations, | ||
| exptypes.AnnotationManifestDescriptor: manifestDescriptorAnnotations, | ||
| }, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.