-
Notifications
You must be signed in to change notification settings - Fork 16
/
pods.go
64 lines (52 loc) · 1.5 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
package inventory
import (
"context"
"fmt"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/anchore/k8s-inventory/internal/tracker"
"github.com/anchore/k8s-inventory/pkg/client"
)
func FetchPodsInNamespace(c client.Client, batchSize, timeout int64, namespace string) ([]v1.Pod, error) {
defer tracker.TrackFunctionTime(time.Now(), "Fetching pods in namespace")
var podList []v1.Pod
cont := ""
for {
opts := metav1.ListOptions{
Limit: batchSize,
Continue: cont,
TimeoutSeconds: &timeout,
}
list, err := c.Clientset.CoreV1().Pods(namespace).List(context.Background(), opts)
if err != nil {
return nil, fmt.Errorf("failed to list pods in namespace %s: %w", namespace, err)
}
podList = append(podList, list.Items...)
cont = list.GetListMeta().GetContinue()
if cont == "" {
break
}
}
return podList, nil
}
func ProcessPods(pods []v1.Pod, namespaceUID string, nodes map[string]Node, includeAnnotations, includeLabels []string, disableMetadata bool) []Pod {
var podList []Pod
for _, p := range pods {
pod := Pod{
Name: p.ObjectMeta.Name,
UID: string(p.UID),
NamespaceUID: namespaceUID,
}
if !disableMetadata {
pod.Labels = processAnnotationsOrLabels(p.Labels, includeLabels)
pod.Annotations = processAnnotationsOrLabels(p.Annotations, includeAnnotations)
}
node, ok := nodes[p.Spec.NodeName]
if ok {
pod.NodeUID = node.UID
}
podList = append(podList, pod)
}
return podList
}