-
Notifications
You must be signed in to change notification settings - Fork 163
/
pods.go
193 lines (162 loc) · 5.61 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package k8s provides a client for interacting with a Kubernetes cluster.
package k8s
import (
"context"
"sort"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const waitLimit = 30
// GeneratePod creates a new pod without adding it to the k8s cluster.
func (k *K8s) GeneratePod(name, namespace string) *corev1.Pod {
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: make(Labels),
},
}
return pod
}
// DeletePod removes a pod from the cluster by namespace & name.
func (k *K8s) DeletePod(namespace string, name string) error {
deleteGracePeriod := int64(0)
deletePolicy := metav1.DeletePropagationForeground
err := k.Clientset.CoreV1().Pods(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{
GracePeriodSeconds: &deleteGracePeriod,
PropagationPolicy: &deletePolicy,
})
if err != nil {
return err
}
for {
// Keep checking for the pod to be deleted
_, err := k.Clientset.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return nil
}
time.Sleep(1 * time.Second)
}
}
// DeletePods removes a collection of pods from the cluster by pod lookup.
func (k *K8s) DeletePods(target PodLookup) error {
deleteGracePeriod := int64(0)
deletePolicy := metav1.DeletePropagationForeground
return k.Clientset.CoreV1().Pods(target.Namespace).DeleteCollection(context.TODO(),
metav1.DeleteOptions{
GracePeriodSeconds: &deleteGracePeriod,
PropagationPolicy: &deletePolicy,
},
metav1.ListOptions{
LabelSelector: target.Selector,
},
)
}
// CreatePod inserts the given pod into the cluster.
func (k *K8s) CreatePod(pod *corev1.Pod) (*corev1.Pod, error) {
createOptions := metav1.CreateOptions{}
return k.Clientset.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, createOptions)
}
// GetAllPods returns a list of pods from the cluster for all namespaces.
func (k *K8s) GetAllPods() (*corev1.PodList, error) {
return k.GetPods(corev1.NamespaceAll, metav1.ListOptions{})
}
// GetPods returns a list of pods from the cluster by namespace.
func (k *K8s) GetPods(namespace string, listOpts metav1.ListOptions) (*corev1.PodList, error) {
return k.Clientset.CoreV1().Pods(namespace).List(context.TODO(), listOpts)
}
// WaitForPodsAndContainers attempts to find pods matching the given selector and optional inclusion filter
// It will wait up to 90 seconds for the pods to be found and will return a list of matching pod names
// If the timeout is reached, an empty list will be returned.
func (k *K8s) WaitForPodsAndContainers(target PodLookup, include PodFilter) []corev1.Pod {
for count := 0; count < waitLimit; count++ {
pods, err := k.Clientset.CoreV1().Pods(target.Namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: target.Selector,
})
if err != nil {
k.Log("Unable to find matching pods: %w", err)
break
}
k.Log("Found %d pods for target %#v", len(pods.Items), target)
var readyPods = []corev1.Pod{}
// Sort the pods from newest to oldest
sort.Slice(pods.Items, func(i, j int) bool {
return pods.Items[i].CreationTimestamp.After(pods.Items[j].CreationTimestamp.Time)
})
for _, pod := range pods.Items {
k.Log("Testing pod %q", pod.Name)
// If an include function is provided, only keep pods that return true
if include != nil && !include(pod) {
continue
}
// Handle container targeting
if target.Container != "" {
k.Log("Testing pod %q for container %q", pod.Name, target.Container)
var matchesInitContainer bool
// Check the status of initContainers for a running match
for _, initContainer := range pod.Status.InitContainerStatuses {
isRunning := initContainer.State.Running != nil
if isRunning && initContainer.Name == target.Container {
// On running match in initContainer break this loop
matchesInitContainer = true
readyPods = append(readyPods, pod)
break
}
}
// Don't check any further if there's already a match
if matchesInitContainer {
continue
}
// Check the status of regular containers for a running match
for _, container := range pod.Status.ContainerStatuses {
isRunning := container.State.Running != nil
if isRunning && container.Name == target.Container {
readyPods = append(readyPods, pod)
}
}
} else {
status := pod.Status.Phase
k.Log("Testing pod %q phase, want (%q) got (%q)", pod.Name, corev1.PodRunning, status)
// Regular status checking without a container
if status == corev1.PodRunning {
readyPods = append(readyPods, pod)
}
}
}
if len(readyPods) > 0 {
return readyPods
}
time.Sleep(3 * time.Second)
}
k.Log("Pod lookup timeout exceeded")
return []corev1.Pod{}
}
// FindPodContainerPort will find a pod's container port from a service and return it.
//
// Returns 0 if no port is found.
func (k *K8s) FindPodContainerPort(svc corev1.Service) int {
selectorLabelsOfPods := MakeLabels(svc.Spec.Selector)
pods := k.WaitForPodsAndContainers(PodLookup{
Namespace: svc.Namespace,
Selector: selectorLabelsOfPods,
}, nil)
for _, pod := range pods {
// Find the matching name on the port in the pod
for _, container := range pod.Spec.Containers {
for _, port := range container.Ports {
if port.Name == svc.Spec.Ports[0].TargetPort.String() {
return int(port.ContainerPort)
}
}
}
}
return 0
}