mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
make log output of failed healthchecks more focused
This commit is contained in:
parent
13f1d956ea
commit
e87f62dcd5
@ -172,35 +172,38 @@ func getExcludedChecks(r *http.Request) sets.String {
|
|||||||
// handleRootHealthz returns an http.HandlerFunc that serves the provided checks.
|
// handleRootHealthz returns an http.HandlerFunc that serves the provided checks.
|
||||||
func handleRootHealthz(checks ...HealthChecker) http.HandlerFunc {
|
func handleRootHealthz(checks ...HealthChecker) http.HandlerFunc {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
failed := false
|
|
||||||
excluded := getExcludedChecks(r)
|
excluded := getExcludedChecks(r)
|
||||||
var verboseOut bytes.Buffer
|
// failedVerboseLogOutput is for output to the log. It indicates detailed failed output information for the log.
|
||||||
|
var failedVerboseLogOutput bytes.Buffer
|
||||||
|
var failedChecks []string
|
||||||
|
var individualCheckOutput bytes.Buffer
|
||||||
for _, check := range checks {
|
for _, check := range checks {
|
||||||
// no-op the check if we've specified we want to exclude the check
|
// no-op the check if we've specified we want to exclude the check
|
||||||
if excluded.Has(check.Name()) {
|
if excluded.Has(check.Name()) {
|
||||||
excluded.Delete(check.Name())
|
excluded.Delete(check.Name())
|
||||||
fmt.Fprintf(&verboseOut, "[+]%v excluded: ok\n", check.Name())
|
fmt.Fprintf(&individualCheckOutput, "[+]%s excluded: ok\n", check.Name())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := check.Check(r); err != nil {
|
if err := check.Check(r); err != nil {
|
||||||
// don't include the error since this endpoint is public. If someone wants more detail
|
// don't include the error since this endpoint is public. If someone wants more detail
|
||||||
// they should have explicit permission to the detailed checks.
|
// they should have explicit permission to the detailed checks.
|
||||||
klog.V(4).Infof("healthz check %v failed: %v", check.Name(), err)
|
fmt.Fprintf(&individualCheckOutput, "[-]%s failed: reason withheld\n", check.Name())
|
||||||
fmt.Fprintf(&verboseOut, "[-]%v failed: reason withheld\n", check.Name())
|
// but we do want detailed information for our log
|
||||||
failed = true
|
fmt.Fprintf(&failedVerboseLogOutput, "[-]%s failed: %v\n", check.Name(), err)
|
||||||
|
failedChecks = append(failedChecks, check.Name())
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(&verboseOut, "[+]%v ok\n", check.Name())
|
fmt.Fprintf(&individualCheckOutput, "[+]%s ok\n", check.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if excluded.Len() > 0 {
|
if excluded.Len() > 0 {
|
||||||
fmt.Fprintf(&verboseOut, "warn: some health checks cannot be excluded: no matches for %v\n", formatQuoted(excluded.List()...))
|
fmt.Fprintf(&individualCheckOutput, "warn: some health checks cannot be excluded: no matches for %s\n", formatQuoted(excluded.List()...))
|
||||||
klog.Warningf("cannot exclude some health checks, no health checks are installed matching %v",
|
klog.Warningf("cannot exclude some health checks, no health checks are installed matching %s",
|
||||||
formatQuoted(excluded.List()...))
|
formatQuoted(excluded.List()...))
|
||||||
}
|
}
|
||||||
// always be verbose on failure
|
// always be verbose on failure
|
||||||
if failed {
|
if len(failedChecks) > 0 {
|
||||||
klog.V(2).Infof("%vhealthz check failed", verboseOut.String())
|
klog.V(2).Infof("healthz check failed: %s\n%v", strings.Join(failedChecks, ","), failedVerboseLogOutput.String())
|
||||||
http.Error(httplog.Unlogged(r, w), fmt.Sprintf("%vhealthz check failed", verboseOut.String()), http.StatusInternalServerError)
|
http.Error(httplog.Unlogged(r, w), fmt.Sprintf("%shealthz check failed", individualCheckOutput.String()), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,7 +214,7 @@ func handleRootHealthz(checks ...HealthChecker) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
verboseOut.WriteTo(w)
|
individualCheckOutput.WriteTo(w)
|
||||||
fmt.Fprint(w, "healthz check passed\n")
|
fmt.Fprint(w, "healthz check passed\n")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user