-
Notifications
You must be signed in to change notification settings - Fork 288
/
manifest.go
494 lines (410 loc) · 11.7 KB
/
manifest.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
package model
import (
"fmt"
"runtime"
"strings"
"k8s.io/apimachinery/pkg/labels"
"github.com/docker/distribution/reference"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/internal/sliceutils"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
// TODO(nick): We should probably get rid of ManifestName completely and just use TargetName everywhere.
type ManifestName string
const TiltfileManifestName = ManifestName("(Tiltfile)")
func (m ManifestName) String() string { return string(m) }
func (m ManifestName) TargetName() TargetName { return TargetName(m) }
// NOTE: If you modify Manifest, make sure to modify `Manifest.Equal` appropriately
type Manifest struct {
// Properties for all manifests.
Name ManifestName
// Info needed to build an image. (This struct contains details of DockerBuild, FastBuild... etc.)
ImageTargets []ImageTarget
// Info needed to deploy. Can be k8s yaml, docker compose, etc.
deployTarget TargetSpec
// How updates are triggered:
// - automatically, when we detect a change
// - manually, when the user tells us to
TriggerMode TriggerMode
// The resource in this manifest will not be built until all of its dependencies have been
// ready at least once.
ResourceDependencies []ManifestName
}
func (m Manifest) ID() TargetID {
return TargetID{
Type: TargetTypeManifest,
Name: m.Name.TargetName(),
}
}
func (m Manifest) DependencyIDs() []TargetID {
result := []TargetID{}
for _, iTarget := range m.ImageTargets {
result = append(result, iTarget.ID())
}
if !m.deployTarget.ID().Empty() {
result = append(result, m.deployTarget.ID())
}
return result
}
// A map from each target id to the target IDs that depend on it.
func (m Manifest) ReverseDependencyIDs() map[TargetID][]TargetID {
result := make(map[TargetID][]TargetID)
for _, iTarget := range m.ImageTargets {
for _, depID := range iTarget.DependencyIDs() {
result[depID] = append(result[depID], iTarget.ID())
}
}
if !m.deployTarget.ID().Empty() {
for _, depID := range m.deployTarget.DependencyIDs() {
result[depID] = append(result[depID], m.deployTarget.ID())
}
}
return result
}
func (m Manifest) WithImageTarget(iTarget ImageTarget) Manifest {
m.ImageTargets = []ImageTarget{iTarget}
return m
}
func (m Manifest) WithImageTargets(iTargets []ImageTarget) Manifest {
m.ImageTargets = append([]ImageTarget{}, iTargets...)
return m
}
func (m Manifest) ImageTargetAt(i int) ImageTarget {
if i < len(m.ImageTargets) {
return m.ImageTargets[i]
}
return ImageTarget{}
}
type DockerBuildArgs map[string]string
func (m Manifest) LocalTarget() LocalTarget {
ret, _ := m.deployTarget.(LocalTarget)
return ret
}
func (m Manifest) IsLocal() bool {
_, ok := m.deployTarget.(LocalTarget)
return ok
}
func (m Manifest) DockerComposeTarget() DockerComposeTarget {
ret, _ := m.deployTarget.(DockerComposeTarget)
return ret
}
func (m Manifest) IsDC() bool {
_, ok := m.deployTarget.(DockerComposeTarget)
return ok
}
func (m Manifest) K8sTarget() K8sTarget {
ret, _ := m.deployTarget.(K8sTarget)
return ret
}
func (m Manifest) IsK8s() bool {
_, ok := m.deployTarget.(K8sTarget)
return ok
}
func (m Manifest) NonWorkloadManifest() bool {
if k8sTarget, ok := m.deployTarget.(K8sTarget); ok {
return k8sTarget.NonWorkload
}
return false
}
func (m Manifest) DeployTarget() TargetSpec {
return m.deployTarget
}
func (m Manifest) WithDeployTarget(t TargetSpec) Manifest {
switch typedTarget := t.(type) {
case K8sTarget:
typedTarget.Name = m.Name.TargetName()
t = typedTarget
case DockerComposeTarget:
typedTarget.Name = m.Name.TargetName()
t = typedTarget
}
m.deployTarget = t
return m
}
func (m Manifest) WithTriggerMode(mode TriggerMode) Manifest {
m.TriggerMode = mode
return m
}
func (m Manifest) TargetIDSet() map[TargetID]bool {
result := make(map[TargetID]bool)
specs := m.TargetSpecs()
for _, spec := range specs {
result[spec.ID()] = true
}
return result
}
func (m Manifest) TargetSpecs() []TargetSpec {
result := []TargetSpec{}
for _, t := range m.ImageTargets {
result = append(result, t)
}
if m.deployTarget != nil {
result = append(result, m.deployTarget)
}
return result
}
func (m Manifest) IsImageDeployed(iTarget ImageTarget) bool {
id := iTarget.ID()
for _, depID := range m.DeployTarget().DependencyIDs() {
if depID == id {
return true
}
}
return false
}
func (m Manifest) LocalPaths() []string {
// TODO(matt?) DC syncs should probably stored somewhere more consistent with Docker/Fast Build
switch di := m.deployTarget.(type) {
case DockerComposeTarget:
return di.LocalPaths()
case LocalTarget:
return di.Dependencies()
case ImageTarget, K8sTarget:
paths := []string{}
for _, iTarget := range m.ImageTargets {
paths = append(paths, iTarget.LocalPaths()...)
}
return sliceutils.DedupedAndSorted(paths)
}
panic(fmt.Sprintf("Unknown deploy target type (%T) while trying to get LocalPaths", m.deployTarget))
}
func (m Manifest) Validate() error {
if m.Name == "" {
return fmt.Errorf("[validate] manifest missing name: %+v", m)
}
for _, iTarget := range m.ImageTargets {
err := iTarget.Validate()
if err != nil {
return err
}
}
if m.deployTarget != nil {
err := m.deployTarget.Validate()
if err != nil {
return err
}
}
return nil
}
func (m1 Manifest) Equal(m2 Manifest) bool {
primitivesEq, dockerEq, k8sEq, dcEq, localEq, depsEq := m1.fieldGroupsEqual(m2)
return primitivesEq && dockerEq && k8sEq && dcEq && localEq && depsEq
}
// ChangesInvalidateBuild checks whether the changes from old => new manifest
// invalidate our build of the old one; i.e. if we're replacing `old` with `new`,
// should we perform a full rebuild?
func ChangesInvalidateBuild(old, new Manifest) bool {
_, dockerEq, k8sEq, dcEq, localEq, _ := old.fieldGroupsEqual(new)
// We only need to update for this manifest if any of the field-groups
// affecting build+deploy have changed (i.e. a change in primitives doesn't matter)
return !dockerEq || !k8sEq || !dcEq || !localEq
}
func (m1 Manifest) fieldGroupsEqual(m2 Manifest) (primitivesEq, dockerEq, k8sEq, dcEq, localEq, depsEq bool) {
primitivesEq = m1.Name == m2.Name && m1.TriggerMode == m2.TriggerMode
dockerEq = DeepEqual(m1.ImageTargets, m2.ImageTargets)
dc1 := m1.DockerComposeTarget()
dc2 := m2.DockerComposeTarget()
dcEq = DeepEqual(dc1, dc2)
k8s1 := m1.K8sTarget()
k8s2 := m2.K8sTarget()
k8sEq = DeepEqual(k8s1, k8s2)
lt1 := m1.LocalTarget()
lt2 := m2.LocalTarget()
localEq = DeepEqual(lt1, lt2)
depsEq = DeepEqual(m1.ResourceDependencies, m2.ResourceDependencies)
return primitivesEq, dockerEq, dcEq, k8sEq, localEq, depsEq
}
func (m Manifest) ManifestName() ManifestName {
return m.Name
}
func (m Manifest) Empty() bool {
return m.Equal(Manifest{})
}
func LocalRefSelectorsForManifests(manifests []Manifest) []container.RefSelector {
var res []container.RefSelector
for _, m := range manifests {
for _, iTarg := range m.ImageTargets {
sel := container.NameSelector(iTarg.Refs.LocalRef()).WithNameMatch()
res = append(res, sel)
}
}
return res
}
var _ TargetSpec = Manifest{}
type Sync struct {
LocalPath string
ContainerPath string
}
type Dockerignore struct {
// The path to evaluate the dockerignore contents relative to
LocalPath string
Contents string
}
type LocalGitRepo struct {
LocalPath string
}
func (LocalGitRepo) IsRepo() {}
type Run struct {
// Required. The command to run.
Cmd Cmd
// Optional. If not specified, this command runs on every change.
// If specified, we only run the Cmd if the changed file matches a trigger.
Triggers PathSet
}
func (r Run) WithTriggers(paths []string, baseDir string) Run {
if len(paths) > 0 {
r.Triggers = PathSet{
Paths: paths,
BaseDirectory: baseDir,
}
} else {
r.Triggers = PathSet{}
}
return r
}
type Cmd struct {
Argv []string
}
func (c Cmd) IsShellStandardForm() bool {
return len(c.Argv) == 3 && c.Argv[0] == "sh" && c.Argv[1] == "-c" && !strings.Contains(c.Argv[2], "\n")
}
func (c Cmd) IsWindowsStandardForm() bool {
return len(c.Argv) == 4 && c.Argv[0] == "cmd" && c.Argv[1] == "/S" && c.Argv[2] == "/C"
}
// Get the script when the shell is in standard form.
// Panics if the command is not in shell standard form.
func (c Cmd) ShellStandardScript() string {
if !c.IsShellStandardForm() {
panic(fmt.Sprintf("Not in shell standard form: %+v", c))
}
return c.Argv[2]
}
func (c Cmd) EntrypointStr() string {
if c.IsShellStandardForm() {
return fmt.Sprintf("ENTRYPOINT %s", c.Argv[2])
}
quoted := make([]string, len(c.Argv))
for i, arg := range c.Argv {
quoted[i] = fmt.Sprintf("%q", arg)
}
return fmt.Sprintf("ENTRYPOINT [%s]", strings.Join(quoted, ", "))
}
func (c Cmd) RunStr() string {
if c.IsShellStandardForm() {
return fmt.Sprintf("RUN %s", c.Argv[2])
}
quoted := make([]string, len(c.Argv))
for i, arg := range c.Argv {
quoted[i] = fmt.Sprintf("%q", arg)
}
return fmt.Sprintf("RUN [%s]", strings.Join(quoted, ", "))
}
func (c Cmd) String() string {
if c.IsShellStandardForm() {
return c.Argv[2]
}
if c.IsWindowsStandardForm() {
return c.Argv[3]
}
quoted := make([]string, len(c.Argv))
for i, arg := range c.Argv {
if strings.Contains(arg, " ") {
quoted[i] = fmt.Sprintf("%q", arg)
} else {
quoted[i] = arg
}
}
return strings.Join(quoted, " ")
}
func (c Cmd) Empty() bool {
return len(c.Argv) == 0
}
// Create a shell command for running on the Host OS
func ToHostCmd(cmd string) Cmd {
if cmd == "" {
return Cmd{}
}
if runtime.GOOS == "windows" {
return ToBatCmd(cmd)
}
return ToUnixCmd(cmd)
}
// 🦇🦇🦇
// Named in honor of Bazel
// https://docs.bazel.build/versions/master/be/general.html#genrule.cmd_bat
func ToBatCmd(cmd string) Cmd {
if cmd == "" {
return Cmd{}
}
// from https://docs.docker.com/engine/reference/builder/#run
return Cmd{Argv: []string{"cmd", "/S", "/C", cmd}}
}
func ToUnixCmd(cmd string) Cmd {
if cmd == "" {
return Cmd{}
}
return Cmd{Argv: []string{"sh", "-c", cmd}}
}
func ToUnixCmds(cmds []string) []Cmd {
res := make([]Cmd, len(cmds))
for i, cmd := range cmds {
res[i] = ToUnixCmd(cmd)
}
return res
}
func ToRun(cmd Cmd) Run {
return Run{Cmd: cmd}
}
func ToRuns(cmds []Cmd) []Run {
res := make([]Run, len(cmds))
for i, cmd := range cmds {
res[i] = ToRun(cmd)
}
return res
}
type PortForward struct {
// The port to connect to inside the deployed container.
// If 0, we will connect to the first containerPort.
ContainerPort int
// The port to expose on the current machine.
LocalPort int
// Optional host to bind to on the current machine (localhost by default)
Host string
}
var imageTargetAllowUnexported = cmp.AllowUnexported(ImageTarget{})
var dcTargetAllowUnexported = cmp.AllowUnexported(DockerComposeTarget{})
var labelRequirementAllowUnexported = cmp.AllowUnexported(labels.Requirement{})
var k8sTargetAllowUnexported = cmp.AllowUnexported(K8sTarget{})
var localTargetAllowUnexported = cmp.AllowUnexported(LocalTarget{})
var selectorAllowUnexported = cmp.AllowUnexported(container.RefSelector{})
var refSetAllowUnexported = cmp.AllowUnexported(container.RefSet{})
var registryAllowUnexported = cmp.AllowUnexported(container.Registry{})
var dockerRefEqual = cmp.Comparer(func(a, b reference.Named) bool {
aNil := a == nil
bNil := b == nil
if aNil && bNil {
return true
}
if aNil != bNil {
return false
}
return a.String() == b.String()
})
var imageLocatorEqual = cmp.Comparer(func(a, b K8sImageLocator) bool {
return a.EqualsImageLocator(b)
})
func DeepEqual(x, y interface{}) bool {
return cmp.Equal(x, y,
cmpopts.EquateEmpty(),
imageTargetAllowUnexported,
dcTargetAllowUnexported,
labelRequirementAllowUnexported,
k8sTargetAllowUnexported,
localTargetAllowUnexported,
selectorAllowUnexported,
refSetAllowUnexported,
registryAllowUnexported,
dockerRefEqual,
imageLocatorEqual)
}