-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
pod.go
38 lines (31 loc) · 813 Bytes
/
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
package k8s
import (
"sort"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// PodList returns Kubernetes pods
func (c *Client) PodList(opts metav1.ListOptions) ([]v1.Pod, error) {
var podList []v1.Pod
for _, ns := range c.namespaces {
pods, err := c.API.CoreV1().Pods(ns).List(c.ctx, opts)
if err != nil {
return nil, err
}
for _, pod := range pods.Items {
podList = appendPod(podList, pod)
}
}
sort.Slice(podList, func(i, j int) bool {
return podList[i].Name < podList[j].Name
})
return podList, nil
}
func appendPod(pods []v1.Pod, i v1.Pod) []v1.Pod {
for _, pod := range pods {
if len(pod.OwnerReferences) > 0 && len(i.OwnerReferences) > 0 && pod.OwnerReferences[0].UID == i.OwnerReferences[0].UID {
return pods
}
}
return append(pods, i)
}