-
Notifications
You must be signed in to change notification settings - Fork 298
/
docker.go
737 lines (614 loc) · 18.3 KB
/
docker.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
package tiltfile
import (
"fmt"
"path/filepath"
"strings"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
"go.starlark.net/starlark"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/dockerfile"
"github.com/windmilleng/tilt/internal/ospath"
"github.com/windmilleng/tilt/pkg/model"
)
const fastBuildDeprecationWarning = "FastBuild (`fast_build`; `add_fast_build`; " +
"`docker_build(...).add(...)`, etc.) will be deprecated **ON THURS. 9/12/19**; " +
"you can use Live Update instead! See https://docs.tilt.dev/live_update_tutorial.html " +
"for more information. If Live Update doesn't fit your use case, let us know."
type dockerImage struct {
tiltfilePath string
baseDockerfilePath string
baseDockerfile dockerfile.Dockerfile
configurationRef container.RefSelector
deploymentRef reference.Named
cachePaths []string
matchInEnvVars bool
ignores []string
onlys []string
entrypoint model.Cmd // optional: if specified, we override the image entrypoint/k8s command with this
// fast-build properties -- will be deprecated
syncs []pathSync
runs []model.Run
fbEntrypoint string
hotReload bool
dbDockerfilePath string
dbDockerfile dockerfile.Dockerfile
dbBuildPath string
dbBuildArgs model.DockerBuildArgs
customCommand string
customDeps []string
customTag string
// Whether this has been matched up yet to a deploy resource.
matched bool
dependencyIDs []model.TargetID
disablePush bool
liveUpdate model.LiveUpdate
}
func (d *dockerImage) ID() model.TargetID {
return model.ImageID(d.configurationRef)
}
type dockerImageBuildType int
const (
UnknownBuild = iota
DockerBuild
FastBuild
CustomBuild
)
func (d *dockerImage) Type() dockerImageBuildType {
if d.dbBuildPath != "" {
return DockerBuild
}
if d.baseDockerfilePath != "" {
return FastBuild
}
if d.customCommand != "" {
return CustomBuild
}
return UnknownBuild
}
func (s *tiltfileState) dockerBuild(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var dockerRef, entrypoint string
var contextVal, dockerfilePathVal, buildArgs, dockerfileContentsVal, cacheVal, liveUpdateVal, ignoreVal, onlyVal starlark.Value
var matchInEnvVars bool
if err := starlark.UnpackArgs(fn.Name(), args, kwargs,
"ref", &dockerRef,
"context", &contextVal,
"build_args?", &buildArgs,
"dockerfile?", &dockerfilePathVal,
"dockerfile_contents?", &dockerfileContentsVal,
"cache?", &cacheVal,
"live_update?", &liveUpdateVal,
"match_in_env_vars?", &matchInEnvVars,
"ignore?", &ignoreVal,
"only?", &onlyVal,
"entrypoint?", &entrypoint,
); err != nil {
return nil, err
}
ref, err := container.ParseNamed(dockerRef)
if err != nil {
return nil, fmt.Errorf("Argument 1 (ref): can't parse %q: %v", dockerRef, err)
}
if contextVal == nil {
return nil, fmt.Errorf("Argument 2 (context): empty but is required")
}
context, err := s.absPathFromStarlarkValue(thread, contextVal)
if err != nil {
return nil, err
}
var sba map[string]string
if buildArgs != nil {
d, ok := buildArgs.(*starlark.Dict)
if !ok {
return nil, fmt.Errorf("Argument 3 (build_args): expected dict, got %T", buildArgs)
}
sba, err = skylarkStringDictToGoMap(d)
if err != nil {
return nil, fmt.Errorf("Argument 3 (build_args): %v", err)
}
}
dockerfilePath := filepath.Join(context, "Dockerfile")
var dockerfileContents string
if dockerfileContentsVal != nil && dockerfilePathVal != nil {
return nil, fmt.Errorf("Cannot specify both dockerfile and dockerfile_contents keyword arguments")
}
if dockerfileContentsVal != nil {
switch v := dockerfileContentsVal.(type) {
case *blob:
dockerfileContents = v.text
case starlark.String:
dockerfileContents = v.GoString()
default:
return nil, fmt.Errorf("Argument (dockerfile_contents): must be string or blob.")
}
} else if dockerfilePathVal != nil {
dockerfilePath, err = s.absPathFromStarlarkValue(thread, dockerfilePathVal)
if err != nil {
return nil, err
}
bs, err := s.readFile(dockerfilePath)
if err != nil {
return nil, errors.Wrap(err, "error reading dockerfile")
}
dockerfileContents = string(bs)
} else {
bs, err := s.readFile(dockerfilePath)
if err != nil {
return nil, errors.Wrapf(err, "error reading dockerfile")
}
dockerfileContents = string(bs)
}
cachePaths, err := s.cachePathsFromSkylarkValue(cacheVal)
if err != nil {
return nil, err
}
liveUpdate, err := s.liveUpdateFromSteps(thread, liveUpdateVal)
if err != nil {
return nil, errors.Wrap(err, "live_update")
}
ignores, err := parseValuesToStrings(ignoreVal, "ignore")
if err != nil {
return nil, err
}
onlys, err := s.parseOnly(onlyVal)
if err != nil {
return nil, err
}
var entrypointCmd model.Cmd
if entrypoint != "" {
entrypointCmd = model.ToShellCmd(entrypoint)
}
r := &dockerImage{
tiltfilePath: s.currentTiltfilePath(thread),
dbDockerfilePath: dockerfilePath,
dbDockerfile: dockerfile.Dockerfile(dockerfileContents),
dbBuildPath: context,
configurationRef: container.NewRefSelector(ref),
dbBuildArgs: sba,
cachePaths: cachePaths,
liveUpdate: liveUpdate,
matchInEnvVars: matchInEnvVars,
ignores: ignores,
onlys: onlys,
entrypoint: entrypointCmd,
}
err = s.buildIndex.addImage(r)
if err != nil {
return nil, err
}
// NOTE(maia): docker_build returns a fast build that users can optionally
// populate; if populated, we use it for in-place updates of this image
// (but use DockerBuild info for image builds)
fb := &fastBuild{s: s, img: r}
return fb, nil
}
func (s *tiltfileState) parseOnly(val starlark.Value) ([]string, error) {
paths, err := parseValuesToStrings(val, "only")
if err != nil {
return nil, err
}
for _, p := range paths {
// We want to forbid file globs due to these issues:
// https://github.com/windmilleng/tilt/issues/1982
// https://github.com/moby/moby/issues/30018
if strings.Contains(p, "*") {
return nil, fmt.Errorf("'only' does not support '*' file globs. Must be a real path: %s", p)
}
}
return paths, nil
}
func (s *tiltfileState) fastBuildForImage(image *dockerImage) model.FastBuild {
return model.FastBuild{
BaseDockerfile: image.baseDockerfile.String(),
Syncs: s.syncsToDomain(image),
Runs: image.runs,
Entrypoint: model.ToShellCmd(image.fbEntrypoint),
HotReload: image.hotReload,
}
}
func (s *tiltfileState) customBuild(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var dockerRef string
var command string
var deps *starlark.List
var tag string
var disablePush bool
var liveUpdateVal, ignoreVal starlark.Value
var matchInEnvVars bool
var entrypoint string
err := starlark.UnpackArgs(fn.Name(), args, kwargs,
"ref", &dockerRef,
"command", &command,
"deps", &deps,
"tag?", &tag,
"disable_push?", &disablePush,
"live_update?", &liveUpdateVal,
"match_in_env_vars?", &matchInEnvVars,
"ignore?", &ignoreVal,
"entrypoint?", &entrypoint,
)
if err != nil {
return nil, err
}
ref, err := reference.ParseNormalizedNamed(dockerRef)
if err != nil {
return nil, fmt.Errorf("Argument 1 (ref): can't parse %q: %v", dockerRef, err)
}
if command == "" {
return nil, fmt.Errorf("Argument 2 (command) can't be empty")
}
if deps == nil || deps.Len() == 0 {
return nil, fmt.Errorf("Argument 3 (deps) can't be empty")
}
var localDeps []string
iter := deps.Iterate()
defer iter.Done()
var v starlark.Value
for iter.Next(&v) {
p, err := s.absPathFromStarlarkValue(thread, v)
if err != nil {
return nil, fmt.Errorf("Argument 3 (deps): %v", err)
}
localDeps = append(localDeps, p)
}
liveUpdate, err := s.liveUpdateFromSteps(thread, liveUpdateVal)
if err != nil {
return nil, errors.Wrap(err, "live_update")
}
ignores, error := parseValuesToStrings(ignoreVal, "ignore")
if error != nil {
return nil, error
}
var entrypointCmd model.Cmd
if entrypoint != "" {
entrypointCmd = model.ToShellCmd(entrypoint)
}
img := &dockerImage{
configurationRef: container.NewRefSelector(ref),
customCommand: command,
customDeps: localDeps,
customTag: tag,
disablePush: disablePush,
liveUpdate: liveUpdate,
matchInEnvVars: matchInEnvVars,
ignores: ignores,
entrypoint: entrypointCmd,
}
err = s.buildIndex.addImage(img)
if err != nil {
return nil, err
}
return &customBuild{s: s, img: img}, nil
}
type customBuild struct {
s *tiltfileState
img *dockerImage
}
var _ starlark.Value = &customBuild{}
func (b *customBuild) String() string {
return fmt.Sprintf("custom_build(%q)", b.img.configurationRef.String())
}
func (b *customBuild) Type() string {
return "custom_build"
}
func (b *customBuild) Freeze() {}
func (b *customBuild) Truth() starlark.Bool {
return true
}
func (b *customBuild) Hash() (uint32, error) {
return 0, fmt.Errorf("unhashable type: custom_build")
}
const (
addFastBuildN = "add_fast_build"
)
func (b *customBuild) Attr(name string) (starlark.Value, error) {
switch name {
case addFastBuildN:
return starlark.NewBuiltin(name, b.addFastBuild), nil
default:
return nil, nil
}
}
func (b *customBuild) AttrNames() []string {
return []string{addFastBuildN}
}
func (b *customBuild) addFastBuild(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
return &fastBuild{s: b.s, img: b.img}, nil
}
func parseValuesToStrings(value starlark.Value, param string) ([]string, error) {
tempIgnores := starlarkValueOrSequenceToSlice(value)
var ignores []string
for _, v := range tempIgnores {
switch val := v.(type) {
case starlark.String: // for singular string
goString := val.GoString()
if strings.Contains(goString, "\n") {
return nil, fmt.Errorf(param+" cannot contain newlines; found "+param+": %q", goString)
}
ignores = append(ignores, val.GoString())
default:
return nil, fmt.Errorf(param+" must be a string or a sequence of strings; found a %T", val)
}
}
return ignores, nil
}
func (s *tiltfileState) fastBuild(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var dockerRef, entrypoint string
var baseDockerfile starlark.Value
var cacheVal starlark.Value
err := starlark.UnpackArgs(fn.Name(), args, kwargs,
"ref", &dockerRef,
"base_dockerfile", &baseDockerfile,
"entrypoint?", &entrypoint,
"cache?", &cacheVal,
)
if err != nil {
return nil, err
}
baseDockerfilePath, err := s.absPathFromStarlarkValue(thread, baseDockerfile)
if err != nil {
return nil, fmt.Errorf("Argument 2 (base_dockerfile): %v", err)
}
ref, err := container.ParseNamed(dockerRef)
if err != nil {
return nil, fmt.Errorf("Parsing %q: %v", dockerRef, err)
}
bs, err := s.readFile(baseDockerfilePath)
if err != nil {
return nil, errors.Wrap(err, "error reading dockerfile")
}
df := dockerfile.Dockerfile(bs)
if err = df.ValidateBaseDockerfile(); err != nil {
return nil, err
}
cachePaths, err := s.cachePathsFromSkylarkValue(cacheVal)
if err != nil {
return nil, err
}
r := &dockerImage{
baseDockerfilePath: baseDockerfilePath,
baseDockerfile: df,
configurationRef: container.NewRefSelector(ref),
fbEntrypoint: entrypoint,
cachePaths: cachePaths,
}
err = s.buildIndex.addImage(r)
if err != nil {
return nil, err
}
fb := &fastBuild{s: s, img: r}
return fb, nil
}
func (s *tiltfileState) cachePathsFromSkylarkValue(val starlark.Value) ([]string, error) {
if val == nil {
return nil, nil
}
cachePaths := starlarkValueOrSequenceToSlice(val)
var ret []string
for _, v := range cachePaths {
str, ok := v.(starlark.String)
if !ok {
return nil, fmt.Errorf("cache param %v is a %T; must be a string", v, v)
}
ret = append(ret, string(str))
}
return ret, nil
}
type fastBuild struct {
s *tiltfileState
img *dockerImage
}
var _ starlark.Value = &fastBuild{}
func (b *fastBuild) String() string {
return fmt.Sprintf("fast_build(%q)", b.img.configurationRef.String())
}
func (b *fastBuild) Type() string {
return "fast_build"
}
func (b *fastBuild) Freeze() {}
func (b *fastBuild) Truth() starlark.Bool {
return true
}
func (b *fastBuild) Hash() (uint32, error) {
return 0, fmt.Errorf("unhashable type: fast_build")
}
const (
fbAddN = "add"
fbRunN = "run"
fbHotReloadN = "hot_reload"
)
func (b *fastBuild) Attr(name string) (starlark.Value, error) {
switch name {
case fbAddN:
return starlark.NewBuiltin(name, b.add), nil
case fbRunN:
return starlark.NewBuiltin(name, b.run), nil
case fbHotReloadN:
return starlark.NewBuiltin(name, b.hotReload), nil
default:
return nil, nil
}
}
func (b *fastBuild) AttrNames() []string {
return []string{fbAddN, fbRunN, fbHotReloadN}
}
func (b *fastBuild) hotReload(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if err := starlark.UnpackArgs(fn.Name(), args, kwargs); err != nil {
return nil, err
}
b.img.hotReload = true
return b, nil
}
func (b *fastBuild) add(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if len(b.img.runs) > 0 {
return nil, fmt.Errorf("fast_build(%q).add() called after .run(); must add all code before runs", b.img.configurationRef.String())
}
var src starlark.Value
var mountPoint string
if err := starlark.UnpackArgs(fn.Name(), args, kwargs, "src", &src, "dest", &mountPoint); err != nil {
return nil, err
}
s := pathSync{}
lp, err := b.s.absPathFromStarlarkValue(thread, src)
if err != nil {
return nil, errors.Wrapf(err, "%s.%s(): invalid type for src (arg 1)", b.String(), fn.Name())
}
s.src = lp
s.mountPoint = mountPoint
b.img.syncs = append(b.img.syncs, s)
return b, nil
}
func (b *fastBuild) run(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var cmd string
var trigger starlark.Value
if err := starlark.UnpackArgs(fn.Name(), args, kwargs, "cmd", &cmd, "trigger?", &trigger); err != nil {
return nil, err
}
var triggers []string
switch trigger := trigger.(type) {
case *starlark.List:
l := trigger.Len()
triggers = make([]string, l)
for i := 0; i < l; i++ {
t := trigger.Index(i)
tStr, isStr := t.(starlark.String)
if !isStr {
return nil, badTypeErr(fn, starlark.String(""), t)
}
triggers[i] = string(tStr)
}
case starlark.String:
triggers = []string{string(trigger)}
}
run := model.ToRun(model.ToShellCmd(cmd))
run = run.WithTriggers(triggers, b.s.absWorkingDir(thread))
b.img.runs = append(b.img.runs, run)
return b, nil
}
type pathSync struct {
src string
mountPoint string
}
func (s *tiltfileState) syncsToDomain(image *dockerImage) []model.Sync {
var result []model.Sync
for _, sync := range image.syncs {
result = append(result, model.Sync{LocalPath: sync.src, ContainerPath: sync.mountPoint})
}
return result
}
func isGitRepoBase(path string) bool {
return ospath.IsDir(filepath.Join(path, ".git"))
}
func reposForPaths(paths []string) []model.LocalGitRepo {
var result []model.LocalGitRepo
repoSet := map[string]bool{}
for _, path := range paths {
isRepoBase := isGitRepoBase(path)
if !isRepoBase || repoSet[path] {
continue
}
repoSet[path] = true
result = append(result, model.LocalGitRepo{
LocalPath: path,
})
}
return result
}
func (s *tiltfileState) reposForImage(image *dockerImage) []model.LocalGitRepo {
var paths []string
for _, sync := range image.syncs {
paths = append(paths, sync.src)
}
paths = append(paths,
image.baseDockerfilePath,
image.dbDockerfilePath,
image.dbBuildPath,
image.tiltfilePath)
return reposForPaths(paths)
}
func (s *tiltfileState) defaultRegistry(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if s.defaultRegistryHost != "" {
return starlark.None, errors.New("default registry already defined")
}
var dr string
if err := starlark.UnpackArgs(fn.Name(), args, kwargs, "name", &dr); err != nil {
return nil, err
}
s.defaultRegistryHost = container.Registry(dr)
return starlark.None, nil
}
func (s *tiltfileState) dockerignoresFromPathsAndContextFilters(paths []string, ignores []string, onlys []string) []model.Dockerignore {
var result []model.Dockerignore
dupeSet := map[string]bool{}
ignoreContents := ignoresToDockerignoreContents(ignores)
onlyContents := onlysToDockerignoreContents(onlys)
for _, path := range paths {
if path == "" || dupeSet[path] {
continue
}
dupeSet[path] = true
if !ospath.IsDir(path) {
continue
}
result = append(result, model.Dockerignore{
LocalPath: path,
Contents: ignoreContents,
})
result = append(result, model.Dockerignore{
LocalPath: path,
Contents: onlyContents,
})
contents, err := s.readFile(filepath.Join(path, ".dockerignore"))
if err != nil {
continue
}
result = append(result, model.Dockerignore{
LocalPath: path,
Contents: string(contents),
})
}
return result
}
func ignoresToDockerignoreContents(ignores []string) string {
var output strings.Builder
for _, ignore := range ignores {
output.WriteString(ignore)
output.WriteString("\n")
}
return output.String()
}
func onlysToDockerignoreContents(onlys []string) string {
if len(onlys) == 0 {
return ""
}
var output strings.Builder
output.WriteString("**\n")
for _, ignore := range onlys {
output.WriteString("!")
output.WriteString(ignore)
output.WriteString("\n")
}
return output.String()
}
func (s *tiltfileState) dockerignoresForImage(image *dockerImage) []model.Dockerignore {
var paths []string
for _, sync := range image.syncs {
paths = append(paths, sync.src)
}
switch image.Type() {
case DockerBuild:
paths = append(paths, image.dbBuildPath)
case CustomBuild:
paths = append(paths, image.customDeps...)
}
return s.dockerignoresFromPathsAndContextFilters(paths, image.ignores, image.onlys)
}
func (s *tiltfileState) checkForFastBuilds(manifests []model.Manifest) {
for _, m := range manifests {
for _, iTarg := range m.ImageTargets {
if !iTarg.AnyFastBuildInfo().Empty() {
s.warnings = append(s.warnings, fastBuildDeprecationWarning)
return
}
}
}
}