-
Notifications
You must be signed in to change notification settings - Fork 290
/
engine_state.go
547 lines (457 loc) · 14.9 KB
/
engine_state.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
package store
import (
"fmt"
"net/url"
"os"
"path/filepath"
"sort"
"time"
"github.com/docker/distribution/reference"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/dockercompose"
"github.com/windmilleng/tilt/internal/hud/view"
"github.com/windmilleng/tilt/internal/k8s"
"github.com/windmilleng/tilt/internal/model"
"github.com/windmilleng/tilt/internal/ospath"
v1 "k8s.io/api/core/v1"
)
const emptyTiltfileMsg = "Looks like you don't have any docker builds or services defined in your Tiltfile! Check out https://docs.tilt.build/ to get started."
type EngineState struct {
// saved so that we can render in order
ManifestDefinitionOrder []model.ManifestName
ManifestStates map[model.ManifestName]*ManifestState
CurrentlyBuilding model.ManifestName
WatchMounts bool
// How many builds were queued on startup (i.e., how many manifests there were)
InitialBuildCount int
// How many builds have been completed (pass or fail) since starting tilt
CompletedBuildCount int
// For synchronizing BuildController so that it's only
// doing one action at a time. In the future, we might
// want to allow it to parallelize builds better, but that
// would require better tools for triaging output to different streams.
BuildControllerActionCount int
PermanentError error
// The user has indicated they want to exit
UserExited bool
// The full log stream for tilt. This might deserve gc or file storage at some point.
Log []byte `testdiff:"ignore"`
// GlobalYAML is a special manifest that has no images, but has dependencies
// and a bunch of YAML that is deployed when those dependencies change.
// TODO(dmiller) in the future we may have many of these manifests, but for now it's a special case.
GlobalYAML model.YAMLManifest
GlobalYAMLState *YAMLManifestState
TiltfilePath string
ConfigFiles []string
PendingConfigFileChanges map[string]bool
// InitManifests is the list of manifest names that we were told to init from the CLI.
InitManifests []model.ManifestName
LastTiltfileBuild model.BuildStatus
TriggerMode model.TriggerMode
TriggerQueue []model.ManifestName
IsProfiling bool
}
func (e EngineState) RelativeTiltfilePath() (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Rel(wd, e.TiltfilePath)
}
func (e EngineState) IsEmpty() bool {
return len(e.ManifestStates) == 0 && e.GlobalYAML.Empty()
}
func (e EngineState) LastTiltfileError() error {
return e.LastTiltfileBuild.Error
}
type ResourceState interface {
ResourceState()
}
type ManifestState struct {
Manifest model.Manifest
// k8s-specific state
PodSet PodSet
LBs map[k8s.ServiceName]*url.URL
// State of the running resource -- specific to type (e.g. k8s, docker-compose, etc.)
// TODO(maia): implement for k8s
ResourceState ResourceState
// Store the times of all the pending changes,
// so we can prioritize the oldest one first.
PendingFileChanges map[string]time.Time
PendingManifestChange time.Time
// The current build
CurrentBuild model.BuildStatus
LastSuccessfulResult BuildResult
LastSuccessfulDeployTime time.Time
// The last `BuildHistoryLimit` builds. The most recent build is first in the slice.
BuildHistory []model.BuildStatus
// If the pod isn't running this container then it's possible we're running stale code
ExpectedContainerID container.ID
// We detected stale code and are currently doing an image build
NeedsRebuildFromCrash bool
// If a pod had to be killed because it was crashing, we keep the old log
// around for a little while so we can show it in the UX.
CrashLog string
}
func NewState() *EngineState {
ret := &EngineState{}
ret.ManifestStates = make(map[model.ManifestName]*ManifestState)
ret.PendingConfigFileChanges = make(map[string]bool)
return ret
}
func NewManifestState(manifest model.Manifest) *ManifestState {
return &ManifestState{
Manifest: manifest,
PendingFileChanges: make(map[string]time.Time),
LBs: make(map[k8s.ServiceName]*url.URL),
}
}
func (ms *ManifestState) DCResourceState() dockercompose.State {
ret, _ := ms.ResourceState.(dockercompose.State)
return ret
}
func (ms *ManifestState) IsDC() bool {
_, ok := ms.ResourceState.(dockercompose.State)
return ok
}
func (ms *ManifestState) LastBuild() model.BuildStatus {
if len(ms.BuildHistory) == 0 {
return model.BuildStatus{}
}
return ms.BuildHistory[0]
}
func (ms *ManifestState) AddCompletedBuild(bs model.BuildStatus) {
ms.BuildHistory = append([]model.BuildStatus{bs}, ms.BuildHistory...)
if len(ms.BuildHistory) > model.BuildHistoryLimit {
ms.BuildHistory = ms.BuildHistory[:model.BuildHistoryLimit]
}
}
func (ms *ManifestState) StartedFirstBuild() bool {
return !ms.CurrentBuild.Empty() || len(ms.BuildHistory) > 0
}
func (ms *ManifestState) MostRecentPod() Pod {
return ms.PodSet.MostRecentPod()
}
func (ms *ManifestState) NextBuildReason() model.BuildReason {
reason := model.BuildReasonNone
if len(ms.PendingFileChanges) > 0 {
reason = reason.With(model.BuildReasonFlagMountFiles)
}
if !ms.PendingManifestChange.IsZero() {
reason = reason.With(model.BuildReasonFlagConfig)
}
if !ms.StartedFirstBuild() {
reason = reason.With(model.BuildReasonFlagInit)
}
if ms.NeedsRebuildFromCrash {
reason = reason.With(model.BuildReasonFlagCrash)
}
return reason
}
// Whether a change at the given time should trigger a build.
// Used to determine if changes to mount files or config files
// should kick off a new build.
func (ms *ManifestState) IsPendingTime(t time.Time) bool {
return !t.IsZero() && t.After(ms.LastBuild().StartTime)
}
// Whether changes have been made to this Manifest's mount files
// or config since the last build.
//
// Returns:
// bool: whether changes have been made
// Time: the time of the earliest change
func (ms *ManifestState) HasPendingChanges() (bool, time.Time) {
return ms.HasPendingChangesBefore(time.Now())
}
// Like HasPendingChanges, but relative to a particular time.
func (ms *ManifestState) HasPendingChangesBefore(highWaterMark time.Time) (bool, time.Time) {
ok := false
earliest := highWaterMark
t := ms.PendingManifestChange
if t.Before(earliest) && ms.IsPendingTime(t) {
ok = true
earliest = t
}
for _, t := range ms.PendingFileChanges {
if t.Before(earliest) && ms.IsPendingTime(t) {
ok = true
earliest = t
}
}
if !ok {
return ok, time.Time{}
}
return ok, earliest
}
type YAMLManifestState struct {
HasBeenDeployed bool
CurrentApplyStartTime time.Time
LastError error
LastApplyFinishTime time.Time
LastSuccessfulApplyTime time.Time
LastApplyStartTime time.Time
}
func NewYAMLManifestState() *YAMLManifestState {
return &YAMLManifestState{}
}
type PodSet struct {
Pods map[k8s.PodID]*Pod
ImageID reference.NamedTagged
}
func NewPodSet(pods ...Pod) PodSet {
podMap := make(map[k8s.PodID]*Pod, len(pods))
for _, pod := range pods {
p := pod
podMap[p.PodID] = &p
}
return PodSet{
Pods: podMap,
}
}
func (s PodSet) Len() int {
return len(s.Pods)
}
func (s PodSet) ContainsID(id k8s.PodID) bool {
_, ok := s.Pods[id]
return ok
}
func (s PodSet) PodList() []Pod {
pods := make([]Pod, 0, len(s.Pods))
for _, pod := range s.Pods {
pods = append(pods, *pod)
}
return pods
}
// Get the "most recent pod" from the PodSet.
// For most users, we believe there will be only one pod per manifest.
// So most of this time, this will return the only pod.
// And in other cases, it will return a reasonable, consistent default.
func (s PodSet) MostRecentPod() Pod {
bestPod := Pod{}
found := false
for _, v := range s.Pods {
if !found || v.isAfter(bestPod) {
bestPod = *v
found = true
}
}
return bestPod
}
type Pod struct {
PodID k8s.PodID
Namespace k8s.Namespace
StartedAt time.Time
Status string
Phase v1.PodPhase
// Set when we get ready to replace a pod. We may do the update in-place.
UpdateStartTime time.Time
// If a pod is being deleted, Kubernetes marks it as Running
// until it actually gets removed.
Deleting bool
// The log for the previously active pod, if any
PreRestartLog []byte `testdiff:"ignore"`
// The log for the currently active pod, if any
CurrentLog []byte `testdiff:"ignore"`
// Corresponds to the deployed container.
ContainerName container.Name
ContainerID container.ID
ContainerPorts []int32
ContainerReady bool
// We want to show the user # of restarts since pod has been running current code,
// i.e. OldRestarts - Total Restarts
ContainerRestarts int
OldRestarts int // # times the pod restarted when it was running old code
}
func (p Pod) Empty() bool {
return p.PodID == ""
}
// A stable sort order for pods.
func (p Pod) isAfter(p2 Pod) bool {
if p.StartedAt.After(p2.StartedAt) {
return true
} else if p2.StartedAt.After(p.StartedAt) {
return false
}
return p.PodID > p2.PodID
}
// attempting to include the most recent crash, but no preceding crashes
// (e.g., we don't want to show the same panic 20x in a crash loop)
// if the current pod has crashed, then just print the current pod
// if the current pod is live, print the current pod plus the last pod
func (p Pod) Log() string {
var podLog string
// if the most recent pod is up, then we want the log from the last run (if any), since it crashed
if p.ContainerReady {
podLog = string(p.PreRestartLog) + string(p.CurrentLog)
} else {
// otherwise, the most recent pod has the crash itself, so just return itself
podLog = string(p.CurrentLog)
}
return podLog
}
func shortenFile(baseDirs []string, f string) string {
ret := f
for _, baseDir := range baseDirs {
short, isChild := ospath.Child(baseDir, f)
if isChild && len(short) < len(ret) {
ret = short
}
}
return ret
}
// for each filename in `files`, trims the longest appropriate basedir prefix off the front
func shortenFileList(baseDirs []string, files []string) []string {
baseDirs = append([]string{}, baseDirs...)
var ret []string
for _, f := range files {
ret = append(ret, shortenFile(baseDirs, f))
}
return ret
}
// Returns the manifests in order.
func (s EngineState) Manifests() []model.Manifest {
result := make([]model.Manifest, 0)
for _, name := range s.ManifestDefinitionOrder {
ms := s.ManifestStates[name]
result = append(result, ms.Manifest)
}
return result
}
func ManifestStateEndpoints(ms *ManifestState) (endpoints []string) {
defer func() {
sort.Strings(endpoints)
}()
// If the user specified port-forwards in the Tiltfile, we
// assume that's what they want to see in the UI
portForwards := ms.Manifest.K8sInfo().PortForwards
if len(portForwards) > 0 {
for _, pf := range portForwards {
endpoints = append(endpoints, fmt.Sprintf("http://localhost:%d/", pf.LocalPort))
}
return endpoints
}
for _, u := range ms.LBs {
if u != nil {
endpoints = append(endpoints, u.String())
}
}
return endpoints
}
func StateToView(s EngineState) view.View {
ret := view.View{
TriggerMode: s.TriggerMode,
IsProfiling: s.IsProfiling,
}
for _, name := range s.ManifestDefinitionOrder {
ms := s.ManifestStates[name]
var absWatchDirs []string
var absWatchPaths []string
for _, p := range ms.Manifest.LocalPaths() {
fi, err := os.Stat(p)
if err == nil && !fi.IsDir() {
absWatchPaths = append(absWatchPaths, p)
} else {
absWatchDirs = append(absWatchDirs, p)
}
}
absWatchPaths = append(absWatchPaths, s.TiltfilePath)
relWatchDirs := ospath.TryAsCwdChildren(absWatchDirs)
relWatchPaths := ospath.TryAsCwdChildren(absWatchPaths)
var pendingBuildEdits []string
for f := range ms.PendingFileChanges {
pendingBuildEdits = append(pendingBuildEdits, f)
}
pendingBuildEdits = shortenFileList(absWatchDirs, pendingBuildEdits)
buildHistory := append([]model.BuildStatus{}, ms.BuildHistory...)
for i, build := range buildHistory {
build.Edits = shortenFileList(absWatchDirs, build.Edits)
buildHistory[i] = build
}
currentBuild := ms.CurrentBuild
currentBuild.Edits = shortenFileList(absWatchDirs, ms.CurrentBuild.Edits)
// Sort the strings to make the outputs deterministic.
sort.Strings(pendingBuildEdits)
endpoints := ManifestStateEndpoints(ms)
// NOTE(nick): Right now, the UX is designed to show the output exactly one
// pod. A better UI might summarize the pods in other ways (e.g., show the
// "most interesting" pod that's crash looping, or show logs from all pods
// at once).
_, pendingBuildSince := ms.HasPendingChanges()
r := view.Resource{
Name: name,
DirectoriesWatched: relWatchDirs,
PathsWatched: relWatchPaths,
LastDeployTime: ms.LastSuccessfulDeployTime,
BuildHistory: buildHistory,
PendingBuildEdits: pendingBuildEdits,
PendingBuildSince: pendingBuildSince,
PendingBuildReason: ms.NextBuildReason(),
CurrentBuild: currentBuild,
CrashLog: ms.CrashLog,
Endpoints: endpoints,
ResourceInfo: resourceInfoView(ms),
}
ret.Resources = append(ret.Resources, r)
}
if s.GlobalYAML.K8sYAML() != "" {
var absWatches []string
for _, p := range s.GlobalYAML.Dependencies() {
absWatches = append(absWatches, p)
}
relWatches := ospath.TryAsCwdChildren(absWatches)
r := view.Resource{
Name: s.GlobalYAML.ManifestName(),
DirectoriesWatched: relWatches,
CurrentBuild: model.BuildStatus{StartTime: s.GlobalYAMLState.CurrentApplyStartTime},
BuildHistory: []model.BuildStatus{
model.BuildStatus{
StartTime: s.GlobalYAMLState.LastApplyStartTime,
FinishTime: s.GlobalYAMLState.LastApplyFinishTime,
Error: s.GlobalYAMLState.LastError,
},
},
LastDeployTime: s.GlobalYAMLState.LastSuccessfulApplyTime,
ResourceInfo: view.YAMLResourceInfo{
K8sResources: s.GlobalYAML.Resources(),
},
}
ret.Resources = append(ret.Resources, r)
}
ret.Log = string(s.Log)
if !s.LastTiltfileBuild.Empty() {
err := s.LastTiltfileBuild.Error
if err == nil && s.IsEmpty() {
ret.TiltfileErrorMessage = emptyTiltfileMsg
} else if err != nil {
ret.TiltfileErrorMessage = err.Error()
}
}
return ret
}
func resourceInfoView(ms *ManifestState) view.ResourceInfoView {
if dcState, ok := ms.ResourceState.(dockercompose.State); ok {
return view.NewDCResourceInfo(ms.Manifest.DCInfo().ConfigPath, dcState.Status, dcState.Log(), dcState.StartTime)
} else {
pod := ms.MostRecentPod()
return view.K8SResourceInfo{
PodName: pod.PodID.String(),
PodCreationTime: pod.StartedAt,
PodUpdateStartTime: pod.UpdateStartTime,
PodStatus: pod.Status,
PodRestarts: pod.ContainerRestarts - pod.OldRestarts,
PodLog: pod.Log(),
}
}
}
// DockerComposeConfigPath returns the path to the docker-compose yaml file of any
// docker-compose manifests on this EngineState.
// NOTE(maia): current assumption is only one d-c.yaml per run, so we take the
// path from the first d-c manifest we see.
func (s EngineState) DockerComposeConfigPath() string {
for _, ms := range s.ManifestStates {
if ms.Manifest.IsDC() {
return ms.Manifest.DCInfo().ConfigPath
}
}
return ""
}