-
Notifications
You must be signed in to change notification settings - Fork 290
/
view.go
286 lines (236 loc) · 8.27 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
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
package webview
// TODO(dmiller): delete these tests once StateToWebView is deleted
import (
"time"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/dockercompose"
"github.com/windmilleng/tilt/internal/k8s"
"github.com/windmilleng/tilt/pkg/model"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
proto_webview "github.com/windmilleng/tilt/pkg/webview"
)
type ResourceInfoView interface {
resourceInfoView()
RuntimeLog() model.Log
Status() string
}
type DCResourceInfo struct {
ConfigPaths []string `json:"configPaths"`
ContainerStatus dockercompose.Status `json:"containerStatus"`
ContainerID container.ID `json:"containerID"`
Log model.Log `json:"log"`
StartTime time.Time `json:"startTime"`
}
func NewDCResourceInfo(configPaths []string, status dockercompose.Status, cID container.ID, log model.Log, startTime time.Time) DCResourceInfo {
return DCResourceInfo{
ConfigPaths: configPaths,
ContainerStatus: status,
ContainerID: cID,
Log: log,
StartTime: startTime,
}
}
func NewProtoDCResourceInfo(configPaths []string, status dockercompose.Status, cID container.ID, log model.Log, startTime time.Time) (*proto_webview.DCResourceInfo, error) {
start, err := timeToProto(startTime)
if err != nil {
return nil, err
}
return &proto_webview.DCResourceInfo{
ConfigPaths: configPaths,
ContainerStatus: string(status),
ContainerID: string(cID),
Log: log.String(),
StartTime: start,
}, nil
}
var _ ResourceInfoView = DCResourceInfo{}
func (DCResourceInfo) resourceInfoView() {}
func (dcInfo DCResourceInfo) RuntimeLog() model.Log { return dcInfo.Log }
func (dcInfo DCResourceInfo) Status() string { return string(dcInfo.ContainerStatus) }
type K8sResourceInfo struct {
PodName string `json:"podName"`
PodCreationTime time.Time `json:"podCreationTime"`
PodUpdateStartTime time.Time `json:"podUpdateStartTime"`
PodStatus string `json:"podStatus"`
PodStatusMessage string `json:"podStatusMessage"`
AllContainersReady bool `json:"allContainersReady"`
PodRestarts int `json:"podRestarts"`
PodLog model.Log `json:"podLog"`
}
var _ ResourceInfoView = K8sResourceInfo{}
func (K8sResourceInfo) resourceInfoView() {}
func (k8sInfo K8sResourceInfo) RuntimeLog() model.Log { return k8sInfo.PodLog }
func (k8sInfo K8sResourceInfo) Status() string {
status := k8sInfo.PodStatus
if status == "Running" && !k8sInfo.AllContainersReady {
status = "Pending"
}
return status
}
type YAMLResourceInfo struct {
K8sResources []string `json:"k8sResources"`
}
var _ ResourceInfoView = YAMLResourceInfo{}
func (YAMLResourceInfo) resourceInfoView() {}
func (yamlInfo YAMLResourceInfo) RuntimeLog() model.Log { return model.NewLog("") }
func (yamlInfo YAMLResourceInfo) Status() string { return "" }
// Local resources have no run time info, so it's all empty.
type LocalResourceInfo struct{}
var _ ResourceInfoView = LocalResourceInfo{}
func (LocalResourceInfo) resourceInfoView() {}
func (LocalResourceInfo) RuntimeLog() model.Log { return model.NewLog("") }
func (LocalResourceInfo) Status() string { return "" }
type BuildRecord struct {
Edits []string `json:"edits"`
Error string `json:"error"`
Warnings []string `json:"warnings"`
StartTime time.Time `json:"startTime"`
FinishTime time.Time `json:"finishTime"` // IsZero() == true for in-progress builds
Log model.Log `json:"log"`
IsCrashRebuild bool `json:"isCrashRebuild"`
}
func ToWebViewBuildRecord(br model.BuildRecord) BuildRecord {
e := ""
if br.Error != nil {
e = br.Error.Error()
}
return BuildRecord{
Edits: br.Edits,
Error: e,
Warnings: br.Warnings,
StartTime: br.StartTime,
FinishTime: br.FinishTime,
Log: br.Log,
IsCrashRebuild: br.Reason.IsCrashOnly(),
}
}
func timeToProto(t time.Time) (*timestamp.Timestamp, error) {
ts, err := ptypes.TimestampProto(t)
if err != nil {
return nil, err
}
return ts, nil
}
func ToProtoBuildRecord(br model.BuildRecord) (*proto_webview.BuildRecord, error) {
e := ""
if br.Error != nil {
e = br.Error.Error()
}
start, err := timeToProto(br.StartTime)
if err != nil {
return nil, err
}
finish, err := timeToProto(br.FinishTime)
if err != nil {
return nil, err
}
return &proto_webview.BuildRecord{
Edits: br.Edits,
Error: e,
Warnings: br.Warnings,
StartTime: start,
FinishTime: finish,
Log: br.Log.String(),
IsCrashRebuild: br.Reason.IsCrashOnly(),
}, nil
}
func ToProtoBuildRecords(brs []model.BuildRecord) ([]*proto_webview.BuildRecord, error) {
ret := make([]*proto_webview.BuildRecord, len(brs))
for i, br := range brs {
r, err := ToProtoBuildRecord(br)
if err != nil {
return nil, err
}
ret[i] = r
}
return ret, nil
}
func ToWebViewBuildRecords(brs []model.BuildRecord) []BuildRecord {
ret := make([]BuildRecord, len(brs))
for i, br := range brs {
ret[i] = ToWebViewBuildRecord(br)
}
return ret
}
type Alert struct {
AlertType string `json:"alertType"`
Header string `json:"header"`
Message string `json:"msg"`
Timestamp string `json:"timestamp"`
ResourceName string `json:"resourceName"`
}
type Resource struct {
Name model.ManifestName `json:"name"`
DirectoriesWatched []string `json:"directoriesWatched"`
PathsWatched []string `json:"pathsWatched"`
LastDeployTime time.Time `json:"lastDeployTime"`
TriggerMode model.TriggerMode `json:"triggerMode"`
BuildHistory []BuildRecord `json:"buildHistory"`
CurrentBuild BuildRecord `json:"currentBuild"`
PendingBuildReason model.BuildReason `json:"pendingBuildReason"`
PendingBuildEdits []string `json:"pendingBuildEdits"`
PendingBuildSince time.Time `json:"pendingBuildSince"`
HasPendingChanges bool `json:"hasPendingChanges"`
Endpoints []string `json:"endpoints"`
PodID k8s.PodID `json:"podID"`
// Only one of these resource info fields will be populated
K8sResourceInfo *K8sResourceInfo `json:"k8sResourceInfo,omitempty"`
DCResourceInfo *DCResourceInfo `json:"dcResourceInfo,omitempty"`
YAMLResourceInfo *YAMLResourceInfo `json:"yamlResourceInfo,omitempty"`
LocalResourceInfo *LocalResourceInfo `json:"localResourceInfo,omitempty"`
RuntimeStatus RuntimeStatus `json:"runtimeStatus"`
IsTiltfile bool `json:"isTiltfile"`
ShowBuildStatus bool `json:"showBuildStatus"` // if true, we show status & time in 'Build Status'; else, "N/A"
CombinedLog model.Log `json:"combinedLog"`
CrashLog model.Log `json:"crashLog"`
Alerts []Alert `json:"alerts"`
Facets []model.Facet `json:"facets"`
}
func (r Resource) ResourceInfo() ResourceInfoView {
if r.K8sResourceInfo != nil {
return *r.K8sResourceInfo
}
if r.DCResourceInfo != nil {
return *r.DCResourceInfo
}
if r.YAMLResourceInfo != nil {
return *r.YAMLResourceInfo
}
if r.LocalResourceInfo != nil {
return *r.LocalResourceInfo
}
// return an empty info, just to avoid NPEs
return K8sResourceInfo{}
}
func (r Resource) LastBuild() BuildRecord {
if len(r.BuildHistory) == 0 {
return BuildRecord{}
}
return r.BuildHistory[0]
}
type RuntimeStatus string
const (
RuntimeStatusOK RuntimeStatus = "ok"
RuntimeStatusPending RuntimeStatus = "pending"
RuntimeStatusError RuntimeStatus = "error"
RuntimeStatusNotApplicable RuntimeStatus = "not_applicable"
)
type TiltBuild struct {
Version string `json:"version"`
CommitSHA string `json:"commitSHA"`
Date string `json:"date"`
Dev bool `json:"dev"`
}
type View struct {
Log model.Log `json:"log"`
Resources []Resource `json:"resources"`
FeatureFlags map[string]bool `json:"featureFlags"`
NeedsAnalyticsNudge bool `json:"needsAnalyticsNudge"`
RunningTiltBuild TiltBuild `json:"runningTiltBuild"`
LatestTiltBuild TiltBuild `json:"latestTiltBuild"`
TiltCloudUsername string `json:"tiltCloudUsername"`
TiltCloudSchemeHost string `json:"tiltCloudSchemeHost"`
TiltCloudTeamID string `json:"tiltCloudTeamID"`
FatalError string `json:"fatalError"`
}