-
Notifications
You must be signed in to change notification settings - Fork 560
/
build.go
526 lines (472 loc) · 16.4 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"github.com/compose-spec/compose-go/types"
"path/filepath"
"github.com/containerd/containerd/errdefs"
dockerreference "github.com/containerd/containerd/reference/docker"
"github.com/containerd/nerdctl/pkg/buildkitutil"
"github.com/containerd/nerdctl/pkg/defaults"
"github.com/containerd/nerdctl/pkg/platformutil"
"github.com/containerd/nerdctl/pkg/strutil"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func newBuildCommand(cfg *types.BuildConfig) *cobra.Command {
var buildCommand = &cobra.Command{
Use: "build [flags] PATH",
Short: "Build an image from a Dockerfile. Needs buildkitd to be running.",
Long: `Build an image from a Dockerfile. Needs buildkitd to be running.
If Dockerfile is not present and -f is not specified, it will look for Containerfile and build with it. `,
RunE: buildAction,
SilenceUsage: true,
SilenceErrors: true,
}
AddStringFlag(buildCommand, "buildkit-host", nil, defaults.BuildKitHost(), "BUILDKIT_HOST", "BuildKit address")
buildCommand.Flags().StringArrayP("tag", "t", nil, "Name and optionally a tag in the 'name:tag' format")
buildCommand.Flags().StringP("file", "f", "", "Name of the Dockerfile")
buildCommand.Flags().String("target", "", "Set the target build stage to build")
buildCommand.Flags().StringArray("build-arg", nil, "Set build-time variables")
buildCommand.Flags().Bool("no-cache", false, "Do not use cache when building the image")
buildCommand.Flags().StringP("output", "o", "", "Output destination (format: type=local,dest=path)")
buildCommand.Flags().String("progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output")
buildCommand.Flags().StringArray("secret", nil, "Secret file to expose to the build: id=mysecret,src=/local/secret")
buildCommand.Flags().StringArray("ssh", nil, "SSH agent socket or keys to expose to the build (format: default|<id>[=<socket>|<key>[,<key>]])")
buildCommand.Flags().BoolP("quiet", "q", false, "Suppress the build output and print image ID on success")
buildCommand.Flags().StringArray("cache-from", nil, "External cache sources (eg. user/app:cache, type=local,src=path/to/dir)")
buildCommand.Flags().StringArray("cache-to", nil, "Cache export destinations (eg. user/app:cache, type=local,dest=path/to/dir)")
buildCommand.Flags().Bool("rm", true, "Remove intermediate containers after a successful build")
// #region platform flags
// platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64"
buildCommand.Flags().StringSlice("platform", cfg.Platforms, "Set target platform for build (e.g., \"amd64\", \"arm64\")")
buildCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms)
// #endregion
buildCommand.Flags().Bool("ipfs", false, "Allow pulling base images from IPFS")
buildCommand.Flags().String("iidfile", "", "Write the image ID to the file")
buildCommand.Flags().StringArray("label", nil, "Set metadata for an image")
return buildCommand
}
func getBuildkitHost(cmd *cobra.Command) (string, error) {
if cmd.Flags().Changed("buildkit-host") || os.Getenv("BUILDKIT_HOST") != "" {
// If address is explicitly specified, use it.
buildkitHost, err := cmd.Flags().GetString("buildkit-host")
if err != nil {
return "", err
}
if err := buildkitutil.PingBKDaemon(buildkitHost); err != nil {
return "", err
}
return buildkitHost, nil
}
ns, err := cmd.Flags().GetString("namespace")
if err != nil {
return "", err
}
return buildkitutil.GetBuildkitHost(ns)
}
func isImageSharable(buildkitHost string, namespace, uuid, snapshotter string, platform []string) (bool, error) {
labels, err := buildkitutil.GetWorkerLabels(buildkitHost)
if err != nil {
return false, err
}
logrus.Debugf("worker labels: %+v", labels)
executor, ok := labels["org.mobyproject.buildkit.worker.executor"]
if !ok {
return false, nil
}
containerdUUID, ok := labels["org.mobyproject.buildkit.worker.containerd.uuid"]
if !ok {
return false, nil
}
containerdNamespace, ok := labels["org.mobyproject.buildkit.worker.containerd.namespace"]
if !ok {
return false, nil
}
workerSnapshotter, ok := labels["org.mobyproject.buildkit.worker.snapshotter"]
if !ok {
return false, nil
}
// NOTE: It's possible that BuildKit doesn't download the base image of non-default platform (e.g. when the provided
// Dockerfile doesn't contain instructions require base images like RUN) even if `--output type=image,unpack=true`
// is passed to BuildKit. Thus, we need to use `type=docker` or `type=oci` when nerdctl builds non-default platform
// image using `platform` option.
return executor == "containerd" && containerdUUID == uuid && containerdNamespace == namespace && workerSnapshotter == snapshotter && len(platform) == 0, nil
}
func buildAction(cmd *cobra.Command, args []string) error {
platform, err := cmd.Flags().GetStringSlice("platform")
if err != nil {
return err
}
platform = strutil.DedupeStrSlice(platform)
buildkitHost, err := getBuildkitHost(cmd)
if err != nil {
return err
}
buildctlBinary, buildctlArgs, needsLoading, metaFile, tags, cleanup, err := generateBuildctlArgs(cmd, buildkitHost, platform, args)
if err != nil {
return err
}
if cleanup != nil {
defer cleanup()
}
runIPFSRegistry, err := cmd.Flags().GetBool("ipfs")
if err != nil {
return err
}
if runIPFSRegistry {
logrus.Infof("Ensuring IPFS registry is running")
nerdctlCmd, nerdctlArgs := globalFlags(cmd)
out, err := exec.Command(nerdctlCmd, append(nerdctlArgs, "ipfs", "registry", "up")...).CombinedOutput()
if err != nil {
return fmt.Errorf("failed to start IPFS registry: %v: %v", string(out), err)
}
logrus.Infof("IPFS registry is running: %v", string(out))
}
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
}
logrus.Debugf("running %s %v", buildctlBinary, buildctlArgs)
buildctlCmd := exec.Command(buildctlBinary, buildctlArgs...)
buildctlCmd.Env = os.Environ()
var buildctlStdout io.Reader
if needsLoading {
buildctlStdout, err = buildctlCmd.StdoutPipe()
if err != nil {
return err
}
} else {
buildctlCmd.Stdout = cmd.OutOrStdout()
}
if !quiet {
buildctlCmd.Stderr = cmd.ErrOrStderr()
}
if err := buildctlCmd.Start(); err != nil {
return err
}
if needsLoading {
platMC, err := platformutil.NewMatchComparer(false, platform)
if err != nil {
return err
}
if err = loadImage(buildctlStdout, cmd, platMC, quiet); err != nil {
return err
}
}
if err = buildctlCmd.Wait(); err != nil {
return err
}
iidFile, _ := cmd.Flags().GetString("iidfile")
if iidFile != "" {
id, err := getDigestFromMetaFile(metaFile)
if err != nil {
return err
}
if err := os.WriteFile(iidFile, []byte(id), 0600); err != nil {
return err
}
}
if len(tags) > 1 {
logrus.Debug("Found more than 1 tag")
client, ctx, cancel, err := newClient(cmd)
if err != nil {
return fmt.Errorf("unable to tag images: %s", err)
}
defer cancel()
imageService := client.ImageService()
image, err := imageService.Get(ctx, tags[0])
if err != nil {
return fmt.Errorf("unable to tag image: %s", err)
}
for _, targetRef := range tags[1:] {
image.Name = targetRef
if _, err := imageService.Create(ctx, image); err != nil {
// if already exists; skip.
if errors.Is(err, errdefs.ErrAlreadyExists) {
continue
}
return fmt.Errorf("unable to tag image: %s", err)
}
}
}
return nil
}
func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, args []string) (buildCtlBinary string,
buildctlArgs []string, needsLoading bool, metaFile string, tags []string, cleanup func(), err error) {
if len(args) < 1 {
return "", nil, false, "", nil, nil, errors.New("context needs to be specified")
}
buildContext := args[0]
if buildContext == "-" || strings.Contains(buildContext, "://") {
return "", nil, false, "", nil, nil, fmt.Errorf("unsupported build context: %q", buildContext)
}
buildctlBinary, err := buildkitutil.BuildctlBinary()
if err != nil {
return "", nil, false, "", nil, nil, err
}
output, err := cmd.Flags().GetString("output")
if err != nil {
return "", nil, false, "", nil, nil, err
}
if output == "" {
client, ctx, cancel, err := newClient(cmd)
if err != nil {
return "", nil, false, "", nil, nil, err
}
defer cancel()
info, err := client.Server(ctx)
if err != nil {
return "", nil, false, "", nil, nil, err
}
ns, err := cmd.Flags().GetString("namespace")
if err != nil {
return "", nil, false, "", nil, nil, err
}
snapshotter, err := cmd.Flags().GetString("snapshotter")
if err != nil {
return "", nil, false, "", nil, nil, err
}
sharable, err := isImageSharable(buildkitHost, ns, info.UUID, snapshotter, platform)
if err != nil {
return "", nil, false, "", nil, nil, err
}
if sharable {
output = "type=image,unpack=true" // ensure the target stage is unlazied (needed for any snapshotters)
} else {
output = "type=docker"
if len(platform) > 1 {
// For avoiding `error: failed to solve: docker exporter does not currently support exporting manifest lists`
// TODO: consider using type=oci for single-platform build too
output = "type=oci"
}
needsLoading = true
}
} else {
if !strings.Contains(output, "type=") {
// should accept --output <DIR> as an alias of --output
// type=local,dest=<DIR>
output = fmt.Sprintf("type=local,dest=%s", output)
}
if strings.Contains(output, "type=docker") || strings.Contains(output, "type=oci") {
needsLoading = true
}
}
tagValue, err := cmd.Flags().GetStringArray("tag")
if err != nil {
return "", nil, false, "", nil, nil, err
}
if tags = strutil.DedupeStrSlice(tagValue); len(tags) > 0 {
ref := tags[0]
named, err := dockerreference.ParseNormalizedNamed(ref)
if err != nil {
return "", nil, false, "", nil, nil, err
}
output += ",name=" + dockerreference.TagNameOnly(named).String()
// pick the first tag and add it to output
for idx, tag := range tags {
named, err := dockerreference.ParseNormalizedNamed(tag)
if err != nil {
return "", nil, false, "", nil, nil, err
}
tags[idx] = dockerreference.TagNameOnly(named).String()
}
} else if len(tags) == 0 {
output = output + ",dangling-name-prefix=<none>"
}
buildctlArgs = buildkitutil.BuildctlBaseArgs(buildkitHost)
progressValue, err := cmd.Flags().GetString("progress")
if err != nil {
return "", nil, false, "", nil, nil, err
}
buildctlArgs = append(buildctlArgs, []string{
"build",
"--progress=" + progressValue,
"--frontend=dockerfile.v0",
"--local=context=" + buildContext,
"--output=" + output,
}...)
filename, err := cmd.Flags().GetString("file")
if err != nil {
return "", nil, false, "", nil, nil, err
}
dir := buildContext
file := buildkitutil.DefaultDockerfileName
if filename != "" {
if filename == "-" {
var err error
dir, err = buildkitutil.WriteTempDockerfile(cmd.InOrStdin())
if err != nil {
return "", nil, false, "", nil, nil, err
}
cleanup = func() {
os.RemoveAll(dir)
}
} else {
dir, file = filepath.Split(filename)
}
if dir == "" {
dir = "."
}
}
dir, file, err = buildkitutil.BuildKitFile(dir, file)
if err != nil {
return "", nil, false, "", nil, nil, err
}
buildctlArgs = append(buildctlArgs, "--local=dockerfile="+dir)
buildctlArgs = append(buildctlArgs, "--opt=filename="+file)
target, err := cmd.Flags().GetString("target")
if err != nil {
return "", nil, false, "", nil, cleanup, err
}
if target != "" {
buildctlArgs = append(buildctlArgs, "--opt=target="+target)
}
if len(platform) > 0 {
buildctlArgs = append(buildctlArgs, "--opt=platform="+strings.Join(platform, ","))
}
buildArgsValue, err := cmd.Flags().GetStringArray("build-arg")
if err != nil {
return "", nil, false, "", nil, cleanup, err
}
for _, ba := range strutil.DedupeStrSlice(buildArgsValue) {
arr := strings.Split(ba, "=")
if len(arr) == 1 && len(arr[0]) > 0 {
// Avoid masking default build arg value from Dockerfile if environment variable is not set
// https://github.com/moby/moby/issues/24101
val, ok := os.LookupEnv(arr[0])
if ok {
buildctlArgs = append(buildctlArgs, fmt.Sprintf("--opt=build-arg:%s=%s", ba, val))
} else {
logrus.Debugf("ignoring unset build arg %q", ba)
}
} else if len(arr) > 1 && len(arr[0]) > 0 {
buildctlArgs = append(buildctlArgs, "--opt=build-arg:"+ba)
// Support `--build-arg BUILDKIT_INLINE_CACHE=1` for compatibility with `docker buildx build`
// https://github.com/docker/buildx/blob/v0.6.3/docs/reference/buildx_build.md#-export-build-cache-to-an-external-cache-destination---cache-to
if strings.HasPrefix(ba, "BUILDKIT_INLINE_CACHE=") {
bic := strings.TrimPrefix(ba, "BUILDKIT_INLINE_CACHE=")
bicParsed, err := strconv.ParseBool(bic)
if err == nil {
if bicParsed {
buildctlArgs = append(buildctlArgs, "--export-cache=type=inline")
}
} else {
logrus.WithError(err).Warnf("invalid BUILDKIT_INLINE_CACHE: %q", bic)
}
}
} else {
return "", nil, false, "", nil, nil, fmt.Errorf("invalid build arg %q", ba)
}
}
labels, err := cmd.Flags().GetStringArray("label")
if err != nil {
return "", nil, false, "", nil, nil, err
}
labels = strutil.DedupeStrSlice(labels)
for _, l := range labels {
buildctlArgs = append(buildctlArgs, "--opt=label:"+l)
}
noCache, err := cmd.Flags().GetBool("no-cache")
if err != nil {
return "", nil, false, "", nil, cleanup, err
}
if noCache {
buildctlArgs = append(buildctlArgs, "--no-cache")
}
secretValue, err := cmd.Flags().GetStringArray("secret")
if err != nil {
return "", nil, false, "", nil, cleanup, err
}
for _, s := range strutil.DedupeStrSlice(secretValue) {
buildctlArgs = append(buildctlArgs, "--secret="+s)
}
sshValue, err := cmd.Flags().GetStringArray("ssh")
if err != nil {
return "", nil, false, "", nil, cleanup, err
}
for _, s := range strutil.DedupeStrSlice(sshValue) {
buildctlArgs = append(buildctlArgs, "--ssh="+s)
}
cacheFrom, err := cmd.Flags().GetStringArray("cache-from")
if err != nil {
return "", nil, false, "", nil, cleanup, err
}
for _, s := range strutil.DedupeStrSlice(cacheFrom) {
if !strings.Contains(s, "type=") {
s = "type=registry,ref=" + s
}
buildctlArgs = append(buildctlArgs, "--import-cache="+s)
}
cacheTo, err := cmd.Flags().GetStringArray("cache-to")
if err != nil {
return "", nil, false, "", nil, cleanup, err
}
for _, s := range strutil.DedupeStrSlice(cacheTo) {
if !strings.Contains(s, "type=") {
s = "type=registry,ref=" + s
}
buildctlArgs = append(buildctlArgs, "--export-cache="+s)
}
rm, err := cmd.Flags().GetBool("rm")
if err != nil {
return "", nil, false, "", nil, cleanup, err
}
if !rm {
logrus.Warn("ignoring deprecated flag: '--rm=false'")
}
iidFile, err := cmd.Flags().GetString("iidfile")
if err != nil {
return "", nil, false, "", nil, cleanup, err
}
if iidFile != "" {
file, err := os.CreateTemp("", "buildkit-meta-*")
if err != nil {
return "", nil, false, "", nil, cleanup, err
}
defer file.Close()
metaFile = file.Name()
buildctlArgs = append(buildctlArgs, "--metadata-file="+metaFile)
}
return buildctlBinary, buildctlArgs, needsLoading, metaFile, tags, cleanup, nil
}
func getDigestFromMetaFile(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", err
}
defer os.Remove(path)
metadata := map[string]json.RawMessage{}
if err := json.Unmarshal(data, &metadata); err != nil {
logrus.WithError(err).Errorf("failed to unmarshal metadata file %s", path)
return "", err
}
digestRaw, ok := metadata["containerimage.digest"]
if !ok {
return "", errors.New("failed to find containerimage.digest in metadata file")
}
var digest string
if err := json.Unmarshal(digestRaw, &digest); err != nil {
logrus.WithError(err).Errorf("failed to unmarshal digset")
return "", err
}
return digest, nil
}