1
0
Fork 0

add http headers to healthcheck

This commit is contained in:
Jesse Haka 2018-04-16 11:40:03 +02:00 committed by Traefiker Bot
parent de3aeb9732
commit 1954a49f37
29 changed files with 337 additions and 64 deletions

View file

@ -29,6 +29,8 @@ func GetHealthCheck(metrics metricsRegistry) *HealthCheck {
// Options are the public health check options.
type Options struct {
Headers map[string]string
Hostname string
Path string
Port int
Transport http.RoundTripper
@ -37,7 +39,7 @@ type Options struct {
}
func (opt Options) String() string {
return fmt.Sprintf("[Path: %s Port: %d Interval: %s]", opt.Path, opt.Port, opt.Interval)
return fmt.Sprintf("[Hostname: %s Headers: %v Path: %s Port: %d Interval: %s]", opt.Hostname, opt.Headers, opt.Path, opt.Port, opt.Interval)
}
// BackendHealthCheck HealthCheck configuration for a backend
@ -84,7 +86,7 @@ func NewBackendHealthCheck(options Options, backendName string) *BackendHealthCh
}
}
//SetBackendsConfiguration set backends configuration
// SetBackendsConfiguration set backends configuration
func (hc *HealthCheck) SetBackendsConfiguration(parentCtx context.Context, backends map[string]*BackendHealthCheck) {
hc.Backends = backends
if hc.cancel != nil {
@ -149,20 +151,31 @@ func (hc *HealthCheck) checkBackend(backend *BackendHealthCheck) {
}
}
func (backend *BackendHealthCheck) newRequest(serverURL *url.URL) (*http.Request, error) {
if backend.Port == 0 {
return http.NewRequest(http.MethodGet, serverURL.String()+backend.Path, nil)
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(backend.Port))
u.Path = u.Path + backend.Path
u.Host = net.JoinHostPort(u.Hostname(), strconv.Itoa(b.Port))
u.Path = u.Path + b.Path
return http.NewRequest(http.MethodGet, u.String(), nil)
}
// this function adds additional http headers and hostname to http.request
func (b *BackendHealthCheck) addHeadersAndHost(req *http.Request) *http.Request {
if b.Options.Hostname != "" {
req.Host = b.Options.Hostname
}
for k, v := range b.Options.Headers {
req.Header.Set(k, v)
}
return req
}
// checkHealth returns a nil error in case it was successful and otherwise
// a non-nil error with a meaningful description why the health check failed.
func checkHealth(serverURL *url.URL, backend *BackendHealthCheck) error {
@ -174,6 +187,7 @@ func checkHealth(serverURL *url.URL, backend *BackendHealthCheck) error {
if err != nil {
return fmt.Errorf("failed to create HTTP request: %s", err)
}
req = backend.addHeadersAndHost(req)
resp, err := client.Do(req)
if err == nil {

View file

@ -10,6 +10,8 @@ import (
"time"
"github.com/containous/traefik/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vulcand/oxy/roundrobin"
)
@ -21,57 +23,57 @@ type testHandler struct {
}
func TestSetBackendsConfiguration(t *testing.T) {
tests := []struct {
desc string
startHealthy bool
healthSequence []bool
wantNumRemovedServers int
wantNumUpsertedServers int
wantGaugeValue float64
testCases := []struct {
desc string
startHealthy bool
healthSequence []bool
expectedNumRemovedServers int
expectedNumUpsertedServers int
expectedGaugeValue float64
}{
{
desc: "healthy server staying healthy",
startHealthy: true,
healthSequence: []bool{true},
wantNumRemovedServers: 0,
wantNumUpsertedServers: 0,
wantGaugeValue: 1,
desc: "healthy server staying healthy",
startHealthy: true,
healthSequence: []bool{true},
expectedNumRemovedServers: 0,
expectedNumUpsertedServers: 0,
expectedGaugeValue: 1,
},
{
desc: "healthy server becoming sick",
startHealthy: true,
healthSequence: []bool{false},
wantNumRemovedServers: 1,
wantNumUpsertedServers: 0,
wantGaugeValue: 0,
desc: "healthy server becoming sick",
startHealthy: true,
healthSequence: []bool{false},
expectedNumRemovedServers: 1,
expectedNumUpsertedServers: 0,
expectedGaugeValue: 0,
},
{
desc: "sick server becoming healthy",
startHealthy: false,
healthSequence: []bool{true},
wantNumRemovedServers: 0,
wantNumUpsertedServers: 1,
wantGaugeValue: 1,
desc: "sick server becoming healthy",
startHealthy: false,
healthSequence: []bool{true},
expectedNumRemovedServers: 0,
expectedNumUpsertedServers: 1,
expectedGaugeValue: 1,
},
{
desc: "sick server staying sick",
startHealthy: false,
healthSequence: []bool{false},
wantNumRemovedServers: 0,
wantNumUpsertedServers: 0,
wantGaugeValue: 0,
desc: "sick server staying sick",
startHealthy: false,
healthSequence: []bool{false},
expectedNumRemovedServers: 0,
expectedNumUpsertedServers: 0,
expectedGaugeValue: 0,
},
{
desc: "healthy server toggling to sick and back to healthy",
startHealthy: true,
healthSequence: []bool{false, true},
wantNumRemovedServers: 1,
wantNumUpsertedServers: 1,
wantGaugeValue: 1,
desc: "healthy server toggling to sick and back to healthy",
startHealthy: true,
healthSequence: []bool{false, true},
expectedNumRemovedServers: 1,
expectedNumUpsertedServers: 1,
expectedGaugeValue: 1,
},
}
for _, test := range tests {
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
@ -88,6 +90,7 @@ func TestSetBackendsConfiguration(t *testing.T) {
Interval: healthCheckInterval,
LB: lb,
}, "backendName")
serverURL := testhelpers.MustParseURL(ts.URL)
if test.startHealthy {
lb.servers = append(lb.servers, serverURL)
@ -100,8 +103,10 @@ func TestSetBackendsConfiguration(t *testing.T) {
Backends: make(map[string]*BackendHealthCheck),
metrics: collectingMetrics,
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
check.execute(ctx, backend)
wg.Done()
@ -119,23 +124,16 @@ func TestSetBackendsConfiguration(t *testing.T) {
lb.Lock()
defer lb.Unlock()
if lb.numRemovedServers != test.wantNumRemovedServers {
t.Errorf("got %d removed servers, wanted %d", lb.numRemovedServers, test.wantNumRemovedServers)
}
if lb.numUpsertedServers != test.wantNumUpsertedServers {
t.Errorf("got %d upserted servers, wanted %d", lb.numUpsertedServers, test.wantNumUpsertedServers)
}
if collectingMetrics.Gauge.GaugeValue != test.wantGaugeValue {
t.Errorf("got %v ServerUp Gauge, want %v", collectingMetrics.Gauge.GaugeValue, test.wantGaugeValue)
}
assert.Equal(t, test.expectedNumRemovedServers, lb.numRemovedServers, "removed servers")
assert.Equal(t, test.expectedNumUpsertedServers, lb.numUpsertedServers, "upserted servers")
assert.Equal(t, test.expectedGaugeValue, collectingMetrics.Gauge.GaugeValue, "ServerUp Gauge")
})
}
}
func TestNewRequest(t *testing.T) {
tests := []struct {
testCases := []struct {
desc string
host string
port int
@ -172,10 +170,11 @@ func TestNewRequest(t *testing.T) {
},
}
for _, test := range tests {
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
backend := NewBackendHealthCheck(
Options{
Path: test.path,
@ -187,15 +186,94 @@ func TestNewRequest(t *testing.T) {
Host: test.host,
}
req, err := backend.newRequest(u)
require.NoError(t, err, "failed to create new backend request")
assert.Equal(t, test.expected, req.URL.String())
})
}
}
func TestNewRequestWithAddHeaders(t *testing.T) {
testCases := []struct {
desc string
host string
headers map[string]string
hostname string
port int
path string
expectedHostname string
expectedHeader string
}{
{
desc: "override hostname",
host: "backend1:80",
headers: map[string]string{},
hostname: "myhost",
port: 0,
path: "/",
expectedHostname: "myhost",
expectedHeader: "",
},
{
desc: "not override hostname",
host: "backend1:80",
headers: map[string]string{},
hostname: "",
port: 0,
path: "/",
expectedHostname: "backend1:80",
expectedHeader: "",
},
{
desc: "custom header",
host: "backend1:80",
headers: map[string]string{"Custom-Header": "foo"},
hostname: "",
port: 0,
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: "/",
expectedHostname: "myhost",
expectedHeader: "foo",
},
}
for _, test := range testCases {
test := test
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")
u := &url.URL{
Scheme: "http",
Host: test.host,
}
req, err := backend.newRequest(u)
if err != nil {
t.Fatalf("failed to create new backend request: %s", err)
}
actual := req.URL.String()
if actual != test.expected {
t.Fatalf("got %s for healthcheck URL, wanted %s", actual, test.expected)
}
req = backend.addHeadersAndHost(req)
assert.Equal(t, test.expectedHostname, req.Host)
assert.Equal(t, test.expectedHeader, req.Header.Get("Custom-Header"))
})
}
}