/
namespaces.go
47 lines (38 loc) · 1.02 KB
/
namespaces.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
package pkg
import (
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func (h *Handler) GetNamespaces() (ns []string, err error) {
if h.ExporterConfig.Namespace != "" {
ns = append(ns, h.ExporterConfig.Namespace)
return
}
k8sClient, err := kubernetes.NewForConfig(h.KubeConfig)
if err != nil {
return
}
namespaces, err := k8sClient.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
return
}
h.Logger.Debug("retrieved namespaces from k8s", zap.Int("count", len(namespaces.Items)))
for _, namespace := range namespaces.Items {
if contains(h.ExporterConfig.IgnoreNamespaces, namespace.Name) {
h.Logger.Debug("skipping namespace", zap.String("namespace", namespace.Name))
continue
}
h.Logger.Debug("adding namespace to watch", zap.String("namespace", namespace.Name))
ns = append(ns, namespace.Name)
}
return
}
func contains(sl []string, name string) bool {
for _, value := range sl {
if value == name {
return true
}
}
return false
}