apiserver: fix healthz vs. livez vs. readyz log output

This commit is contained in:
Dr. Stefan Schimanski 2020-09-15 15:46:02 +02:00
parent 66334f02e8
commit 480b4c74b8
2 changed files with 15 additions and 14 deletions

View File

@ -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...)...)) klog.V(5).Infof("Installing health checkers for (%v): %v", path, formatQuoted(checkerNames(checks...)...))
name := strings.Split(strings.TrimPrefix(path, "/"), "/")[0]
mux.Handle(path, mux.Handle(path,
metrics.InstrumentHandlerFunc("GET", metrics.InstrumentHandlerFunc("GET",
/* group = */ "", /* group = */ "",
@ -171,7 +172,7 @@ func InstallPathHandler(mux mux, path string, checks ...HealthChecker) {
/* component = */ "", /* component = */ "",
/* deprecated */ false, /* deprecated */ false,
/* removedRelease */ "", /* removedRelease */ "",
handleRootHealthz(checks...))) handleRootHealth(name, checks...)))
for _, check := range checks { for _, check := range checks {
mux.Handle(fmt.Sprintf("%s/%v", path, check.Name()), adaptCheckToHandler(check.Check)) 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() return sets.NewString()
} }
// handleRootHealthz returns an http.HandlerFunc that serves the provided checks. // handleRootHealth returns an http.HandlerFunc that serves the provided checks.
func handleRootHealthz(checks ...HealthChecker) http.HandlerFunc { func handleRootHealth(name string, checks ...HealthChecker) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
excluded := getExcludedChecks(r) excluded := getExcludedChecks(r)
// failedVerboseLogOutput is for output to the log. It indicates detailed failed output information for the log. // 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 // always be verbose on failure
if len(failedChecks) > 0 { if len(failedChecks) > 0 {
klog.V(2).Infof("healthz check failed: %s\n%v", strings.Join(failedChecks, ","), failedVerboseLogOutput.String()) klog.V(2).Infof("%s check failed: %s\n%v", strings.Join(failedChecks, ","), name, failedVerboseLogOutput.String())
http.Error(httplog.Unlogged(r, w), fmt.Sprintf("%shealthz check failed", individualCheckOutput.String()), http.StatusInternalServerError) http.Error(httplog.Unlogged(r, w), fmt.Sprintf("%s%s check failed", individualCheckOutput.String(), name), http.StatusInternalServerError)
return return
} }
@ -253,7 +254,7 @@ func handleRootHealthz(checks ...HealthChecker) http.HandlerFunc {
} }
individualCheckOutput.WriteTo(w) individualCheckOutput.WriteTo(w)
fmt.Fprint(w, "healthz check passed\n") fmt.Fprintf(w, "%s check passed\n", name)
}) })
} }

View File

@ -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 { tests := []struct {
path string path string
expectedResponse string expectedResponse string
expectedStatus int expectedStatus int
addBadCheck bool 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=dontexist", "ok", http.StatusOK, false},
{"?exclude=bad", "ok", http.StatusOK, true}, {"?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=bad", fmt.Sprintf("[+]ping ok\n[+]bad excluded: ok\n%s check passed\n", name), 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=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}, {"/ping", "ok", http.StatusOK, false},
{"", "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}, {"/ping", "ok", http.StatusOK, true},
{"/bad", "internal server error: this will fail\n", http.StatusInternalServerError, 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 { for i, test := range tests {
@ -148,11 +148,11 @@ func testMultipleChecks(path string, t *testing.T) {
} }
func TestMultipleChecks(t *testing.T) { func TestMultipleChecks(t *testing.T) {
testMultipleChecks("", t) testMultipleChecks("", "healthz", t)
} }
func TestMultiplePathChecks(t *testing.T) { func TestMultiplePathChecks(t *testing.T) {
testMultipleChecks("/ready", t) testMultipleChecks("/ready", "ready", t)
} }
func TestCheckerNames(t *testing.T) { func TestCheckerNames(t *testing.T) {