From 4cf3f7725b45c372362fb6afa491e5b2402e6ad7 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Tue, 15 Sep 2020 15:46:02 +0200 Subject: [PATCH] apiserver: fix healthz vs. livez vs. readyz log output Kubernetes-commit: 480b4c74b858a9caac0b33cb4d9a1ab30ae65cf4 --- pkg/server/healthz/healthz.go | 13 +++++++------ pkg/server/healthz/healthz_test.go | 16 ++++++++-------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pkg/server/healthz/healthz.go b/pkg/server/healthz/healthz.go index b2d0007f5..e80b8501e 100644 --- a/pkg/server/healthz/healthz.go +++ b/pkg/server/healthz/healthz.go @@ -161,6 +161,7 @@ func InstallPathHandler(mux mux, path string, checks ...HealthChecker) { klog.V(5).Infof("Installing health checkers for (%v): %v", path, formatQuoted(checkerNames(checks...)...)) + name := strings.Split(strings.TrimPrefix(path, "/"), "/")[0] mux.Handle(path, metrics.InstrumentHandlerFunc("GET", /* group = */ "", @@ -171,7 +172,7 @@ func InstallPathHandler(mux mux, path string, checks ...HealthChecker) { /* component = */ "", /* deprecated */ false, /* removedRelease */ "", - handleRootHealthz(checks...))) + handleRootHealth(name, checks...))) for _, check := range checks { mux.Handle(fmt.Sprintf("%s/%v", path, check.Name()), adaptCheckToHandler(check.Check)) } @@ -207,8 +208,8 @@ func getExcludedChecks(r *http.Request) sets.String { return sets.NewString() } -// handleRootHealthz returns an http.HandlerFunc that serves the provided checks. -func handleRootHealthz(checks ...HealthChecker) http.HandlerFunc { +// handleRootHealth returns an http.HandlerFunc that serves the provided checks. +func handleRootHealth(name string, checks ...HealthChecker) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { excluded := getExcludedChecks(r) // failedVerboseLogOutput is for output to the log. It indicates detailed failed output information for the log. @@ -240,8 +241,8 @@ func handleRootHealthz(checks ...HealthChecker) http.HandlerFunc { } // always be verbose on failure if len(failedChecks) > 0 { - klog.V(2).Infof("healthz check failed: %s\n%v", strings.Join(failedChecks, ","), failedVerboseLogOutput.String()) - http.Error(httplog.Unlogged(r, w), fmt.Sprintf("%shealthz check failed", individualCheckOutput.String()), http.StatusInternalServerError) + klog.V(2).Infof("%s check failed: %s\n%v", strings.Join(failedChecks, ","), name, failedVerboseLogOutput.String()) + http.Error(httplog.Unlogged(r, w), fmt.Sprintf("%s%s check failed", individualCheckOutput.String(), name), http.StatusInternalServerError) return } @@ -253,7 +254,7 @@ func handleRootHealthz(checks ...HealthChecker) http.HandlerFunc { } individualCheckOutput.WriteTo(w) - fmt.Fprint(w, "healthz check passed\n") + fmt.Fprintf(w, "%s check passed\n", name) }) } diff --git a/pkg/server/healthz/healthz_test.go b/pkg/server/healthz/healthz_test.go index 5d41013bd..ef5a7827c 100644 --- a/pkg/server/healthz/healthz_test.go +++ b/pkg/server/healthz/healthz_test.go @@ -94,24 +94,24 @@ func TestInstallPathHandler(t *testing.T) { } -func testMultipleChecks(path string, t *testing.T) { +func testMultipleChecks(path, name string, t *testing.T) { tests := []struct { path string expectedResponse string expectedStatus int addBadCheck bool }{ - {"?verbose", "[+]ping ok\nhealthz check passed\n", http.StatusOK, false}, + {"?verbose", fmt.Sprintf("[+]ping ok\n%s check passed\n", name), http.StatusOK, false}, {"?exclude=dontexist", "ok", http.StatusOK, false}, {"?exclude=bad", "ok", http.StatusOK, true}, - {"?verbose=true&exclude=bad", "[+]ping ok\n[+]bad excluded: ok\nhealthz check passed\n", http.StatusOK, true}, - {"?verbose=true&exclude=dontexist", "[+]ping ok\nwarn: some health checks cannot be excluded: no matches for \"dontexist\"\nhealthz check passed\n", http.StatusOK, false}, + {"?verbose=true&exclude=bad", fmt.Sprintf("[+]ping ok\n[+]bad excluded: ok\n%s check passed\n", name), http.StatusOK, true}, + {"?verbose=true&exclude=dontexist", fmt.Sprintf("[+]ping ok\nwarn: some health checks cannot be excluded: no matches for \"dontexist\"\n%s check passed\n", name), http.StatusOK, false}, {"/ping", "ok", http.StatusOK, false}, {"", "ok", http.StatusOK, false}, - {"?verbose", "[+]ping ok\n[-]bad failed: reason withheld\nhealthz check failed\n", http.StatusInternalServerError, true}, + {"?verbose", fmt.Sprintf("[+]ping ok\n[-]bad failed: reason withheld\n%s check failed\n", name), http.StatusInternalServerError, true}, {"/ping", "ok", http.StatusOK, true}, {"/bad", "internal server error: this will fail\n", http.StatusInternalServerError, true}, - {"", "[+]ping ok\n[-]bad failed: reason withheld\nhealthz check failed\n", http.StatusInternalServerError, true}, + {"", fmt.Sprintf("[+]ping ok\n[-]bad failed: reason withheld\n%s check failed\n", name), http.StatusInternalServerError, true}, } for i, test := range tests { @@ -148,11 +148,11 @@ func testMultipleChecks(path string, t *testing.T) { } func TestMultipleChecks(t *testing.T) { - testMultipleChecks("", t) + testMultipleChecks("", "healthz", t) } func TestMultiplePathChecks(t *testing.T) { - testMultipleChecks("/ready", t) + testMultipleChecks("/ready", "ready", t) } func TestCheckerNames(t *testing.T) {