/
k8sstatic.go
51 lines (44 loc) · 1.11 KB
/
k8sstatic.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
package k8sinterface
import (
"context"
"github.com/armosec/utils-go/boolutils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
func IsLabel(labels map[string]string, key string) *bool {
if len(labels) == 0 {
return nil
}
var k bool
if l, ok := labels[key]; ok {
if boolutils.StringToBool(l) {
k = true
} else if !boolutils.StringToBool(l) {
k = false
}
return &k
}
return nil
}
func SetLabel(labels map[string]string, key string, val bool) {
if labels == nil {
return
}
labels[key] = boolutils.BoolToString(val)
}
func (k8sAPI *KubernetesApi) ListPods(namespace string, podLabels map[string]string, fieldSelector string) (*corev1.PodList, error) {
listOptions := metav1.ListOptions{}
if len(podLabels) > 0 {
set := labels.Set(podLabels)
listOptions.LabelSelector = set.AsSelector().String()
}
if fieldSelector != "" {
listOptions.FieldSelector = fieldSelector
}
pods, err := k8sAPI.KubernetesClient.CoreV1().Pods(namespace).List(context.Background(), listOptions)
if err != nil {
return nil, err
}
return pods, nil
}