diff --git a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go index 24fafcefa0f..122dd0b6eed 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go +++ b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go @@ -112,7 +112,7 @@ func InstallPathHandler(mux mux, path string, checks ...HealthzChecker) { checks = []HealthzChecker{PingHealthz} } - glog.V(5).Info("Installing healthz checkers:", strings.Join(checkerNames(checks...), ", ")) + glog.V(5).Info("Installing healthz checkers:", formatQuoted(checkerNames(checks...)...)) mux.Handle(path, handleRootHealthz(checks...)) for _, check := range checks { @@ -187,14 +187,20 @@ func adaptCheckToHandler(c func(r *http.Request) error) http.HandlerFunc { // checkerNames returns the names of the checks in the same order as passed in. func checkerNames(checks ...HealthzChecker) []string { - if len(checks) > 0 { - // accumulate the names of checks for printing them out. - checkerNames := make([]string, 0, len(checks)) - for _, check := range checks { - // quote the Name so we can disambiguate - checkerNames = append(checkerNames, fmt.Sprintf("%q", check.Name())) - } - return checkerNames + // accumulate the names of checks for printing them out. + checkerNames := make([]string, 0, len(checks)) + for _, check := range checks { + checkerNames = append(checkerNames, check.Name()) } - return nil + return checkerNames +} + +// formatQuoted returns a formatted string of the health check names, +// preserving the order passed in. +func formatQuoted(names ...string) string { + quoted := make([]string, 0, len(names)) + for _, name := range names { + quoted = append(quoted, fmt.Sprintf("%q", name)) + } + return strings.Join(quoted, ",") } diff --git a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go index 7ae26c4e9ce..aee379f3faa 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go +++ b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go @@ -148,10 +148,32 @@ func TestCheckerNames(t *testing.T) { for _, tc := range testCases { result := checkerNames(tc.have...) t.Run(tc.desc, func(t *testing.T) { - if reflect.DeepEqual(tc.want, result) { + if !reflect.DeepEqual(tc.want, result) { t.Errorf("want %#v, got %#v", tc.want, result) } }) } - +} + +func TestFormatQuoted(t *testing.T) { + n1 := "n1" + n2 := "n2" + testCases := []struct { + desc string + names []string + expected string + }{ + {"empty", []string{}, ""}, + {"single name", []string{n1}, "\"n1\""}, + {"two names", []string{n1, n2}, "\"n1\",\"n2\""}, + {"two names, reverse order", []string{n2, n1}, "\"n2\",\"n1\""}, + } + for _, tc := range testCases { + result := formatQuoted(tc.names...) + t.Run(tc.desc, func(t *testing.T) { + if result != tc.expected { + t.Errorf("expected %#v, got %#v", tc.expected, result) + } + }) + } }