/
entitytypes.go
124 lines (112 loc) · 3.1 KB
/
entitytypes.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package actions
import (
libs "github.com/k8guard/k8guardlibs"
"github.com/k8guard/k8guardlibs/k8s"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type ActionableEntity interface {
DoAction()
}
// See http://stackoverflow.com/questions/28800672/how-to-add-new-methods-to-an-existing-type-in-go
type ActionPod libs.Pod
type ActionNamespace libs.Namespace
type ActionDeployment libs.Deployment
type ActionDaemonSet libs.DaemonSet
type ActionIngress libs.Ingress
type ActionJob libs.Job
type ActionCronJob libs.CronJob
func (a ActionPod) DoAction() {
clientset, err := k8s.LoadClientset()
if err != nil {
panic(err)
}
libs.Log.Debug("Deleting Pod ", a.Name, " in namespace ", a.Namespace)
err = clientset.CoreV1().Pods(a.Namespace).Delete(a.Name, &metav1.DeleteOptions{})
if err != nil {
libs.Log.Error(err)
}
}
func (a ActionDeployment) DoAction() {
clientset, err := k8s.LoadClientset()
if err != nil {
panic(err)
}
libs.Log.Debug("Scaling Deployment ", a.Name, " in namespace ", a.Namespace)
kd, err := clientset.AppsV1beta1().Deployments(a.Namespace).Get(a.Name, metav1.GetOptions{})
if err != nil {
libs.Log.Error(err)
return
}
replicas := int32(0)
kd.Spec.Replicas = &replicas
_, err = clientset.AppsV1beta1().Deployments(a.Namespace).Update(kd)
if err != nil {
libs.Log.Error(err)
}
}
func (a ActionNamespace) DoAction() {
clientset, err := k8s.LoadClientset()
if err != nil {
panic(err)
}
libs.Log.Debug("Deleting Namespace ", a.Name)
err = clientset.Namespaces().Delete(a.Name, &metav1.DeleteOptions{})
if err != nil {
libs.Log.Error(err)
}
}
func (a ActionDaemonSet) DoAction() {
clientset, err := k8s.LoadClientset()
if err != nil {
panic(err)
}
libs.Log.Debug("Deleting DaemonSet ", a.Name, " in namespace ", a.Namespace)
err = clientset.ExtensionsV1beta1().DaemonSets(a.Namespace).Delete(a.Name, &metav1.DeleteOptions{})
if err != nil {
libs.Log.Error(err)
}
}
func (a ActionIngress) DoAction() {
clientset, err := k8s.LoadClientset()
if err != nil {
panic(err)
}
libs.Log.Debug("Deleting Ingress ", a.Name, " in namespace ", a.Namespace)
err = clientset.Ingresses(a.Namespace).Delete(a.Name, &metav1.DeleteOptions{})
if err != nil {
libs.Log.Error(err)
}
}
func (a ActionJob) DoAction() {
clientset, err := k8s.LoadClientset()
if err != nil {
panic(err)
}
libs.Log.Debug("Deleting Job ", a.Name, " in namespace ", a.Namespace)
err = clientset.BatchV1().Jobs(a.Namespace).Delete(a.Name, &metav1.DeleteOptions{})
if err != nil {
libs.Log.Error(err)
}
}
func (a ActionCronJob) DoAction() {
if libs.Cfg.IncludeAlpha == false {
libs.Log.Debug("Ignoring CronJob action as alpha features are not enabled ")
return
}
clientset, err := k8s.LoadClientset()
if err != nil {
panic(err)
}
libs.Log.Debug("Disabling CronJob ", a.Name, " in namespace ", a.Namespace)
kcj, err := clientset.BatchV2alpha1().CronJobs(a.Namespace).Get(a.Name, metav1.GetOptions{})
if err != nil {
libs.Log.Error(err)
return
}
suspend := true
kcj.Spec.Suspend = &suspend
_, err = clientset.BatchV2alpha1().CronJobs(a.Namespace).Update(kcj)
if err != nil {
libs.Log.Error(err)
}
}