/
namespaces.go
114 lines (92 loc) · 3.08 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
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
package k8s
import (
"context"
"regexp"
"strconv"
"time"
"github.com/Qovery/pleco/pkg/common"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type kubernetesNamespace struct {
Name string
NamespaceCreateTime time.Time
Status string
TTL int64
}
func listNamespaces(clientSet *kubernetes.Clientset, tagName string, disableTTLCheck bool) []v1.Namespace {
var listOptions metav1.ListOptions
if !disableTTLCheck {
listOptions = metav1.ListOptions{
LabelSelector: tagName,
}
}
log.Debugf("Listing all Kubernetes namespaces with %s label", tagName)
namespaces, err := clientSet.CoreV1().Namespaces().List(context.TODO(), listOptions)
if err != nil {
log.Errorf("Can'list namespaces with %s label: %s", tagName, err.Error())
}
return namespaces.Items
}
func getExpiredNamespaces(clientSet *kubernetes.Clientset, tagName string, disableTTLCheck bool) []kubernetesNamespace {
namespaces := listNamespaces(clientSet, tagName, disableTTLCheck)
expiredNamespaces := []kubernetesNamespace{}
for _, namespace := range namespaces {
if namespace.Status.Phase != "Active" {
continue
}
if disableTTLCheck {
match, _ := regexp.Compile("z([a-z0-9]+)-z(([a-z0-9]+))")
if !match.MatchString(namespace.Name) {
continue
}
}
for key, value := range namespace.ObjectMeta.Labels {
if key == tagName || disableTTLCheck {
ttlValue, err := strconv.Atoi(value)
if err != nil && !disableTTLCheck {
log.Errorf("ttl value unrecognized for namespace %s", namespace.Name)
continue
}
if disableTTLCheck {
ttlValue = -1
}
creationDate, _ := time.Parse(time.RFC3339, namespace.CreationTimestamp.Time.Format(time.RFC3339))
if common.CheckIfExpired(creationDate, int64(ttlValue), "Namespace: "+namespace.Name, disableTTLCheck) {
expiredNamespaces = append(expiredNamespaces, kubernetesNamespace{
Name: namespace.Name,
NamespaceCreateTime: namespace.CreationTimestamp.Time,
Status: string(namespace.Status.Phase),
TTL: int64(ttlValue),
})
}
}
}
}
return expiredNamespaces
}
func deleteNamespace(clientSet *kubernetes.Clientset, namespace kubernetesNamespace, dryRun bool) {
deleteOptions := metav1.DeleteOptions{}
if !dryRun {
err := clientSet.CoreV1().Namespaces().Delete(context.TODO(), namespace.Name, deleteOptions)
if err != nil {
log.Errorf("Can't delete namsespace %s", namespace.Name)
} else {
log.Debugf("K8S namespace %s deleted.", namespace.Name)
}
}
}
func DeleteExpiredNamespaces(clientSet *kubernetes.Clientset, tagName string, dryRun bool, disableTTLCheck bool) {
namespaces := getExpiredNamespaces(clientSet, tagName, disableTTLCheck)
count, start := common.ElemToDeleteFormattedInfos("expired Kubernetes namespace", len(namespaces), "")
log.Info(count)
if dryRun || len(namespaces) == 0 {
return
}
log.Info(start)
for _, namespace := range namespaces {
deleteNamespace(clientSet, namespace, dryRun)
}
}