This commit is contained in:
CrazyMax
2023-06-13 16:56:37 +02:00
committed by GitHub
12 changed files with 846 additions and 540 deletions

View File

@@ -11,11 +11,11 @@ import (
"github.com/docker/buildx/bake"
"github.com/docker/buildx/build"
"github.com/docker/buildx/builder"
cbuild "github.com/docker/buildx/controller/build"
controllerapi "github.com/docker/buildx/controller/pb"
"github.com/docker/buildx/util/buildflags"
"github.com/docker/buildx/util/cobrautil/completion"
"github.com/docker/buildx/util/confutil"
"github.com/docker/buildx/util/desktop"
"github.com/docker/buildx/util/dockerutil"
"github.com/docker/buildx/util/progress"
"github.com/docker/buildx/util/tracing"
"github.com/docker/cli/cli/command"
@@ -25,16 +25,15 @@ import (
)
type bakeOptions struct {
files []string
overrides []string
printOnly bool
sbom string
provenance string
builder string
files []string
overrides []string
printOnly bool
sbom string
provenance string
metadataFile string
exportPush bool
exportLoad bool
controllerapi.CommonOptions
//control.ControlOptions
}
func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags commonFlags) (err error) {
@@ -69,12 +68,12 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
}
overrides := in.overrides
if in.exportPush {
if in.exportLoad {
if in.ExportPush {
if in.ExportLoad {
return errors.Errorf("push and load may not be set together at the moment")
}
overrides = append(overrides, "*.push=true")
} else if in.exportLoad {
} else if in.ExportLoad {
overrides = append(overrides, "*.output=type=docker")
}
if cFlags.noCache != nil {
@@ -102,7 +101,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
// instance only needed for reading remote bake files or building
if url != "" || !in.printOnly {
b, err := builder.New(dockerCli,
builder.WithName(in.builder),
builder.WithName(in.Builder),
builder.WithContextPathHash(contextPathHash),
)
if err != nil {
@@ -176,11 +175,19 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
}
// this function can update target context string from the input so call before printOnly check
bo, err := bake.TargetsToBuildOpt(tgts, inp)
opts, err := bake.TargetsToControllerOptions(tgts, inp)
if err != nil {
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"`
@@ -201,7 +208,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
return nil
}
resp, err := build.Build(ctx, nodes, bo, dockerutil.NewClient(dockerCli), confutil.ConfigDir(dockerCli), printer)
resp, _, err := cbuild.RunBuilds(ctx, dockerCli, updatedOpts, os.Stdin, printer, false)
if err != nil {
return wrapBuildError(err, true)
}
@@ -235,7 +242,7 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
if !cmd.Flags().Lookup("pull").Changed {
cFlags.pull = nil
}
options.builder = rootOpts.builder
options.Builder = rootOpts.builder
options.metadataFile = cFlags.metadataFile
// Other common flags (noCache, pull and progress) are processed in runBake function.
return runBake(dockerCli, args, options, cFlags)
@@ -246,9 +253,9 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
flags := cmd.Flags()
flags.StringArrayVarP(&options.files, "file", "f", []string{}, "Build definition file")
flags.BoolVar(&options.exportLoad, "load", false, `Shorthand for "--set=*.output=type=docker"`)
flags.BoolVar(&options.ExportLoad, "load", false, `Shorthand for "--set=*.output=type=docker"`)
flags.BoolVar(&options.printOnly, "print", false, "Print the options without building")
flags.BoolVar(&options.exportPush, "push", false, `Shorthand for "--set=*.output=type=registry"`)
flags.BoolVar(&options.ExportPush, "push", false, `Shorthand for "--set=*.output=type=registry"`)
flags.StringVar(&options.sbom, "sbom", "", `Shorthand for "--set=*.attest=type=sbom"`)
flags.StringVar(&options.provenance, "provenance", "", `Shorthand for "--set=*.attest=type=provenance"`)
flags.StringArrayVar(&options.overrides, "set", nil, `Override target value (e.g., "targetpattern.key=value")`)

View File

@@ -63,6 +63,7 @@ type buildOptions struct {
dockerfileName string
extraHosts []string
imageIDFile string
metadataFile string
labels []string
networkMode string
noCacheFilter []string
@@ -75,49 +76,51 @@ type buildOptions struct {
tags []string
target string
ulimits *dockeropts.UlimitOpt
attests []string
sbom string
provenance string
progress string
quiet bool
invoke *invokeConfig
noBuild bool
attests []string
sbom string
provenance string
progress string
quiet bool
builder string
metadataFile string
noCache bool
pull bool
exportPush bool
exportLoad bool
controllerapi.CommonOptions
control.ControlOptions
}
func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error) {
var err error
inputs := controllerapi.Inputs{
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 {
return nil, err
}
opts := controllerapi.BuildOptions{
Allow: o.allow,
BuildArgs: listToMap(o.buildArgs, true),
CgroupParent: o.cgroupParent,
ContextPath: o.contextPath,
DockerfileName: o.dockerfileName,
ExtraHosts: o.extraHosts,
Labels: listToMap(o.labels, false),
NetworkMode: o.networkMode,
NoCacheFilter: o.noCacheFilter,
Platforms: o.platforms,
ShmSize: int64(o.shmSize),
Tags: o.tags,
Target: o.target,
Ulimits: dockerUlimitToControllerUlimit(o.ulimits),
Builder: o.builder,
NoCache: o.noCache,
Pull: o.pull,
ExportPush: o.exportPush,
ExportLoad: o.exportLoad,
Inputs: &inputs,
Opts: &o.CommonOptions,
Allow: o.allow,
BuildArgs: listToMap(o.buildArgs, true),
CgroupParent: o.cgroupParent,
ExtraHosts: o.extraHosts,
Labels: listToMap(o.labels, false),
NetworkMode: o.networkMode,
NoCacheFilter: o.noCacheFilter,
Platforms: o.platforms,
ShmSize: int64(o.shmSize),
Tags: o.tags,
Target: o.target,
Ulimits: dockerUlimitToControllerUlimit(o.ulimits),
}
// TODO: extract env var parsing to a method easily usable by library consumers
@@ -144,11 +147,6 @@ func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error
return nil, err
}
opts.NamedContexts, err = buildflags.ParseContextNames(o.contexts)
if err != nil {
return nil, err
}
opts.Exports, err = buildflags.ParseExports(o.outputs)
if err != nil {
return nil, err
@@ -223,13 +221,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.WithName(options.Builder),
builder.WithContextPathHash(opts.Inputs.ContextPathHash),
)
if err != nil {
return err
@@ -418,15 +412,15 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
options.contextPath = args[0]
options.builder = rootOpts.builder
options.Builder = rootOpts.builder
options.metadataFile = cFlags.metadataFile
options.noCache = false
options.NoCache = false
if cFlags.noCache != nil {
options.noCache = *cFlags.noCache
options.NoCache = *cFlags.noCache
}
options.pull = false
options.Pull = false
if cFlags.pull != nil {
options.pull = *cFlags.pull
options.Pull = *cFlags.pull
}
options.progress = cFlags.progress
cmd.Flags().VisitAll(checkWarnedFlags)
@@ -476,7 +470,7 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
flags.StringArrayVar(&options.labels, "label", []string{}, "Set metadata for an image")
flags.BoolVar(&options.exportLoad, "load", false, `Shorthand for "--output=type=docker"`)
flags.BoolVar(&options.ExportLoad, "load", false, `Shorthand for "--output=type=docker"`)
flags.StringVar(&options.networkMode, "network", "default", `Set the networking mode for the "RUN" instructions during build`)
@@ -490,7 +484,7 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
flags.StringVar(&options.printFunc, "print", "", "Print result of information request (e.g., outline, targets) [experimental]")
}
flags.BoolVar(&options.exportPush, "push", false, `Shorthand for "--output=type=registry"`)
flags.BoolVar(&options.ExportPush, "push", false, `Shorthand for "--output=type=registry"`)
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Suppress the build output and print image ID on success")