From 768c6d7b293a0cbe68aac2cc3b81445d52c1657d Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Sun, 17 Mar 2019 09:54:52 -0700 Subject: [PATCH] builder-next: allow outputs configuration Signed-off-by: Tonis Tiigi --- api/server/router/build/build_routes.go | 11 +++++++++++ api/swagger.yaml | 5 +++++ api/types/client.go | 9 +++++++++ builder/builder-next/builder.go | 24 +++++++++++++++++++++--- client/image_build.go | 8 ++++++++ docs/api/version-history.md | 1 + 6 files changed, 55 insertions(+), 3 deletions(-) diff --git a/api/server/router/build/build_routes.go b/api/server/router/build/build_routes.go index f41a6d43e2..57d9fc8c46 100644 --- a/api/server/router/build/build_routes.go +++ b/api/server/router/build/build_routes.go @@ -148,6 +148,17 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui } options.Version = builderVersion + if versions.GreaterThanOrEqualTo(version, "1.40") { + outputsJSON := r.FormValue("outputs") + if outputsJSON != "" { + var outputs []types.ImageBuildOutput + if err := json.Unmarshal([]byte(outputsJSON), &outputs); err != nil { + return nil, err + } + options.Outputs = outputs + } + } + return options, nil } diff --git a/api/swagger.yaml b/api/swagger.yaml index 8652c368c4..94ee91f74a 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -6459,6 +6459,11 @@ paths: description: "Target build stage" type: "string" default: "" + - name: "outputs" + in: "query" + description: "BuildKit output configuration" + type: "string" + default: "" responses: 200: description: "no error" diff --git a/api/types/client.go b/api/types/client.go index 3b698c2c24..4b9f50282b 100644 --- a/api/types/client.go +++ b/api/types/client.go @@ -187,6 +187,15 @@ type ImageBuildOptions struct { // build request. The same identifier can be used to gracefully cancel the // build with the cancel request. BuildID string + // Outputs defines configurations for exporting build results. Only supported + // in BuildKit mode + Outputs []ImageBuildOutput +} + +// ImageBuildOutput defines configuration for exporting a build result +type ImageBuildOutput struct { + Type string + Attrs map[string]string } // BuilderVersion sets the version of underlying builder to use diff --git a/builder/builder-next/builder.go b/builder/builder-next/builder.go index fc6410eeda..859a65f7f1 100644 --- a/builder/builder-next/builder.go +++ b/builder/builder-next/builder.go @@ -313,10 +313,25 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder. } frontendAttrs["add-hosts"] = extraHosts + exporterName := "" exporterAttrs := map[string]string{} - if len(opt.Options.Tags) > 0 { - exporterAttrs["name"] = strings.Join(opt.Options.Tags, ",") + if len(opt.Options.Outputs) > 1 { + return nil, errors.Errorf("multiple outputs not supported") + } else if len(opt.Options.Outputs) == 0 { + exporterName = "moby" + } else { + // cacheonly is a special type for triggering skipping all exporters + if opt.Options.Outputs[0].Type != "cacheonly" { + exporterName = opt.Options.Outputs[0].Type + exporterAttrs = opt.Options.Outputs[0].Attrs + } + } + + if exporterName == "moby" { + if len(opt.Options.Tags) > 0 { + exporterAttrs["name"] = strings.Join(opt.Options.Tags, ",") + } } cache := controlapi.CacheOptions{} @@ -331,7 +346,7 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder. req := &controlapi.SolveRequest{ Ref: id, - Exporter: "moby", + Exporter: exporterName, ExporterAttrs: exporterAttrs, Frontend: "dockerfile.v0", FrontendAttrs: frontendAttrs, @@ -352,6 +367,9 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder. if err != nil { return err } + if exporterName != "moby" { + return nil + } id, ok := resp.ExporterResponse["containerimage.digest"] if !ok { return errors.Errorf("missing image id") diff --git a/client/image_build.go b/client/image_build.go index 9add3c10b3..8fcf995036 100644 --- a/client/image_build.go +++ b/client/image_build.go @@ -134,5 +134,13 @@ func (cli *Client) imageBuildOptionsToQuery(options types.ImageBuildOptions) (ur query.Set("buildid", options.BuildID) } query.Set("version", string(options.Version)) + + if options.Outputs != nil { + outputsJSON, err := json.Marshal(options.Outputs) + if err != nil { + return query, err + } + query.Set("outputs", string(outputsJSON)) + } return query, nil } diff --git a/docs/api/version-history.md b/docs/api/version-history.md index f6d6e54ad9..1bd6747bf2 100644 --- a/docs/api/version-history.md +++ b/docs/api/version-history.md @@ -65,6 +65,7 @@ keywords: "API, Docker, rcli, REST, documentation" back to `shareable` by using `DefaultIpcMode` daemon configuration parameter. * `POST /containers/{id}/update` now accepts a `PidsLimit` field to tune a container's PID limit. Set `0` or `-1` for unlimited. Leave `null` to not change the current value. +* `POST /build` now accepts `outputs` key for configuring build outputs when using BuildKit mode. ## V1.39 API changes