forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.go
174 lines (157 loc) · 5.28 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
package kube
import (
"fmt"
"sort"
"strings"
"time"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
)
// 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)
}
// 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
}
// waits for the pod to become ready using label selector to match the pod
func WaitForPodToBeReady(client *kubernetes.Clientset, selector labels.Selector, namespace string, timeout time.Duration) error {
options := meta_v1.ListOptions{LabelSelector: selector.String()}
return waitForPodSelectorToBeReady(client, namespace, options, timeout)
}
func waitForPodSelectorToBeReady(client *kubernetes.Clientset, namespace string, options meta_v1.ListOptions, timeout time.Duration) error {
w, err := client.CoreV1().Pods(namespace).Watch(options)
if err != nil {
return err
}
defer w.Stop()
condition := func(event watch.Event) (bool, error) {
pod := event.Object.(*v1.Pod)
return IsPodReady(pod), nil
}
_, err = watch.Until(timeout, w, condition)
if err == wait.ErrWaitTimeout {
return fmt.Errorf("pod %s never became ready", options.String())
}
return nil
}
// waits for the pod to become ready using the pod name
func WaitForPodNameToBeReady(client *kubernetes.Clientset, 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(),
}
return waitForPodSelectorToBeReady(client, namespace, options, timeout)
}
func GetReadyPodNames(client *kubernetes.Clientset, 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 _, p := range list.Items {
name := p.Name
if filter == "" || strings.Contains(name, filter) && IsPodReady(&p) {
names = append(names, name)
}
}
sort.Strings(names)
return names, nil
}
func GetPodNames(client *kubernetes.Clientset, 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.Clientset, 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) {
names = append(names, name)
}
}
sort.Strings(names)
return names, m, nil
}
// GetDevPodNames returns the users dev pod names
func GetDevPodNames(client *kubernetes.Clientset, ns string, username 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: LabelDevPodUsername + "=" + username,
})
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
}