Override health check scheme
This commit is contained in:
parent
b7d20496f3
commit
df11e67bb4
33 changed files with 194 additions and 104 deletions
|
@ -31,6 +31,7 @@ func GetHealthCheck(metrics metricsRegistry) *HealthCheck {
|
|||
type Options struct {
|
||||
Headers map[string]string
|
||||
Hostname string
|
||||
Scheme string
|
||||
Path string
|
||||
Port int
|
||||
Transport http.RoundTripper
|
||||
|
@ -50,7 +51,7 @@ type BackendHealthCheck struct {
|
|||
requestTimeout time.Duration
|
||||
}
|
||||
|
||||
//HealthCheck struct
|
||||
// HealthCheck struct
|
||||
type HealthCheck struct {
|
||||
Backends map[string]*BackendHealthCheck
|
||||
metrics metricsRegistry
|
||||
|
@ -71,8 +72,8 @@ func newHealthCheck(metrics metricsRegistry) *HealthCheck {
|
|||
}
|
||||
}
|
||||
|
||||
// metricsRegistry is a local interface in the healthcheck package, exposing only the required metrics
|
||||
// necessary for the healthcheck package. This makes it easier for the tests.
|
||||
// metricsRegistry is a local interface in the health check package, exposing only the required metrics
|
||||
// necessary for the health check package. This makes it easier for the tests.
|
||||
type metricsRegistry interface {
|
||||
BackendServerUpGauge() metrics.Gauge
|
||||
}
|
||||
|
@ -152,15 +153,18 @@ func (hc *HealthCheck) checkBackend(backend *BackendHealthCheck) {
|
|||
}
|
||||
|
||||
func (b *BackendHealthCheck) newRequest(serverURL *url.URL) (*http.Request, error) {
|
||||
if b.Port == 0 {
|
||||
return http.NewRequest(http.MethodGet, serverURL.String()+b.Path, nil)
|
||||
}
|
||||
|
||||
// copy the url and add the port to the host
|
||||
u := &url.URL{}
|
||||
*u = *serverURL
|
||||
u.Host = net.JoinHostPort(u.Hostname(), strconv.Itoa(b.Port))
|
||||
u.Path = u.Path + b.Path
|
||||
|
||||
if len(b.Scheme) > 0 {
|
||||
u.Scheme = b.Scheme
|
||||
}
|
||||
|
||||
if b.Port != 0 {
|
||||
u.Host = net.JoinHostPort(u.Hostname(), strconv.Itoa(b.Port))
|
||||
}
|
||||
|
||||
u.Path += b.Path
|
||||
|
||||
return http.NewRequest(http.MethodGet, u.String(), nil)
|
||||
}
|
||||
|
|
|
@ -134,40 +134,57 @@ func TestSetBackendsConfiguration(t *testing.T) {
|
|||
|
||||
func TestNewRequest(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
host string
|
||||
port int
|
||||
path string
|
||||
expected string
|
||||
desc string
|
||||
serverURL string
|
||||
options Options
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
desc: "no port override",
|
||||
host: "backend1:80",
|
||||
port: 0,
|
||||
path: "/test",
|
||||
desc: "no port override",
|
||||
serverURL: "http://backend1:80",
|
||||
options: Options{
|
||||
Path: "/test",
|
||||
Port: 0,
|
||||
},
|
||||
expected: "http://backend1:80/test",
|
||||
},
|
||||
{
|
||||
desc: "port override",
|
||||
host: "backend2:80",
|
||||
port: 8080,
|
||||
path: "/test",
|
||||
desc: "port override",
|
||||
serverURL: "http://backend2:80",
|
||||
options: Options{
|
||||
Path: "/test",
|
||||
Port: 8080,
|
||||
},
|
||||
expected: "http://backend2:8080/test",
|
||||
},
|
||||
{
|
||||
desc: "no port override with no port in host",
|
||||
host: "backend1",
|
||||
port: 0,
|
||||
path: "/health",
|
||||
desc: "no port override with no port in server URL",
|
||||
serverURL: "http://backend1",
|
||||
options: Options{
|
||||
Path: "/health",
|
||||
Port: 0,
|
||||
},
|
||||
expected: "http://backend1/health",
|
||||
},
|
||||
{
|
||||
desc: "port override with no port in host",
|
||||
host: "backend2",
|
||||
port: 8080,
|
||||
path: "/health",
|
||||
desc: "port override with no port in server URL",
|
||||
serverURL: "http://backend2",
|
||||
options: Options{
|
||||
Path: "/health",
|
||||
Port: 8080,
|
||||
},
|
||||
expected: "http://backend2:8080/health",
|
||||
},
|
||||
{
|
||||
desc: "scheme override",
|
||||
serverURL: "https://backend1:80",
|
||||
options: Options{
|
||||
Scheme: "http",
|
||||
Path: "/test",
|
||||
Port: 0,
|
||||
},
|
||||
expected: "http://backend1:80/test",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
@ -175,16 +192,10 @@ func TestNewRequest(t *testing.T) {
|
|||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
backend := NewBackendHealthCheck(
|
||||
Options{
|
||||
Path: test.path,
|
||||
Port: test.port,
|
||||
}, "backendName")
|
||||
backend := NewBackendHealthCheck(test.options, "backendName")
|
||||
|
||||
u := &url.URL{
|
||||
Scheme: "http",
|
||||
Host: test.host,
|
||||
}
|
||||
u, err := url.Parse(test.serverURL)
|
||||
require.NoError(t, err)
|
||||
|
||||
req, err := backend.newRequest(u)
|
||||
require.NoError(t, err, "failed to create new backend request")
|
||||
|
@ -194,54 +205,53 @@ func TestNewRequest(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestNewRequestWithAddHeaders(t *testing.T) {
|
||||
func TestAddHeadersAndHost(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
host string
|
||||
headers map[string]string
|
||||
hostname string
|
||||
port int
|
||||
path string
|
||||
serverURL string
|
||||
options Options
|
||||
expectedHostname string
|
||||
expectedHeader string
|
||||
}{
|
||||
{
|
||||
desc: "override hostname",
|
||||
host: "backend1:80",
|
||||
headers: map[string]string{},
|
||||
hostname: "myhost",
|
||||
port: 0,
|
||||
path: "/",
|
||||
desc: "override hostname",
|
||||
serverURL: "http://backend1:80",
|
||||
options: Options{
|
||||
Hostname: "myhost",
|
||||
Path: "/",
|
||||
},
|
||||
expectedHostname: "myhost",
|
||||
expectedHeader: "",
|
||||
},
|
||||
{
|
||||
desc: "not override hostname",
|
||||
host: "backend1:80",
|
||||
headers: map[string]string{},
|
||||
hostname: "",
|
||||
port: 0,
|
||||
path: "/",
|
||||
desc: "not override hostname",
|
||||
serverURL: "http://backend1:80",
|
||||
options: Options{
|
||||
Hostname: "",
|
||||
Path: "/",
|
||||
},
|
||||
expectedHostname: "backend1:80",
|
||||
expectedHeader: "",
|
||||
},
|
||||
{
|
||||
desc: "custom header",
|
||||
host: "backend1:80",
|
||||
headers: map[string]string{"Custom-Header": "foo"},
|
||||
hostname: "",
|
||||
port: 0,
|
||||
path: "/",
|
||||
desc: "custom header",
|
||||
serverURL: "http://backend1:80",
|
||||
options: Options{
|
||||
Headers: map[string]string{"Custom-Header": "foo"},
|
||||
Hostname: "",
|
||||
Path: "/",
|
||||
},
|
||||
expectedHostname: "backend1:80",
|
||||
expectedHeader: "foo",
|
||||
},
|
||||
{
|
||||
desc: "custom header with host override",
|
||||
host: "backend1:80",
|
||||
headers: map[string]string{"Custom-Header": "foo"},
|
||||
hostname: "myhost",
|
||||
port: 0,
|
||||
path: "/",
|
||||
desc: "custom header with hostname override",
|
||||
serverURL: "http://backend1:80",
|
||||
options: Options{
|
||||
Headers: map[string]string{"Custom-Header": "foo"},
|
||||
Hostname: "myhost",
|
||||
Path: "/",
|
||||
},
|
||||
expectedHostname: "myhost",
|
||||
expectedHeader: "foo",
|
||||
},
|
||||
|
@ -252,26 +262,17 @@ func TestNewRequestWithAddHeaders(t *testing.T) {
|
|||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
backend := NewBackendHealthCheck(
|
||||
Options{
|
||||
Hostname: test.hostname,
|
||||
Path: test.path,
|
||||
Port: test.port,
|
||||
Headers: test.headers,
|
||||
}, "backendName")
|
||||
backend := NewBackendHealthCheck(test.options, "backendName")
|
||||
|
||||
u := &url.URL{
|
||||
Scheme: "http",
|
||||
Host: test.host,
|
||||
}
|
||||
u, err := url.Parse(test.serverURL)
|
||||
require.NoError(t, err)
|
||||
|
||||
req, err := backend.newRequest(u)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new backend request: %s", err)
|
||||
}
|
||||
require.NoError(t, err, "failed to create new backend request")
|
||||
|
||||
req = backend.addHeadersAndHost(req)
|
||||
|
||||
assert.Equal(t, "http://backend1:80/", req.URL.String())
|
||||
assert.Equal(t, test.expectedHostname, req.Host)
|
||||
assert.Equal(t, test.expectedHeader, req.Header.Get("Custom-Header"))
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue