/
view.go
188 lines (160 loc) · 5.95 KB
/
view.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
package server
import (
"os"
"sort"
"github.com/windmilleng/tilt/internal/dockercompose"
"github.com/windmilleng/tilt/internal/hud/view"
"github.com/windmilleng/tilt/internal/hud/webview"
"github.com/windmilleng/tilt/internal/model"
"github.com/windmilleng/tilt/internal/ospath"
"github.com/windmilleng/tilt/internal/store"
)
func StateToWebView(s store.EngineState) webview.View {
ret := webview.View{}
ret.Resources = append(ret.Resources, tiltfileResourceView(s))
for _, name := range s.ManifestDefinitionOrder {
mt, ok := s.ManifestTargets[name]
if !ok {
continue
}
ms := mt.State
var absWatchDirs []string
var absWatchPaths []string
for _, p := range mt.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 _, status := range ms.BuildStatuses {
for f := range status.PendingFileChanges {
pendingBuildEdits = append(pendingBuildEdits, f)
}
}
pendingBuildEdits = ospath.FileListDisplayNames(absWatchDirs, pendingBuildEdits)
buildHistory := append([]model.BuildRecord{}, ms.BuildHistory...)
for i, build := range buildHistory {
build.Edits = ospath.FileListDisplayNames(absWatchDirs, build.Edits)
buildHistory[i] = build
}
currentBuild := ms.CurrentBuild
currentBuild.Edits = ospath.FileListDisplayNames(absWatchDirs, ms.CurrentBuild.Edits)
// Sort the strings to make the outputs deterministic.
sort.Strings(pendingBuildEdits)
endpoints := store.ManifestTargetEndpoints(mt)
// 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 := webview.Resource{
Name: name,
DirectoriesWatched: relWatchDirs,
PathsWatched: relWatchPaths,
LastDeployTime: ms.LastSuccessfulDeployTime,
BuildHistory: buildHistory,
PendingBuildEdits: pendingBuildEdits,
PendingBuildSince: pendingBuildSince,
PendingBuildReason: ms.NextBuildReason(),
CurrentBuild: currentBuild,
Endpoints: endpoints,
ResourceInfo: resourceInfoView(mt),
ShowBuildStatus: len(mt.Manifest.ImageTargets) > 0 || mt.Manifest.IsDC(),
CombinedLog: ms.CombinedLog,
}
r.RuntimeStatus = runtimeStatus(r.ResourceInfo)
ret.Resources = append(ret.Resources, r)
}
if s.GlobalYAML.K8sTarget().YAML != "" {
absWatches := append([]string{}, s.ConfigFiles...)
relWatches := ospath.TryAsCwdChildren(absWatches)
r := webview.Resource{
Name: s.GlobalYAML.ManifestName(),
DirectoriesWatched: relWatches,
CurrentBuild: s.GlobalYAMLState.ActiveBuild(),
BuildHistory: []model.BuildRecord{
s.GlobalYAMLState.LastBuild(),
},
LastDeployTime: s.GlobalYAMLState.LastSuccessfulApplyTime,
ResourceInfo: webview.YAMLResourceInfo{
K8sResources: s.GlobalYAML.K8sTarget().ResourceNames,
},
}
r.RuntimeStatus = runtimeStatus(r.ResourceInfo)
ret.Resources = append(ret.Resources, r)
}
ret.Log = s.Log
return ret
}
func tiltfileResourceView(s store.EngineState) webview.Resource {
ltfb := s.LastTiltfileBuild
if !s.CurrentTiltfileBuild.Empty() {
ltfb.Log = s.CurrentTiltfileBuild.Log
}
tr := webview.Resource{
Name: view.TiltfileResourceName,
IsTiltfile: true,
CurrentBuild: s.CurrentTiltfileBuild,
BuildHistory: []model.BuildRecord{
ltfb,
},
CombinedLog: s.TiltfileCombinedLog,
RuntimeStatus: webview.RuntimeStatusOK,
}
if !s.CurrentTiltfileBuild.Empty() {
tr.PendingBuildSince = s.CurrentTiltfileBuild.StartTime
} else {
tr.LastDeployTime = s.LastTiltfileBuild.FinishTime
}
return tr
}
func resourceInfoView(mt *store.ManifestTarget) webview.ResourceInfoView {
if dcState, ok := mt.State.ResourceState.(dockercompose.State); ok {
return webview.NewDCResourceInfo(mt.Manifest.DockerComposeTarget().ConfigPath, dcState.Status, dcState.ContainerID, dcState.Log(), dcState.StartTime)
} else {
pod := mt.State.MostRecentPod()
return webview.K8SResourceInfo{
PodName: pod.PodID.String(),
PodCreationTime: pod.StartedAt,
PodUpdateStartTime: pod.UpdateStartTime,
PodStatus: pod.Status,
PodRestarts: pod.ContainerRestarts - pod.OldRestarts,
PodLog: pod.Log(),
YAML: mt.Manifest.K8sTarget().YAML,
}
}
}
func runtimeStatus(res webview.ResourceInfoView) webview.RuntimeStatus {
// if we have no images to build, we have no runtime status monitoring.
_, isYAML := res.(webview.YAMLResourceInfo)
if isYAML {
return webview.RuntimeStatusOK
}
result, ok := runtimeStatusMap[res.Status()]
if !ok {
return webview.RuntimeStatusError
}
return result
}
var runtimeStatusMap = map[string]webview.RuntimeStatus{
"Running": webview.RuntimeStatusOK,
"ContainerCreating": webview.RuntimeStatusPending,
"Pending": webview.RuntimeStatusPending,
"PodInitializing": webview.RuntimeStatusPending,
"Error": webview.RuntimeStatusError,
"CrashLoopBackOff": webview.RuntimeStatusError,
"ErrImagePull": webview.RuntimeStatusError,
"ImagePullBackOff": webview.RuntimeStatusError,
string(dockercompose.StatusInProg): webview.RuntimeStatusPending,
string(dockercompose.StatusUp): webview.RuntimeStatusOK,
string(dockercompose.StatusDown): webview.RuntimeStatusError,
"Completed": webview.RuntimeStatusOK,
// If the runtime status hasn't shown up yet, we assume it's pending.
"": webview.RuntimeStatusPending,
}