-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
81 lines (73 loc) · 1.87 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
// XXX
// This is a sample application that allocates 512kb of memory per second,
// this is the application we use on our documentation.
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var (
// This grows indefinetely.
bloat []int8
// This indicates if we are healthy or not.
unhealthy bool
)
func main() {
// set up a liveness probe, it will indicate healthy until se set unhealthy
// variable to true.
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if unhealthy {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println("liveness reporting unhealthy")
return
}
fmt.Println("liveness reporting healthy")
})
go http.ListenAndServe(":8080", nil)
sigs := make(chan os.Signal, 1)
// SIGTERM will be send by K8s on pod eviction, we should receive it
// here only after setting the container as unhealthy. SIGUSR1 will
// be send by OOMHero once we cross the Warning threshold and SIGUSR2
// will be send when we cross Critical.
signal.Notify(
sigs,
syscall.SIGTERM,
syscall.SIGUSR1,
syscall.SIGUSR2,
)
// start bloating our process memory.
go leak()
for {
sig := <-sigs
switch sig {
case syscall.SIGUSR1:
// here we could shrink our memory usage, maybe
// by cleaning up caches.
fmt.Println("warning level reached")
case syscall.SIGUSR2:
// if we are over Critical we should set ourselves
// as unhealthy so K8S would send us a SIGTERM and
// give us some room for a graceful shutdown.
fmt.Println("critical level reached")
unhealthy = true
case syscall.SIGTERM:
// we manage to set ourselves as unhealthy and to
// receive a SIGTERM directly from K8S.
fmt.Println("k8s says it is time to die, bye")
os.Exit(0)
}
}
}
func leak() {
limit := 1 << 20
for {
for i := 0; i < limit; i++ {
bloat = append(bloat, 1)
}
time.Sleep(2 * time.Second)
}
}