-
Notifications
You must be signed in to change notification settings - Fork 787
/
pod.go
288 lines (262 loc) · 8.79 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
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
package kube
import (
"context"
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/jenkins-x/jx/v2/pkg/kube/naming"
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
tools_watch "k8s.io/client-go/tools/watch"
)
// credit https://github.com/kubernetes/kubernetes/blob/8719b4a/pkg/api/v1/pod/util.go
// IsPodReady returns true if a pod is ready; false otherwise.
func IsPodReady(pod *v1.Pod) bool {
phase := pod.Status.Phase
if phase != v1.PodRunning || pod.DeletionTimestamp != nil {
return false
}
return IsPodReadyConditionTrue(pod.Status)
}
// IsPodCompleted returns true if a pod is completed (succeeded or failed); false otherwise.
func IsPodCompleted(pod *v1.Pod) bool {
phase := pod.Status.Phase
if phase == v1.PodSucceeded || phase == v1.PodFailed {
return true
}
return false
}
// IsPodSucceeded returns true if a pod is succeeded
func IsPodSucceeded(pod *v1.Pod) bool {
phase := pod.Status.Phase
if phase == v1.PodSucceeded {
return true
}
return false
}
// credit https://github.com/kubernetes/kubernetes/blob/8719b4a/pkg/api/v1/pod/util.go
// IsPodReady retruns true if a pod is ready; false otherwise.
func IsPodReadyConditionTrue(status v1.PodStatus) bool {
condition := GetPodReadyCondition(status)
return condition != nil && condition.Status == v1.ConditionTrue
}
func PodStatus(pod *v1.Pod) string {
if pod.DeletionTimestamp != nil {
return "Terminating"
}
phase := pod.Status.Phase
if IsPodReady(pod) {
return "Ready"
}
return string(phase)
}
// credit https://github.com/kubernetes/kubernetes/blob/8719b4a/pkg/api/v1/pod/util.go
// Extracts the pod ready condition from the given status and returns that.
// Returns nil if the condition is not present.
func GetPodReadyCondition(status v1.PodStatus) *v1.PodCondition {
_, condition := GetPodCondition(&status, v1.PodReady)
return condition
}
// credit https://github.com/kubernetes/kubernetes/blob/8719b4a/pkg/api/v1/pod/util.go
// GetPodCondition extracts the provided condition from the given status and returns that.
// Returns nil and -1 if the condition is not present, and the index of the located condition.
func GetPodCondition(status *v1.PodStatus, conditionType v1.PodConditionType) (int, *v1.PodCondition) {
if status == nil {
return -1, nil
}
for i := range status.Conditions {
if status.Conditions[i].Type == conditionType {
return i, &status.Conditions[i]
}
}
return -1, nil
}
// GetCurrentPod returns the current pod the code is running in or nil if it cannot be deduced
func GetCurrentPod(kubeClient kubernetes.Interface, ns string) (*v1.Pod, error) {
name := os.Getenv("HOSTNAME")
if name == "" {
return nil, nil
}
name = naming.ToValidName(name)
return kubeClient.CoreV1().Pods(ns).Get(name, meta_v1.GetOptions{})
}
func waitForPodSelector(client kubernetes.Interface, namespace string, options meta_v1.ListOptions,
timeout time.Duration, condition func(event watch.Event) (bool, error)) error {
w, err := client.CoreV1().Pods(namespace).Watch(options)
if err != nil {
return err
}
defer w.Stop()
ctx, _ := context.WithTimeout(context.Background(), timeout)
_, err = tools_watch.UntilWithoutRetry(ctx, w, condition)
if err == wait.ErrWaitTimeout {
return fmt.Errorf("pod %s never became ready", options.String())
}
return nil
}
// HasContainerStarted returns true if the given Container has started running
func HasContainerStarted(pod *v1.Pod, idx int) bool {
if pod == nil {
return false
}
_, statuses, _ := GetContainersWithStatusAndIsInit(pod)
if idx >= len(statuses) {
return false
}
ic := statuses[idx]
if ic.State.Running != nil || ic.State.Terminated != nil {
return true
}
return false
}
// waits for the pod to become ready using the pod name
func WaitForPodNameToBeReady(client kubernetes.Interface, namespace string, name string, timeout time.Duration) error {
options := meta_v1.ListOptions{
// TODO
//FieldSelector: fields.OneTermEqualSelector(api.ObjectNameField, name).String(),
FieldSelector: fields.OneTermEqualSelector("metadata.name", name).String(),
}
condition := func(event watch.Event) (bool, error) {
pod := event.Object.(*v1.Pod)
return IsPodReady(pod), nil
}
return waitForPodSelector(client, namespace, options, timeout, condition)
}
// WaitForPodNameToBeComplete waits for the pod to complete (succeed or fail) using the pod name
func WaitForPodNameToBeComplete(client kubernetes.Interface, namespace string, name string,
timeout time.Duration) error {
options := meta_v1.ListOptions{
// TODO
//FieldSelector: fields.OneTermEqualSelector(api.ObjectNameField, name).String(),
FieldSelector: fields.OneTermEqualSelector("metadata.name", name).String(),
}
condition := func(event watch.Event) (bool, error) {
pod := event.Object.(*v1.Pod)
return IsPodCompleted(pod), nil
}
return waitForPodSelector(client, namespace, options, timeout, condition)
}
func GetPodNames(client kubernetes.Interface, ns string, filter string) ([]string, error) {
names := []string{}
list, err := client.CoreV1().Pods(ns).List(meta_v1.ListOptions{})
if err != nil {
return names, fmt.Errorf("Failed to load Pods %s", err)
}
for _, d := range list.Items {
name := d.Name
if filter == "" || strings.Contains(name, filter) {
names = append(names, name)
}
}
sort.Strings(names)
return names, nil
}
func GetPods(client kubernetes.Interface, ns string, filter string) ([]string, map[string]*v1.Pod, error) {
names := []string{}
m := map[string]*v1.Pod{}
list, err := client.CoreV1().Pods(ns).List(meta_v1.ListOptions{})
if err != nil {
return names, m, fmt.Errorf("Failed to load Pods %s", err)
}
for _, d := range list.Items {
c := d
name := d.Name
m[name] = &c
if filter == "" || strings.Contains(name, filter) && d.DeletionTimestamp == nil {
names = append(names, name)
}
}
sort.Strings(names)
return names, m, nil
}
func GetPodsWithLabels(client kubernetes.Interface, ns string, selector string) ([]string, map[string]*v1.Pod, error) {
names := []string{}
m := map[string]*v1.Pod{}
list, err := client.CoreV1().Pods(ns).List(meta_v1.ListOptions{
LabelSelector: selector,
})
if err != nil {
return names, m, fmt.Errorf("Failed to load Pods %s", err)
}
for _, d := range list.Items {
c := d
name := d.Name
m[name] = &c
if d.DeletionTimestamp == nil {
names = append(names, name)
}
}
sort.Strings(names)
return names, m, nil
}
// GetDevPodNames returns the users dev pod names. If username is blank, all devpod names will be returned
func GetDevPodNames(client kubernetes.Interface, ns string, username string) ([]string, map[string]*v1.Pod, error) {
names := []string{}
m := map[string]*v1.Pod{}
listOptions := meta_v1.ListOptions{}
if username != "" {
listOptions.LabelSelector = LabelDevPodUsername + "=" + username
} else {
listOptions.LabelSelector = LabelDevPodName
}
list, err := client.CoreV1().Pods(ns).List(listOptions)
if err != nil {
return names, m, fmt.Errorf("Failed to load Pods %s", err)
}
for _, d := range list.Items {
c := d
name := d.Name
m[name] = &c
names = append(names, name)
}
sort.Strings(names)
return names, m, nil
}
// GetPodRestars returns the number of restarts of a POD
func GetPodRestarts(pod *v1.Pod) int32 {
var restarts int32
statuses := pod.Status.ContainerStatuses
if len(statuses) == 0 {
return restarts
}
for _, status := range statuses {
restarts += status.RestartCount
}
return restarts
}
// GetContainersWithStatusAndIsInit gets the containers in the pod, either init containers or non-init depending on whether
// non-init containers are present, and a flag as to whether this list of containers are init containers or not.
func GetContainersWithStatusAndIsInit(pod *v1.Pod) ([]v1.Container, []v1.ContainerStatus, bool) {
isInit := true
allContainers := pod.Spec.InitContainers
statuses := pod.Status.InitContainerStatuses
containers := pod.Spec.Containers
if len(containers) > 1 && len(pod.Status.ContainerStatuses) == len(containers) {
isInit = false
// Add the non-init containers
// If there's a "nop" container at the end, the pod was created with before Tekton 0.5.x, so trim off the no-op container at the end of the list.
if containers[len(containers)-1].Name == "nop" {
allContainers = append(allContainers, containers[:len(containers)-1]...)
} else {
allContainers = append(allContainers, containers...)
}
// Since status ordering is unpredictable, don't trim here - we'll be sorting/filtering below anyway.
statuses = append(statuses, pod.Status.ContainerStatuses...)
}
var sortedStatuses []v1.ContainerStatus
for _, c := range allContainers {
for _, cs := range statuses {
if cs.Name == c.Name {
sortedStatuses = append(sortedStatuses, cs)
break
}
}
}
return allContainers, sortedStatuses, isInit
}