Skip to content

Commit 480b4c7

Browse files
committed
apiserver: fix healthz vs. livez vs. readyz log output
1 parent 66334f0 commit 480b4c7

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func InstallPathHandler(mux mux, path string, checks ...HealthChecker) {
161161

162162
klog.V(5).Infof("Installing health checkers for (%v): %v", path, formatQuoted(checkerNames(checks...)...))
163163

164+
name := strings.Split(strings.TrimPrefix(path, "/"), "/")[0]
164165
mux.Handle(path,
165166
metrics.InstrumentHandlerFunc("GET",
166167
/* group = */ "",
@@ -171,7 +172,7 @@ func InstallPathHandler(mux mux, path string, checks ...HealthChecker) {
171172
/* component = */ "",
172173
/* deprecated */ false,
173174
/* removedRelease */ "",
174-
handleRootHealthz(checks...)))
175+
handleRootHealth(name, checks...)))
175176
for _, check := range checks {
176177
mux.Handle(fmt.Sprintf("%s/%v", path, check.Name()), adaptCheckToHandler(check.Check))
177178
}
@@ -207,8 +208,8 @@ func getExcludedChecks(r *http.Request) sets.String {
207208
return sets.NewString()
208209
}
209210

210-
// handleRootHealthz returns an http.HandlerFunc that serves the provided checks.
211-
func handleRootHealthz(checks ...HealthChecker) http.HandlerFunc {
211+
// handleRootHealth returns an http.HandlerFunc that serves the provided checks.
212+
func handleRootHealth(name string, checks ...HealthChecker) http.HandlerFunc {
212213
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
213214
excluded := getExcludedChecks(r)
214215
// 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 {
240241
}
241242
// always be verbose on failure
242243
if len(failedChecks) > 0 {
243-
klog.V(2).Infof("healthz check failed: %s\n%v", strings.Join(failedChecks, ","), failedVerboseLogOutput.String())
244-
http.Error(httplog.Unlogged(r, w), fmt.Sprintf("%shealthz check failed", individualCheckOutput.String()), http.StatusInternalServerError)
244+
klog.V(2).Infof("%s check failed: %s\n%v", strings.Join(failedChecks, ","), name, failedVerboseLogOutput.String())
245+
http.Error(httplog.Unlogged(r, w), fmt.Sprintf("%s%s check failed", individualCheckOutput.String(), name), http.StatusInternalServerError)
245246
return
246247
}
247248

@@ -253,7 +254,7 @@ func handleRootHealthz(checks ...HealthChecker) http.HandlerFunc {
253254
}
254255

255256
individualCheckOutput.WriteTo(w)
256-
fmt.Fprint(w, "healthz check passed\n")
257+
fmt.Fprintf(w, "%s check passed\n", name)
257258
})
258259
}
259260

staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,24 @@ func TestInstallPathHandler(t *testing.T) {
9494

9595
}
9696

97-
func testMultipleChecks(path string, t *testing.T) {
97+
func testMultipleChecks(path, name string, t *testing.T) {
9898
tests := []struct {
9999
path string
100100
expectedResponse string
101101
expectedStatus int
102102
addBadCheck bool
103103
}{
104-
{"?verbose", "[+]ping ok\nhealthz check passed\n", http.StatusOK, false},
104+
{"?verbose", fmt.Sprintf("[+]ping ok\n%s check passed\n", name), http.StatusOK, false},
105105
{"?exclude=dontexist", "ok", http.StatusOK, false},
106106
{"?exclude=bad", "ok", http.StatusOK, true},
107-
{"?verbose=true&exclude=bad", "[+]ping ok\n[+]bad excluded: ok\nhealthz check passed\n", http.StatusOK, true},
108-
{"?verbose=true&exclude=dontexist", "[+]ping ok\nwarn: some health checks cannot be excluded: no matches for \"dontexist\"\nhealthz check passed\n", http.StatusOK, false},
107+
{"?verbose=true&exclude=bad", fmt.Sprintf("[+]ping ok\n[+]bad excluded: ok\n%s check passed\n", name), http.StatusOK, true},
108+
{"?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},
109109
{"/ping", "ok", http.StatusOK, false},
110110
{"", "ok", http.StatusOK, false},
111-
{"?verbose", "[+]ping ok\n[-]bad failed: reason withheld\nhealthz check failed\n", http.StatusInternalServerError, true},
111+
{"?verbose", fmt.Sprintf("[+]ping ok\n[-]bad failed: reason withheld\n%s check failed\n", name), http.StatusInternalServerError, true},
112112
{"/ping", "ok", http.StatusOK, true},
113113
{"/bad", "internal server error: this will fail\n", http.StatusInternalServerError, true},
114-
{"", "[+]ping ok\n[-]bad failed: reason withheld\nhealthz check failed\n", http.StatusInternalServerError, true},
114+
{"", fmt.Sprintf("[+]ping ok\n[-]bad failed: reason withheld\n%s check failed\n", name), http.StatusInternalServerError, true},
115115
}
116116

117117
for i, test := range tests {
@@ -148,11 +148,11 @@ func testMultipleChecks(path string, t *testing.T) {
148148
}
149149

150150
func TestMultipleChecks(t *testing.T) {
151-
testMultipleChecks("", t)
151+
testMultipleChecks("", "healthz", t)
152152
}
153153

154154
func TestMultiplePathChecks(t *testing.T) {
155-
testMultipleChecks("/ready", t)
155+
testMultipleChecks("/ready", "ready", t)
156156
}
157157

158158
func TestCheckerNames(t *testing.T) {

0 commit comments

Comments
 (0)