Skip to content

Commit

Permalink
Only reset the cache exporter configuration attribute if the inline
Browse files Browse the repository at this point in the history
cache was configured.

Signed-off-by: a-palchikov <[email protected]>
  • Loading branch information
a-palchikov committed Oct 6, 2022
1 parent 5585377 commit a4e8174
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 59 deletions.
58 changes: 24 additions & 34 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ func TestIntegration(t *testing.T) {
testTarExporterSymlink,
testMultipleRegistryCacheImportExport,
testMultipleExporters,
testPreventsMultipleExportWithSameExporter,
testSourceMap,
testSourceMapFromRef,
testLazyImagePush,
Expand Down Expand Up @@ -2185,32 +2184,6 @@ func testUser(t *testing.T, sb integration.Sandbox) {
checkAllReleasable(t, c, sb, true)
}

func testPreventsMultipleExportWithSameExporter(t *testing.T, sb integration.Sandbox) {
requiresLinux(t)

c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

def, err := llb.Scratch().File(llb.Mkfile("foo.txt", 0o755, nil)).Marshal(sb.Context())
require.NoError(t, err)

dummyOutput := func(map[string]string) (io.WriteCloser, error) { return nil, fmt.Errorf("not implemented") }
_, err = c.Solve(sb.Context(), def, SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterOCI,
Output: dummyOutput,
},
{
Type: ExporterOCI,
Output: dummyOutput,
},
},
}, nil)
require.Errorf(t, err, "using multiple ExporterOCI is not supported")
}

