-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
187 lines (154 loc) · 3.57 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
package main
import (
"fmt"
"os"
"sort"
k8s "github.com/dtan4/k8s-job-cleaner/kubernetes"
flag "github.com/spf13/pflag"
)
const (
defaultMaxCount = 10
jobNameLabel = "job-name"
)
func main() {
var (
context string
dryRun bool
inCluster bool
kubeconfig string
labelGroup string
maxCount int64
namespace string
version bool
)
flags := flag.NewFlagSet("k8stail", flag.ExitOnError)
flags.Usage = func() {
flags.PrintDefaults()
}
flags.StringVar(&context, "context", "", "Kubernetes context")
flags.BoolVar(&dryRun, "dry-run", false, "Dry run")
flags.BoolVar(&inCluster, "in-cluster", false, "Execute in Kubernetes cluster")
flags.StringVar(&kubeconfig, "kubeconfig", "", "Path of kubeconfig")
flags.StringVar(&labelGroup, "label-group", "", "Label name for grouping Jobs")
flags.Int64Var(&maxCount, "max-count", int64(defaultMaxCount), "Number of Jobs to remain")
flags.StringVar(&namespace, "namespace", "", "Kubernetes namespace")
flags.BoolVarP(&version, "version", "v", false, "Print version")
if err := flags.Parse(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if kubeconfig == "" {
if os.Getenv("KUBECONFIG") != "" {
kubeconfig = os.Getenv("KUBECONFIG")
} else {
kubeconfig = k8s.DefaultConfigFile()
}
}
if labelGroup == "" {
fmt.Fprintln(os.Stderr, "--label-group must be set")
os.Exit(1)
}
if version {
printVersion()
os.Exit(0)
}
var client *k8s.Client
if inCluster {
c, err := k8s.NewClientInCluster()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if namespace == "" {
namespace = k8s.DefaultNamespace()
}
client = c
} else {
c, err := k8s.NewClient(kubeconfig, context)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if namespace == "" {
namespaceInConfig, err := c.NamespaceInConfig()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if namespaceInConfig == "" {
namespace = k8s.DefaultNamespace()
} else {
namespace = namespaceInConfig
}
}
client = c
}
jobs, err := client.ListJobs(namespace)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
jobGroup := map[string]k8s.Jobs{}
for _, job := range jobs.Items {
if !k8s.IsJobFinished(job) {
continue
}
label := job.Labels[labelGroup]
if label == "" {
continue
}
if jobGroup[label] == nil {
jobGroup[label] = k8s.Jobs{}
}
jobGroup[label] = append(jobGroup[label], job)
}
pods, err := client.ListPods(namespace)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
podGroup := map[string]k8s.Pods{}
for _, pod := range pods.Items {
if !k8s.IsPodFinished(pod) {
continue
}
label := pod.Labels[jobNameLabel]
if label == "" {
continue
}
if podGroup[label] == nil {
podGroup[label] = k8s.Pods{}
}
podGroup[label] = append(podGroup[label], pod)
}
for _, jobs := range jobGroup {
i := int64(0)
sort.Sort(sort.Reverse(jobs))
for _, job := range jobs {
if i < maxCount {
i++
continue
}
if dryRun {
fmt.Printf("Deleting Job %s... [dry-run]\n", job.Name)
} else {
fmt.Printf("Deleting Job %s...\n", job.Name)
if err := client.DeleteJob(job); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
for _, pod := range podGroup[job.Name] {
if dryRun {
fmt.Printf(" Deleting Pod %s... [dry-run]\n", pod.Name)
} else {
fmt.Printf(" Deleting Pod %s...\n", pod.Name)
if err := client.DeletePod(pod); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
}
}
}
}