diff --git a/staging/src/k8s.io/apiserver/pkg/server/healthz/BUILD b/staging/src/k8s.io/apiserver/pkg/server/healthz/BUILD index a0c5ecadb2e..ae2d20238bc 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/healthz/BUILD +++ b/staging/src/k8s.io/apiserver/pkg/server/healthz/BUILD @@ -18,6 +18,7 @@ go_library( "doc.go", "healthz.go", ], + deps = ["//vendor/github.com/golang/glog:go_default_library"], ) filegroup( diff --git a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go index 441500dcb5b..d5dbb5ccdd7 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go +++ b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go @@ -20,7 +20,10 @@ import ( "bytes" "fmt" "net/http" + "strings" "sync" + + "github.com/golang/glog" ) // HealthzChecker is a named healthz checker. @@ -58,11 +61,18 @@ func NamedCheck(name string, check func(r *http.Request) error) HealthzChecker { return &healthzCheck{name, check} } -// InstallHandler registers a handler for health checking on the path "/healthz" to mux. +// InstallHandler registers handlers for health checking on the path +// "/healthz" to mux. *All handlers* for mux must be specified in +// exactly one call to InstallHandler. Calling InstallHandler more +// than once for the same mux will result in a panic. func InstallHandler(mux mux, checks ...HealthzChecker) { if len(checks) == 0 { + glog.V(5).Info("No default health checks specified. Installing the ping handler.") checks = []HealthzChecker{PingHealthz} } + + glog.V(5).Info("Installing healthz checkers:", strings.Join(checkerNames(checks...), ", ")) + mux.Handle("/healthz", handleRootHealthz(checks...)) for _, check := range checks { mux.Handle(fmt.Sprintf("/healthz/%v", check.Name()), adaptCheckToHandler(check.Check)) @@ -132,3 +142,17 @@ func adaptCheckToHandler(c func(r *http.Request) error) http.HandlerFunc { } }) } + +// checkerNames returns the names of the checks in the same order as passed in. +func checkerNames(checks ...HealthzChecker) []string { + if len(checks) > 0 { + // accumulate the names of checks for printing them out. + checkerNames := make([]string, 0, len(checks)) + for _, check := range checks { + // quote the Name so we can disambiguate + checkerNames = append(checkerNames, fmt.Sprintf("%q", check.Name())) + } + return checkerNames + } + return nil +} diff --git a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go index b32c072d2d0..99aae5a9eab 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go +++ b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go @@ -21,6 +21,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "reflect" "testing" ) @@ -80,3 +81,32 @@ func TestMulitipleChecks(t *testing.T) { } } } + +func TestCheckerNames(t *testing.T) { + n1 := "n1" + n2 := "n2" + c1 := &healthzCheck{name: n1} + c2 := &healthzCheck{name: n2} + + testCases := []struct { + desc string + have []HealthzChecker + want []string + }{ + {"no checker", []HealthzChecker{}, []string{}}, + {"one checker", []HealthzChecker{c1}, []string{n1}}, + {"other checker", []HealthzChecker{c2}, []string{n2}}, + {"checker order", []HealthzChecker{c1, c2}, []string{n1, n2}}, + {"different checker order", []HealthzChecker{c2, c1}, []string{n2, n1}}, + } + + for _, tc := range testCases { + result := checkerNames(tc.have...) + t.Run(tc.desc, func(t *testing.T) { + if reflect.DeepEqual(tc.want, result) { + t.Errorf("want %#v, got %#v", tc.want, result) + } + }) + } + +}