func testMultipleExporters(t *testing.T, sb integration.Sandbox) {
requiresLinux(t)

Expand All @@ -2221,12 +2194,17 @@ func testMultipleExporters(t *testing.T, sb integration.Sandbox) {
def, err := llb.Scratch().File(llb.Mkfile("foo.txt", 0o755, nil)).Marshal(context.TODO())
require.NoError(t, err)

destDir := t.TempDir()
destDir, destDir2 := t.TempDir(), t.TempDir()
out := filepath.Join(destDir, "out.tar")
outW, err := os.Create(out)
require.NoError(t, err)
defer outW.Close()

out2 := filepath.Join(destDir, "out2.tar")
outW2, err := os.Create(out2)
require.NoError(t, err)
defer outW2.Close()

registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) {
t.Skip(err.Error())
Expand All @@ -2243,15 +2221,25 @@ func testMultipleExporters(t *testing.T, sb integration.Sandbox) {

resp, err := c.Solve(sb.Context(), def, SolveOpt{
Exports: []ExportEntry{
// Ensure that multiple local exporter destinations are written properly
{
Type: ExporterLocal,
OutputDir: destDir,
},
// TODO(dima): this won't work just yet
//{
// Type: ExporterTar,
// Output: fixedWriteCloser(outW),
//},
{
Type: ExporterLocal,
OutputDir: destDir2,
},
// Ensure that multiple instances of the same exporter are possible
{
Type: ExporterTar,
Output: fixedWriteCloser(outW),
},
{
Type: ExporterTar,
Output: fixedWriteCloser(outW2),
},

{
Type: imageExporter,
Attrs: map[string]string{
Expand All @@ -2262,8 +2250,10 @@ func testMultipleExporters(t *testing.T, sb integration.Sandbox) {
}, nil)
require.NoError(t, err)
require.Equal(t, resp.ExporterResponse["image.name"], target1+","+target2)
// require.FileExists(t, filepath.Join(destDir, "out.tar"))
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"))
}

func testOCIExporter(t *testing.T, sb integration.Sandbox) {
Expand Down
15 changes: 7 additions & 8 deletions client/solve.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *Client) Solve(ctx context.Context, def *llb.Definition, opt SolveOpt, s

type runGatewayCB func(ref string, s *session.Session, opts map[string]string) error

func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runGatewayCB, opt SolveOpt, statusChan chan *SolveStatus) (result *SolveResponse, err error) {
func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runGatewayCB, opt SolveOpt, statusChan chan *SolveStatus) (*SolveResponse, error) {
if def != nil && runGateway != nil {
return nil, errors.New("invalid with def and cb")
}
Expand Down Expand Up @@ -211,7 +211,6 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
s.Allow(filesync.NewFSSyncTarget(exporterConfig.outputs))
}


eg.Go(func() error {
sd := c.sessionDialer
if sd == nil {
Expand All @@ -229,6 +228,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
}

solveCtx, cancelSolve := context.WithCancel(ctx)
var res *SolveResponse
eg.Go(func() error {
ctx := solveCtx
defer cancelSolve()
Expand Down Expand Up @@ -272,7 +272,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
}
if localExporter {
// Add a single instance of the local exporter
// since it's replicated entirely in the client
// since it's replicated entirely on the client
exports = append(exports, &controlapi.Exporter{
Name: ExporterLocal,
})
Expand All @@ -292,8 +292,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
if err != nil {
return errors.Wrap(err, "failed to solve")
}

result = &SolveResponse{
res = &SolveResponse{
ExporterResponse: resp.ExporterResponse,
}
return nil
Expand Down Expand Up @@ -391,7 +390,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
}
// Update index.json of exported cache content store
// FIXME(AkihiroSuda): dedupe const definition of cache/remotecache.ExporterResponseManifestDesc = "cache.manifest"
if manifestDescJSON := result.ExporterResponse["cache.manifest"]; manifestDescJSON != "" {
if manifestDescJSON := res.ExporterResponse["cache.manifest"]; manifestDescJSON != "" {
var manifestDesc ocispecs.Descriptor
if err = json.Unmarshal([]byte(manifestDescJSON), &manifestDesc); err != nil {
return nil, err
Expand All @@ -402,7 +401,7 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG
}
}
}
return result, nil
return res, nil
}

func prepareSyncedDirs(def *llb.Definition, localDirs map[string]string) ([]filesync.SyncedDir, error) {
Expand Down Expand Up @@ -476,7 +475,7 @@ func parseCacheOptions(ctx context.Context, isGateway bool, opt SolveOpt) (*cach
if csDir == "" {
return nil, errors.New("local cache exporter requires dest")
}
if err := os.MkdirAll(csDir, 0o755); err != nil {
if err := os.MkdirAll(csDir, 0755); err != nil {
return nil, err
}
cs, err := contentlocal.NewStore(csDir)
Expand Down
4 changes: 2 additions & 2 deletions cmd/buildctl/build/exportcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func parseExportCacheCSV(s string) (client.CacheOptionsEntry, error) {
for _, field := range fields {
parts := strings.SplitN(field, "=", 2)
if len(parts) != 2 {
return ex, errors.Errorf("invalid value %q", field)
return ex, errors.Errorf("invalid value %s", field)
}
key := strings.ToLower(parts[0])
value := parts[1]
Expand Down Expand Up @@ -63,7 +63,7 @@ func ParseExportCache(exportCaches []string) ([]client.CacheOptionsEntry, error)
} else {
ex, err := parseExportCacheCSV(exportCache)
if err != nil {
return nil, errors.Wrap(err, "parsing export-cache csv")
return nil, err
}
exports = append(exports, ex)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/buildctl/build/importcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func parseImportCacheCSV(s string) (client.CacheOptionsEntry, error) {
for _, field := range fields {
parts := strings.SplitN(field, "=", 2)
if len(parts) != 2 {
return im, errors.Errorf("invalid value %q", field)
return im, errors.Errorf("invalid value %s", field)
}
key := strings.ToLower(parts[0])
value := parts[1]
Expand Down Expand Up @@ -57,7 +57,7 @@ func ParseImportCache(importCaches []string) ([]client.CacheOptionsEntry, error)
} else {
im, err := parseImportCacheCSV(importCache)
if err != nil {
return nil, errors.Wrap(err, "parsing import-cache csv")
return nil, err
}
imports = append(imports, im)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/buildctl/build/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func parseOutputCSV(s string) (client.ExportEntry, error) {
// ParseOutput parses --output
func ParseOutput(exports []string) ([]client.ExportEntry, error) {
var entries []client.ExportEntry
for _, exp := range exports {
opts, err := parseOutputCSV(exp)
for _, s := range exports {
e, err := parseOutputCSV(s)
if err != nil {
return nil, err
}
entries = append(entries, opts)
entries = append(entries, e)
}
return entries, nil
}
Expand Down
1 change: 0 additions & 1 deletion control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ func (c *Controller) Solve(ctx context.Context, req *controlapi.SolveRequest) (*
if err != nil {
return nil, err
}

return &controlapi.SolveResponse{
ExporterResponse: resp.ExporterResponse,
}, nil
Expand Down
6 changes: 2 additions & 4 deletions exporter/oci/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ func (e *imageExporterInstance) ExportImage(ctx context.Context, src exporter.So
return nil, err
}
defer func() {
// TODO(dima): is it safe to remove the descriptor while branching
// off into streamer?
e.opt.ImageWriter.ContentStore().Delete(context.TODO(), desc.Digest)
}()

Expand Down Expand Up @@ -235,7 +233,7 @@ func (e *imageExporterInstance) ExportImage(ctx context.Context, src exporter.So
if err := archiveexporter.Export(ctx, mprovider, w, expOpts...); err != nil {
w.Close()
if grpcerrors.Code(err) == codes.AlreadyExists {
return nil, report(nil)
return resp, report(nil)
}
return nil, report(err)
}
Expand All @@ -251,7 +249,7 @@ func normalizedNames(name string) ([]string, error) {
return nil, nil
}
names := strings.Split(name, ",")
tagNames := make([]string, len(names))
var tagNames = make([]string, len(names))
for i, name := range names {
parsed, err := reference.ParseNormalizedNamed(name)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion session/filesync/diffcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ func writeTargetFile(ds grpc.ServerStream, fs map[string]FileOutputFunc, opts me
return errors.WithStack(err)
}
if wc == nil {
// TODO(dima): factor this out
md := map[string]string{}
for k, v := range opts {
if strings.HasPrefix(k, keyExporterMetaPrefix) {
Expand Down
9 changes: 5 additions & 4 deletions solver/llbsolver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (s *Solver) Solve(ctx context.Context, id string, sessionID string, req fro
inp, err := result.ConvertResult(cached, func(res solver.CachedResult) (cache.ImmutableRef, error) {
workerRef, ok := res.Sys().(*worker.WorkerRef)
if !ok {
return nil, errors.Errorf("invalid reference: %T", res.Sys())
return nil, errors.Errorf("invalid reference: %T", r.Sys())
}
return workerRef.ImmutableRef, nil
})
Expand All @@ -213,7 +213,9 @@ func (s *Solver) Solve(ctx context.Context, id string, sessionID string, req fro
}

cacheExporter := exp.CacheExporter
exp.CacheExporter = nil
if _, ok := asInlineCache(cacheExporter); ok {
exp.CacheExporter = nil
}
exporterResponse, err = s.runExporters(ctx, exp.Exporters, cacheExporter, cached, inp, j)
if err != nil {
return nil, err
Expand Down Expand Up @@ -344,7 +346,7 @@ func (s *Solver) runExporters(ctx context.Context, exporters []exporter.Exporter

func exportInlineCache(ctx context.Context, compression compression.Config, e remotecache.Exporter, cached *result.Result[solver.CachedResult], job *solver.Job, sessionID string) (*exptypes.InlineCache, error) {
result := &exptypes.InlineCache{}
if err := inBuilderContext(ctx, job, "preparing layers for inline cache", j.SessionID+"-cache-inline", func(ctx context.Context, _ session.Group) error {
if err := inBuilderContext(ctx, job, "preparing layers for inline cache", job.SessionID+"-cache-inline", func(ctx context.Context, _ session.Group) error {
if cached.Ref != nil {
dtic, err := inlineCache(ctx, e, cached.Ref, compression, session.NewGroup(sessionID))
if err != nil {
Expand Down Expand Up @@ -451,7 +453,6 @@ func defaultResolver(wc *worker.Controller) ResolveWorkerFunc {
return wc.GetDefault()
}
}

func allWorkers(wc *worker.Controller) func(func(w worker.Worker) error) error {
return func(f func(worker.Worker) error) error {
all, err := wc.List()
Expand Down

0 comments on commit a4e8174

Please sign in to comment.