Skip to content

Commit 16a74c3

Browse files
committed
exporter: use implicit ids for exporters
We can derive exporter ids from their place in the exporter array in a SolveRequest - this removes the need to manually generate and handle multiple sets of IDs. Signed-off-by: Justin Chadwell <me@jedevc.com>
1 parent ccdc0b3 commit 16a74c3

File tree

12 files changed

+267
-336
lines changed

12 files changed

+267
-336
lines changed

api/services/control/control.pb.go

Lines changed: 163 additions & 232 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/services/control/control.proto

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ message Descriptor {
230230
message BuildResultInfo {
231231
Descriptor ResultDeprecated = 1;
232232
repeated Descriptor Attestations = 2;
233-
map<string, Descriptor> Results = 3;
233+
map<int64, Descriptor> Results = 3;
234234
}
235235

236236
// Exporter describes the output exporter
@@ -239,6 +239,4 @@ message Exporter {
239239
string Type = 1;
240240
// Attrs specifies exporter configuration
241241
map<string, string> Attrs = 2;
242-
// ID identifies the exporter in the wire protocol
243-
string id = 3 [(gogoproto.customname) = "ID"];
244242
}

client/client_test.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,8 +2583,23 @@ func testMultipleExporters(t *testing.T, sb integration.Sandbox) {
25832583
imageExporter = "moby"
25842584
}
25852585

2586+
ref := identity.NewID()
25862587
resp, err := c.Solve(sb.Context(), def, SolveOpt{
2588+
Ref: ref,
25872589
Exports: []ExportEntry{
2590+
{
2591+
Type: imageExporter,
2592+
Attrs: map[string]string{
2593+
"name": target1,
2594+
},
2595+
},
2596+
{
2597+
Type: imageExporter,
2598+
Attrs: map[string]string{
2599+
"name": target2,
2600+
"oci-mediatypes": "true",
2601+
},
2602+
},
25882603
// Ensure that multiple local exporter destinations are written properly
25892604
{
25902605
Type: ExporterLocal,
@@ -2603,21 +2618,34 @@ func testMultipleExporters(t *testing.T, sb integration.Sandbox) {
26032618
Type: ExporterTar,
26042619
Output: fixedWriteCloser(outW2),
26052620
},
2606-
{
2607-
Type: imageExporter,
2608-
Attrs: map[string]string{
2609-
"name": strings.Join([]string{target1, target2}, ","),
2610-
},
2611-
},
26122621
},
26132622
}, nil)
26142623
require.NoError(t, err)
26152624

2616-
require.Equal(t, resp.ExporterResponse["image.name"], target1+","+target2)
2625+
require.Equal(t, resp.ExporterResponse["image.name"], target2)
26172626
require.FileExists(t, filepath.Join(destDir, "out.tar"))
26182627
require.FileExists(t, filepath.Join(destDir, "out2.tar"))
26192628
require.FileExists(t, filepath.Join(destDir, "foo.txt"))
26202629
require.FileExists(t, filepath.Join(destDir2, "foo.txt"))
2630+
2631+
history, err := c.ControlClient().ListenBuildHistory(sb.Context(), &controlapi.BuildHistoryRequest{
2632+
Ref: ref,
2633+
EarlyExit: true,
2634+
})
2635+
require.NoError(t, err)
2636+
for {
2637+
ev, err := history.Recv()
2638+
if err != nil {
2639+
require.Equal(t, io.EOF, err)
2640+
break
2641+
}
2642+
require.Equal(t, ref, ev.Record.Ref)
2643+
2644+
require.Len(t, ev.Record.Result.Results, 2)
2645+
require.Equal(t, images.MediaTypeDockerSchema2Manifest, ev.Record.Result.Results[0].MediaType)
2646+
require.Equal(t, ocispecs.MediaTypeImageManifest, ev.Record.Result.Results[1].MediaType)
2647+
require.Equal(t, ev.Record.Result.Results[0], ev.Record.Result.ResultDeprecated)
2648+
}
26212649
}
26222650

26232651
func testOCIExporter(t *testing.T, sb integration.Sandbox) {

client/solve.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/base64"
66
"encoding/json"
7-
"fmt"
87
"io"
98
"os"
109
"path/filepath"
@@ -126,25 +125,6 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
126125
return nil, err
127126
}
128127

129-
type exporter struct {
130-
ExportEntry
131-
id string
132-
}
133-
134-
var exporters []exporter
135-
ids := make(map[string]int)
136-
for _, exp := range opt.Exports {
137-
if id, ok := ids[exp.Type]; !ok {
138-
ids[exp.Type] = 1
139-
} else {
140-
ids[exp.Type] = id + 1
141-
}
142-
exporters = append(exporters, exporter{
143-
ExportEntry: exp,
144-
id: fmt.Sprint(exp.Type, ids[exp.Type]),
145-
})
146-
}
147-
148128
storesToUpdate := []string{}
149129

150130
if !opt.SessionPreInitialized {
@@ -169,7 +149,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
169149
}
170150

171151
var syncTargets []filesync.FSSyncTarget
172-
for _, ex := range exporters {
152+
for exID, ex := range opt.Exports {
173153
var supportFile bool
174154
var supportDir bool
175155
switch ex.Type {
@@ -194,7 +174,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
194174
if ex.Output == nil {
195175
return nil, errors.Errorf("output file writer is required for %s exporter", ex.Type)
196176
}
197-
syncTargets = append(syncTargets, filesync.WithFSSync(ex.id, ex.Output))
177+
syncTargets = append(syncTargets, filesync.WithFSSync(exID, ex.Output))
198178
}
199179
if supportDir {
200180
if ex.OutputDir == "" {
@@ -212,7 +192,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
212192
contentStores["export"] = cs
213193
storesToUpdate = append(storesToUpdate, ex.OutputDir)
214194
default:
215-
syncTargets = append(syncTargets, filesync.WithFSSyncDir(ex.id, ex.OutputDir))
195+
syncTargets = append(syncTargets, filesync.WithFSSyncDir(exID, ex.OutputDir))
216196
}
217197
}
218198
}
@@ -275,13 +255,12 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
275255
exports := make([]*controlapi.Exporter, 0, len(opt.Exports))
276256
exportDeprecated := ""
277257
exportAttrDeprecated := map[string]string{}
278-
for i, exp := range exporters {
258+
for i, exp := range opt.Exports {
279259
if i == 0 {
280260
exportDeprecated = exp.Type
281261
exportAttrDeprecated = exp.Attrs
282262
}
283263
exports = append(exports, &controlapi.Exporter{
284-
ID: exp.id,
285264
Type: exp.Type,
286265
Attrs: exp.Attrs,
287266
})

control/control.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,12 @@ func (c *Controller) Solve(ctx context.Context, req *controlapi.SolveRequest) (*
366366
}
367367

368368
var expis []exporter.ExporterInstance
369-
for _, ex := range req.Exporters {
369+
for i, ex := range req.Exporters {
370370
exp, err := w.Exporter(ex.Type, c.opt.SessionManager)
371371
if err != nil {
372372
return nil, err
373373
}
374-
expi, err := exp.Resolve(ctx, ex.ID, ex.Attrs)
374+
expi, err := exp.Resolve(ctx, i, ex.Attrs)
375375
if err != nil {
376376
return nil, err
377377
}

exporter/containerimage/export.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func New(opt Opt) (exporter.Exporter, error) {
6464
return im, nil
6565
}
6666

67-
func (e *imageExporter) Resolve(ctx context.Context, id string, opt map[string]string) (exporter.ExporterInstance, error) {
67+
func (e *imageExporter) Resolve(ctx context.Context, id int, opt map[string]string) (exporter.ExporterInstance, error) {
6868
i := &imageExporterInstance{
6969
imageExporter: e,
7070
id: id,
@@ -168,7 +168,7 @@ func (e *imageExporter) Resolve(ctx context.Context, id string, opt map[string]s
168168

169169
type imageExporterInstance struct {
170170
*imageExporter
171-
id string
171+
id int
172172

173173
opts ImageCommitOpts
174174
push bool
@@ -182,7 +182,7 @@ type imageExporterInstance struct {
182182
meta map[string][]byte
183183
}
184184

185-
func (e *imageExporterInstance) ID() string {
185+
func (e *imageExporterInstance) ID() int {
186186
return e.id
187187
}
188188

exporter/exporter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ type Source = result.Result[cache.ImmutableRef]
1515
type Attestation = result.Attestation[cache.ImmutableRef]
1616

1717
type Exporter interface {
18-
Resolve(context.Context, string, map[string]string) (ExporterInstance, error)
18+
Resolve(context.Context, int, map[string]string) (ExporterInstance, error)
1919
}
2020

2121
type ExporterInstance interface {
22-
ID() string
22+
ID() int
2323
Name() string
2424
Config() *Config
2525
Export(ctx context.Context, src *Source, inlineCache exptypes.InlineCache, sessionID string) (map[string]string, DescriptorReference, error)

exporter/local/export.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func New(opt Opt) (exporter.Exporter, error) {
3535
return le, nil
3636
}
3737

38-
func (e *localExporter) Resolve(ctx context.Context, id string, opt map[string]string) (exporter.ExporterInstance, error) {
38+
func (e *localExporter) Resolve(ctx context.Context, id int, opt map[string]string) (exporter.ExporterInstance, error) {
3939
i := &localExporterInstance{
4040
id: id,
4141
localExporter: e,
@@ -50,12 +50,12 @@ func (e *localExporter) Resolve(ctx context.Context, id string, opt map[string]s
5050

5151
type localExporterInstance struct {
5252
*localExporter
53-
id string
53+
id int
5454

5555
opts CreateFSOpts
5656
}
5757

58-
func (e *localExporterInstance) ID() string {
58+
func (e *localExporterInstance) ID() int {
5959
return e.id
6060
}
6161

exporter/oci/export.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func New(opt Opt) (exporter.Exporter, error) {
5858
return im, nil
5959
}
6060

61-
func (e *imageExporter) Resolve(ctx context.Context, id string, opt map[string]string) (exporter.ExporterInstance, error) {
61+
func (e *imageExporter) Resolve(ctx context.Context, id int, opt map[string]string) (exporter.ExporterInstance, error) {
6262
i := &imageExporterInstance{
6363
imageExporter: e,
6464
id: id,
@@ -100,14 +100,14 @@ func (e *imageExporter) Resolve(ctx context.Context, id string, opt map[string]s
100100

101101
type imageExporterInstance struct {
102102
*imageExporter
103-
id string
103+
id int
104104

105105
opts containerimage.ImageCommitOpts
106106
tar bool
107107
meta map[string][]byte
108108
}
109109

110-
func (e *imageExporterInstance) ID() string {
110+
func (e *imageExporterInstance) ID() int {
111111
return e.id
112112
}
113113

exporter/tar/export.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func New(opt Opt) (exporter.Exporter, error) {
3333
return le, nil
3434
}
3535

36-
func (e *localExporter) Resolve(ctx context.Context, id string, opt map[string]string) (exporter.ExporterInstance, error) {
36+
func (e *localExporter) Resolve(ctx context.Context, id int, opt map[string]string) (exporter.ExporterInstance, error) {
3737
li := &localExporterInstance{
3838
localExporter: e,
3939
id: id,
@@ -49,12 +49,12 @@ func (e *localExporter) Resolve(ctx context.Context, id string, opt map[string]s
4949

5050
type localExporterInstance struct {
5151
*localExporter
52-
id string
52+
id int
5353

5454
opts local.CreateFSOpts
5555
}
5656

57-
func (e *localExporterInstance) ID() string {
57+
func (e *localExporterInstance) ID() int {
5858
return e.id
5959
}
6060

0 commit comments

Comments
 (0)