-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
186 lines (170 loc) · 5.24 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
package main
import (
"net/http"
"os"
"os/exec"
"strconv"
"time"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
syscall "golang.org/x/sys/unix"
)
var gitCommit string
var gitBranch string
func printVersion() {
log.Printf("Current build version: %s", gitCommit)
log.Printf("Current build branch: %s", gitBranch)
}
type DiskStatus struct {
All uint64 `json:"all"`
Used uint64 `json:"used"`
Free uint64 `json:"free"`
Avail uint64 `json:"avail"`
Percent float64 `json:"percent"`
}
// disk usage of path/disk
func DiskUsage(path string) (disk DiskStatus) {
fs := syscall.Statfs_t{}
err := syscall.Statfs(path, &fs)
if err != nil {
return
}
disk.All = fs.Blocks * uint64(fs.Bsize)
disk.Avail = fs.Bavail * uint64(fs.Bsize)
disk.Free = fs.Bfree * uint64(fs.Bsize)
disk.Used = disk.All - disk.Free
disk.Percent = float64(disk.Used) / float64(disk.All)
return
}
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
)
func main() {
log.Println("Starting k3s-janitor")
printVersion()
log.Println("Starting health check")
r := mux.NewRouter()
r.HandleFunc("/liveness", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
r.HandleFunc("/readiness", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
r.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(gitCommit))
w.Write([]byte("\n"))
w.Write([]byte(gitBranch))
w.Write([]byte("\n"))
})
r.HandleFunc("/disk", func(w http.ResponseWriter, r *http.Request) {
disk := DiskUsage("/")
w.WriteHeader(http.StatusOK)
w.Write([]byte(strconv.FormatUint(disk.All, 10)))
})
r.HandleFunc("/disk/used", func(w http.ResponseWriter, r *http.Request) {
disk := DiskUsage("/")
w.WriteHeader(http.StatusOK)
w.Write([]byte(strconv.FormatUint(disk.Used, 10)))
})
r.HandleFunc("/disk/free", func(w http.ResponseWriter, r *http.Request) {
disk := DiskUsage("/")
w.WriteHeader(http.StatusOK)
w.Write([]byte(strconv.FormatUint(disk.Free, 10)))
})
r.HandleFunc("/disk/avail", func(w http.ResponseWriter, r *http.Request) {
disk := DiskUsage("/")
w.WriteHeader(http.StatusOK)
w.Write([]byte(strconv.FormatUint(disk.Avail, 10)))
})
r.HandleFunc("/disk/percent", func(w http.ResponseWriter, r *http.Request) {
disk := DiskUsage("/")
w.WriteHeader(http.StatusOK)
w.Write([]byte(strconv.FormatFloat(disk.Percent, 'f', 2, 64)))
})
go http.ListenAndServe(":8080", r)
percentThesholdEnv := os.Getenv("PERCENT_THRESHOLD")
if percentThesholdEnv == "" {
percentThesholdEnv = "90"
}
percentThreshold, err := strconv.ParseFloat(percentThesholdEnv, 64)
if err != nil {
log.Println("Error parsing percent threshold:", err)
os.Exit(1)
}
log.Println("Percent threshold:", percentThreshold)
sleepBackgroundEnv := os.Getenv("SLEEP_BACKGROUND")
if sleepBackgroundEnv == "" {
sleepBackgroundEnv = "15"
}
sleepBackground, err := strconv.ParseInt(sleepBackgroundEnv, 10, 64)
if err != nil {
log.Println("Error parsing sleep:", err)
os.Exit(1)
}
log.Println("Background Sleep:", sleepBackground)
sleepForegroundEnv := os.Getenv("SLEEP_FOREGROUND")
if sleepForegroundEnv == "" {
sleepForegroundEnv = "5"
}
sleepForeground, err := strconv.ParseInt(sleepForegroundEnv, 10, 64)
if err != nil {
log.Println("Error parsing sleep:", err)
os.Exit(1)
}
// Verify that crictl is available
cmd := exec.Command("/bin/sh", "-c", "/var/lib/rancher/k3s/data/current/bin/crictl --version")
stdout, err := cmd.Output()
if err != nil {
log.Println("crictl not available")
log.Println("Error:", err)
//os.Exit(1)
}
log.Print(string(stdout))
// Dump crictl info
cmd = exec.Command("/bin/sh", "-c", "/var/lib/rancher/k3s/data/current/bin/crictl info")
stdout, err = cmd.Output()
if err != nil {
log.Println("crictl info not available")
log.Println("Error:", err)
//os.Exit(1)
}
log.Print(string(stdout))
for {
log.Println("Checking filesystem usage")
disk := DiskUsage("/")
log.Printf("Disk usage: %.2f%%\n", disk.Percent*100)
log.Printf("Percent threshold: %.2f%%\n", percentThreshold)
if disk.Percent*100 > float64(percentThreshold) {
log.Println("Disk usage is above threshold, starting cleaning up")
log.Println("Cleaning up unused containers")
cmd = exec.Command("/bin/sh", "-c", "for id in `/var/lib/rancher/k3s/data/current/bin/crictl ps -a | grep -i exited | awk '{print $1}'`; do /var/lib/rancher/k3s/data/current/bin/crictl rm $id ; done")
err = cmd.Run()
if err != nil {
log.Println("Error cleaning up unused containers:", err)
os.Exit(2)
}
log.Println("Cleaning up unused images")
cmd := exec.Command("/bin/sh", "-c", "/var/lib/rancher/k3s/data/current/bin/crictl", "rmi", "prune")
err := cmd.Run()
if err != nil {
log.Println("Error cleaning up:", err)
os.Exit(2)
}
log.Println("Successfully cleaned up, sleeping...")
time.Sleep(time.Duration(sleepForeground) * time.Minute)
} else {
log.Println("Disk usage is below threshold, sleeping...")
time.Sleep(time.Duration(sleepBackground) * time.Minute)
}
}
}