Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: docker/buildx
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8852925c77f4cf02e651e19588d990f37d0033e6
Choose a base ref
..
head repository: docker/buildx
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a481e15392acab89a468dc4c7cb07e5ff9dada15
Choose a head ref
Showing with 190 additions and 172 deletions.
  1. +3 −3 bake/bake.go
  2. +9 −1 commands/bake.go
  3. +8 −8 commands/build.go
  4. +23 −23 controller/build/build.go
  5. +1 −1 controller/local/controller.go
  6. +140 −131 controller/pb/controller.pb.go
  7. +5 −4 controller/pb/controller.proto
  8. +1 −1 controller/remote/controller.go
6 changes: 3 additions & 3 deletions bake/bake.go
Original file line number Diff line number Diff line change
@@ -907,14 +907,14 @@ func (t *Target) GetName(ectx *hcl.EvalContext, block *hcl.Block, loadDeps func(
return value.AsString(), nil
}

func TargetsToControllerOptions(m map[string]*Target, inp *Input) (map[string]*controllerapi.BuildOptions, error) {
m2 := make(map[string]*controllerapi.BuildOptions, len(m))
func TargetsToControllerOptions(m map[string]*Target, inp *Input) (map[string]controllerapi.BuildOptions, error) {
m2 := make(map[string]controllerapi.BuildOptions, len(m))
for k, v := range m {
opts, err := toControllerOpt(v, inp)
if err != nil {
return nil, err
}
m2[k] = opts
m2[k] = *opts
}
return m2, nil
}
10 changes: 9 additions & 1 deletion commands/bake.go
Original file line number Diff line number Diff line change
@@ -170,6 +170,14 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
return err
}

// set builder name and context hash for all targets
updatedOpts := make(map[string]controllerapi.BuildOptions, len(opts))
for i, opt := range opts {
opt.Opts.Builder = in.Builder
opt.Inputs.ContextPathHash = contextPathHash
updatedOpts[i] = opt
}

if in.printOnly {
dt, err := json.MarshalIndent(struct {
Group map[string]*bake.Group `json:"group,omitempty"`
@@ -190,7 +198,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
return nil
}

resp, _, err := cbuild.RunBuilds(ctx, dockerCli, in.Builder, opts, os.Stdin, printer, false)
resp, _, err := cbuild.RunBuilds(ctx, dockerCli, updatedOpts, os.Stdin, printer, false)
if err != nil {
return wrapBuildError(err, true)
}
16 changes: 8 additions & 8 deletions commands/build.go
Original file line number Diff line number Diff line change
@@ -92,8 +92,12 @@ func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error
var err error

inputs := controllerapi.Inputs{
ContextPath: o.contextPath,
DockerfileName: o.dockerfileName,
ContextPath: o.contextPath,
ContextPathHash: o.contextPath,
DockerfileName: o.dockerfileName,
}
if absContextPath, err := filepath.Abs(inputs.ContextPathHash); err == nil {
inputs.ContextPathHash = absContextPath
}
inputs.NamedContexts, err = buildflags.ParseContextNames(o.contexts)
if err != nil {
@@ -216,13 +220,9 @@ func runBuild(dockerCli command.Cli, options buildOptions) (err error) {
}
}

contextPathHash := options.contextPath
if absContextPath, err := filepath.Abs(contextPathHash); err == nil {
contextPathHash = absContextPath
}
b, err := builder.New(dockerCli,
builder.WithName(options.Builder),
builder.WithContextPathHash(contextPathHash),
builder.WithContextPathHash(opts.Inputs.ContextPathHash),
)
if err != nil {
return err
@@ -298,7 +298,7 @@ func getImageID(resp map[string]string) string {
}

func runBasicBuild(ctx context.Context, dockerCli command.Cli, opts *controllerapi.BuildOptions, options buildOptions, printer *progress.Printer) (*client.SolveResponse, error) {
resp, _, err := cbuild.RunBuild(ctx, dockerCli, options.Builder, opts, os.Stdin, printer, false)
resp, _, err := cbuild.RunBuild(ctx, dockerCli, *opts, os.Stdin, printer, false)
return resp, err
}

46 changes: 23 additions & 23 deletions controller/build/build.go
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ import (
"context"
"io"
"os"
"path/filepath"
"strings"
"sync"

@@ -31,42 +30,54 @@ import (
"google.golang.org/grpc/codes"
)

const DefaultTargetName = "default"
const defaultTargetName = "default"

// RunBuild runs the specified build and returns the result.
//
// NOTE: When an error happens during the build and this function acquires the debuggable *build.ResultContext,
// this function returns it in addition to the error (i.e. it does "return nil, res, err"). The caller can
// inspect the result and debug the cause of that error.
func RunBuild(ctx context.Context, dockerCli command.Cli, builderName string, in *controllerapi.BuildOptions, inStream io.Reader, progress progress.Writer, generateResult bool) (*client.SolveResponse, *build.ResultContext, error) {
cResp, cRes, err := RunBuilds(ctx, dockerCli, builderName, map[string]*controllerapi.BuildOptions{DefaultTargetName: in}, inStream, progress, generateResult)
func RunBuild(ctx context.Context, dockerCli command.Cli, in controllerapi.BuildOptions, inStream io.Reader, progress progress.Writer, generateResult bool) (*client.SolveResponse, *build.ResultContext, error) {
cResp, cRes, err := RunBuilds(ctx, dockerCli, map[string]controllerapi.BuildOptions{defaultTargetName: in}, inStream, progress, generateResult)
var resp *client.SolveResponse
if v, ok := cResp[DefaultTargetName]; ok {
if v, ok := cResp[defaultTargetName]; ok {
resp = v
}
var res *build.ResultContext
if v, ok := cRes[DefaultTargetName]; ok {
if v, ok := cRes[defaultTargetName]; ok {
res = v
}
return resp, res, err
}

func RunBuilds(ctx context.Context, dockerCli command.Cli, builderName string, in map[string]*controllerapi.BuildOptions, inStream io.Reader, progress progress.Writer, generateResult bool) (map[string]*client.SolveResponse, map[string]*build.ResultContext, error) {
// RunBuilds same as RunBuild but runs multiple builds.
func RunBuilds(ctx context.Context, dockerCli command.Cli, in map[string]controllerapi.BuildOptions, inStream io.Reader, progress progress.Writer, generateResult bool) (map[string]*client.SolveResponse, map[string]*build.ResultContext, error) {
var err error
var builderName string
var contextPathHash string

opts := make(map[string]build.Options, len(in))
optsMu := sync.Mutex{}
mu := sync.Mutex{}
eg, _ := errgroup.WithContext(ctx)
for t, o := range in {
func(t string, o *controllerapi.BuildOptions) {
func(t string, o controllerapi.BuildOptions) {
eg.Go(func() error {
opt, err := ToBuildOpts(o, inStream)
if err != nil {
return err
}
optsMu.Lock()
mu.Lock()
opts[t] = *opt
optsMu.Unlock()
// we assume that all the targets are using the same builder and
// context path hash. This assumption is currently valid but, we
// may need to revisit this in the future.
if builderName == "" {
builderName = o.Opts.Builder
}
if contextPathHash == "" {
contextPathHash = o.Inputs.ContextPathHash
}
mu.Unlock()
return nil
})
}(t, o)
@@ -75,17 +86,6 @@ func RunBuilds(ctx context.Context, dockerCli command.Cli, builderName string, i
return nil, nil, err
}

// key string used for kubernetes "sticky" mode
contextPathHash, _ := os.Getwd()
if len(in) == 1 {
for _, o := range in {
contextPathHash, err = filepath.Abs(o.Inputs.ContextPath)
if err != nil {
contextPathHash = o.Inputs.ContextPath
}
}
}

b, err := builder.New(dockerCli,
builder.WithName(builderName),
builder.WithContextPathHash(contextPathHash),
@@ -109,7 +109,7 @@ func RunBuilds(ctx context.Context, dockerCli command.Cli, builderName string, i
return resp, res, nil
}

func ToBuildOpts(in *controllerapi.BuildOptions, inStream io.Reader) (*build.Options, error) {
func ToBuildOpts(in controllerapi.BuildOptions, inStream io.Reader) (*build.Options, error) {
if in.Opts.NoCache && len(in.NoCacheFilter) > 0 {
return nil, errors.Errorf("--no-cache and --no-cache-filter cannot currently be used together")
}
2 changes: 1 addition & 1 deletion controller/local/controller.go
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ func (b *localController) Build(ctx context.Context, options controllerapi.Build
}
defer b.buildOnGoing.Store(false)

resp, res, buildErr := cbuild.RunBuild(ctx, b.dockerCli, options.Opts.Builder, &options, in, progress, true)
resp, res, buildErr := cbuild.RunBuild(ctx, b.dockerCli, options, in, progress, true)
// NOTE: RunBuild can return *build.ResultContext even on error.
if res != nil {
b.buildConfig = buildConfig{
Loading