Skip to content

Commit

Permalink
exporter: explicitly error if annotation attached for non-existent pl…
Browse files Browse the repository at this point in the history
…atform

Signed-off-by: Justin Chadwell <me@jedevc.com>
  • Loading branch information
jedevc committed Nov 14, 2022
1 parent c748ec5 commit 7c1f555
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions exporter/containerimage/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ func ParseAnnotations(data map[string][]byte) (AnnotationsGroup, map[string][]by
return ag, rest, nil
}

func (ag AnnotationsGroup) Platforms() []ocispecs.Platform {
var ps []ocispecs.Platform
for pk := range ag {
p, err := platforms.Parse(pk)
if err == nil {
ps = append(ps, p)
}
}
return ps
}

func (ag AnnotationsGroup) Platform(p *ocispecs.Platform) *Annotations {
res := &Annotations{
IndexDescriptor: make(map[string]string),
Expand Down
9 changes: 9 additions & 0 deletions exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
return nil, err
}
opts.AddAnnotations(as)
for _, p := range opts.Annotations.Platforms() {
pk := platforms.Format(p)
if src.Refs == nil {
return nil, errors.Errorf("invalid annotation: no platform %s found in source", pk)
}
if _, ok := src.Refs[pk]; !ok {
return nil, errors.Errorf("invalid annotation: no platform %s found in source", pk)
}
}

ctx, done, err := leaseutil.WithLease(ctx, e.opt.LeaseManager, leaseutil.MakeTemporary)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions exporter/oci/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

archiveexporter "github.com/containerd/containerd/images/archive"
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/platforms"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/cache"
cacheconfig "github.com/moby/buildkit/cache/config"
Expand Down Expand Up @@ -109,6 +110,15 @@ func (e *imageExporterInstance) Export(ctx context.Context, src *exporter.Source
return nil, err
}
opts.AddAnnotations(as)
for _, p := range opts.Annotations.Platforms() {
pk := platforms.Format(p)
if src.Refs == nil {
return nil, errors.Errorf("invalid annotation: no platform %s found in source", pk)
}
if _, ok := src.Refs[pk]; !ok {
return nil, errors.Errorf("invalid annotation: no platform %s found in source", pk)
}
}

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

0 comments on commit 7c1f555

Please sign in to comment.