This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
239 lines (179 loc) · 4.83 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"flag"
"fmt"
"log"
"path/filepath"
"time"
"k8s.io/api/core/v1"
"os"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/helm/pkg/helm"
)
const (
tillerHost = "tiller-deploy.kube-system:44134"
)
func authIncluster() *rest.Config {
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
return config
}
func authLocal() *rest.Config {
// //testing locally
var kubeconfig *string
if home := homeDir(); home != "" {
fmt.Println(filepath.Join(home, ".kube", "config"))
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
fmt.Println("error build config")
panic(err.Error())
}
// create the clientset
// clientset, err := kubernetes.NewForConfig(config)
// if err != nil {
// fmt.Println("error config")
// panic(err.Error())
// }
return config
}
func cleanupHelms(revnamespaces []string) {
for _, revnamespace := range revnamespaces {
fmt.Println(revnamespace)
cleanupHelm(revnamespace)
}
}
//clean up using helm
func cleanupHelm(revnamespace string) {
helmClient := helm.NewClient(helm.Host(tillerHost))
Releases, err := helmClient.ListReleases(helm.ReleaseListFilter(revnamespace))
release := Releases.GetReleases()
for _, r := range release {
fmt.Println("purge release: " + r.GetName())
helmClient.DeleteRelease(r.GetName(), helm.DeletePurge(true))
}
if err != nil {
log.Fatalf("err: %v", err)
}
}
func listNamespace(clientset *kubernetes.Clientset) *v1.NamespaceList {
namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
fmt.Println("namespace")
panic(err.Error())
}
return namespaces
}
func deleteNamespace(clientset *kubernetes.Clientset, revnamespaces []string) {
deletePolicy := metav1.DeletePropagationBackground
orphandependent := false
for _, deletenamespace := range revnamespaces {
fmt.Println("deleting namespace: " + deletenamespace)
err := clientset.CoreV1().Namespaces().Delete(deletenamespace, &metav1.DeleteOptions{
OrphanDependents: &orphandependent,
PropagationPolicy: &deletePolicy,
})
if err != nil {
fmt.Println(err)
}
}
}
func getenv(key string, fallback bool) bool {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
if value == "true" {
return true
} else if value == "false" {
return false
}
return fallback
}
func main() {
exempts := os.Getenv("EXEMPTION")
helm := getenv("HELM", false)
fmt.Printf("helm is true '%t'", helm)
duration, err := time.ParseDuration(os.Getenv("DURATION"))
if err != nil {
fmt.Println("Error Duration is empty")
panic(err)
}
config := authIncluster()
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
//get environment variable duration
//list environment excemption comma separated
exemptList := strings.Split(exempts, ",")
if exempts == "" {
fmt.Println("Error Exemption is empty")
panic(err)
}
//tags := os.Getenv("TAG")
tag := "rev"
fmt.Println("Duration: " + duration.String())
fmt.Println("Exemptions: ")
fmt.Println(exemptList)
exempt := "rev-master"
for {
var revnamespaces []string
now := time.Now()
//set time in minutes
namespaces := listNamespace(clientset)
for _, namespace := range namespaces.Items {
if strings.Contains(namespace.Name, tag) && namespace.Name != exempt {
fmt.Println(namespace.Name + " : " + now.Sub(namespace.CreationTimestamp.Time).String())
if duration.Nanoseconds() <= now.Sub(namespace.CreationTimestamp.Time).Nanoseconds() {
revnamespaces = append(revnamespaces, namespace.Name)
fmt.Println(namespace.Name + " is pass " + duration.String())
fmt.Println(now.Sub(namespace.CreationTimestamp.Time))
}
}
}
revnamespaces = difference(revnamespaces, exemptList)
if helm == true {
cleanupHelms(revnamespaces)
}
deleteNamespace(clientset, revnamespaces)
fmt.Println(".....END")
time.Sleep(60 * time.Second)
}
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
func difference(toRemoval []string, toSave []string) []string {
var remove []string
for _, checkRemoval := range toRemoval {
found := false
for _, save := range toSave {
save = strings.TrimSpace(save)
if checkRemoval == save {
found = true
break
}
}
if !found {
remove = append(remove, checkRemoval)
fmt.Println(remove)
}
}
return remove
}