Skip to content

Commit fdb6ef2

Browse files
authored
Merge pull request #1134 from 0gajun/avoid_redirection
🐛 Avoid the redirection to `/healthz/` when calling `/healthz`
2 parents 2d69b1e + d6fcdd6 commit fdb6ef2

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
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: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"net"
2525
"net/http"
2626
"path"
27-
"strings"
2827
"sync"
2928
"time"
3029

@@ -970,10 +969,15 @@ var _ = Describe("manger.Manager", func() {
970969
Expect(err).NotTo(HaveOccurred())
971970
Expect(resp.StatusCode).To(Equal(http.StatusOK))
972971

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

@@ -1016,10 +1020,15 @@ var _ = Describe("manger.Manager", func() {
10161020
Expect(err).NotTo(HaveOccurred())
10171021
Expect(resp.StatusCode).To(Equal(http.StatusOK))
10181022

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

0 commit comments

Comments
 (0)