Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple healthz handler #94

Merged
merged 2 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ If you want to increase or decrease the amount of chaos change the interval betw

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

`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`.

## Filtering targets

However, you can limit the search space of `chaoskube` by providing label, annotation and namespace selectors.
Expand Down
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"fmt"
"math/rand"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -34,6 +36,7 @@ var (
interval time.Duration
dryRun bool
debug bool
metricsAddress string
)

func init() {
Expand All @@ -51,6 +54,7 @@ func init() {
kingpin.Flag("interval", "Interval between Pod terminations").Default("10m").DurationVar(&interval)
kingpin.Flag("dry-run", "If true, don't actually do anything.").Default("true").BoolVar(&dryRun)
kingpin.Flag("debug", "Enable debug logging.").BoolVar(&debug)
kingpin.Flag("metrics-address", "Listening address for metrics handler").Default(":8080").StringVar(&metricsAddress)
}

func main() {
Expand Down Expand Up @@ -149,6 +153,20 @@ func main() {
dryRun,
)

if metricsAddress != "" {
http.HandleFunc("/healthz",
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "OK")
})
go func() {
if err := http.ListenAndServe(metricsAddress, nil); err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to start HTTP server")
}
}()
}

for {
if err := chaoskube.TerminateVictim(); err != nil {
log.WithField("err", err).Error("failed to terminate victim")
Expand Down