Skip to content

Commit 044b717

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 d4b80ce commit 044b717

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
@@ -233,7 +233,7 @@ message Descriptor {
233233
message BuildResultInfo {
234234
Descriptor ResultDeprecated = 1;
235235
repeated Descriptor Attestations = 2;
236-
map<string, Descriptor> Results = 3;
236+
map<int64, Descriptor> Results = 3;
237237
}
238238

239239
// Exporter describes the output exporter
@@ -242,6 +242,4 @@ message Exporter {
242242
string Type = 1;
243243
// Attrs specifies exporter configuration
244244
map<string, string> Attrs = 2;
245-
// ID identifies the exporter in the wire protocol
246-
string id = 3 [(gogoproto.customname) = "ID"];
247245
}

client/client_test.go

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

2589+
ref := identity.NewID()
25892590
resp, err := c.Solve(sb.Context(), def, SolveOpt{
2591+
Ref: ref,
25902592
Exports: []ExportEntry{
2593+
{
2594+
Type: imageExporter,
2595+
Attrs: map[string]string{
2596+
"name": target1,
2597+
},
2598+
},
2599+
{
2600+
Type: imageExporter,
2601+
Attrs: map[string]string{
2602+
"name": target2,
2603+
"oci-mediatypes": "true",
2604+
},
2605+
},
25912606
// Ensure that multiple local exporter destinations are written properly
25922607
{
25932608
Type: ExporterLocal,
@@ -2606,21 +2621,34 @@ func testMultipleExporters(t *testing.T, sb integration.Sandbox) {
26062621
Type: ExporterTar,
26072622
Output: fixedWriteCloser(outW2),
26082623
},
2609-
{
2610-
Type: imageExporter,
2611-
Attrs: map[string]string{
2612-
"name": strings.Join([]string{target1, target2}, ","),
2613-
},
2614-
},
26152624
},
26162625
}, nil)
26172626
require.NoError(t, err)
26182627

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

26262654
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"
@@ -131,25 +130,6 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
131130
return nil, err
132131
}
133132

134-
type exporter struct {
135-
ExportEntry
136-
id string
137-
}
138-
139-
var exporters []exporter
140-
ids := make(map[string]int)
141-
for _, exp := range opt.Exports {
142-
if id, ok := ids[exp.Type]; !ok {
143-
ids[exp.Type] = 1
144-
} else {
145-
ids[exp.Type] = id + 1
146-
}
147-
exporters = append(exporters, exporter{
148-
ExportEntry: exp,
149-
id: fmt.Sprint(exp.Type, ids[exp.Type]),
150-
})
151-
}
152-
153133
storesToUpdate := []string{}
154134

155135
if !opt.SessionPreInitialized {
@@ -174,7 +154,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
174154
}
175155

176156
var syncTargets []filesync.FSSyncTarget
177-
for _, ex := range exporters {
157+
for exID, ex := range opt.Exports {
178158
var supportFile bool
179159
var supportDir bool
180160
switch ex.Type {
@@ -199,7 +179,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
199179
if ex.Output == nil {
200180
return nil, errors.Errorf("output file writer is required for %s exporter", ex.Type)
201181
}
202-
syncTargets = append(syncTargets, filesync.WithFSSync(ex.id, ex.Output))
182+
syncTargets = append(syncTargets, filesync.WithFSSync(exID, ex.Output))
203183
}
204184
if supportDir {
205185
if ex.OutputDir == "" {
@@ -217,7 +197,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
217197
contentStores["export"] = cs
218198
storesToUpdate = append(storesToUpdate, ex.OutputDir)
219199
default:
220-
syncTargets = append(syncTargets, filesync.WithFSSyncDir(ex.id, ex.OutputDir))
200+
syncTargets = append(syncTargets, filesync.WithFSSyncDir(exID, ex.OutputDir))
221201
}
222202
}
223203
}
@@ -280,13 +260,12 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
280260
exports := make([]*controlapi.Exporter, 0, len(opt.Exports))
281261
exportDeprecated := ""
282262
exportAttrDeprecated := map[string]string{}
283-
for i, exp := range exporters {
263+
for i, exp := range opt.Exports {
284264
if i == 0 {
285265
exportDeprecated = exp.Type
286266
exportAttrDeprecated = exp.Attrs
287267
}
288268
exports = append(exports, &controlapi.Exporter{
289-
ID: exp.id,
290269
Type: exp.Type,
291270
Attrs: exp.Attrs,
292271
})

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)