-
Notifications
You must be signed in to change notification settings - Fork 9
/
podstatus.go
79 lines (67 loc) · 2.47 KB
/
podstatus.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package kubeobserver
import (
"context"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "k8s.io/client-go/kubernetes"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/schema/orchestration"
"namespacelabs.dev/foundation/schema/runtime"
)
func PodStatusToWaitStatus(ns, name string, ps v1.PodStatus) *orchestration.Event_WaitStatus {
if ps.Phase == v1.PodPending && len(ps.ContainerStatuses) == 0 {
return &orchestration.Event_WaitStatus{Description: "Pending..."}
}
_, isReady := MatchPodCondition(ps, v1.PodReady)
cw := &runtime.ContainerWaitStatus{
IsReady: isReady,
}
for _, container := range ps.ContainerStatuses {
if status := StatusToDiagnostic(container); status != nil {
cw.Containers = append(cw.Containers, &runtime.ContainerUnitWaitStatus{
Reference: kubedef.MakePodRef(ns, name, container.Name, nil),
Name: container.Name,
Status: status,
})
}
}
for _, init := range ps.InitContainerStatuses {
if status := StatusToDiagnostic(init); status != nil {
cw.Initializers = append(cw.Initializers, &runtime.ContainerUnitWaitStatus{
Reference: kubedef.MakePodRef(ns, name, init.Name, nil),
Name: init.Name,
Status: status,
})
}
}
return &orchestration.Event_WaitStatus{
Description: cw.WaitStatus(),
Opaque: protos.WrapAnyOrDie(cw),
}
}
func podWaitingStatus(ctx context.Context, cli *k8s.Clientset, namespace string, owner string) ([]*orchestration.Event_WaitStatus, error) {
// TODO explore how to limit the list here (e.g. through labels or by using a different API)
pods, err := cli.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: kubedef.SerializeSelector(kubedef.ManagedByUs())})
if err != nil {
return nil, fnerrors.InvocationError("kubernetes", "unable to list pods: %w", err)
}
var statuses []*orchestration.Event_WaitStatus
for _, pod := range pods.Items {
owned := false
for _, ref := range pod.ObjectMeta.OwnerReferences {
if ref.Name == owner {
owned = true
}
}
if !owned {
continue
}
statuses = append(statuses, PodStatusToWaitStatus(pod.Namespace, pod.Name, pod.Status))
}
return statuses, nil
}