Skip to content

Commit 91fdb04

Browse files
authored
Merge pull request #1713 from ktock/monitor-buildapi-options
controller: Extract nested CommonOptions on controller API
2 parents 16e41ba + 8ba8659 commit 91fdb04

File tree

6 files changed

+231
-266
lines changed

6 files changed

+231
-266
lines changed

commands/bake.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/docker/buildx/bake"
1111
"github.com/docker/buildx/build"
1212
"github.com/docker/buildx/builder"
13-
controllerapi "github.com/docker/buildx/controller/pb"
1413
"github.com/docker/buildx/util/buildflags"
1514
"github.com/docker/buildx/util/confutil"
1615
"github.com/docker/buildx/util/dockerutil"
@@ -29,7 +28,11 @@ type bakeOptions struct {
2928
printOnly bool
3029
sbom string
3130
provenance string
32-
controllerapi.CommonOptions
31+
32+
builder string
33+
metadataFile string
34+
exportPush bool
35+
exportLoad bool
3336
}
3437

3538
func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags commonFlags) (err error) {
@@ -64,12 +67,12 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
6467
}
6568

6669
overrides := in.overrides
67-
if in.ExportPush {
68-
if in.ExportLoad {
70+
if in.exportPush {
71+
if in.exportLoad {
6972
return errors.Errorf("push and load may not be set together at the moment")
7073
}
7174
overrides = append(overrides, "*.push=true")
72-
} else if in.ExportLoad {
75+
} else if in.exportLoad {
7376
overrides = append(overrides, "*.output=type=docker")
7477
}
7578
if cFlags.noCache != nil {
@@ -97,7 +100,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
97100
// instance only needed for reading remote bake files or building
98101
if url != "" || !in.printOnly {
99102
b, err := builder.New(dockerCli,
100-
builder.WithName(in.Builder),
103+
builder.WithName(in.builder),
101104
builder.WithContextPathHash(contextPathHash),
102105
)
103106
if err != nil {
@@ -193,12 +196,12 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
193196
return wrapBuildError(err, true)
194197
}
195198

196-
if len(in.MetadataFile) > 0 {
199+
if len(in.metadataFile) > 0 {
197200
dt := make(map[string]interface{})
198201
for t, r := range resp {
199202
dt[t] = decodeExporterResponse(r.ExporterResponse)
200203
}
201-
if err := writeMetadataFile(in.MetadataFile, dt); err != nil {
204+
if err := writeMetadataFile(in.metadataFile, dt); err != nil {
202205
return err
203206
}
204207
}
@@ -222,8 +225,8 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
222225
if !cmd.Flags().Lookup("pull").Changed {
223226
cFlags.pull = nil
224227
}
225-
options.Builder = rootOpts.builder
226-
options.MetadataFile = cFlags.metadataFile
228+
options.builder = rootOpts.builder
229+
options.metadataFile = cFlags.metadataFile
227230
// Other common flags (noCache, pull and progress) are processed in runBake function.
228231
return runBake(dockerCli, args, options, cFlags)
229232
},
@@ -232,9 +235,9 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
232235
flags := cmd.Flags()
233236

234237
flags.StringArrayVarP(&options.files, "file", "f", []string{}, "Build definition file")
235-
flags.BoolVar(&options.ExportLoad, "load", false, `Shorthand for "--set=*.output=type=docker"`)
238+
flags.BoolVar(&options.exportLoad, "load", false, `Shorthand for "--set=*.output=type=docker"`)
236239
flags.BoolVar(&options.printOnly, "print", false, "Print the options without building")
237-
flags.BoolVar(&options.ExportPush, "push", false, `Shorthand for "--set=*.output=type=registry"`)
240+
flags.BoolVar(&options.exportPush, "push", false, `Shorthand for "--set=*.output=type=registry"`)
238241
flags.StringVar(&options.sbom, "sbom", "", `Shorthand for "--set=*.attest=type=sbom"`)
239242
flags.StringVar(&options.provenance, "provenance", "", `Shorthand for "--set=*.attest=type=provenance"`)
240243
flags.StringArrayVar(&options.overrides, "set", nil, `Override target value (e.g., "targetpattern.key=value")`)

commands/build.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ type buildOptions struct {
7575
progress string
7676
quiet bool
7777

78-
controllerapi.CommonOptions
78+
builder string
79+
metadataFile string
80+
noCache bool
81+
pull bool
82+
exportPush bool
83+
exportLoad bool
84+
7985
control.ControlOptions
8086
}
8187

@@ -97,7 +103,12 @@ func (o *buildOptions) toControllerOptions() (controllerapi.BuildOptions, error)
97103
Tags: o.tags,
98104
Target: o.target,
99105
Ulimits: dockerUlimitToControllerUlimit(o.ulimits),
100-
Opts: &o.CommonOptions,
106+
Builder: o.builder,
107+
MetadataFile: o.metadataFile,
108+
NoCache: o.noCache,
109+
Pull: o.pull,
110+
ExportPush: o.exportPush,
111+
ExportLoad: o.exportLoad,
101112
}
102113

103114
// TODO: extract env var parsing to a method easily usable by library consumers
@@ -225,15 +236,15 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
225236
Args: cli.ExactArgs(1),
226237
RunE: func(cmd *cobra.Command, args []string) error {
227238
options.contextPath = args[0]
228-
options.Builder = rootOpts.builder
229-
options.MetadataFile = cFlags.metadataFile
230-
options.NoCache = false
239+
options.builder = rootOpts.builder
240+
options.metadataFile = cFlags.metadataFile
241+
options.noCache = false
231242
if cFlags.noCache != nil {
232-
options.NoCache = *cFlags.noCache
243+
options.noCache = *cFlags.noCache
233244
}
234-
options.Pull = false
245+
options.pull = false
235246
if cFlags.pull != nil {
236-
options.Pull = *cFlags.pull
247+
options.pull = *cFlags.pull
237248
}
238249
options.progress = cFlags.progress
239250
cmd.Flags().VisitAll(checkWarnedFlags)
@@ -274,7 +285,7 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
274285

275286
flags.StringArrayVar(&options.labels, "label", []string{}, "Set metadata for an image")
276287

277-
flags.BoolVar(&options.ExportLoad, "load", false, `Shorthand for "--output=type=docker"`)
288+
flags.BoolVar(&options.exportLoad, "load", false, `Shorthand for "--output=type=docker"`)
278289

279290
flags.StringVar(&options.networkMode, "network", "default", `Set the networking mode for the "RUN" instructions during build`)
280291

@@ -288,7 +299,7 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
288299
flags.StringVar(&options.printFunc, "print", "", "Print result of information request (e.g., outline, targets) [experimental]")
289300
}
290301

291-
flags.BoolVar(&options.ExportPush, "push", false, `Shorthand for "--output=type=registry"`)
302+
flags.BoolVar(&options.exportPush, "push", false, `Shorthand for "--output=type=registry"`)
292303

293304
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Suppress the build output and print image ID on success")
294305

@@ -814,8 +825,8 @@ func resolvePaths(options *controllerapi.BuildOptions) (_ *controllerapi.BuildOp
814825
}
815826
options.SSH = ssh
816827

817-
if options.Opts != nil && options.Opts.MetadataFile != "" {
818-
options.Opts.MetadataFile, err = filepath.Abs(options.Opts.MetadataFile)
828+
if options.MetadataFile != "" {
829+
options.MetadataFile, err = filepath.Abs(options.MetadataFile)
819830
if err != nil {
820831
return nil, err
821832
}

commands/build_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,10 @@ func TestResolvePaths(t *testing.T) {
233233
{
234234
name: "metadatafile",
235235
options: controllerapi.BuildOptions{
236-
Opts: &controllerapi.CommonOptions{
237-
MetadataFile: "test1",
238-
},
236+
MetadataFile: "test1",
239237
},
240238
want: controllerapi.BuildOptions{
241-
Opts: &controllerapi.CommonOptions{
242-
MetadataFile: filepath.Join(tmpwd, "test1"),
243-
},
239+
MetadataFile: filepath.Join(tmpwd, "test1"),
244240
},
245241
},
246242
}

controller/build/build.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import (
4242
const defaultTargetName = "default"
4343

4444
func RunBuild(ctx context.Context, dockerCli command.Cli, in controllerapi.BuildOptions, inStream io.Reader, progressMode string, statusChan chan *client.SolveStatus) (*client.SolveResponse, *build.ResultContext, error) {
45-
if in.Opts.NoCache && len(in.NoCacheFilter) > 0 {
45+
if in.NoCache && len(in.NoCacheFilter) > 0 {
4646
return nil, nil, errors.Errorf("--no-cache and --no-cache-filter cannot currently be used together")
4747
}
4848

@@ -67,9 +67,9 @@ func RunBuild(ctx context.Context, dockerCli command.Cli, in controllerapi.Build
6767
ExtraHosts: in.ExtraHosts,
6868
Labels: in.Labels,
6969
NetworkMode: in.NetworkMode,
70-
NoCache: in.Opts.NoCache,
70+
NoCache: in.NoCache,
7171
NoCacheFilter: in.NoCacheFilter,
72-
Pull: in.Opts.Pull,
72+
Pull: in.Pull,
7373
ShmSize: dockeropts.MemBytes(in.ShmSize),
7474
Tags: in.Tags,
7575
Target: in.Target,
@@ -106,8 +106,8 @@ func RunBuild(ctx context.Context, dockerCli command.Cli, in controllerapi.Build
106106
if err != nil {
107107
return nil, nil, err
108108
}
109-
if in.Opts.ExportPush {
110-
if in.Opts.ExportLoad {
109+
if in.ExportPush {
110+
if in.ExportLoad {
111111
return nil, nil, errors.Errorf("push and load may not be set together at the moment")
112112
}
113113
if len(outputs) == 0 {
@@ -126,7 +126,7 @@ func RunBuild(ctx context.Context, dockerCli command.Cli, in controllerapi.Build
126126
}
127127
}
128128
}
129-
if in.Opts.ExportLoad {
129+
if in.ExportLoad {
130130
if len(outputs) == 0 {
131131
outputs = []client.ExportEntry{{
132132
Type: "docker",
@@ -160,7 +160,7 @@ func RunBuild(ctx context.Context, dockerCli command.Cli, in controllerapi.Build
160160
}
161161

162162
b, err := builder.New(dockerCli,
163-
builder.WithName(in.Opts.Builder),
163+
builder.WithName(in.Builder),
164164
builder.WithContextPathHash(contextPathHash),
165165
)
166166
if err != nil {
@@ -174,7 +174,7 @@ func RunBuild(ctx context.Context, dockerCli command.Cli, in controllerapi.Build
174174
return nil, nil, err
175175
}
176176

177-
resp, res, err := buildTargets(ctx, dockerCli, b.NodeGroup, nodes, map[string]build.Options{defaultTargetName: opts}, progressMode, in.Opts.MetadataFile, statusChan)
177+
resp, res, err := buildTargets(ctx, dockerCli, b.NodeGroup, nodes, map[string]build.Options{defaultTargetName: opts}, progressMode, in.MetadataFile, statusChan)
178178
err = wrapBuildError(err, false)
179179
if err != nil {
180180
return nil, nil, err

0 commit comments

Comments
 (0)