Skip to content

Commit 1a0a89a

Browse files
authored
Merge pull request #94 from bakins/healthz
Add simple healthz handler
2 parents 3273f98 + 3889705 commit 1a0a89a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ If you want to increase or decrease the amount of chaos change the interval betw
9696

9797
Remember that `chaoskube` by default kills any pod in all your namespaces, including system pods and itself.
9898

99+
`chaoskube` provides a simple HTTP endpoint that can be used to check that it is running. This can be used for [Kubernetes liveness and readiness probes](https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/). By default, this listens on port 8080. To disable, pass `--metrics-address=""` to `chaoskube`.
100+
99101
## Filtering targets
100102

101103
However, you can limit the search space of `chaoskube` by providing label, annotation and namespace selectors.

main.go

+18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"fmt"
45
"math/rand"
6+
"net/http"
57
"os"
68
"time"
79

@@ -35,6 +37,7 @@ var (
3537
interval time.Duration
3638
dryRun bool
3739
debug bool
40+
metricsAddress string
3841
)
3942

4043
func init() {
@@ -53,6 +56,7 @@ func init() {
5356
kingpin.Flag("interval", "Interval between Pod terminations").Default("10m").DurationVar(&interval)
5457
kingpin.Flag("dry-run", "If true, don't actually do anything.").Default("true").BoolVar(&dryRun)
5558
kingpin.Flag("debug", "Enable debug logging.").BoolVar(&debug)
59+
kingpin.Flag("metrics-address", "Listening address for metrics handler").Default(":8080").StringVar(&metricsAddress)
5660
}
5761

5862
func main() {
@@ -153,6 +157,20 @@ func main() {
153157
dryRun,
154158
)
155159

160+
if metricsAddress != "" {
161+
http.HandleFunc("/healthz",
162+
func(w http.ResponseWriter, r *http.Request) {
163+
fmt.Fprintln(w, "OK")
164+
})
165+
go func() {
166+
if err := http.ListenAndServe(metricsAddress, nil); err != nil {
167+
log.WithFields(log.Fields{
168+
"err": err,
169+
}).Fatal("failed to start HTTP server")
170+
}
171+
}()
172+
}
173+
156174
for {
157175
if err := chaoskube.TerminateVictim(); err != nil {
158176
log.WithField("err", err).Error("failed to terminate victim")

0 commit comments

Comments
 (0)