Skip to content

Commit

Permalink
exporter: use implicit ids for exporters
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jedevc committed Sep 18, 2023
1 parent ccdc0b3 commit 606823e
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 336 deletions.
395 changes: 163 additions & 232 deletions api/services/control/control.pb.go

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions api/services/control/control.proto
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ message Descriptor {
message BuildResultInfo {
Descriptor ResultDeprecated = 1;
repeated Descriptor Attestations = 2;
map<string, Descriptor> Results = 3;
map<int64, Descriptor> Results = 3;
}

// Exporter describes the output exporter
Expand All @@ -239,6 +239,4 @@ message Exporter {
string Type = 1;
// Attrs specifies exporter configuration
map<string, string> Attrs = 2;
// ID identifies the exporter in the wire protocol
string id = 3 [(gogoproto.customname) = "ID"];
}
32 changes: 30 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2583,7 +2583,9 @@ func testMultipleExporters(t *testing.T, sb integration.Sandbox) {
imageExporter = "moby"
}

ref := identity.NewID()
resp, err := c.Solve(sb.Context(), def, SolveOpt{
Ref: ref,
Exports: []ExportEntry{
// Ensure that multiple local exporter destinations are written properly
{
Expand All @@ -2606,18 +2608,44 @@ func testMultipleExporters(t *testing.T, sb integration.Sandbox) {
{
Type: imageExporter,
Attrs: map[string]string{
"name": strings.Join([]string{target1, target2}, ","),
"name": target1,
},
},
{
Type: imageExporter,
Attrs: map[string]string{
"name": target2,
"oci-mediatypes": "true",
},
},
},
}, nil)
require.NoError(t, err)

require.Equal(t, resp.ExporterResponse["image.name"], target1+","+target2)
require.Equal(t, resp.ExporterResponse["image.name"], target2)
require.FileExists(t, filepath.Join(destDir, "out.tar"))
require.FileExists(t, filepath.Join(destDir, "out2.tar"))
require.FileExists(t, filepath.Join(destDir, "foo.txt"))
require.FileExists(t, filepath.Join(destDir2, "foo.txt"))

history, err := c.ControlClient().ListenBuildHistory(sb.Context(), &controlapi.BuildHistoryRequest{
Ref: ref,
EarlyExit: true,
})
require.NoError(t, err)
for {
ev, err := history.Recv()
if err != nil {
require.Equal(t, io.EOF, err)
break
}
require.Equal(t, ref, ev.Record.Ref)

require.Len(t, ev.Record.Result.Results, 2)
require.Equal(t, images.MediaTypeDockerSchema2Manifest, ev.Record.Result.Results[4].MediaType)
require.Equal(t, ocispecs.MediaTypeImageManifest, ev.Record.Result.Results[5].MediaType)
require.Equal(t, ev.Record.Result.Results[4], ev.Record.Result.ResultDeprecated)
}
}

func testOCIExporter(t *testing.T, sb integration.Sandbox) {
Expand Down
29 changes: 4 additions & 25 deletions client/solve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -126,25 +125,6 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
return nil, err
}

type exporter struct {
ExportEntry
id string
}

var exporters []exporter
ids := make(map[string]int)
for _, exp := range opt.Exports {
if id, ok := ids[exp.Type]; !ok {
ids[exp.Type] = 1
} else {
ids[exp.Type] = id + 1
}
exporters = append(exporters, exporter{
ExportEntry: exp,
id: fmt.Sprint(exp.Type, ids[exp.Type]),
})
}

storesToUpdate := []string{}

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

var syncTargets []filesync.FSSyncTarget
for _, ex := range exporters {
for _, ex := range opt.Exports {
var supportFile bool
var supportDir bool
switch ex.Type {
Expand All @@ -194,7 +174,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
if ex.Output == nil {
return nil, errors.Errorf("output file writer is required for %s exporter", ex.Type)
}
syncTargets = append(syncTargets, filesync.WithFSSync(ex.id, ex.Output))
syncTargets = append(syncTargets, filesync.WithFSSync(ex.Output))
}
if supportDir {
if ex.OutputDir == "" {
Expand All @@ -212,7 +192,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
contentStores["export"] = cs
storesToUpdate = append(storesToUpdate, ex.OutputDir)
default:
syncTargets = append(syncTargets, filesync.WithFSSyncDir(ex.id, ex.OutputDir))
syncTargets = append(syncTargets, filesync.WithFSSyncDir(ex.OutputDir))
}
}
}
Expand Down Expand Up @@ -275,13 +255,12 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
exports := make([]*controlapi.Exporter, 0, len(opt.Exports))
exportDeprecated := ""
exportAttrDeprecated := map[string]string{}
for i, exp := range exporters {
for i, exp := range opt.Exports {
if i == 0 {
exportDeprecated = exp.Type
exportAttrDeprecated = exp.Attrs
}
exports = append(exports, &controlapi.Exporter{
ID: exp.id,
Type: exp.Type,
Attrs: exp.Attrs,
})
Expand Down
4 changes: 2 additions & 2 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,12 @@ func (c *Controller) Solve(ctx context.Context, req *controlapi.SolveRequest) (*
}

var expis []exporter.ExporterInstance
for _, ex := range req.Exporters {
for i, ex := range req.Exporters {
exp, err := w.Exporter(ex.Type, c.opt.SessionManager)
if err != nil {
return nil, err
}
expi, err := exp.Resolve(ctx, ex.ID, ex.Attrs)
expi, err := exp.Resolve(ctx, i, ex.Attrs)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func New(opt Opt) (exporter.Exporter, error) {
return im, nil
}

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

type imageExporterInstance struct {
*imageExporter
id string
id int

opts ImageCommitOpts
push bool
Expand All @@ -182,7 +182,7 @@ type imageExporterInstance struct {
meta map[string][]byte
}

func (e *imageExporterInstance) ID() string {
func (e *imageExporterInstance) ID() int {
return e.id
}

Expand Down
4 changes: 2 additions & 2 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type Source = result.Result[cache.ImmutableRef]
type Attestation = result.Attestation[cache.ImmutableRef]

type Exporter interface {
Resolve(context.Context, string, map[string]string) (ExporterInstance, error)
Resolve(context.Context, int, map[string]string) (ExporterInstance, error)
}

type ExporterInstance interface {
ID() string
ID() int
Name() string
Config() *Config
Export(ctx context.Context, src *Source, inlineCache exptypes.InlineCache, sessionID string) (map[string]string, DescriptorReference, error)
Expand Down
6 changes: 3 additions & 3 deletions exporter/local/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func New(opt Opt) (exporter.Exporter, error) {
return le, nil
}

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

type localExporterInstance struct {
*localExporter
id string
id int

opts CreateFSOpts
}

func (e *localExporterInstance) ID() string {
func (e *localExporterInstance) ID() int {
return e.id
}

Expand Down
6 changes: 3 additions & 3 deletions exporter/oci/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func New(opt Opt) (exporter.Exporter, error) {
return im, nil
}

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

type imageExporterInstance struct {
*imageExporter
id string
id int

opts containerimage.ImageCommitOpts
tar bool
meta map[string][]byte
}

func (e *imageExporterInstance) ID() string {
func (e *imageExporterInstance) ID() int {
return e.id
}

Expand Down
6 changes: 3 additions & 3 deletions exporter/tar/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func New(opt Opt) (exporter.Exporter, error) {
return le, nil
}

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

type localExporterInstance struct {
*localExporter
id string
id int

opts local.CreateFSOpts
}

func (e *localExporterInstance) ID() string {
func (e *localExporterInstance) ID() int {
return e.id
}

Expand Down
Loading

0 comments on commit 606823e

Please sign in to comment.