Skip to content

Commit df7c46b

Browse files
authored
Merge pull request #3384 from crazy-max/export-annotations-check
build: fail early if trying to export index annotations with moby exporter
2 parents 026e55b + a8f546e commit df7c46b

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

bake/bake.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,12 +1544,12 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
15441544
return nil, err
15451545
}
15461546

1547-
annotations, err := buildflags.ParseAnnotations(t.Annotations)
1547+
bo.Annotations, err = buildflags.ParseAnnotations(t.Annotations)
15481548
if err != nil {
15491549
return nil, err
15501550
}
15511551
for _, e := range bo.Exports {
1552-
for k, v := range annotations {
1552+
for k, v := range bo.Annotations {
15531553
e.Attrs[k.String()] = v
15541554
}
15551555
}

build/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ type Options struct {
9393
ProvenanceResponseMode confutil.MetadataProvenanceMode
9494
SourcePolicy *spb.Policy
9595
GroupRef string
96+
Annotations map[exptypes.AnnotationKey]string // Not used during build, annotations are already set in Exports. Just used to check for support with drivers.
9697
}
9798

9899
type CallFunc struct {

build/opt.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/moby/buildkit/client"
2929
"github.com/moby/buildkit/client/llb"
3030
"github.com/moby/buildkit/client/ociindex"
31+
"github.com/moby/buildkit/exporter/containerimage/exptypes"
3132
gateway "github.com/moby/buildkit/frontend/gateway/client"
3233
"github.com/moby/buildkit/identity"
3334
"github.com/moby/buildkit/session"
@@ -187,6 +188,20 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt *O
187188
}
188189
}
189190

191+
// check if index annotations are supported by docker driver
192+
if len(opt.Exports) > 0 && opt.CallFunc == nil && len(opt.Annotations) > 0 && nodeDriver.IsMobyDriver() && !nodeDriver.Features(ctx)[driver.MultiPlatform] {
193+
for _, exp := range opt.Exports {
194+
if exp.Type == "image" || exp.Type == "docker" {
195+
for ak := range opt.Annotations {
196+
switch ak.Type {
197+
case exptypes.AnnotationIndex, exptypes.AnnotationIndexDescriptor:
198+
return nil, nil, errors.New("index annotations not supported for single platform export")
199+
}
200+
}
201+
}
202+
}
203+
}
204+
190205
// fill in image exporter names from tags
191206
if len(opt.Tags) > 0 {
192207
tags := make([]string, len(opt.Tags))

commands/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,13 +1062,13 @@ func RunBuild(ctx context.Context, dockerCli command.Cli, in *BuildOptions, inSt
10621062
}
10631063
}
10641064

1065-
annotations, err := buildflags.ParseAnnotations(in.Annotations)
1065+
opts.Annotations, err = buildflags.ParseAnnotations(in.Annotations)
10661066
if err != nil {
10671067
return nil, nil, errors.Wrap(err, "parse annotations")
10681068
}
10691069

10701070
for _, o := range outputs {
1071-
for k, v := range annotations {
1071+
for k, v := range opts.Annotations {
10721072
o.Attrs[k.String()] = v
10731073
}
10741074
}

tests/build.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){
7878
testBuildCall,
7979
testBuildCheckCallOutput,
8080
testBuildExtraHosts,
81+
testBuildIndexAnnotationsLoadDocker,
8182
}
8283

8384
func testBuild(t *testing.T, sb integration.Sandbox) {
@@ -1341,6 +1342,17 @@ RUN cat /etc/hosts | grep myhostmulti | grep 162.242.195.82
13411342
require.NoError(t, err, string(out))
13421343
}
13431344

1345+
func testBuildIndexAnnotationsLoadDocker(t *testing.T, sb integration.Sandbox) {
1346+
if sb.DockerAddress() == "" {
1347+
t.Skip("only testing with docker available")
1348+
}
1349+
skipNoCompatBuildKit(t, sb, ">= 0.11.0-0", "annotations")
1350+
dir := createTestProject(t)
1351+
out, err := buildCmd(sb, withArgs("--annotation", "index:foo=bar", "--provenance", "false", "--output", "type=docker", dir))
1352+
require.Error(t, err, out)
1353+
require.Contains(t, out, "index annotations not supported for single platform export")
1354+
}
1355+
13441356
func createTestProject(t *testing.T) string {
13451357
dockerfile := []byte(`
13461358
FROM busybox:latest AS base

0 commit comments

Comments
 (0)