-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubernetes.go
60 lines (47 loc) · 1.26 KB
/
kubernetes.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
package main
import (
"context"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
var (
allNamespaces = "*"
)
func getClientSet() (*kubernetes.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return clientset, nil
}
func getNamespaces(ctx context.Context, clientSet *kubernetes.Clientset) (namespaces []string, err error) {
nsInf := clientSet.CoreV1().Namespaces()
opts := metav1.ListOptions{}
ns, err := nsInf.List(ctx, opts)
if err != nil {
return nil, err
}
for _, namespace := range ns.Items {
namespaces = append(namespaces, namespace.Name)
}
return namespaces, nil
}
func getSecrets(ctx context.Context, clientSet *kubernetes.Clientset, namespaces ...string) (secrets []corev1.Secret, err error) {
secretItems := make([]corev1.Secret, 0)
for _, namespace := range namespaces {
secretInf := clientSet.CoreV1().Secrets(namespace)
opts := metav1.ListOptions{}
secrets, err := secretInf.List(ctx, opts)
if err != nil {
return nil, err
}
secretItems = append(secretItems, secrets.Items...)
}
return secretItems, nil
}