-
Notifications
You must be signed in to change notification settings - Fork 2
/
getOwnerAnno.go
94 lines (83 loc) · 2.28 KB
/
getOwnerAnno.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package owlk8s
import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/klog"
)
func getOwnerAnno(ns, podName, nodename string, clientset *kubernetes.Clientset) (string, bool) {
var annoPrefix = "owlk8s/"
var ignoreAnno = annoPrefix + "ignore"
var anno bool
podVal, err := clientset.CoreV1().Pods(ns).
Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
klog.Exitln("Error getting pods")
}
if val, ok := podVal.GetAnnotations()[ignoreAnno]; ok && val == "true" {
anno = true
} else if getNSAnno(ns, clientset, ignoreAnno) {
anno = true
} else if getNodeAnno(clientset, nodename, ignoreAnno) {
anno = true
} else {
anno = false
}
var name string
for _, or := range podVal.OwnerReferences {
if or.Controller != nil && *or.Controller {
// klog.Infoln("KIND", or.Kind)
// Aici am ramas. De implementat astea. Pt fiecare caz
switch or.Kind {
case "ReplicaSet":
name = getOwnerRS(ns, or.Name, clientset)
case "StatefulSet":
name = or.Name
case "DaemonSet":
name = or.Name
}
}
}
return name, anno
}
func getOwnerRS(ns, rsName string, clientset *kubernetes.Clientset) string {
var name string
rsVal, err := clientset.AppsV1().ReplicaSets(ns).
Get(context.TODO(), rsName, metav1.GetOptions{})
if err != nil {
klog.Exitln("Error getting ReplicaSets.")
}
for _, or := range rsVal.OwnerReferences {
if or.Controller != nil && *or.Controller {
if or.Kind == "Deployment" {
name = or.Name
} else {
name = rsName
}
}
}
return name
}
func getNSAnno(ns string, clientset *kubernetes.Clientset, ignoreAnno string) bool {
nsVal, err := clientset.CoreV1().Namespaces().
Get(context.TODO(), ns, metav1.GetOptions{})
if err != nil {
klog.Exitln("Error getting namespaces.")
}
if val, ok := nsVal.GetAnnotations()[ignoreAnno]; ok && val == "true" {
return true
}
return false
}
func getNodeAnno(clientset *kubernetes.Clientset, nodename, ignoreAnno string) bool {
nodeVal, err := clientset.CoreV1().Nodes().
Get(context.TODO(), nodename, metav1.GetOptions{})
if err != nil {
klog.Exitln("Error getting nodes.")
}
if val, ok := nodeVal.GetAnnotations()[ignoreAnno]; ok && val == "true" {
return true
}
return false
}