Skip to content

Commit

Permalink
Avoid the redirection to /healthz/ when calling /healthz
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
0gajun committed Aug 23, 2020
1 parent 011cd8a commit c9ee156
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 6 additions & 2 deletions pkg/manager/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ const (
defaultRetryPeriod = 2 * time.Second
defaultGracefulShutdownPeriod = 30 * time.Second

defaultReadinessEndpoint = "/readyz/"
defaultLivenessEndpoint = "/healthz/"
defaultReadinessEndpoint = "/readyz"
defaultLivenessEndpoint = "/healthz"
defaultMetricsEndpoint = "/metrics"
)

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

if cm.readyzHandler != nil {
mux.Handle(cm.readinessEndpointName, http.StripPrefix(cm.readinessEndpointName, cm.readyzHandler))
// Append '/' suffix to handle subpaths
mux.Handle(cm.readinessEndpointName+"/", http.StripPrefix(cm.readinessEndpointName, cm.readyzHandler))
}
if cm.healthzHandler != nil {
mux.Handle(cm.livenessEndpointName, http.StripPrefix(cm.livenessEndpointName, cm.healthzHandler))
// Append '/' suffix to handle subpaths
mux.Handle(cm.livenessEndpointName+"/", http.StripPrefix(cm.livenessEndpointName, cm.healthzHandler))
}

server := http.Server{
Expand Down
18 changes: 14 additions & 4 deletions pkg/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,10 +970,15 @@ var _ = Describe("manger.Manager", func() {
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))

// Check readiness path without trailing slash
// Check readiness path without trailing slash without redirect
readinessEndpoint = fmt.Sprint("http://", listener.Addr().String(), strings.TrimSuffix(defaultReadinessEndpoint, "/"))
res = nil
resp, err = http.Get(readinessEndpoint)
httpClient := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse // Do not follow redirect
},
}
resp, err = httpClient.Get(readinessEndpoint)
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))

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

// Check liveness path without trailing slash
// Check liveness path without trailing slash without redirect
livenessEndpoint = fmt.Sprint("http://", listener.Addr().String(), strings.TrimSuffix(defaultLivenessEndpoint, "/"))
res = nil
resp, err = http.Get(livenessEndpoint)
httpClient := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse // Do not follow redirect
},
}
resp, err = httpClient.Get(livenessEndpoint)
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))

Expand Down

0 comments on commit c9ee156

Please sign in to comment.