Skip to content

Commit

Permalink
Add buildx build --opt (EXPERIMENTAL)
Browse files Browse the repository at this point in the history
This allows opt-in to new features of BuildKit that are not officially
exposed to buildx yet.

Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda committed Feb 19, 2024
1 parent c9d1c41 commit 43210bd
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 122 deletions.
5 changes: 5 additions & 0 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ type Options struct {
PrintFunc *PrintFunc
SourcePolicy *spb.Policy
GroupRef string

RawOpts map[string]string
}

type PrintFunc struct {
Expand Down Expand Up @@ -226,6 +228,9 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op
AllowedEntitlements: opt.Allow,
SourcePolicy: opt.SourcePolicy,
}
if opt.RawOpts != nil {
so.FrontendAttrs = opt.RawOpts
}

if so.Ref == "" {
so.Ref = identity.NewID()
Expand Down
11 changes: 10 additions & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ type buildOptions struct {
control.ControlOptions

invokeConfig *invokeConfig

rawOpts []string
}

func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error) {
Expand All @@ -120,6 +122,11 @@ func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error
return nil, err
}

rawOpts, err := listToMap(o.rawOpts, true)
if err != nil {
return nil, err
}

opts := controllerapi.BuildOptions{
Allow: o.allow,
Annotations: o.annotations,
Expand All @@ -141,6 +148,7 @@ func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error
Pull: o.pull,
ExportPush: o.exportPush,
ExportLoad: o.exportLoad,
RawOpts: rawOpts,
}

// TODO: extract env var parsing to a method easily usable by library consumers
Expand Down Expand Up @@ -590,7 +598,8 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions, debugConfig *debug.D

if isExperimental() {
flags.StringVar(&options.printFunc, "print", "", "Print result of information request (e.g., outline, targets)")
cobrautil.MarkFlagsExperimental(flags, "print")
flags.StringArrayVar(&options.rawOpts, "opt", []string{}, "Set BuildKit frontend option directly")
cobrautil.MarkFlagsExperimental(flags, "print", "opt")
}

flags.BoolVar(&options.exportPush, "push", false, `Shorthand for "--output=type=registry"`)
Expand Down
1 change: 1 addition & 0 deletions controller/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func RunBuild(ctx context.Context, dockerCli command.Cli, in controllerapi.Build
Target: in.Target,
Ulimits: controllerUlimitOpt2DockerUlimit(in.Ulimits),
GroupRef: in.GroupRef,
RawOpts: in.RawOpts,
}

platforms, err := platformutil.Parse(in.Platforms)
Expand Down
252 changes: 131 additions & 121 deletions controller/pb/controller.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions controller/pb/controller.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ message BuildOptions {
string Ref = 29;
string GroupRef = 30;
repeated string Annotations = 31;
map<string, string> RawOpts = 32;
}

message ExportEntry {
Expand Down
1 change: 1 addition & 0 deletions docs/reference/buildx_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Start a build
| `--network` | `string` | `default` | Set the networking mode for the `RUN` instructions during build |
| `--no-cache` | | | Do not use cache when building the image |
| [`--no-cache-filter`](#no-cache-filter) | `stringArray` | | Do not cache specified stages |
| `--opt` | `stringArray` | | Set BuildKit frontend option directly (EXPERIMENTAL) |
| [`-o`](#output), [`--output`](#output) | `stringArray` | | Output destination (format: `type=local,dest=path`) |
| [`--platform`](#platform) | `stringArray` | | Set target platform for build |
| `--print` | `string` | | Print result of information request (e.g., outline, targets) (EXPERIMENTAL) |
Expand Down
1 change: 1 addition & 0 deletions docs/reference/buildx_debug_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Start a build
| `--network` | `string` | `default` | Set the networking mode for the `RUN` instructions during build |
| `--no-cache` | | | Do not use cache when building the image |
| `--no-cache-filter` | `stringArray` | | Do not cache specified stages |
| `--opt` | `stringArray` | | Set BuildKit frontend option directly (EXPERIMENTAL) |
| `-o`, `--output` | `stringArray` | | Output destination (format: `type=local,dest=path`) |
| `--platform` | `stringArray` | | Set target platform for build |
| `--print` | `string` | | Print result of information request (e.g., outline, targets) (EXPERIMENTAL) |
Expand Down

0 comments on commit 43210bd

Please sign in to comment.