Skip to content

Commit

Permalink
Merge pull request #3283 from jedevc/annotations-sanity-checks
Browse files Browse the repository at this point in the history
Introduce annotation sanity checks
  • Loading branch information
tonistiigi committed Nov 16, 2022
2 parents 4b136c2 + 5e8752d commit 1094612
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
23 changes: 16 additions & 7 deletions exporter/containerimage/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,27 @@ func (ag AnnotationsGroup) Merge(other AnnotationsGroup) AnnotationsGroup {
if other == nil {
return ag
}
if ag == nil {
ag = make(AnnotationsGroup)
}

for k, v := range other {
if _, ok := ag[k]; ok {
ag[k].merge(v)
} else {
ag[k] = v
}
ag[k] = ag[k].merge(v)
}
return ag
}

func (a *Annotations) merge(other *Annotations) {
func (a *Annotations) merge(other *Annotations) *Annotations {
if other == nil {
return
return a
}
if a == nil {
a = &Annotations{
IndexDescriptor: make(map[string]string),
Index: make(map[string]string),
Manifest: make(map[string]string),
ManifestDescriptor: make(map[string]string),
}
}

for k, v := range other.Index {
Expand All @@ -128,4 +135,6 @@ func (a *Annotations) merge(other *Annotations) {
for k, v := range other.ManifestDescriptor {
a.ManifestDescriptor[k] = v
}

return a
}
2 changes: 1 addition & 1 deletion exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
if err != nil {
return nil, err
}
opts.AddAnnotations(as)
opts.Annotations = opts.Annotations.Merge(as)

ctx, done, err := leaseutil.WithLease(ctx, e.opt.LeaseManager, leaseutil.MakeTemporary)
if err != nil {
Expand Down
17 changes: 1 addition & 16 deletions exporter/containerimage/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,11 @@ func (c *ImageCommitOpts) Load(opt map[string]string) (map[string]string, error)
c.EnableForceCompression(c.RefCfg.Compression.Type.String())
}

c.AddAnnotations(as)
c.Annotations = c.Annotations.Merge(as)

return rest, nil
}

func (c *ImageCommitOpts) AddAnnotations(annotations AnnotationsGroup) {
if annotations == nil {
return
}
if c.Annotations == nil {
c.Annotations = AnnotationsGroup{}
}
c.Annotations = c.Annotations.Merge(annotations)
for _, a := range annotations {
if len(a.Index)+len(a.IndexDescriptor)+len(a.ManifestDescriptor) > 0 {
c.EnableOCITypes("annotations")
}
}
}

func (c *ImageCommitOpts) EnableOCITypes(reason string) {
if !c.OCITypes {
message := "forcibly turning on oci-mediatype mode"
Expand Down
21 changes: 20 additions & 1 deletion exporter/containerimage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ func (ic *ImageWriter) Commit(ctx context.Context, inp *exporter.Source, session
}
}

for pk, a := range opts.Annotations {
if pk != "" {
if inp.Refs == nil {
return nil, errors.Errorf("invalid annotation: no platforms defined")
}
if _, ok := inp.Refs[pk]; !ok {
return nil, errors.Errorf("invalid annotation: no platform %s found in source", pk)
}
}
if len(a.Index)+len(a.IndexDescriptor)+len(a.ManifestDescriptor) > 0 {
opts.EnableOCITypes("annotations")
}
}

if len(inp.Refs) == 0 {
remotes, err := ic.exportLayers(ctx, opts.RefCfg, session.NewGroup(sessionID), inp.Ref)
if err != nil {
Expand All @@ -95,7 +109,12 @@ func (ic *ImageWriter) Commit(ctx context.Context, inp *exporter.Source, session
}
}

mfstDesc, configDesc, err := ic.commitDistributionManifest(ctx, opts, inp.Ref, inp.Metadata[exptypes.ExporterImageConfigKey], &remotes[0], opts.Annotations.Platform(nil), inp.Metadata[exptypes.ExporterInlineCache], dtbi, opts.Epoch, session.NewGroup(sessionID))
annotations := opts.Annotations.Platform(nil)
if len(annotations.Index) > 0 || len(annotations.IndexDescriptor) > 0 {
return nil, errors.Errorf("index annotations not supported for single platform export")
}

mfstDesc, configDesc, err := ic.commitDistributionManifest(ctx, opts, inp.Ref, inp.Metadata[exptypes.ExporterImageConfigKey], &remotes[0], annotations, inp.Metadata[exptypes.ExporterInlineCache], dtbi, opts.Epoch, session.NewGroup(sessionID))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion exporter/oci/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
if err != nil {
return nil, err
}
opts.AddAnnotations(as)
opts.Annotations = opts.Annotations.Merge(as)

ctx, done, err := leaseutil.WithLease(ctx, e.opt.LeaseManager, leaseutil.MakeTemporary)
if err != nil {
Expand Down

0 comments on commit 1094612

Please sign in to comment.