-
Notifications
You must be signed in to change notification settings - Fork 290
/
upper.go
955 lines (808 loc) · 27.4 KB
/
upper.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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
package engine
import (
"context"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/windmilleng/tilt/internal/sail/client"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/dockercompose"
"github.com/windmilleng/tilt/internal/hud"
"github.com/windmilleng/tilt/internal/hud/view"
"github.com/windmilleng/tilt/internal/k8s"
"github.com/windmilleng/tilt/internal/logger"
"github.com/windmilleng/tilt/internal/model"
"github.com/windmilleng/tilt/internal/sliceutils"
"github.com/windmilleng/tilt/internal/store"
"github.com/windmilleng/tilt/internal/synclet/sidecar"
"github.com/windmilleng/tilt/internal/watch"
)
// When we see a file change, wait this long to see if any other files have changed, and bundle all changes together.
// 200ms is not the result of any kind of research or experimentation
// it might end up being a significant part of deployment delay, if we get the total latency <2s
// it might also be long enough that it misses some changes if the user has some operation involving a large file
// (e.g., a binary dependency in git), but that's hopefully less of a problem since we'd get it in the next build
const watchBufferMinRestInMs = 200
// When waiting for a `watchBufferDurationInMs`-long break in file modifications to aggregate notifications,
// if we haven't seen a break by the time `watchBufferMaxTimeInMs` has passed, just send off whatever we've got
const watchBufferMaxTimeInMs = 10000
var watchBufferMinRestDuration = watchBufferMinRestInMs * time.Millisecond
var watchBufferMaxDuration = watchBufferMaxTimeInMs * time.Millisecond
// When we kick off a build because some files changed, only print the first `maxChangedFilesToPrint`
const maxChangedFilesToPrint = 5
// TODO(nick): maybe this should be called 'BuildEngine' or something?
// Upper seems like a poor and undescriptive name.
type Upper struct {
store *store.Store
}
type FsWatcherMaker func() (watch.Notify, error)
type ServiceWatcherMaker func(context.Context, *store.Store) error
type PodWatcherMaker func(context.Context, *store.Store) error
type timerMaker func(d time.Duration) <-chan time.Time
func ProvideFsWatcherMaker() FsWatcherMaker {
return func() (watch.Notify, error) {
return watch.NewWatcher()
}
}
func ProvideTimerMaker() timerMaker {
return func(t time.Duration) <-chan time.Time {
return time.After(t)
}
}
func NewUpper(ctx context.Context, st *store.Store, subs []store.Subscriber) Upper {
// There's not really a good reason to add all the subscribers
// in NewUpper(), but it's as good a place as any.
for _, sub := range subs {
st.AddSubscriber(ctx, sub)
}
return Upper{
store: st,
}
}
func (u Upper) Dispatch(action store.Action) {
u.store.Dispatch(action)
}
func (u Upper) Start(ctx context.Context, args []string, b model.TiltBuild, watch bool, triggerMode model.TriggerMode, fileName string, useActionWriter bool, enableSail bool) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "Start")
defer span.Finish()
startTime := time.Now()
absTfPath, err := filepath.Abs(fileName)
if err != nil {
return err
}
var manifestNames []model.ManifestName
matching := map[string]bool{}
for _, arg := range args {
manifestNames = append(manifestNames, model.ManifestName(arg))
matching[arg] = true
}
configFiles := []string{absTfPath}
return u.Init(ctx, InitAction{
WatchFiles: watch,
TiltfilePath: absTfPath,
ConfigFiles: configFiles,
InitManifests: manifestNames,
TriggerMode: triggerMode,
TiltBuild: b,
StartTime: startTime,
FinishTime: time.Now(),
ExecuteTiltfile: false,
EnableSail: enableSail,
})
}
func (u Upper) Init(ctx context.Context, action InitAction) error {
u.store.Dispatch(action)
return u.store.Loop(ctx)
}
var UpperReducer = store.Reducer(func(ctx context.Context, state *store.EngineState, action store.Action) {
var err error
switch action := action.(type) {
case InitAction:
err = handleInitAction(ctx, state, action)
case store.ErrorAction:
err = action.Error
case hud.ExitAction:
handleExitAction(state, action)
case targetFilesChangedAction:
handleFSEvent(ctx, state, action)
case PodChangeAction:
handlePodChangeAction(ctx, state, action.Pod)
case ServiceChangeAction:
handleServiceEvent(ctx, state, action)
case PodLogAction:
handlePodLogAction(state, action)
case BuildLogAction:
handleBuildLogAction(state, action)
case BuildCompleteAction:
err = handleBuildCompleted(ctx, state, action)
case BuildStartedAction:
handleBuildStarted(ctx, state, action)
case DeployIDAction:
handleDeployIDAction(ctx, state, action)
case store.LogAction:
handleLogAction(state, action)
case GlobalYAMLApplyStartedAction:
handleGlobalYAMLApplyStarted(ctx, state, action)
case GlobalYAMLApplyCompleteAction:
handleGlobalYAMLApplyComplete(ctx, state, action)
case ConfigsReloadStartedAction:
handleConfigsReloadStarted(ctx, state, action)
case ConfigsReloadedAction:
handleConfigsReloaded(ctx, state, action)
case DockerComposeEventAction:
handleDockerComposeEvent(ctx, state, action)
case DockerComposeLogAction:
handleDockerComposeLogAction(state, action)
case view.AppendToTriggerQueueAction:
appendToTriggerQueue(state, action.Name)
case hud.StartProfilingAction:
handleStartProfilingAction(state)
case hud.StopProfilingAction:
handleStopProfilingAction(state)
case hud.SetLogTimestampsAction:
handleLogTimestampsAction(state, action)
case client.SailRoomConnectedAction:
handleSailRoomConnectedAction(ctx, state, action)
case TiltfileLogAction:
handleTiltfileLogAction(ctx, state, action)
case hud.DumpEngineStateAction:
handleDumpEngineStateAction(ctx, state)
default:
err = fmt.Errorf("unrecognized action: %T", action)
}
if err != nil {
state.PermanentError = err
}
})
func handleBuildStarted(ctx context.Context, state *store.EngineState, action BuildStartedAction) {
mn := action.ManifestName
ms, ok := state.ManifestState(mn)
if !ok {
return
}
edits := []string{}
edits = append(edits, action.FilesChanged...)
bs := model.BuildRecord{
Edits: append(edits, ms.ConfigFilesThatCausedChange...),
StartTime: action.StartTime,
Reason: action.Reason,
}
ms.ConfigFilesThatCausedChange = []string{}
ms.CurrentBuild = bs
ms.ExpectedContainerID = ""
for _, pod := range ms.PodSet.Pods {
pod.CurrentLog = model.Log{}
pod.UpdateStartTime = action.StartTime
}
if dcState, ok := ms.ResourceState.(dockercompose.State); ok {
ms.ResourceState = dcState.WithCurrentLog(model.Log{})
}
// Keep the crash log around until we have a rebuild
// triggered by a explicit change (i.e., not a crash rebuild)
if !action.Reason.IsCrashOnly() {
ms.CrashLog = model.Log{}
}
state.CurrentlyBuilding = mn
removeFromTriggerQueue(state, mn)
}
func handleBuildCompleted(ctx context.Context, engineState *store.EngineState, cb BuildCompleteAction) error {
defer func() {
engineState.CurrentlyBuilding = ""
}()
engineState.CompletedBuildCount++
engineState.BuildControllerActionCount++
defer func() {
if engineState.CompletedBuildCount == engineState.InitialBuildCount {
logger.Get(ctx).Debugf("[timing.py] finished initial build") // hook for timing.py
}
}()
err := cb.Error
mt, ok := engineState.ManifestTargets[engineState.CurrentlyBuilding]
if !ok {
return nil
}
ms := mt.State
bs := ms.CurrentBuild
bs.Error = err
bs.FinishTime = time.Now()
ms.AddCompletedBuild(bs)
ms.CurrentBuild = model.BuildRecord{}
ms.NeedsRebuildFromCrash = false
if err != nil {
if isPermanentError(err) {
return err
} else if engineState.WatchFiles {
l := logger.Get(ctx)
p := logger.Red(l).Sprintf("Build Failed:")
l.Infof("%s %v", p, err)
} else {
return errors.Wrap(err, "Build Failed")
}
} else {
// Remove pending file changes that were consumed by this build.
for _, status := range ms.BuildStatuses {
for file, modTime := range status.PendingFileChanges {
if modTime.Before(bs.StartTime) {
delete(status.PendingFileChanges, file)
}
}
}
if !ms.PendingManifestChange.IsZero() &&
ms.PendingManifestChange.Before(bs.StartTime) {
ms.PendingManifestChange = time.Time{}
}
ms.LastSuccessfulDeployTime = time.Now()
for id, result := range cb.Result {
ms.MutableBuildStatus(id).LastSuccessfulResult = result
}
for _, pod := range ms.PodSet.Pods {
// # of pod restarts from old code (shouldn't be reflected in HUD)
pod.OldRestarts = pod.ContainerRestarts
}
}
if mt.Manifest.IsDC() {
state, _ := ms.ResourceState.(dockercompose.State)
cid := cb.Result.OneAndOnlyContainerID()
if cid != "" {
state = state.WithContainerID(cid)
}
// If we have a container ID and no status yet, set status to Up
// (this is an expected case when we run docker-compose up while the service
// is already running, and we won't get an event to tell us so).
// If the container is crashing we will get an event subsequently.
isFirstBuild := cid != "" && state.Status == ""
if isFirstBuild {
state = state.WithStatus(dockercompose.StatusUp)
}
ms.ResourceState = state
}
if engineState.WatchFiles {
logger.Get(ctx).Debugf("[timing.py] finished build from file change") // hook for timing.py
cID := cb.Result.OneAndOnlyContainerID()
if cID != "" {
ms.ExpectedContainerID = cID
bestPod := ms.MostRecentPod()
if bestPod.StartedAt.After(bs.StartTime) ||
bestPod.UpdateStartTime.Equal(bs.StartTime) {
checkForPodCrash(ctx, engineState, ms, bestPod)
}
}
}
return nil
}
func handleDeployIDAction(ctx context.Context, state *store.EngineState, action DeployIDAction) {
mns := state.ManifestNamesForTargetID(action.TargetID)
for _, mn := range mns {
ms, ok := state.ManifestState(mn)
if !ok {
continue
}
ms.DeployID = action.DeployID
}
}
func appendToTriggerQueue(state *store.EngineState, mn model.ManifestName) {
if state.TriggerMode != model.TriggerManual {
return
}
ms, ok := state.ManifestState(mn)
if !ok {
return
}
ok, _ = ms.HasPendingChanges()
if !ok {
return
}
for _, triggerName := range state.TriggerQueue {
if mn == triggerName {
return
}
}
state.TriggerQueue = append(state.TriggerQueue, mn)
}
func removeFromTriggerQueue(state *store.EngineState, mn model.ManifestName) {
for i, triggerName := range state.TriggerQueue {
if triggerName == mn {
state.TriggerQueue = append(state.TriggerQueue[:i], state.TriggerQueue[i+1:]...)
break
}
}
}
func handleStopProfilingAction(state *store.EngineState) {
state.IsProfiling = false
}
func handleStartProfilingAction(state *store.EngineState) {
state.IsProfiling = true
}
func handleLogTimestampsAction(state *store.EngineState, action hud.SetLogTimestampsAction) {
state.LogTimestamps = action.Value
}
func handleSailRoomConnectedAction(ctx context.Context, state *store.EngineState, action client.SailRoomConnectedAction) {
if action.Err != nil {
logger.Get(ctx).Infof("Error connecting Sail room: %v\n", action.Err)
return
}
state.SailURL = action.ViewURL
}
func handleFSEvent(
ctx context.Context,
state *store.EngineState,
event targetFilesChangedAction) {
if event.targetID.Type == model.TargetTypeConfigs {
for _, f := range event.files {
state.PendingConfigFileChanges[f] = event.time
}
return
}
mns := state.ManifestNamesForTargetID(event.targetID)
for _, mn := range mns {
ms, ok := state.ManifestState(mn)
if !ok {
return
}
status := ms.MutableBuildStatus(event.targetID)
for _, f := range event.files {
status.PendingFileChanges[f] = event.time
}
}
}
func handleGlobalYAMLApplyStarted(
ctx context.Context,
state *store.EngineState,
event GlobalYAMLApplyStartedAction,
) {
state.GlobalYAMLState.CurrentApplyStartTime = time.Now()
state.GlobalYAMLState.LastError = nil
}
func handleGlobalYAMLApplyComplete(
ctx context.Context,
state *store.EngineState,
event GlobalYAMLApplyCompleteAction,
) {
ms := state.GlobalYAMLState
ms.LastApplyStartTime = ms.CurrentApplyStartTime
ms.LastApplyFinishTime = time.Now()
ms.CurrentApplyStartTime = time.Time{}
ms.LastError = event.Error
if event.Error == nil {
ms.HasBeenDeployed = true
ms.LastSuccessfulApplyTime = time.Now()
}
}
func handleConfigsReloadStarted(
ctx context.Context,
state *store.EngineState,
event ConfigsReloadStartedAction,
) {
filesChanged := []string{}
for f, _ := range event.FilesChanged {
filesChanged = append(filesChanged, f)
}
status := model.BuildRecord{
StartTime: event.StartTime,
Reason: model.BuildReasonFlagConfig,
Edits: filesChanged,
}
state.CurrentTiltfileBuild = status
}
func handleConfigsReloaded(
ctx context.Context,
state *store.EngineState,
event ConfigsReloadedAction,
) {
manifests := event.Manifests
status := state.CurrentTiltfileBuild
status.FinishTime = event.FinishTime
status.Error = event.Err
status.Warnings = event.Warnings
state.LastTiltfileBuild = status
state.CurrentTiltfileBuild = model.BuildRecord{}
if event.Err != nil {
// There was an error, so don't update status with the new, nonexistent state
// EXCEPT for the config file list, because we want to watch new config files even when the tiltfile is broken
// append any new config files found in the reload action
state.ConfigFiles = sliceutils.AppendWithoutDupes(state.ConfigFiles, event.ConfigFiles...)
return
}
newDefOrder := make([]model.ManifestName, len(manifests))
for i, m := range manifests {
mt, ok := state.ManifestTargets[m.ManifestName()]
if !ok {
mt = store.NewManifestTarget(m)
}
newDefOrder[i] = m.ManifestName()
configFilesThatChanged := state.LastTiltfileBuild.Edits
if !m.Equal(mt.Manifest) {
mt.Manifest = m
// Manifest has changed, ensure we do an image build so that we apply the changes
state := mt.State
state.BuildStatuses = make(map[model.TargetID]*store.BuildStatus)
state.PendingManifestChange = time.Now()
state.ConfigFilesThatCausedChange = configFilesThatChanged
}
state.UpsertManifestTarget(mt)
}
// TODO(dmiller) handle deleting manifests
// TODO(maia): update ConfigsManifest with new ConfigFiles/update watches
state.ManifestDefinitionOrder = newDefOrder
state.GlobalYAML = event.GlobalYAML
state.ConfigFiles = event.ConfigFiles
state.TiltIgnoreContents = event.TiltIgnoreContents
// Remove pending file changes that were consumed by this build.
for file, modTime := range state.PendingConfigFileChanges {
if modTime.Before(status.StartTime) {
delete(state.PendingConfigFileChanges, file)
}
}
}
// Get a pointer to a mutable manifest state,
// ensuring that some Pod exists on the state.
//
// Intended as a helper for pod-mutating events.
func ensureManifestTargetWithPod(state *store.EngineState, pod *v1.Pod) (*store.ManifestTarget, *store.Pod) {
manifestName := model.ManifestName(pod.ObjectMeta.Labels[k8s.ManifestNameLabel])
if manifestName == "" {
// if there's no ManifestNameLabel, then maybe it matches some manifest's ExtraPodSelectors
for _, m := range state.Manifests() {
if m.IsK8s() {
for _, lps := range m.K8sTarget().ExtraPodSelectors {
if lps.Matches(labels.Set(pod.ObjectMeta.GetLabels())) {
manifestName = m.Name
break
}
}
}
}
}
mt, ok := state.ManifestTargets[manifestName]
if !ok {
// This is OK. The user could have edited the manifest recently.
return nil, nil
}
ms := mt.State
deployID := ms.DeployID
if podDeployID, ok := pod.ObjectMeta.Labels[k8s.TiltDeployIDLabel]; ok {
if pdID, err := strconv.Atoi(podDeployID); err != nil || pdID != int(deployID) {
return nil, nil
}
}
podID := k8s.PodIDFromPod(pod)
startedAt := pod.CreationTimestamp.Time
status := podStatusToString(*pod)
ns := k8s.NamespaceFromPod(pod)
hasSynclet := sidecar.PodSpecContainsSynclet(pod.Spec)
// CASE 1: We don't have a set of pods for this DeployID yet
if ms.PodSet.DeployID == 0 || ms.PodSet.DeployID != deployID {
ms.PodSet = store.PodSet{
DeployID: deployID,
Pods: make(map[k8s.PodID]*store.Pod),
}
ms.PodSet.Pods[podID] = &store.Pod{
PodID: podID,
StartedAt: startedAt,
Status: status,
Namespace: ns,
HasSynclet: hasSynclet,
}
return mt, ms.PodSet.Pods[podID]
}
podInfo, ok := ms.PodSet.Pods[podID]
if !ok {
// CASE 2: We have a set of pods for this DeployID, but not this particular pod -- record it
podInfo = &store.Pod{
PodID: podID,
StartedAt: startedAt,
Status: status,
Namespace: ns,
HasSynclet: hasSynclet,
}
ms.PodSet.Pods[podID] = podInfo
}
// CASE 3: This pod is already in the PodSet, nothing to do.
return mt, podInfo
}
// Fill in container fields on the pod state.
func populateContainerStatus(ctx context.Context, manifest model.Manifest, podInfo *store.Pod, pod *v1.Pod, cStatus v1.ContainerStatus) {
cName := k8s.ContainerNameFromContainerStatus(cStatus)
podInfo.ContainerName = cName
podInfo.ContainerReady = cStatus.Ready
cID, err := k8s.ContainerIDFromContainerStatus(cStatus)
if err != nil {
logger.Get(ctx).Debugf("Error parsing container ID: %v", err)
return
}
podInfo.ContainerID = cID
cRef, err := container.ParseNamed(cStatus.Image)
if err != nil {
logger.Get(ctx).Debugf("Error parsing container image ID: %v", err)
return
}
podInfo.ContainerImageRef = cRef
ports := make([]int32, 0)
cSpec := k8s.ContainerSpecOf(pod, cStatus)
for _, cPort := range cSpec.Ports {
ports = append(ports, cPort.ContainerPort)
}
podInfo.ContainerPorts = ports
forwards := PopulatePortForwards(manifest, *podInfo)
if len(forwards) < len(manifest.K8sTarget().PortForwards) {
logger.Get(ctx).Infof(
"WARNING: Resource %s is using port forwards, but no container ports on pod %s",
manifest.Name, podInfo.PodID)
}
// HACK(maia): Go through ALL containers (except tilt-synclet), grab minimum info we need
// to stream logs from them.
var cInfos []store.ContainerInfo
for _, cStat := range pod.Status.ContainerStatuses {
if cStat.Name == sidecar.SyncletContainerName {
// We don't want logs for the Tilt synclet.
continue
}
cID, err := k8s.ContainerIDFromContainerStatus(cStat)
if err != nil {
logger.Get(ctx).Debugf("Error parsing container ID: %v", err)
return
}
if err != nil {
return
}
cInfos = append(cInfos, store.ContainerInfo{
ID: cID,
Name: k8s.ContainerNameFromContainerStatus(cStat),
})
}
podInfo.ContainerInfos = cInfos
}
func handlePodChangeAction(ctx context.Context, state *store.EngineState, pod *v1.Pod) {
mt, podInfo := ensureManifestTargetWithPod(state, pod)
if mt == nil || podInfo == nil {
return
}
ms := mt.State
manifest := mt.Manifest
podID := k8s.PodIDFromPod(pod)
if podInfo.PodID != podID {
// This is an event from an old pod.
return
}
// Update the status
podInfo.Deleting = pod.DeletionTimestamp != nil
podInfo.Phase = pod.Status.Phase
podInfo.Status = podStatusToString(*pod)
defer prunePods(ms)
// Check if the container is ready.
var cStatus v1.ContainerStatus
var err error
if len(manifest.ImageTargets) > 0 {
// Get status of (first) container matching (an) image we built for this manifest.
for _, iTarget := range manifest.ImageTargets {
cStatus, err = k8s.ContainerMatching(pod, container.NameSelector(iTarget.DeploymentRef))
if err != nil {
logger.Get(ctx).Debugf("Error matching container: %v", err)
return
}
if cStatus.Name != "" {
break
}
}
} else {
// We didn't build images for this manifest so we have no good way of figuring
// out which container(s) we care about; for now, take the first.
if len(pod.Status.ContainerStatuses) > 0 {
cStatus = pod.Status.ContainerStatuses[0]
}
}
if cStatus.Name == "" {
return
}
populateContainerStatus(ctx, manifest, podInfo, pod, cStatus)
checkForPodCrash(ctx, state, ms, *podInfo)
if int(cStatus.RestartCount) > podInfo.ContainerRestarts {
podInfo.CurrentLog = model.Log{}
}
podInfo.ContainerRestarts = int(cStatus.RestartCount)
}
func checkForPodCrash(ctx context.Context, state *store.EngineState, ms *store.ManifestState, podInfo store.Pod) {
if ms.NeedsRebuildFromCrash {
// We're already aware the pod is crashing.
return
}
if ms.ExpectedContainerID == "" || ms.ExpectedContainerID == podInfo.ContainerID {
// The pod is what we expect it to be.
return
}
// The pod isn't what we expect!
ms.CrashLog = podInfo.CurrentLog
ms.NeedsRebuildFromCrash = true
ms.ExpectedContainerID = ""
msg := fmt.Sprintf("Detected a container change for %s. We could be running stale code. Rebuilding and deploying a new image.", ms.Name)
le := store.NewLogEvent([]byte(msg + "\n"))
if len(ms.BuildHistory) > 0 {
ms.BuildHistory[0].Log = model.AppendLog(ms.BuildHistory[0].Log, le, state.LogTimestamps)
}
ms.CurrentBuild.Log = model.AppendLog(ms.CurrentBuild.Log, le, state.LogTimestamps)
logger.Get(ctx).Infof("%s", msg)
}
// If there's more than one pod, prune the deleting/dead ones so
// that they don't clutter the output.
func prunePods(ms *store.ManifestState) {
// Continue pruning until we have 1 pod.
for ms.PodSet.Len() > 1 {
bestPod := ms.MostRecentPod()
for key, pod := range ms.PodSet.Pods {
// Always remove pods that were manually deleted.
if pod.Deleting {
delete(ms.PodSet.Pods, key)
break
}
// Remove terminated pods if they aren't the most recent one.
isDead := pod.Phase == v1.PodSucceeded || pod.Phase == v1.PodFailed
if isDead && pod.PodID != bestPod.PodID {
delete(ms.PodSet.Pods, key)
break
}
}
// found nothing to delete, break out
return
}
}
func handlePodLogAction(state *store.EngineState, action PodLogAction) {
manifestName := action.ManifestName
ms, ok := state.ManifestState(manifestName)
if !ok {
// This is OK. The user could have edited the manifest recently.
return
}
ms.CombinedLog = model.AppendLog(ms.CombinedLog, action, state.LogTimestamps)
podID := action.PodID
if !ms.PodSet.ContainsID(podID) {
// NOTE(nick): There are two cases where this could happen:
// 1) Pod 1 died and kubernetes started Pod 2. What should we do with
// logs from Pod 1 that are still in the action queue?
// This is an open product question. A future HUD may aggregate
// logs across pod restarts.
// 2) Due to race conditions, we got the logs for Pod 1 before
// we saw Pod 1 materialize on the Pod API. The best way to fix
// this would be to make PodLogManager a subscriber that only
// starts listening on logs once the pod has materialized.
// We may prioritize this higher or lower based on how often
// this happens in practice.
return
}
podInfo := ms.PodSet.Pods[podID]
podInfo.CurrentLog = model.AppendLog(podInfo.CurrentLog, action, state.LogTimestamps)
}
func handleBuildLogAction(state *store.EngineState, action BuildLogAction) {
manifestName := action.ManifestName
ms, ok := state.ManifestState(manifestName)
if !ok || state.CurrentlyBuilding != manifestName {
// This is OK. The user could have edited the manifest recently.
return
}
ms.CombinedLog = model.AppendLog(ms.CombinedLog, action, state.LogTimestamps)
ms.CurrentBuild.Log = model.AppendLog(ms.CurrentBuild.Log, action, state.LogTimestamps)
}
func handleLogAction(state *store.EngineState, action store.LogAction) {
state.Log = model.AppendLog(state.Log, action, state.LogTimestamps)
}
func handleServiceEvent(ctx context.Context, state *store.EngineState, action ServiceChangeAction) {
service := action.Service
manifestName := model.ManifestName(service.ObjectMeta.Labels[k8s.ManifestNameLabel])
if manifestName == "" || manifestName == model.GlobalYAMLManifestName {
return
}
ms, ok := state.ManifestState(manifestName)
if !ok {
logger.Get(ctx).Infof("error: got notified of service for unknown manifest '%s'", manifestName)
return
}
ms.LBs[k8s.ServiceName(service.Name)] = action.URL
}
func handleDumpEngineStateAction(ctx context.Context, engineState *store.EngineState) {
f, err := ioutil.TempFile("", "tilt-engine-state-*.txt")
if err != nil {
logger.Get(ctx).Infof("error creating temp file to write engine state: %v", err)
return
}
logger.Get(ctx).Infof("dumped tilt engine state to %q", f.Name())
spew.Fdump(f, engineState)
err = f.Close()
if err != nil {
logger.Get(ctx).Infof("error closing engine state temp file: %v", err)
return
}
}
func handleInitAction(ctx context.Context, engineState *store.EngineState, action InitAction) error {
watchFiles := action.WatchFiles
engineState.TiltBuildInfo = action.TiltBuild
engineState.TiltStartTime = action.StartTime
engineState.TiltfilePath = action.TiltfilePath
engineState.TriggerMode = action.TriggerMode
engineState.ConfigFiles = action.ConfigFiles
engineState.InitManifests = action.InitManifests
engineState.SailEnabled = action.EnableSail
if action.ExecuteTiltfile {
engineState.GlobalYAML = action.GlobalYAMLManifest
engineState.GlobalYAMLState = store.NewYAMLManifestState()
status := model.BuildRecord{
StartTime: action.StartTime,
FinishTime: action.FinishTime,
Error: action.Err,
Warnings: action.Warnings,
Reason: model.BuildReasonFlagInit,
}
engineState.LastTiltfileBuild = status
manifests := action.Manifests
for _, m := range manifests {
engineState.UpsertManifestTarget(store.NewManifestTarget(m))
}
engineState.InitialBuildCount = len(manifests)
} else {
// NOTE(dmiller): this kicks off a Tiltfile build
engineState.PendingConfigFileChanges[action.TiltfilePath] = time.Now()
engineState.InitialBuildCount = len(action.InitManifests)
}
engineState.GlobalYAMLState = store.NewYAMLManifestState()
engineState.WatchFiles = watchFiles
return nil
}
func handleExitAction(state *store.EngineState, action hud.ExitAction) {
if action.Err != nil {
state.PermanentError = action.Err
} else {
state.UserExited = true
}
}
func handleDockerComposeEvent(ctx context.Context, engineState *store.EngineState, action DockerComposeEventAction) {
evt := action.Event
mn := evt.Service
ms, ok := engineState.ManifestState(model.ManifestName(mn))
if !ok {
// No corresponding manifest, nothing to do
logger.Get(ctx).Infof("event for unrecognized manifest %s", mn)
return
}
if evt.Type != dockercompose.TypeContainer {
// We currently only support Container events.
return
}
state, _ := ms.ResourceState.(dockercompose.State)
state = state.WithContainerID(container.ID(evt.ID))
// For now, just guess at state.
status := evt.GuessStatus()
if status != "" {
state = state.WithStatus(status)
}
if evt.IsStartupEvent() {
state = state.WithStartTime(time.Now())
state = state.WithStopping(false)
}
if evt.IsStopEvent() {
state = state.WithStopping(true)
}
if evt.Action == dockercompose.ActionDie && !state.IsStopping {
state = state.WithStatus(dockercompose.StatusCrash)
}
ms.ResourceState = state
}
func handleDockerComposeLogAction(state *store.EngineState, action DockerComposeLogAction) {
manifestName := action.ManifestName
ms, ok := state.ManifestState(manifestName)
if !ok {
// This is OK. The user could have edited the manifest recently.
return
}
dcState, _ := ms.ResourceState.(dockercompose.State)
ms.ResourceState = dcState.WithCurrentLog(model.AppendLog(dcState.CurrentLog, action, state.LogTimestamps))
ms.CombinedLog = model.AppendLog(ms.CombinedLog, action, state.LogTimestamps)
}
func handleTiltfileLogAction(ctx context.Context, state *store.EngineState, action TiltfileLogAction) {
state.CurrentTiltfileBuild.Log = model.AppendLog(state.CurrentTiltfileBuild.Log, action, state.LogTimestamps)
state.TiltfileCombinedLog = model.AppendLog(state.TiltfileCombinedLog, action, state.LogTimestamps)
}