Merge pull request #94766 from sttts/sttts-healthz-name

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

Kubernetes-commit: f448dc54bd2f7f7e56debd97c2467cd1a5a3c9fa
This commit is contained in:
Kubernetes Publisher 2020-09-25 18:40:48 -07:00
commit 3f80aac972
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...)...))
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)
})
}

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 {
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) {