/
pod.go
255 lines (248 loc) · 7.55 KB
/
pod.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
package monitor
import (
"context"
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
func startPodMonitoring(ctx context.Context, m Recorder, client kubernetes.Interface) {
podInformer := cache.NewSharedIndexInformer(
NewErrorRecordingListWatcher(m, &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
items, err := client.CoreV1().Pods("").List(options)
if err == nil {
last := 0
for i := range items.Items {
item := &items.Items[i]
if !filterToSystemNamespaces(item) {
continue
}
if i != last {
items.Items[last] = *item
last++
}
}
items.Items = items.Items[:last]
}
return items, err
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
w, err := client.CoreV1().Pods("").Watch(options)
if err == nil {
w = watch.Filter(w, func(in watch.Event) (watch.Event, bool) {
return in, filterToSystemNamespaces(in.Object)
})
}
return w, err
},
}),
&corev1.Pod{},
time.Hour,
nil,
)
m.AddSampler(func(now time.Time) []*Condition {
var conditions []*Condition
for _, obj := range podInformer.GetStore().List() {
pod, ok := obj.(*corev1.Pod)
if !ok {
continue
}
if pod.Status.Phase == "Pending" {
if now.Sub(pod.CreationTimestamp.Time) > time.Minute {
conditions = append(conditions, &Condition{
Level: Warning,
Locator: locatePod(pod),
Message: "pod has been pending longer than a minute",
})
}
}
}
return conditions
})
podChangeFns := []func(pod, oldPod *corev1.Pod) []Condition{
// check phase transitions
func(pod, oldPod *corev1.Pod) []Condition {
new, old := pod.Status.Phase, oldPod.Status.Phase
if new == old || len(old) == 0 {
return nil
}
var conditions []Condition
switch {
case new == corev1.PodPending && old != corev1.PodUnknown:
switch {
case pod.DeletionTimestamp != nil:
conditions = append(conditions, Condition{
Level: Warning,
Locator: locatePod(pod),
Message: fmt.Sprintf("invariant violation (bug): pod should not transition %s->%s even when terminated", old, new),
})
case len(pod.Annotations["kubernetes.io/config.mirror"]) > 0:
conditions = append(conditions, Condition{
Level: Warning,
Locator: locatePod(pod),
Message: fmt.Sprintf("invariant violation (bug): static pod should not transition %s->%s with same UID", old, new),
})
default:
conditions = append(conditions, Condition{
Level: Error,
Locator: locatePod(pod),
Message: fmt.Sprintf("invariant violation: pod may not transition %s->%s", old, new),
})
}
case new == corev1.PodUnknown:
conditions = append(conditions, Condition{
Level: Warning,
Locator: locatePod(pod),
Message: fmt.Sprintf("pod moved to the Unknown phase"),
})
case new == corev1.PodFailed && old != corev1.PodFailed:
for _, s := range pod.Status.InitContainerStatuses {
if t := s.State.Terminated; t != nil && t.ExitCode != 0 {
conditions = append(conditions, Condition{
Level: Error,
Locator: locatePodContainer(pod, s.Name),
Message: fmt.Sprintf("container exited with code %d (%s): %s", t.ExitCode, t.Reason, t.Message),
})
}
}
for _, s := range pod.Status.ContainerStatuses {
if t := s.State.Terminated; t != nil && t.ExitCode != 0 {
conditions = append(conditions, Condition{
Level: Error,
Locator: locatePodContainer(pod, s.Name),
Message: fmt.Sprintf("init container exited with code %d (%s): %s", t.ExitCode, t.Reason, t.Message),
})
}
}
}
return conditions
},
// check for transitions to being deleted
func(pod, oldPod *corev1.Pod) []Condition {
var conditions []Condition
if pod.DeletionGracePeriodSeconds != nil && oldPod.DeletionGracePeriodSeconds == nil {
conditions = append(conditions, Condition{
Level: Warning,
Locator: locatePod(pod),
Message: fmt.Sprintf("graceful deletion within %ds", *pod.DeletionGracePeriodSeconds),
})
}
if pod.DeletionGracePeriodSeconds == nil && oldPod.DeletionGracePeriodSeconds != nil {
conditions = append(conditions, Condition{
Level: Error,
Locator: locatePod(pod),
Message: "invariant violation: pod was marked for deletion and then deletion grace period was cleared",
})
}
return conditions
},
// check restarts, readiness drop outs, or other status changes
func(pod, oldPod *corev1.Pod) []Condition {
var conditions []Condition
for i := range pod.Status.ContainerStatuses {
s := &pod.Status.ContainerStatuses[i]
previous := findContainerStatus(oldPod.Status.ContainerStatuses, s.Name, i)
if previous == nil {
continue
}
if t := s.State.Terminated; t != nil && previous.State.Terminated == nil && t.ExitCode != 0 {
conditions = append(conditions, Condition{
Level: Error,
Locator: locatePodContainer(pod, s.Name),
Message: fmt.Sprintf("container exited with code %d (%s): %s", t.ExitCode, t.Reason, t.Message),
})
}
if s.RestartCount != previous.RestartCount {
conditions = append(conditions, Condition{
Level: Warning,
Locator: locatePodContainer(pod, s.Name),
Message: "container restarted",
})
}
if s.State.Terminated == nil && previous.Ready && !s.Ready {
conditions = append(conditions, Condition{
Level: Warning,
Locator: locatePodContainer(pod, s.Name),
Message: "container stopped being ready",
})
}
}
for i := range pod.Status.InitContainerStatuses {
s := &pod.Status.InitContainerStatuses[i]
previous := findContainerStatus(oldPod.Status.InitContainerStatuses, s.Name, i)
if previous == nil {
continue
}
if t := s.State.Terminated; t != nil && previous.State.Terminated == nil && t.ExitCode != 0 {
conditions = append(conditions, Condition{
Level: Error,
Locator: locatePodContainer(pod, s.Name),
Message: fmt.Sprintf("init container exited with code %d (%s): %s", t.ExitCode, t.Reason, t.Message),
})
}
if s.RestartCount != previous.RestartCount {
conditions = append(conditions, Condition{
Level: Warning,
Locator: locatePodContainer(pod, s.Name),
Message: "init container restarted",
})
}
}
return conditions
},
}
startTime := time.Now().Add(-time.Minute)
podInformer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pod, ok := obj.(*corev1.Pod)
if !ok {
return
}
// filter out old pods so our monitor doesn't send a big chunk
// of pod creations
if pod.CreationTimestamp.Time.Before(startTime) {
return
}
m.Record(Condition{
Level: Info,
Locator: locatePod(pod),
Message: "created",
})
},
DeleteFunc: func(obj interface{}) {
pod, ok := obj.(*corev1.Pod)
if !ok {
return
}
m.Record(Condition{
Level: Warning,
Locator: locatePod(pod),
Message: "deleted",
})
},
UpdateFunc: func(old, obj interface{}) {
pod, ok := obj.(*corev1.Pod)
if !ok {
return
}
oldPod, ok := old.(*corev1.Pod)
if !ok {
return
}
if pod.UID != oldPod.UID {
return
}
for _, fn := range podChangeFns {
m.Record(fn(pod, oldPod)...)
}
},
},
)
go podInformer.Run(ctx.Done())
}