Browse code

builder-next: allow outputs configuration

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>

Tonis Tiigi authored on 2019/03/18 01:54:52
Showing 6 changed files
... ...
@@ -148,6 +148,17 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui
148 148
 	}
149 149
 	options.Version = builderVersion
150 150
 
151
+	if versions.GreaterThanOrEqualTo(version, "1.40") {
152
+		outputsJSON := r.FormValue("outputs")
153
+		if outputsJSON != "" {
154
+			var outputs []types.ImageBuildOutput
155
+			if err := json.Unmarshal([]byte(outputsJSON), &outputs); err != nil {
156
+				return nil, err
157
+			}
158
+			options.Outputs = outputs
159
+		}
160
+	}
161
+
151 162
 	return options, nil
152 163
 }
153 164
 
... ...
@@ -6459,6 +6459,11 @@ paths:
6459 6459
           description: "Target build stage"
6460 6460
           type: "string"
6461 6461
           default: ""
6462
+        - name: "outputs"
6463
+          in: "query"
6464
+          description: "BuildKit output configuration"
6465
+          type: "string"
6466
+          default: ""
6462 6467
       responses:
6463 6468
         200:
6464 6469
           description: "no error"
... ...
@@ -187,6 +187,15 @@ type ImageBuildOptions struct {
187 187
 	// build request. The same identifier can be used to gracefully cancel the
188 188
 	// build with the cancel request.
189 189
 	BuildID string
190
+	// Outputs defines configurations for exporting build results. Only supported
191
+	// in BuildKit mode
192
+	Outputs []ImageBuildOutput
193
+}
194
+
195
+// ImageBuildOutput defines configuration for exporting a build result
196
+type ImageBuildOutput struct {
197
+	Type  string
198
+	Attrs map[string]string
190 199
 }
191 200
 
192 201
 // BuilderVersion sets the version of underlying builder to use
... ...
@@ -313,10 +313,25 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.
313 313
 	}
314 314
 	frontendAttrs["add-hosts"] = extraHosts
315 315
 
316
+	exporterName := ""
316 317
 	exporterAttrs := map[string]string{}
317 318
 
318
-	if len(opt.Options.Tags) > 0 {
319
-		exporterAttrs["name"] = strings.Join(opt.Options.Tags, ",")
319
+	if len(opt.Options.Outputs) > 1 {
320
+		return nil, errors.Errorf("multiple outputs not supported")
321
+	} else if len(opt.Options.Outputs) == 0 {
322
+		exporterName = "moby"
323
+	} else {
324
+		// cacheonly is a special type for triggering skipping all exporters
325
+		if opt.Options.Outputs[0].Type != "cacheonly" {
326
+			exporterName = opt.Options.Outputs[0].Type
327
+			exporterAttrs = opt.Options.Outputs[0].Attrs
328
+		}
329
+	}
330
+
331
+	if exporterName == "moby" {
332
+		if len(opt.Options.Tags) > 0 {
333
+			exporterAttrs["name"] = strings.Join(opt.Options.Tags, ",")
334
+		}
320 335
 	}
321 336
 
322 337
 	cache := controlapi.CacheOptions{}
... ...
@@ -331,7 +346,7 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.
331 331
 
332 332
 	req := &controlapi.SolveRequest{
333 333
 		Ref:           id,
334
-		Exporter:      "moby",
334
+		Exporter:      exporterName,
335 335
 		ExporterAttrs: exporterAttrs,
336 336
 		Frontend:      "dockerfile.v0",
337 337
 		FrontendAttrs: frontendAttrs,
... ...
@@ -352,6 +367,9 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.
352 352
 		if err != nil {
353 353
 			return err
354 354
 		}
355
+		if exporterName != "moby" {
356
+			return nil
357
+		}
355 358
 		id, ok := resp.ExporterResponse["containerimage.digest"]
356 359
 		if !ok {
357 360
 			return errors.Errorf("missing image id")
... ...
@@ -134,5 +134,13 @@ func (cli *Client) imageBuildOptionsToQuery(options types.ImageBuildOptions) (ur
134 134
 		query.Set("buildid", options.BuildID)
135 135
 	}
136 136
 	query.Set("version", string(options.Version))
137
+
138
+	if options.Outputs != nil {
139
+		outputsJSON, err := json.Marshal(options.Outputs)
140
+		if err != nil {
141
+			return query, err
142
+		}
143
+		query.Set("outputs", string(outputsJSON))
144
+	}
137 145
 	return query, nil
138 146
 }
... ...
@@ -65,6 +65,7 @@ keywords: "API, Docker, rcli, REST, documentation"
65 65
   back to `shareable` by using `DefaultIpcMode` daemon configuration parameter.
66 66
 * `POST /containers/{id}/update` now accepts a `PidsLimit` field to tune a container's
67 67
   PID limit. Set `0` or `-1` for unlimited. Leave `null` to not change the current value.
68
+* `POST /build` now accepts `outputs` key for configuring build outputs when using BuildKit mode.
68 69
 
69 70
 ## V1.39 API changes
70 71