-
Notifications
You must be signed in to change notification settings - Fork 10
/
pod.go
185 lines (154 loc) · 3.89 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
package pod
import (
"context"
"fmt"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/triton-io/triton/pkg/kube/types/workload"
"github.com/triton-io/triton/pkg/setting"
"github.com/triton-io/triton/pkg/utils/strconv"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
)
// Pod is the wrapper for corev1.Pod type.
type Pod struct {
*corev1.Pod
*workload.Base
}
var _ workload.Interface = &Pod{}
func FromPod(p *corev1.Pod) *Pod {
if p == nil {
return nil
}
pod := &Pod{
Pod: p,
}
pod.Base = &workload.Base{Interface: pod}
return pod
}
// Unwrap returns the appsv1.Pod object.
func (p *Pod) Unwrap() *corev1.Pod {
return p.Pod
}
func (p *Pod) GetUpdateRevision() string {
l := p.GetLabels()
for k, v := range l {
if k == appsv1.ControllerRevisionHashLabelKey {
return v[strings.LastIndex(v, "-")+1:]
}
}
return ""
}
func (p *Pod) GetHostIP() string {
return p.Status.HostIP
}
func (p *Pod) GetPodIP() string {
return p.Status.PodIP
}
func (p *Pod) String() string {
return fmt.Sprintf("%s/%s", p.GetNamespace(), p.GetName())
}
func (p *Pod) GetPhase() corev1.PodPhase {
if p.Ready() {
return setting.PodReady
}
if p.ContainersReady() {
return setting.ContainersReady
}
if p.Failed() {
return setting.PodFailed
}
return p.Status.Phase
}
func (p *Pod) Ready() bool {
cs := p.Status.Conditions
for _, c := range cs {
if c.Type == corev1.PodReady {
return c.Status == corev1.ConditionTrue
}
}
return false
}
// For a Pod that uses custom conditions, that Pod is evaluated to be ready only when both the following statements apply:
// All containers in the Pod are ready.
// All conditions specified in readinessGates are True.
// When a Pod's containers are Ready but at least one custom condition is missing or False, the kubelet sets the Pod's condition to ContainersReady.
// See details here: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-readiness-status
func (p *Pod) ContainersReady() bool {
cs := p.Status.Conditions
for _, c := range cs {
if c.Type == corev1.ContainersReady {
return c.Status == corev1.ConditionTrue
}
}
return false
}
func (p *Pod) Failed() bool {
if p.Ready() {
return false
}
return p.Restarted()
}
func (p *Pod) Restarted() bool {
for _, cs := range p.Status.ContainerStatuses {
if cs.RestartCount > 0 {
return true
}
}
return false
}
func (p *Pod) GetAppID() int {
podLabels := p.GetLabels()
if appID, ok := podLabels[setting.AppIDLabel]; ok {
return strconv.MustAtoi(appID)
}
return 0
}
func (p *Pod) GetGroupID() int {
podLabels := p.GetLabels()
if groupID, ok := podLabels[setting.GroupIDLabel]; ok {
return strconv.MustAtoi(groupID)
}
return 0
}
func (p *Pod) GetInstanceName() string {
podLabels := p.GetLabels()
if ins, ok := podLabels[setting.AppInstanceLabel]; ok {
return ins
}
return ""
}
func (p *Pod) GetContainers() []corev1.Container {
return p.Spec.Containers
}
func (p *Pod) PodReady() bool {
for _, c := range p.Status.Conditions {
if c.Type == setting.PodReady {
return c.Status == corev1.ConditionTrue
}
}
return false
}
func SetPodReadinessGate(ns, name string, cl client.Client) error {
patchBytes := []byte(fmt.Sprintf(`{"status":{"conditions":[{"type":"%s", "status":"True"}]}}`, setting.PodReadinessGate))
return patchPodStatus(ns, name, patchBytes, cl)
}
func DeletePod(ns, name string, cl client.Client) error {
err := cl.Delete(context.TODO(), &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
})
return client.IgnoreNotFound(err)
}
func patchPodStatus(ns, name string, patchBytes []byte, cl client.Client) error {
return cl.Status().Patch(context.TODO(), &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
}, client.RawPatch(types.StrategicMergePatchType, patchBytes))
}