Skip to content

Commit

Permalink
add healthz
Browse files Browse the repository at this point in the history
  • Loading branch information
andyxning committed Jun 2, 2017
1 parent 008a583 commit 406c7e7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Documentation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ The following configuration illustrates the use of most options with `udp` backe
--subnet-lease-renew-margin=60: subnet lease renewal margin, in minutes.
--ip-masq=false: setup IP masquerade for traffic destined for outside the flannel network. Flannel assumes that the default policy is ACCEPT in the NAT POSTROUTING chain.
-v=0: log level for V logs. Set to 1 to see messages related to data path.
--healthz-ip="0.0.0.0": The IP address for healthz server to listen (default "0.0.0.0")
--healthz-port=0: The port for healthz server to listen(0 to disable)
--version: print version and exit
```
Expand All @@ -68,3 +70,9 @@ MTU is calculated and set automatically by flannel. It then reports that value i
The command line options outlined above can also be specified via environment variables.
For example `--etcd-endpoints=http://10.0.0.2:2379` is equivalent to `FLANNELD_ETCD_ENDPOINTS=http://10.0.0.2:2379` environment variable.
Any command line option can be turned into an environment variable by prefixing it with `FLANNELD_`, stripping leading dashes, converting to uppercase and replacing all other dashes to underscores.
## Health Check
Flannel provides a health check http endpoint `healthz`. Currently this endpoint will blindly
return http status ok(i.e. 200) when flannel is running. This feature is by default disabled.
Set `healthz-port` to a non-zero value will enable a healthz server for flannel.
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import (
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"strconv"

"github.com/coreos/pkg/flagutil"
log "github.com/golang/glog"
Expand Down Expand Up @@ -67,6 +69,8 @@ type CmdLineOpts struct {
subnetDir string
publicIP string
subnetLeaseRenewMargin int
healthzIP string
healthzPort int
}

var (
Expand All @@ -91,6 +95,8 @@ func init() {
flag.BoolVar(&opts.kubeSubnetMgr, "kube-subnet-mgr", false, "Contact the Kubernetes API for subnet assignement instead of etcd.")
flag.BoolVar(&opts.help, "help", false, "print this message")
flag.BoolVar(&opts.version, "version", false, "print version and exit")
flag.StringVar(&opts.healthzIP, "healthz-ip", "0.0.0.0", "The IP address for healthz server to listen")
flag.IntVar(&opts.healthzPort, "healthz-port", 0, "The port for healthz server to listen(0 to disable)")
}

func newSubnetManager() (subnet.Manager, error) {
Expand Down Expand Up @@ -154,6 +160,10 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())
go shutdown(sigs, cancel)

if opts.healthzPort > 0 {
go mustRunHealthz()
}

// Fetch the network config (i.e. what backend to use etc..).
config, err := getConfig(ctx, sm)
if err == errCanceled {
Expand Down Expand Up @@ -378,3 +388,18 @@ func WriteSubnetFile(path string, nw ip.IP4Net, ipMasq bool, bn backend.Network)
return os.Rename(tempFile, path)
//TODO - is this safe? What if it's not on the same FS?
}

func mustRunHealthz() {
address := net.JoinHostPort(opts.healthzIP, strconv.Itoa(opts.healthzPort))
log.Infof("Start healthz server on %s", address)

http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("flanneld is running"))
})

if err := http.ListenAndServe(address, nil); err != nil {
log.Errorf("Start healthz server error. %v", err)
panic(err)
}
}

0 comments on commit 406c7e7

Please sign in to comment.