Skip to content

Commit c9ee156

Browse files
committed
Avoid the redirection to /healthz/ when calling /healthz
By #1100, subpaths of `/healthz/` can be handled. However, this change introduced the redirection from `/healthz` to `/healthz/` with 301 (Moved Permanently) when accessing `/healthz` endpoint. (Accessing `/healthz/` works fine without the redirect.) This is unnecessary overhead in the health checking. So, this commit enable to access `/healthz` without the redirect with keeping that subpaths of `/healthz/` are accessible.
1 parent 011cd8a commit c9ee156

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

pkg/manager/internal.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ const (
5252
defaultRetryPeriod = 2 * time.Second
5353
defaultGracefulShutdownPeriod = 30 * time.Second
5454

55-
defaultReadinessEndpoint = "/readyz/"
56-
defaultLivenessEndpoint = "/healthz/"
55+
defaultReadinessEndpoint = "/readyz"
56+
defaultLivenessEndpoint = "/healthz"
5757
defaultMetricsEndpoint = "/metrics"
5858
)
5959

@@ -414,9 +414,13 @@ func (cm *controllerManager) serveHealthProbes(stop <-chan struct{}) {
414414

415415
if cm.readyzHandler != nil {
416416
mux.Handle(cm.readinessEndpointName, http.StripPrefix(cm.readinessEndpointName, cm.readyzHandler))
417+
// Append '/' suffix to handle subpaths
418+
mux.Handle(cm.readinessEndpointName+"/", http.StripPrefix(cm.readinessEndpointName, cm.readyzHandler))
417419
}
418420
if cm.healthzHandler != nil {
419421
mux.Handle(cm.livenessEndpointName, http.StripPrefix(cm.livenessEndpointName, cm.healthzHandler))
422+
// Append '/' suffix to handle subpaths
423+
mux.Handle(cm.livenessEndpointName+"/", http.StripPrefix(cm.livenessEndpointName, cm.healthzHandler))
420424
}
421425

422426
server := http.Server{

pkg/manager/manager_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -970,10 +970,15 @@ var _ = Describe("manger.Manager", func() {
970970
Expect(err).NotTo(HaveOccurred())
971971
Expect(resp.StatusCode).To(Equal(http.StatusOK))
972972

973-
// Check readiness path without trailing slash
973+
// Check readiness path without trailing slash without redirect
974974
readinessEndpoint = fmt.Sprint("http://", listener.Addr().String(), strings.TrimSuffix(defaultReadinessEndpoint, "/"))
975975
res = nil
976-
resp, err = http.Get(readinessEndpoint)
976+
httpClient := http.Client{
977+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
978+
return http.ErrUseLastResponse // Do not follow redirect
979+
},
980+
}
981+
resp, err = httpClient.Get(readinessEndpoint)
977982
Expect(err).NotTo(HaveOccurred())
978983
Expect(resp.StatusCode).To(Equal(http.StatusOK))
979984

@@ -1016,10 +1021,15 @@ var _ = Describe("manger.Manager", func() {
10161021
Expect(err).NotTo(HaveOccurred())
10171022
Expect(resp.StatusCode).To(Equal(http.StatusOK))
10181023

1019-
// Check liveness path without trailing slash
1024+
// Check liveness path without trailing slash without redirect
10201025
livenessEndpoint = fmt.Sprint("http://", listener.Addr().String(), strings.TrimSuffix(defaultLivenessEndpoint, "/"))
10211026
res = nil
1022-
resp, err = http.Get(livenessEndpoint)
1027+
httpClient := http.Client{
1028+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
1029+
return http.ErrUseLastResponse // Do not follow redirect
1030+
},
1031+
}
1032+
resp, err = httpClient.Get(livenessEndpoint)
10231033
Expect(err).NotTo(HaveOccurred())
10241034
Expect(resp.StatusCode).To(Equal(http.StatusOK))
10251035

0 commit comments

Comments
 (0)