From 478d51c8272c4ff5e07a4daf0821b745e502bea9 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Sat, 26 Nov 2016 21:09:59 -0300 Subject: [PATCH] Add healthz checker --- controllers/nginx/pkg/cmd/controller/nginx.go | 19 ++++++++++++++++ core/pkg/ingress/controller/controller.go | 22 ------------------- core/pkg/ingress/controller/launch.go | 6 ++++- core/pkg/ingress/doc.go | 8 +++++++ core/pkg/ingress/types.go | 5 +++++ 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/controllers/nginx/pkg/cmd/controller/nginx.go b/controllers/nginx/pkg/cmd/controller/nginx.go index 92ff980c9..b2a4cec94 100644 --- a/controllers/nginx/pkg/cmd/controller/nginx.go +++ b/controllers/nginx/pkg/cmd/controller/nginx.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "io/ioutil" + "net/http" "os" "os/exec" @@ -252,6 +253,24 @@ func (n NGINXController) OnUpdate(cmap *api.ConfigMap, ingressCfg ingress.Config }, n.testTemplate) } +// Name returns the healthcheck name +func (n NGINXController) Name() string { + return "Ingress Controller" +} + +// Check returns if the nginx healthz endpoint is returning ok (status code 200) +func (n NGINXController) Check(_ *http.Request) error { + res, err := http.Get("http://127.0.0.1:18080/healthz") + if err != nil { + return err + } + defer res.Body.Close() + if res.StatusCode != 200 { + return fmt.Errorf("Ingress controller is not healthy") + } + return nil +} + // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 // https://play.golang.org/p/TVSyCcdxUh func nextPowerOf2(v int) int { diff --git a/core/pkg/ingress/controller/controller.go b/core/pkg/ingress/controller/controller.go index 1306e4e01..73ed53e18 100644 --- a/core/pkg/ingress/controller/controller.go +++ b/core/pkg/ingress/controller/controller.go @@ -18,7 +18,6 @@ package controller import ( "fmt" - "net/http" "reflect" "sort" "strconv" @@ -35,7 +34,6 @@ import ( unversionedcore "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" "k8s.io/kubernetes/pkg/client/record" "k8s.io/kubernetes/pkg/fields" - "k8s.io/kubernetes/pkg/healthz" "k8s.io/kubernetes/pkg/util/flowcontrol" "k8s.io/kubernetes/pkg/util/intstr" @@ -78,8 +76,6 @@ var ( // GenericController holds the boilerplate code required to build an Ingress controlller. type GenericController struct { - healthz.HealthzChecker - cfg *Configuration ingController *cache.Controller @@ -283,24 +279,6 @@ func (ic *GenericController) controllersInSync() bool { ic.mapController.HasSynced() } -// Name returns the healthcheck name -func (ic GenericController) Name() string { - return "Ingress Controller" -} - -// Check returns if the nginx healthz endpoint is returning ok (status code 200) -func (ic GenericController) Check(_ *http.Request) error { - res, err := http.Get("http://127.0.0.1:18080/healthz") - if err != nil { - return err - } - defer res.Body.Close() - if res.StatusCode != 200 { - return fmt.Errorf("Ingress controller is not healthy") - } - return nil -} - // Info returns information about the backend func (ic GenericController) Info() *ingress.BackendInfo { return ic.cfg.Backend.Info() diff --git a/core/pkg/ingress/controller/launch.go b/core/pkg/ingress/controller/launch.go index 41dbaf284..b21c648bf 100644 --- a/core/pkg/ingress/controller/launch.go +++ b/core/pkg/ingress/controller/launch.go @@ -155,7 +155,11 @@ func NewIngressController(backend ingress.Controller) *GenericController { func registerHandlers(enableProfiling bool, port int, ic *GenericController) { mux := http.NewServeMux() - healthz.InstallHandler(mux, ic) + // expose health check endpoint (/healthz) + healthz.InstallHandler(mux, + healthz.PingHealthz, + ic.cfg.Backend, + ) mux.Handle("/metrics", prometheus.Handler()) diff --git a/core/pkg/ingress/doc.go b/core/pkg/ingress/doc.go index 54ddb718f..068cdaecc 100644 --- a/core/pkg/ingress/doc.go +++ b/core/pkg/ingress/doc.go @@ -55,6 +55,14 @@ package ingress // return ingress.NewStandardDefaults() // } // +// func (n DummyController) Name() string { +// return "dummy Controller" +// } +// +// func (n DummyController) Check(_ *http.Request) error { +// return nil +// } +// // func (dc DummyController) Info() *BackendInfo { // Name: "dummy", // Release: "0.0.0", diff --git a/core/pkg/ingress/types.go b/core/pkg/ingress/types.go index c1dfc0fa2..7a0f5929a 100644 --- a/core/pkg/ingress/types.go +++ b/core/pkg/ingress/types.go @@ -18,6 +18,7 @@ package ingress import ( "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/healthz" "k8s.io/ingress/core/pkg/ingress/annotations/auth" "k8s.io/ingress/core/pkg/ingress/annotations/authreq" @@ -40,6 +41,10 @@ var ( // Controller holds the methods to handle an Ingress backend // TODO (#18): Make sure this is sufficiently supportive of other backends. type Controller interface { + // HealthzChecker returns is a named healthz check that returns the ingress + // controller status + healthz.HealthzChecker + // Reload takes a byte array representing the new loadbalancer configuration, // and returns a byte array containing any output/errors from the backend and // if a reload was required.