-
Notifications
You must be signed in to change notification settings - Fork 298
/
manifest.go
659 lines (560 loc) · 17.5 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
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
package model
import (
"fmt"
"net/url"
"strings"
"github.com/docker/distribution/reference"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/validation/path"
"k8s.io/apimachinery/pkg/labels"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/internal/sliceutils"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
)
// TODO(nick): We should probably get rid of ManifestName completely and just use TargetName everywhere.
type ManifestName string
type ManifestNameSet map[ManifestName]bool
func ManifestNames(names []string) []ManifestName {
mNames := make([]ManifestName, len(names))
for i, name := range names {
mNames[i] = ManifestName(name)
}
return mNames
}
const MainTiltfileManifestName = ManifestName("(Tiltfile)")
func (m ManifestName) String() string { return string(m) }
func (m ManifestName) TargetName() TargetName { return TargetName(m) }
func (m ManifestName) TargetID() TargetID {
return TargetID{
Type: TargetTypeManifest,
Name: m.TargetName(),
}
}
// NOTE: If you modify Manifest, make sure to modify `equalForBuildInvalidation` appropriately
type Manifest struct {
// Properties for all manifests.
Name ManifestName
// Info needed to build an image. (This struct contains details of DockerBuild, CustomBuild... 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, only 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
SourceTiltfile ManifestName
Labels map[string]string
}
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{}
}
func (m Manifest) ImageTargetWithID(id TargetID) ImageTarget {
for _, target := range m.ImageTargets {
if target.ID() == id {
return target
}
}
return ImageTarget{}
}
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) PodReadinessMode() PodReadinessMode {
if k8sTarget, ok := m.DeployTarget.(K8sTarget); ok {
return k8sTarget.PodReadinessMode
}
return PodReadinessNone
}
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 {
switch di := m.DeployTarget.(type) {
case LocalTarget:
return di.Dependencies()
case ImageTarget, K8sTarget, DockerComposeTarget:
// fall through to paths for image targets, below
}
paths := []string{}
for _, iTarget := range m.ImageTargets {
paths = append(paths, iTarget.LocalPaths()...)
}
return sliceutils.DedupedAndSorted(paths)
}
func (m Manifest) WithLabels(labels map[string]string) Manifest {
m.Labels = make(map[string]string)
for k, v := range labels {
m.Labels[k] = v
}
return m
}
func (m Manifest) Validate() error {
if m.Name == "" {
return fmt.Errorf("[validate] manifest missing name: %+v", m)
}
if errs := path.ValidatePathSegmentName(m.Name.String(), false); len(errs) != 0 {
return fmt.Errorf("invalid value %q: %v", m.Name.String(), errs[0])
}
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 (m *Manifest) ClusterName() string {
if m.IsDC() {
return v1alpha1.ClusterNameDocker
}
if m.IsK8s() {
return v1alpha1.ClusterNameDefault
}
return ""
}
// Infer image properties for each image.
func (m *Manifest) InferImageProperties() error {
var deployImageIDs []TargetID
if m.DeployTarget != nil {
deployImageIDs = m.DeployTarget.DependencyIDs()
}
deployImageIDSet := make(map[TargetID]bool, len(deployImageIDs))
for _, depID := range deployImageIDs {
deployImageIDSet[depID] = true
}
for i, iTarget := range m.ImageTargets {
// An image only needs to be pushed if it's used in-cluster.
clusterNeeds := v1alpha1.ClusterImageNeedsBase
if deployImageIDSet[iTarget.ID()] {
clusterNeeds = v1alpha1.ClusterImageNeedsPush
}
iTarget, err := iTarget.inferImageProperties(clusterNeeds, m.ClusterName())
if err != nil {
return fmt.Errorf("manifest %s: %v", m.Name, err)
}
m.ImageTargets[i] = iTarget
}
return nil
}
// Assemble selectors that point to other API objects created by this manifest.
func (m *Manifest) InferLiveUpdateSelectors() error {
dag, err := NewTargetGraph(m.TargetSpecs())
if err != nil {
return err
}
for i, iTarget := range m.ImageTargets {
luSpec := iTarget.LiveUpdateSpec
luName := iTarget.LiveUpdateName
if luName == "" || (len(luSpec.Syncs) == 0 && len(luSpec.Execs) == 0) {
continue
}
if m.IsK8s() {
kSelector := luSpec.Selector.Kubernetes
if kSelector == nil {
kSelector = &v1alpha1.LiveUpdateKubernetesSelector{}
luSpec.Selector.Kubernetes = kSelector
}
if kSelector.ApplyName == "" {
kSelector.ApplyName = m.Name.String()
}
if kSelector.DiscoveryName == "" {
kSelector.DiscoveryName = m.Name.String()
}
// infer a selector from the ImageTarget if a container name
// selector was not specified (currently, this is always the case
// except in some k8s_custom_deploy configurations)
if kSelector.ContainerName == "" {
if iTarget.IsLiveUpdateOnly {
// use the selector (image name) as-is; Tilt isn't building
// this image, so no image name rewriting will occur
kSelector.Image = iTarget.Selector
} else {
// refer to the ImageMap so that the LU reconciler can find
// the true image name after any registry rewriting
kSelector.ImageMapName = iTarget.ImageMapName()
}
}
}
if m.IsDC() {
dcSelector := luSpec.Selector.DockerCompose
if dcSelector == nil {
dcSelector = &v1alpha1.LiveUpdateDockerComposeSelector{}
luSpec.Selector.DockerCompose = dcSelector
}
if dcSelector.Service == "" {
dcSelector.Service = m.Name.String()
}
}
luSpec.Sources = nil
err := dag.VisitTree(iTarget, func(dep TargetSpec) error {
// Relies on the idea that ImageTargets creates
// FileWatches and ImageMaps related to the ImageTarget ID.
id := dep.ID()
fw := id.String()
// LiveUpdateOnly targets do NOT have an associated image map
var imageMap string
if depImg, ok := dep.(ImageTarget); ok && !depImg.IsLiveUpdateOnly {
imageMap = id.Name.String()
}
luSpec.Sources = append(luSpec.Sources, v1alpha1.LiveUpdateSource{
FileWatch: fw,
ImageMap: imageMap,
})
return nil
})
if err != nil {
return err
}
iTarget.LiveUpdateSpec = luSpec
m.ImageTargets[i] = iTarget
}
return nil
}
// Set DisableSource for any pieces of the manifest that are disable-able but not yet in the API
func (m Manifest) WithDisableSource(disableSource *v1alpha1.DisableSource) Manifest {
if lt, ok := m.DeployTarget.(LocalTarget); ok {
lt.ServeCmdDisableSource = disableSource
m.DeployTarget = lt
}
return m
}
// 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.fieldGroupsEqualForBuildInvalidation(new)
return !dockerEq || !k8sEq || !dcEq || !localEq
}
// Compare all fields that might invalidate a build
func (m1 Manifest) fieldGroupsEqualForBuildInvalidation(m2 Manifest) (dockerEq, k8sEq, dcEq, localEq bool) {
dockerEq = equalForBuildInvalidation(m1.ImageTargets, m2.ImageTargets)
dc1 := m1.DockerComposeTarget()
dc2 := m2.DockerComposeTarget()
dcEq = equalForBuildInvalidation(dc1, dc2)
k8s1 := m1.K8sTarget()
k8s2 := m2.K8sTarget()
k8sEq = equalForBuildInvalidation(k8s1, k8s2)
lt1 := m1.LocalTarget()
lt2 := m2.LocalTarget()
localEq = equalForBuildInvalidation(lt1, lt2)
return dockerEq, dcEq, k8sEq, localEq
}
func (m Manifest) ManifestName() ManifestName {
return m.Name
}
func LocalRefSelectorsForManifests(manifests []Manifest, clusters map[string]*v1alpha1.Cluster) []container.RefSelector {
var res []container.RefSelector
for _, m := range manifests {
cluster := clusters[m.ClusterName()]
for _, iTarg := range m.ImageTargets {
refs, err := iTarg.Refs(cluster)
if err != nil {
// silently ignore any invalid image references because this
// logic is only used for Docker pruning, and we can't prune
// something invalid anyway
continue
}
sel := container.NameSelector(refs.LocalRef())
res = append(res, sel)
}
}
return res
}
var _ TargetSpec = Manifest{}
// Self-contained spec for syncing files from local to a container.
//
// Unlike v1alpha1.LiveUpdateSync, all fields of this object must be absolute
// paths.
type Sync struct {
LocalPath string
ContainerPath string
}
// Self-contained spec for running in a container.
//
// Unlike v1alpha1.LiveUpdateExec, all fields of this object must be absolute
// paths.
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 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
// Optional name of the port forward; if given, used as text of the URL
// displayed in the web UI (e.g. <a href="localhost:8888">Debugger</a>)
Name string
// Optional path at the port forward that we link to in UIs
// (useful if e.g. nothing lives at "/" and devs will always
// want "localhost:xxxx/v1/app")
// (Private with getter/setter b/c may be nil.)
path *url.URL
}
func (pf PortForward) PathForAppend() string {
if pf.path == nil {
return ""
}
return strings.TrimPrefix(pf.path.String(), "/")
}
func (pf PortForward) WithPath(p *url.URL) PortForward {
pf.path = p
return pf
}
func MustPortForward(local int, container int, host string, name string, path string) PortForward {
var parsedPath *url.URL
var err error
if path != "" {
parsedPath, err = url.Parse(path)
if err != nil {
panic(err)
}
}
return PortForward{
ContainerPort: container,
LocalPort: local,
Host: host,
Name: name,
path: parsedPath,
}
}
// A link associated with resource; may represent a port forward, an endpoint
// derived from a Service/Ingress/etc., or a URL manually associated with a
// resource via the Tiltfile
type Link struct {
URL *url.URL
// Optional name of the link; if given, used as text of the URL
// displayed in the web UI (e.g. <a href="localhost:8888">Debugger</a>)
Name string
}
func (li Link) URLString() string { return li.URL.String() }
func NewLink(urlStr string, name string) (Link, error) {
u, err := url.Parse(urlStr)
if err != nil {
return Link{}, errors.Wrapf(err, "parsing URL %q", urlStr)
}
return Link{
URL: u,
Name: name,
}, nil
}
func MustNewLink(urlStr string, name string) Link {
li, err := NewLink(urlStr, name)
if err != nil {
panic(err)
}
return li
}
// ByURL implements sort.Interface based on the URL field.
type ByURL []Link
func (lns ByURL) Len() int { return len(lns) }
func (lns ByURL) Less(i, j int) bool { return lns[i].URLString() < lns[j].URLString() }
func (lns ByURL) Swap(i, j int) { lns[i], lns[j] = lns[j], lns[i] }
func PortForwardToLink(pf v1alpha1.Forward) Link {
host := pf.Host
if host == "" {
host = "localhost"
}
u := fmt.Sprintf("http://%s:%d/%s", host, pf.LocalPort, strings.TrimPrefix(pf.Path, "/"))
// We panic on error here because we provide the URL format ourselves,
// so if it's bad, something is very wrong.
return MustNewLink(u, pf.Name)
}
func LinksToURLStrings(lns []Link) []string {
res := make([]string, len(lns))
for i, ln := range lns {
res[i] = ln.URLString()
}
return res
}
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 portForwardPathAllowUnexported = cmp.AllowUnexported(PortForward{})
var ignoreCustomBuildDepsField = cmpopts.IgnoreFields(CustomBuild{}, "Deps")
var ignoreLocalTargetDepsField = cmpopts.IgnoreFields(LocalTarget{}, "Deps")
var ignoreDockerBuildCacheFrom = cmpopts.IgnoreFields(DockerBuild{}, "CacheFrom")
var ignoreLabels = cmpopts.IgnoreFields(Manifest{}, "Labels")
var ignoreDockerComposeProject = cmpopts.IgnoreFields(v1alpha1.DockerComposeServiceSpec{}, "Project")
// ignoreLinks ignores user-defined links for the purpose of build invalidation
//
// This is done both because they don't actually invalidate the build AND because url.URL is not directly comparable
// in all cases (e.g. a URL with a user@ value will result in url.URL->User being populated which has unexported fields).
var ignoreLinks = cmpopts.IgnoreTypes(Link{})
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()
})
// Determine whether interfaces x and y are equal, excluding fields that don't invalidate a build.
func equalForBuildInvalidation(x, y interface{}) bool {
return cmp.Equal(x, y,
cmpopts.EquateEmpty(),
imageTargetAllowUnexported,
dcTargetAllowUnexported,
labelRequirementAllowUnexported,
k8sTargetAllowUnexported,
localTargetAllowUnexported,
selectorAllowUnexported,
refSetAllowUnexported,
registryAllowUnexported,
portForwardPathAllowUnexported,
dockerRefEqual,
// deps changes don't invalidate a build, so don't compare fields used only for deps
ignoreCustomBuildDepsField,
ignoreLocalTargetDepsField,
// DockerBuild.CacheFrom doesn't invalidate a build (b/c it affects HOW we build but
// shouldn't affect the result of the build), so don't compare these fields
ignoreDockerBuildCacheFrom,
// user-added labels don't invalidate a build
ignoreLabels,
// user-added links don't invalidate a build
ignoreLinks,
// We don't want a change to the DockerCompose Project to invalidate
// all individual services. We track the service-specific YAML with
// a seprate ServiceYAML field.
ignoreDockerComposeProject,
)
}