This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pods.go
66 lines (54 loc) · 1.53 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
package kubernetes
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// Pod represents a kubernetes pod
type Pod struct {
*corev1.Pod
}
// PodOnNode filters the pods to find by a specific node name
func PodOnNode(name string) func(*metav1.ListOptions) {
return func(o *metav1.ListOptions) {
o.FieldSelector = mergeSelectors(o.FieldSelector, fmt.Sprintf("spec.nodeName=%q", name))
}
}
// PodNotTerminated filters the specific pods to find by not being terminated
func PodNotTerminated() func(*metav1.ListOptions) {
return func(o *metav1.ListOptions) {
o.FieldSelector = mergeSelectors(o.FieldSelector, fmt.Sprintf("status.phase!=%s,status.phase!=%s", string(corev1.PodSucceeded), string(corev1.PodFailed)))
}
}
// GetPods lists all pods with optional filters
func GetPods(client kubernetes.Interface, opts ...func(*metav1.ListOptions)) ([]*Pod, error) {
options := &metav1.ListOptions{}
for _, opt := range opts {
opt(options)
}
kp, err := client.CoreV1().Pods(metav1.NamespaceAll).List(*options)
if err != nil {
return nil, err
}
var pods []*Pod
for _, p := range kp.Items {
controller := metav1.GetControllerOf(&p)
if controller != nil && controller.Kind == "DaemonSet" {
continue
}
pods = append(pods, &Pod{p.DeepCopy()})
}
return pods, nil
}
func mergeSelectors(s ...string) string {
result := []string{}
for _, v := range s {
if v == "" {
continue
}
result = append(result, v)
}
return strings.Join(result, ",")
}