Skip to content

Commit

Permalink
Merge pull request #1134 from 0gajun/avoid_redirection
Browse files Browse the repository at this point in the history
🐛 Avoid the redirection to `/healthz/` when calling `/healthz`
  • Loading branch information
k8s-ci-robot committed Aug 24, 2020
2 parents 2d69b1e + d6fcdd6 commit fdb6ef2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 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
23 changes: 16 additions & 7 deletions pkg/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"net"
"net/http"
"path"
"strings"
"sync"
"time"

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

// Check readiness path without trailing slash
readinessEndpoint = fmt.Sprint("http://", listener.Addr().String(), strings.TrimSuffix(defaultReadinessEndpoint, "/"))
// Check readiness path without trailing slash without redirect
readinessEndpoint = fmt.Sprint("http://", listener.Addr().String(), 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 +1020,15 @@ var _ = Describe("manger.Manager", func() {
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))

// Check liveness path without trailing slash
livenessEndpoint = fmt.Sprint("http://", listener.Addr().String(), strings.TrimSuffix(defaultLivenessEndpoint, "/"))
// Check liveness path without trailing slash without redirect
livenessEndpoint = fmt.Sprint("http://", listener.Addr().String(), 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 fdb6ef2

Please sign in to comment.