-
Notifications
You must be signed in to change notification settings - Fork 117
/
pods.go
391 lines (343 loc) · 12.2 KB
/
pods.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
package k8s
import (
"context"
"fmt"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sapiv1 "github.com/lyft/clutch/backend/api/k8s/v1"
)
func (s *svc) DescribePod(ctx context.Context, clientset, cluster, namespace, name string) (*k8sapiv1.Pod, error) {
cs, err := s.manager.GetK8sClientset(ctx, clientset, cluster, namespace)
if err != nil {
return nil, err
}
pods, err := cs.CoreV1().Pods(cs.Namespace()).List(ctx, metav1.ListOptions{
FieldSelector: "metadata.name=" + name,
})
if err != nil {
return nil, err
}
if len(pods.Items) == 1 {
return podDescription(&pods.Items[0], cs.Cluster()), nil
} else if len(pods.Items) > 1 {
return nil, status.Error(codes.FailedPrecondition, "located multiple pods")
}
return nil, status.Error(codes.NotFound, "unable to locate specified pod")
}
func (s *svc) DeletePod(ctx context.Context, clientset, cluster, namespace, name string) error {
cs, err := s.manager.GetK8sClientset(ctx, clientset, cluster, namespace)
if err != nil {
return err
}
return cs.CoreV1().Pods(cs.Namespace()).Delete(ctx, name, metav1.DeleteOptions{})
}
func (s *svc) ListPods(ctx context.Context, clientset, cluster, namespace string, listOpts *k8sapiv1.ListOptions) ([]*k8sapiv1.Pod, error) {
cs, err := s.manager.GetK8sClientset(ctx, clientset, cluster, namespace)
if err != nil {
return nil, err
}
opts, err := ApplyListOptions(listOpts)
if err != nil {
return nil, err
}
podList, err := cs.CoreV1().Pods(cs.Namespace()).List(ctx, opts)
if err != nil {
return nil, err
}
var pods []*k8sapiv1.Pod
for _, p := range podList.Items {
pod := p
pods = append(pods, podDescription(&pod, cs.Cluster()))
}
return pods, nil
}
// Update pod fields if the current field values match with that's described by expectedObjectMetaFields
//
// TODO: add support for updating pod labels
func (s *svc) UpdatePod(ctx context.Context, clientset, cluster, namespace, name string, expectedObjectMetaFields *k8sapiv1.ExpectedObjectMetaFields, objectMetaFields *k8sapiv1.ObjectMetaFields, removeObjectMetaFields *k8sapiv1.RemoveObjectMetaFields) error {
if len(objectMetaFields.GetLabels()) > 0 || len(removeObjectMetaFields.GetLabels()) > 0 {
return status.Error(codes.InvalidArgument, "update of pod labels not implemented")
}
// Ensure that the caller is not trying to delete an annotation and update it at the same time
newAnnotations := objectMetaFields.GetAnnotations()
for _, annotation := range removeObjectMetaFields.GetAnnotations() {
_, annotationIsUpdated := newAnnotations[annotation]
if annotationIsUpdated {
return status.Errorf(codes.InvalidArgument, "annotation '%s' can't be updated and removed at once", annotation)
}
}
cs, err := s.manager.GetK8sClientset(ctx, clientset, cluster, namespace)
if err != nil {
return err
}
pod, err := cs.CoreV1().Pods(cs.Namespace()).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return err
}
// Check that the current state of the pod matches with expectedObjectMetaFields.
//
// If there is a mismatch, checkExpectedObjectMetaFields() will return an error with the list of mismatches.
err = s.checkExpectedObjectMetaFields(expectedObjectMetaFields, pod.GetObjectMeta())
if err != nil {
return err
}
// Update/add annotations
podAnnotations := pod.GetAnnotations()
for annotation, value := range newAnnotations {
podAnnotations[annotation] = value
}
// Delete annotations to be removed
for _, annotation := range removeObjectMetaFields.GetAnnotations() {
delete(podAnnotations, annotation)
}
_, err = cs.CoreV1().Pods(cs.Namespace()).Update(ctx, pod, metav1.UpdateOptions{})
return err
}
func (s *svc) checkExpectedObjectMetaFields(expectedObjectMetaFields *k8sapiv1.ExpectedObjectMetaFields, object metav1.Object) error {
if len(expectedObjectMetaFields.Labels) > 0 {
return status.Error(codes.InvalidArgument, "checking label expectations not implemented")
}
podAnnotations := object.GetAnnotations()
var mismatchedAnnotations []*mismatchedAnnotation
for expectedAnnotation, expectedValue := range expectedObjectMetaFields.GetAnnotations() {
currentValue, annotationIsPresent := podAnnotations[expectedAnnotation]
switch expectedValue.Kind.(type) {
case *k8sapiv1.NullableString_Null:
// Existance precondition not met
if annotationIsPresent {
mismatchedAnnotations = append(
mismatchedAnnotations,
&mismatchedAnnotation{
Annotation: expectedAnnotation,
ExpectedValue: expectedValue.GetValue(),
CurrentValue: currentValue,
},
)
}
case *k8sapiv1.NullableString_Value:
if !annotationIsPresent || expectedValue.GetValue() != currentValue {
// Annotation values mismatched
mismatchedAnnotations = append(
mismatchedAnnotations,
&mismatchedAnnotation{
Annotation: expectedAnnotation,
ExpectedValue: expectedValue.GetValue(),
CurrentValue: currentValue,
},
)
}
}
}
if len(mismatchedAnnotations) == 0 {
return nil
}
return &ExpectedObjectMetaFieldsCheckError{MismatchedAnnotations: mismatchedAnnotations}
}
func podDescription(k8spod *corev1.Pod, cluster string) *k8sapiv1.Pod {
// TODO: There's a mismatch between the serialization of the timestamp here and what's expected
// on the frontend.
// var launch *timestamp.Timestamp
// if converted, err := ptypes.TimestampProto(k8spod.Status.StartTime.Time); err == nil {
// launch = converted
// }
clusterName := GetKubeClusterName(k8spod)
if clusterName == "" {
clusterName = cluster
}
pod := &k8sapiv1.Pod{
Cluster: clusterName,
Namespace: k8spod.Namespace,
Name: k8spod.Name,
Containers: makeContainers(k8spod.Status.ContainerStatuses),
NodeIp: k8spod.Status.HostIP,
PodIp: k8spod.Status.PodIP,
State: protoForPodState(k8spod.Status.Phase),
// StartTime: launch,
Labels: k8spod.Labels,
Annotations: k8spod.Annotations,
StateReason: k8spod.Status.Reason,
PodConditions: makeConditions(k8spod.Status.Conditions),
InitContainers: makeContainers(k8spod.Status.InitContainerStatuses),
Status: getPodStatus(k8spod),
}
// This is here as a workaround since we can't use StartTime
if k8spod.Status.StartTime != nil {
// Note .Unix() gives seconds, not milliseconds
pod.StartTimeMillis = k8spod.Status.StartTime.UnixNano() / 1e6
}
return pod
}
func makeConditions(conditions []corev1.PodCondition) []*k8sapiv1.PodCondition {
podConditions := make([]*k8sapiv1.PodCondition, 0, len(conditions))
for _, condition := range conditions {
cond := &k8sapiv1.PodCondition{
Type: protoForConditionType(condition.Type),
Status: protoForConditionStatus(condition.Status),
}
podConditions = append(podConditions, cond)
}
return podConditions
}
func makeContainers(statuses []corev1.ContainerStatus) []*k8sapiv1.Container {
containers := make([]*k8sapiv1.Container, 0, len(statuses))
for _, status := range statuses {
container := &k8sapiv1.Container{
Name: status.Name,
Image: status.Image,
State: protoForContainerState(status.State),
Ready: status.Ready,
RestartCount: status.RestartCount,
}
switch container.State {
case k8sapiv1.Container_TERMINATED:
container.StateDetails = protoForContainerStateTerminated(status.State.Terminated)
case k8sapiv1.Container_RUNNING:
container.StateDetails = protoForContainerStateRunning(status.State.Running)
case k8sapiv1.Container_WAITING:
container.StateDetails = protoForContainerStateWaiting(status.State.Waiting)
}
containers = append(containers, container)
}
return containers
}
func protoForPodState(state corev1.PodPhase) k8sapiv1.Pod_State {
// Look up value in generated enum map after ensuring consistent case with generated code.
val, ok := k8sapiv1.Pod_State_value[strings.ToUpper(string(state))]
if !ok {
return k8sapiv1.Pod_UNKNOWN
}
return k8sapiv1.Pod_State(val)
}
func protoForContainerState(state corev1.ContainerState) k8sapiv1.Container_State {
switch {
case state.Terminated != nil:
return k8sapiv1.Container_TERMINATED
case state.Running != nil:
return k8sapiv1.Container_RUNNING
case state.Waiting != nil:
return k8sapiv1.Container_WAITING
default:
return k8sapiv1.Container_UNKNOWN
}
}
func protoForContainerStateWaiting(state *corev1.ContainerStateWaiting) *k8sapiv1.Container_StateWaiting {
return &k8sapiv1.Container_StateWaiting{
StateWaiting: &k8sapiv1.StateWaiting{
Reason: state.Reason,
Message: state.Message,
},
}
}
func protoForContainerStateTerminated(state *corev1.ContainerStateTerminated) *k8sapiv1.Container_StateTerminated {
return &k8sapiv1.Container_StateTerminated{
StateTerminated: &k8sapiv1.StateTerminated{
Reason: state.Reason,
Message: state.Message,
ExitCode: state.ExitCode,
Signal: state.Signal,
},
}
}
func protoForContainerStateRunning(state *corev1.ContainerStateRunning) *k8sapiv1.Container_StateRunning {
return &k8sapiv1.Container_StateRunning{
StateRunning: &k8sapiv1.StateRunning{
// FE serialization currently does not support timestamp
// StartTime: state.StartedAt,
},
}
}
func protoForConditionType(conditionType corev1.PodConditionType) k8sapiv1.PodCondition_Type {
switch conditionType {
case corev1.ContainersReady:
return k8sapiv1.PodCondition_CONTAINERS_READY
case corev1.PodInitialized:
return k8sapiv1.PodCondition_INITIALIZED
case corev1.PodReady:
return k8sapiv1.PodCondition_READY
case corev1.PodScheduled:
return k8sapiv1.PodCondition_POD_SCHEDULED
default:
return k8sapiv1.PodCondition_TYPE_UNSPECIFIED
}
}
func protoForConditionStatus(status corev1.ConditionStatus) k8sapiv1.PodCondition_Status {
switch status {
case corev1.ConditionTrue:
return k8sapiv1.PodCondition_TRUE
case corev1.ConditionFalse:
return k8sapiv1.PodCondition_FALSE
case corev1.ConditionUnknown:
return k8sapiv1.PodCondition_UNKNOWN
default:
return k8sapiv1.PodCondition_STATUS_UNSPECIFIED
}
}
func getPodStatus(pod *corev1.Pod) string {
restarts := 0
readyContainers := 0
reason := string(pod.Status.Phase)
if pod.Status.Reason != "" {
reason = pod.Status.Reason
}
initializing := false
for i := range pod.Status.InitContainerStatuses {
container := pod.Status.InitContainerStatuses[i]
restarts += int(container.RestartCount)
switch {
case container.State.Terminated != nil && container.State.Terminated.ExitCode == 0:
continue
case container.State.Terminated != nil:
// initialization is failed
if len(container.State.Terminated.Reason) == 0 {
if container.State.Terminated.Signal != 0 {
reason = fmt.Sprintf("Init: Signal %d", container.State.Terminated.Signal)
} else {
reason = fmt.Sprintf("Init: ExitCode %d", container.State.Terminated.ExitCode)
}
} else {
reason = "Init:" + container.State.Terminated.Reason
}
initializing = true
case container.State.Waiting != nil && len(container.State.Waiting.Reason) > 0 && container.State.Waiting.Reason != "PodInitializing":
reason = fmt.Sprintf("Init: %s", container.State.Waiting.Reason)
initializing = true
default:
reason = fmt.Sprintf("Init: %d/%d", i, len(pod.Spec.InitContainers))
initializing = true
}
break
}
if !initializing {
restarts = 0
for i := len(pod.Status.ContainerStatuses) - 1; i >= 0; i-- {
container := pod.Status.ContainerStatuses[i]
restarts += int(container.RestartCount)
switch {
case container.State.Waiting != nil && container.State.Waiting.Reason != "":
reason = container.State.Waiting.Reason
case container.State.Terminated != nil && container.State.Terminated.Reason != "":
reason = container.State.Terminated.Reason
case container.State.Terminated != nil && container.State.Terminated.Reason == "":
if container.State.Terminated.Signal != 0 {
reason = fmt.Sprintf("Signal: %d", container.State.Terminated.Signal)
} else {
reason = fmt.Sprintf("ExitCode: %d", container.State.Terminated.ExitCode)
}
case container.Ready && container.State.Running != nil:
readyContainers++
}
}
}
if pod.DeletionTimestamp != nil && pod.Status.Reason == "NodeLost" {
reason = string(corev1.PodUnknown)
} else if pod.DeletionTimestamp != nil {
reason = "Terminating"
}
if len(reason) == 0 {
reason = string(corev1.PodUnknown)
}
return reason
}