-
Notifications
You must be signed in to change notification settings - Fork 288
/
runtime_state.go
307 lines (247 loc) · 7.43 KB
/
runtime_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
package store
import (
"fmt"
"net/url"
"time"
"github.com/docker/distribution/reference"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/k8s"
"github.com/windmilleng/tilt/pkg/model"
)
type RuntimeState interface {
RuntimeState()
// There are two types of resource dependencies:
// - servers (Deployments) where what's important is that the server is running
// - tasks (Jobs, local resources) where what's important is that the job completed
// Currently, we don't try to distinguish between these two cases.
//
// In the future, it might make sense to check "IsBlocking()" or something,
// and alter the behavior based on whether the underlying resource is a server
// or a task.
HasEverBeenReadyOrSucceeded() bool
RuntimeStatus() model.RuntimeStatus
// If the runtime status is in Error mode,
// RuntimeStatusError() should report a reason.
RuntimeStatusError() error
}
type LocalRuntimeState struct {
Status model.RuntimeStatus
HasSucceededAtLeastOnce bool
PID int
SpanID model.LogSpanID
}
func (LocalRuntimeState) RuntimeState() {}
func (l LocalRuntimeState) RuntimeStatus() model.RuntimeStatus {
return l.Status
}
func (l LocalRuntimeState) RuntimeStatusError() error {
status := l.RuntimeStatus()
if status != model.RuntimeStatusError {
return nil
}
return fmt.Errorf("Process %d exited with non-zero status", l.PID)
}
func (l LocalRuntimeState) HasEverBeenReadyOrSucceeded() bool {
return l.HasSucceededAtLeastOnce
}
var _ RuntimeState = LocalRuntimeState{}
type K8sRuntimeState struct {
// The ancestor that we match pods against to associate them with this manifest.
// If we deployed Pod YAML, this will be the Pod UID.
// In many cases, this will be a Deployment UID.
PodAncestorUID types.UID
Pods map[k8s.PodID]*Pod
LBs map[k8s.ServiceName]*url.URL
DeployedUIDSet UIDSet // for the most recent successful deploy
DeployedPodTemplateSpecHashSet PodTemplateSpecHashSet // for the most recent successful deploy
LastReadyOrSucceededTime time.Time
}
func (K8sRuntimeState) RuntimeState() {}
var _ RuntimeState = K8sRuntimeState{}
func NewK8sRuntimeState(pods ...Pod) K8sRuntimeState {
podMap := make(map[k8s.PodID]*Pod, len(pods))
for _, pod := range pods {
p := pod
podMap[p.PodID] = &p
}
return K8sRuntimeState{
Pods: podMap,
LBs: make(map[k8s.ServiceName]*url.URL),
DeployedUIDSet: NewUIDSet(),
DeployedPodTemplateSpecHashSet: NewPodTemplateSpecHashSet(),
}
}
func (s K8sRuntimeState) RuntimeStatusError() error {
status := s.RuntimeStatus()
if status != model.RuntimeStatusError {
return nil
}
pod := s.MostRecentPod()
return fmt.Errorf("Pod %s in error state: %s", pod.PodID, pod.Status)
}
func (s K8sRuntimeState) RuntimeStatus() model.RuntimeStatus {
pod := s.MostRecentPod()
status := pod.Status
if status == "Running" && !pod.AllContainersReady() {
status = "Pending"
}
runtimeStatus, ok := runtimeStatusMap[status]
if !ok {
return model.RuntimeStatusError
}
return runtimeStatus
}
func (s K8sRuntimeState) HasEverBeenReadyOrSucceeded() bool {
return !s.LastReadyOrSucceededTime.IsZero()
}
func (s K8sRuntimeState) PodLen() int {
return len(s.Pods)
}
func (s K8sRuntimeState) ContainsID(id k8s.PodID) bool {
_, ok := s.Pods[id]
return ok
}
func (s K8sRuntimeState) 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 K8sRuntimeState.
// 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 K8sRuntimeState) MostRecentPod() Pod {
bestPod := Pod{}
found := false
for _, v := range s.Pods {
if !found || v.isAfter(bestPod) {
bestPod = *v
found = true
}
}
return bestPod
}
func (s K8sRuntimeState) HasOKPodTemplateSpecHash(pod *v1.Pod) bool {
// if it doesn't have a label, just let it through - maybe it's from a CRD w/ no pod template spec
hash, ok := pod.Labels[k8s.TiltPodTemplateHashLabel]
if !ok {
return true
}
return s.DeployedPodTemplateSpecHashSet.Contains(k8s.PodTemplateSpecHash(hash))
}
type Pod struct {
PodID k8s.PodID
Namespace k8s.Namespace
StartedAt time.Time
Status string
Phase v1.PodPhase
// Error messages from the pod state if it's in an error state.
StatusMessages []string
// 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
HasSynclet bool
Containers []Container
// We want to show the user # of restarts since some baseline time
// i.e. Total Restarts - BaselineRestarts
BaselineRestarts int
Conditions []v1.PodCondition
SpanID model.LogSpanID
}
type Container struct {
Name container.Name
ID container.ID
Ports []int32
Ready bool
Running bool
ImageRef reference.Named
Restarts int
}
func (c Container) Empty() bool {
return c.Name == "" && c.ID == ""
}
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
}
func (p Pod) AllContainerPorts() []int32 {
result := make([]int32, 0)
for _, c := range p.Containers {
result = append(result, c.Ports...)
}
return result
}
func (p Pod) AllContainersReady() bool {
if len(p.Containers) == 0 {
return false
}
for _, c := range p.Containers {
if !c.Ready {
return false
}
}
return true
}
func (p Pod) VisibleContainerRestarts() int {
return p.AllContainerRestarts() - p.BaselineRestarts
}
func (p Pod) AllContainerRestarts() int {
result := 0
for _, c := range p.Containers {
result += c.Restarts
}
return result
}
type UIDSet map[types.UID]bool
func NewUIDSet() UIDSet {
return make(map[types.UID]bool)
}
func (s UIDSet) Add(uids ...types.UID) {
for _, uid := range uids {
s[uid] = true
}
}
func (s UIDSet) Contains(uid types.UID) bool {
return s[uid]
}
type PodTemplateSpecHashSet map[k8s.PodTemplateSpecHash]bool
func NewPodTemplateSpecHashSet() PodTemplateSpecHashSet {
return make(map[k8s.PodTemplateSpecHash]bool)
}
func (s PodTemplateSpecHashSet) Add(hashes ...k8s.PodTemplateSpecHash) {
for _, hash := range hashes {
s[hash] = true
}
}
func (s PodTemplateSpecHashSet) Contains(hash k8s.PodTemplateSpecHash) bool {
return s[hash]
}
var runtimeStatusMap = map[string]model.RuntimeStatus{
"Running": model.RuntimeStatusOK,
"ContainerCreating": model.RuntimeStatusPending,
"Pending": model.RuntimeStatusPending,
"PodInitializing": model.RuntimeStatusPending,
"Error": model.RuntimeStatusError,
"CrashLoopBackOff": model.RuntimeStatusError,
"ErrImagePull": model.RuntimeStatusError,
"ImagePullBackOff": model.RuntimeStatusError,
"RunContainerError": model.RuntimeStatusError,
"StartError": model.RuntimeStatusError,
"Completed": model.RuntimeStatusOK,
// If the runtime status hasn't shown up yet, we assume it's pending.
"": model.RuntimeStatusPending,
}