Add a method option to the service Health Check
This commit is contained in:
parent
2a2ea759d1
commit
af749f1864
17 changed files with 93 additions and 8 deletions
|
@ -151,10 +151,10 @@
|
||||||
- "traefik.http.services.service01.loadbalancer.healthcheck.hostname=foobar"
|
- "traefik.http.services.service01.loadbalancer.healthcheck.hostname=foobar"
|
||||||
- "traefik.http.services.service01.loadbalancer.healthcheck.interval=foobar"
|
- "traefik.http.services.service01.loadbalancer.healthcheck.interval=foobar"
|
||||||
- "traefik.http.services.service01.loadbalancer.healthcheck.path=foobar"
|
- "traefik.http.services.service01.loadbalancer.healthcheck.path=foobar"
|
||||||
|
- "traefik.http.services.service01.loadbalancer.healthcheck.method=foobar"
|
||||||
- "traefik.http.services.service01.loadbalancer.healthcheck.port=42"
|
- "traefik.http.services.service01.loadbalancer.healthcheck.port=42"
|
||||||
- "traefik.http.services.service01.loadbalancer.healthcheck.scheme=foobar"
|
- "traefik.http.services.service01.loadbalancer.healthcheck.scheme=foobar"
|
||||||
- "traefik.http.services.service01.loadbalancer.healthcheck.timeout=foobar"
|
- "traefik.http.services.service01.loadbalancer.healthcheck.timeout=foobar"
|
||||||
- "traefik.http.services.service01.loadbalancer.healthcheck.followredirects=true"
|
|
||||||
- "traefik.http.services.service01.loadbalancer.passhostheader=true"
|
- "traefik.http.services.service01.loadbalancer.passhostheader=true"
|
||||||
- "traefik.http.services.service01.loadbalancer.responseforwarding.flushinterval=foobar"
|
- "traefik.http.services.service01.loadbalancer.responseforwarding.flushinterval=foobar"
|
||||||
- "traefik.http.services.service01.loadbalancer.serverstransport=foobar"
|
- "traefik.http.services.service01.loadbalancer.serverstransport=foobar"
|
||||||
|
|
|
@ -54,6 +54,7 @@
|
||||||
[http.services.Service01.loadBalancer.healthCheck]
|
[http.services.Service01.loadBalancer.healthCheck]
|
||||||
scheme = "foobar"
|
scheme = "foobar"
|
||||||
path = "foobar"
|
path = "foobar"
|
||||||
|
method = "foobar"
|
||||||
port = 42
|
port = 42
|
||||||
interval = "foobar"
|
interval = "foobar"
|
||||||
timeout = "foobar"
|
timeout = "foobar"
|
||||||
|
|
|
@ -59,6 +59,7 @@ http:
|
||||||
healthCheck:
|
healthCheck:
|
||||||
scheme: foobar
|
scheme: foobar
|
||||||
path: foobar
|
path: foobar
|
||||||
|
method: foobar
|
||||||
port: 42
|
port: 42
|
||||||
interval: foobar
|
interval: foobar
|
||||||
timeout: foobar
|
timeout: foobar
|
||||||
|
|
|
@ -207,6 +207,7 @@
|
||||||
| `traefik/http/services/Service01/loadBalancer/healthCheck/headers/name1` | `foobar` |
|
| `traefik/http/services/Service01/loadBalancer/healthCheck/headers/name1` | `foobar` |
|
||||||
| `traefik/http/services/Service01/loadBalancer/healthCheck/hostname` | `foobar` |
|
| `traefik/http/services/Service01/loadBalancer/healthCheck/hostname` | `foobar` |
|
||||||
| `traefik/http/services/Service01/loadBalancer/healthCheck/interval` | `foobar` |
|
| `traefik/http/services/Service01/loadBalancer/healthCheck/interval` | `foobar` |
|
||||||
|
| `traefik/http/services/Service01/loadBalancer/healthCheck/method` | `foobar` |
|
||||||
| `traefik/http/services/Service01/loadBalancer/healthCheck/path` | `foobar` |
|
| `traefik/http/services/Service01/loadBalancer/healthCheck/path` | `foobar` |
|
||||||
| `traefik/http/services/Service01/loadBalancer/healthCheck/port` | `42` |
|
| `traefik/http/services/Service01/loadBalancer/healthCheck/port` | `42` |
|
||||||
| `traefik/http/services/Service01/loadBalancer/healthCheck/scheme` | `foobar` |
|
| `traefik/http/services/Service01/loadBalancer/healthCheck/scheme` | `foobar` |
|
||||||
|
|
|
@ -151,10 +151,10 @@
|
||||||
"traefik.http.services.service01.loadbalancer.healthcheck.hostname": "foobar",
|
"traefik.http.services.service01.loadbalancer.healthcheck.hostname": "foobar",
|
||||||
"traefik.http.services.service01.loadbalancer.healthcheck.interval": "foobar",
|
"traefik.http.services.service01.loadbalancer.healthcheck.interval": "foobar",
|
||||||
"traefik.http.services.service01.loadbalancer.healthcheck.path": "foobar",
|
"traefik.http.services.service01.loadbalancer.healthcheck.path": "foobar",
|
||||||
|
"traefik.http.services.service01.loadbalancer.healthcheck.method": "foobar",
|
||||||
"traefik.http.services.service01.loadbalancer.healthcheck.port": "42",
|
"traefik.http.services.service01.loadbalancer.healthcheck.port": "42",
|
||||||
"traefik.http.services.service01.loadbalancer.healthcheck.scheme": "foobar",
|
"traefik.http.services.service01.loadbalancer.healthcheck.scheme": "foobar",
|
||||||
"traefik.http.services.service01.loadbalancer.healthcheck.timeout": "foobar",
|
"traefik.http.services.service01.loadbalancer.healthcheck.timeout": "foobar",
|
||||||
"traefik.http.services.service01.loadbalancer.healthcheck.followredirects": "true",
|
|
||||||
"traefik.http.services.service01.loadbalancer.passhostheader": "true",
|
"traefik.http.services.service01.loadbalancer.passhostheader": "true",
|
||||||
"traefik.http.services.service01.loadbalancer.responseforwarding.flushinterval": "foobar",
|
"traefik.http.services.service01.loadbalancer.responseforwarding.flushinterval": "foobar",
|
||||||
"traefik.http.services.service01.loadbalancer.serverstransport": "foobar",
|
"traefik.http.services.service01.loadbalancer.serverstransport": "foobar",
|
||||||
|
|
|
@ -185,6 +185,14 @@ you'd add the tag `traefik.http.services.{name-of-your-choice}.loadbalancer.pass
|
||||||
traefik.http.services.myservice.loadbalancer.healthcheck.path=/foo
|
traefik.http.services.myservice.loadbalancer.healthcheck.path=/foo
|
||||||
```
|
```
|
||||||
|
|
||||||
|
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.method`"
|
||||||
|
|
||||||
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
traefik.http.services.myservice.loadbalancer.healthcheck.method=foobar
|
||||||
|
```
|
||||||
|
|
||||||
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.port`"
|
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.port`"
|
||||||
|
|
||||||
See [health check](../services/index.md#health-check) for more information.
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
|
@ -339,6 +339,14 @@ you'd add the label `traefik.http.services.<name-of-your-choice>.loadbalancer.pa
|
||||||
- "traefik.http.services.myservice.loadbalancer.healthcheck.path=/foo"
|
- "traefik.http.services.myservice.loadbalancer.healthcheck.path=/foo"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.method`"
|
||||||
|
|
||||||
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- "traefik.http.services.myservice.loadbalancer.healthcheck.method=foobar"
|
||||||
|
```
|
||||||
|
|
||||||
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.port`"
|
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.port`"
|
||||||
|
|
||||||
See [health check](../services/index.md#health-check) for more information.
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
|
@ -187,6 +187,14 @@ you'd add the label `traefik.http.services.{name-of-your-choice}.loadbalancer.pa
|
||||||
traefik.http.services.myservice.loadbalancer.healthcheck.path=/foo
|
traefik.http.services.myservice.loadbalancer.healthcheck.path=/foo
|
||||||
```
|
```
|
||||||
|
|
||||||
|
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.method`"
|
||||||
|
|
||||||
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
traefik.http.services.myservice.loadbalancer.healthcheck.method=foobar
|
||||||
|
```
|
||||||
|
|
||||||
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.port`"
|
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.port`"
|
||||||
|
|
||||||
See [health check](../services/index.md#health-check) for more information.
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
|
@ -164,6 +164,14 @@ A Story of key & values
|
||||||
|-----------------------------------------------------------------|--------|
|
|-----------------------------------------------------------------|--------|
|
||||||
| `traefik/http/services/myservice/loadbalancer/healthcheck/path` | `/foo` |
|
| `traefik/http/services/myservice/loadbalancer/healthcheck/path` | `/foo` |
|
||||||
|
|
||||||
|
??? info "`traefik/http/services/<service_name>/loadbalancer/healthcheck/method`"
|
||||||
|
|
||||||
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
||||||
|
| Key (Path) | Value |
|
||||||
|
|-------------------------------------------------------------------|----------|
|
||||||
|
| `traefik/http/services/myservice/loadbalancer/healthcheck/method` | `foobar` |
|
||||||
|
|
||||||
??? info "`traefik/http/services/<service_name>/loadbalancer/healthcheck/port`"
|
??? info "`traefik/http/services/<service_name>/loadbalancer/healthcheck/port`"
|
||||||
|
|
||||||
See [health check](../services/index.md#health-check) for more information.
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
|
@ -214,6 +214,14 @@ For example, to change the passHostHeader behavior, you'd add the label `"traefi
|
||||||
"traefik.http.services.myservice.loadbalancer.healthcheck.path": "/foo"
|
"traefik.http.services.myservice.loadbalancer.healthcheck.path": "/foo"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.method`"
|
||||||
|
|
||||||
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
||||||
|
```json
|
||||||
|
"traefik.http.services.myservice.loadbalancer.healthcheck.method": "foobar"
|
||||||
|
```
|
||||||
|
|
||||||
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.port`"
|
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.port`"
|
||||||
|
|
||||||
See [health check](../services/index.md#health-check) for more information.
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
|
@ -220,6 +220,14 @@ you'd add the label `traefik.http.services.{name-of-your-choice}.loadbalancer.pa
|
||||||
- "traefik.http.services.myservice.loadbalancer.healthcheck.path=/foo"
|
- "traefik.http.services.myservice.loadbalancer.healthcheck.path=/foo"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.method`"
|
||||||
|
|
||||||
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- "traefik.http.services.myservice.loadbalancer.healthcheck.method=foobar"
|
||||||
|
```
|
||||||
|
|
||||||
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.port`"
|
??? info "`traefik.http.services.<service_name>.loadbalancer.healthcheck.port`"
|
||||||
|
|
||||||
See [health check](../services/index.md#health-check) for more information.
|
See [health check](../services/index.md#health-check) for more information.
|
||||||
|
|
|
@ -330,6 +330,7 @@ Below are the available options for the health check mechanism:
|
||||||
- `timeout` (default: 5s), defines the maximum duration Traefik will wait for a health check request before considering the server unhealthy.
|
- `timeout` (default: 5s), defines the maximum duration Traefik will wait for a health check request before considering the server unhealthy.
|
||||||
- `headers` (optional), defines custom headers to be sent to the health check endpoint.
|
- `headers` (optional), defines custom headers to be sent to the health check endpoint.
|
||||||
- `followRedirects` (default: true), defines whether redirects should be followed during the health check calls.
|
- `followRedirects` (default: true), defines whether redirects should be followed during the health check calls.
|
||||||
|
- `method` (default: GET), defines the HTTP method that will be used while connecting to the endpoint.
|
||||||
|
|
||||||
!!! info "Interval & Timeout Format"
|
!!! info "Interval & Timeout Format"
|
||||||
|
|
||||||
|
|
|
@ -214,6 +214,7 @@ func (s *Server) SetDefaults() {
|
||||||
type ServerHealthCheck struct {
|
type ServerHealthCheck struct {
|
||||||
Scheme string `json:"scheme,omitempty" toml:"scheme,omitempty" yaml:"scheme,omitempty" export:"true"`
|
Scheme string `json:"scheme,omitempty" toml:"scheme,omitempty" yaml:"scheme,omitempty" export:"true"`
|
||||||
Path string `json:"path,omitempty" toml:"path,omitempty" yaml:"path,omitempty" export:"true"`
|
Path string `json:"path,omitempty" toml:"path,omitempty" yaml:"path,omitempty" export:"true"`
|
||||||
|
Method string `json:"method,omitempty" toml:"method,omitempty" yaml:"method,omitempty" export:"true"`
|
||||||
Port int `json:"port,omitempty" toml:"port,omitempty,omitzero" yaml:"port,omitempty" export:"true"`
|
Port int `json:"port,omitempty" toml:"port,omitempty,omitzero" yaml:"port,omitempty" export:"true"`
|
||||||
// FIXME change string to ptypes.Duration
|
// FIXME change string to ptypes.Duration
|
||||||
Interval string `json:"interval,omitempty" toml:"interval,omitempty" yaml:"interval,omitempty" export:"true"`
|
Interval string `json:"interval,omitempty" toml:"interval,omitempty" yaml:"interval,omitempty" export:"true"`
|
||||||
|
|
|
@ -150,6 +150,7 @@ func TestDecodeConfiguration(t *testing.T) {
|
||||||
"traefik.http.services.Service0.loadbalancer.healthcheck.hostname": "foobar",
|
"traefik.http.services.Service0.loadbalancer.healthcheck.hostname": "foobar",
|
||||||
"traefik.http.services.Service0.loadbalancer.healthcheck.interval": "foobar",
|
"traefik.http.services.Service0.loadbalancer.healthcheck.interval": "foobar",
|
||||||
"traefik.http.services.Service0.loadbalancer.healthcheck.path": "foobar",
|
"traefik.http.services.Service0.loadbalancer.healthcheck.path": "foobar",
|
||||||
|
"traefik.http.services.Service0.loadbalancer.healthcheck.method": "foobar",
|
||||||
"traefik.http.services.Service0.loadbalancer.healthcheck.port": "42",
|
"traefik.http.services.Service0.loadbalancer.healthcheck.port": "42",
|
||||||
"traefik.http.services.Service0.loadbalancer.healthcheck.scheme": "foobar",
|
"traefik.http.services.Service0.loadbalancer.healthcheck.scheme": "foobar",
|
||||||
"traefik.http.services.Service0.loadbalancer.healthcheck.timeout": "foobar",
|
"traefik.http.services.Service0.loadbalancer.healthcheck.timeout": "foobar",
|
||||||
|
@ -165,6 +166,7 @@ func TestDecodeConfiguration(t *testing.T) {
|
||||||
"traefik.http.services.Service1.loadbalancer.healthcheck.hostname": "foobar",
|
"traefik.http.services.Service1.loadbalancer.healthcheck.hostname": "foobar",
|
||||||
"traefik.http.services.Service1.loadbalancer.healthcheck.interval": "foobar",
|
"traefik.http.services.Service1.loadbalancer.healthcheck.interval": "foobar",
|
||||||
"traefik.http.services.Service1.loadbalancer.healthcheck.path": "foobar",
|
"traefik.http.services.Service1.loadbalancer.healthcheck.path": "foobar",
|
||||||
|
"traefik.http.services.Service1.loadbalancer.healthcheck.method": "foobar",
|
||||||
"traefik.http.services.Service1.loadbalancer.healthcheck.port": "42",
|
"traefik.http.services.Service1.loadbalancer.healthcheck.port": "42",
|
||||||
"traefik.http.services.Service1.loadbalancer.healthcheck.scheme": "foobar",
|
"traefik.http.services.Service1.loadbalancer.healthcheck.scheme": "foobar",
|
||||||
"traefik.http.services.Service1.loadbalancer.healthcheck.timeout": "foobar",
|
"traefik.http.services.Service1.loadbalancer.healthcheck.timeout": "foobar",
|
||||||
|
@ -649,6 +651,7 @@ func TestDecodeConfiguration(t *testing.T) {
|
||||||
HealthCheck: &dynamic.ServerHealthCheck{
|
HealthCheck: &dynamic.ServerHealthCheck{
|
||||||
Scheme: "foobar",
|
Scheme: "foobar",
|
||||||
Path: "foobar",
|
Path: "foobar",
|
||||||
|
Method: "foobar",
|
||||||
Port: 42,
|
Port: 42,
|
||||||
Interval: "foobar",
|
Interval: "foobar",
|
||||||
Timeout: "foobar",
|
Timeout: "foobar",
|
||||||
|
@ -676,6 +679,7 @@ func TestDecodeConfiguration(t *testing.T) {
|
||||||
HealthCheck: &dynamic.ServerHealthCheck{
|
HealthCheck: &dynamic.ServerHealthCheck{
|
||||||
Scheme: "foobar",
|
Scheme: "foobar",
|
||||||
Path: "foobar",
|
Path: "foobar",
|
||||||
|
Method: "foobar",
|
||||||
Port: 42,
|
Port: 42,
|
||||||
Interval: "foobar",
|
Interval: "foobar",
|
||||||
Timeout: "foobar",
|
Timeout: "foobar",
|
||||||
|
@ -1138,6 +1142,7 @@ func TestEncodeConfiguration(t *testing.T) {
|
||||||
HealthCheck: &dynamic.ServerHealthCheck{
|
HealthCheck: &dynamic.ServerHealthCheck{
|
||||||
Scheme: "foobar",
|
Scheme: "foobar",
|
||||||
Path: "foobar",
|
Path: "foobar",
|
||||||
|
Method: "foobar",
|
||||||
Port: 42,
|
Port: 42,
|
||||||
Interval: "foobar",
|
Interval: "foobar",
|
||||||
Timeout: "foobar",
|
Timeout: "foobar",
|
||||||
|
@ -1164,6 +1169,7 @@ func TestEncodeConfiguration(t *testing.T) {
|
||||||
HealthCheck: &dynamic.ServerHealthCheck{
|
HealthCheck: &dynamic.ServerHealthCheck{
|
||||||
Scheme: "foobar",
|
Scheme: "foobar",
|
||||||
Path: "foobar",
|
Path: "foobar",
|
||||||
|
Method: "foobar",
|
||||||
Port: 42,
|
Port: 42,
|
||||||
Interval: "foobar",
|
Interval: "foobar",
|
||||||
Timeout: "foobar",
|
Timeout: "foobar",
|
||||||
|
@ -1324,6 +1330,7 @@ func TestEncodeConfiguration(t *testing.T) {
|
||||||
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Hostname": "foobar",
|
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Hostname": "foobar",
|
||||||
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Interval": "foobar",
|
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Interval": "foobar",
|
||||||
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Path": "foobar",
|
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Path": "foobar",
|
||||||
|
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Method": "foobar",
|
||||||
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Port": "42",
|
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Port": "42",
|
||||||
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Scheme": "foobar",
|
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Scheme": "foobar",
|
||||||
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Timeout": "foobar",
|
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Timeout": "foobar",
|
||||||
|
@ -1339,6 +1346,7 @@ func TestEncodeConfiguration(t *testing.T) {
|
||||||
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Hostname": "foobar",
|
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Hostname": "foobar",
|
||||||
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Interval": "foobar",
|
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Interval": "foobar",
|
||||||
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Path": "foobar",
|
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Path": "foobar",
|
||||||
|
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Method": "foobar",
|
||||||
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Port": "42",
|
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Port": "42",
|
||||||
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Scheme": "foobar",
|
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Scheme": "foobar",
|
||||||
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Timeout": "foobar",
|
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Timeout": "foobar",
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -60,6 +61,7 @@ type Options struct {
|
||||||
Hostname string
|
Hostname string
|
||||||
Scheme string
|
Scheme string
|
||||||
Path string
|
Path string
|
||||||
|
Method string
|
||||||
Port int
|
Port int
|
||||||
FollowRedirects bool
|
FollowRedirects bool
|
||||||
Transport http.RoundTripper
|
Transport http.RoundTripper
|
||||||
|
@ -69,7 +71,7 @@ type Options struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opt Options) String() string {
|
func (opt Options) String() string {
|
||||||
return fmt.Sprintf("[Hostname: %s Headers: %v Path: %s Port: %d Interval: %s Timeout: %s FollowRedirects: %v]", opt.Hostname, opt.Headers, opt.Path, opt.Port, opt.Interval, opt.Timeout, opt.FollowRedirects)
|
return fmt.Sprintf("[Hostname: %s Headers: %v Path: %s Method: %s Port: %d Interval: %s Timeout: %s FollowRedirects: %v]", opt.Hostname, opt.Headers, opt.Path, opt.Method, opt.Port, opt.Interval, opt.Timeout, opt.FollowRedirects)
|
||||||
}
|
}
|
||||||
|
|
||||||
type backendURL struct {
|
type backendURL struct {
|
||||||
|
@ -101,8 +103,8 @@ func (b *BackendConfig) newRequest(serverURL *url.URL) (*http.Request, error) {
|
||||||
return http.NewRequest(http.MethodGet, u.String(), http.NoBody)
|
return http.NewRequest(http.MethodGet, u.String(), http.NoBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
// this function adds additional http headers and hostname to http.request.
|
// setRequestOptions sets all request options present on the BackendConfig.
|
||||||
func (b *BackendConfig) addHeadersAndHost(req *http.Request) *http.Request {
|
func (b *BackendConfig) setRequestOptions(req *http.Request) *http.Request {
|
||||||
if b.Options.Hostname != "" {
|
if b.Options.Hostname != "" {
|
||||||
req.Host = b.Options.Hostname
|
req.Host = b.Options.Hostname
|
||||||
}
|
}
|
||||||
|
@ -110,6 +112,11 @@ func (b *BackendConfig) addHeadersAndHost(req *http.Request) *http.Request {
|
||||||
for k, v := range b.Options.Headers {
|
for k, v := range b.Options.Headers {
|
||||||
req.Header.Set(k, v)
|
req.Header.Set(k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if b.Options.Method != "" {
|
||||||
|
req.Method = strings.ToUpper(b.Options.Method)
|
||||||
|
}
|
||||||
|
|
||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,7 +253,7 @@ func checkHealth(serverURL *url.URL, backend *BackendConfig) error {
|
||||||
return fmt.Errorf("failed to create HTTP request: %w", err)
|
return fmt.Errorf("failed to create HTTP request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req = backend.addHeadersAndHost(req)
|
req = backend.setRequestOptions(req)
|
||||||
|
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
Timeout: backend.Options.Timeout,
|
Timeout: backend.Options.Timeout,
|
||||||
|
|
|
@ -288,13 +288,14 @@ func TestNewRequest(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddHeadersAndHost(t *testing.T) {
|
func TestRequestOptions(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
serverURL string
|
serverURL string
|
||||||
options Options
|
options Options
|
||||||
expectedHostname string
|
expectedHostname string
|
||||||
expectedHeader string
|
expectedHeader string
|
||||||
|
expectedMethod string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "override hostname",
|
desc: "override hostname",
|
||||||
|
@ -305,6 +306,7 @@ func TestAddHeadersAndHost(t *testing.T) {
|
||||||
},
|
},
|
||||||
expectedHostname: "myhost",
|
expectedHostname: "myhost",
|
||||||
expectedHeader: "",
|
expectedHeader: "",
|
||||||
|
expectedMethod: http.MethodGet,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "not override hostname",
|
desc: "not override hostname",
|
||||||
|
@ -315,6 +317,7 @@ func TestAddHeadersAndHost(t *testing.T) {
|
||||||
},
|
},
|
||||||
expectedHostname: "backend1:80",
|
expectedHostname: "backend1:80",
|
||||||
expectedHeader: "",
|
expectedHeader: "",
|
||||||
|
expectedMethod: http.MethodGet,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "custom header",
|
desc: "custom header",
|
||||||
|
@ -326,6 +329,7 @@ func TestAddHeadersAndHost(t *testing.T) {
|
||||||
},
|
},
|
||||||
expectedHostname: "backend1:80",
|
expectedHostname: "backend1:80",
|
||||||
expectedHeader: "foo",
|
expectedHeader: "foo",
|
||||||
|
expectedMethod: http.MethodGet,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "custom header with hostname override",
|
desc: "custom header with hostname override",
|
||||||
|
@ -337,6 +341,17 @@ func TestAddHeadersAndHost(t *testing.T) {
|
||||||
},
|
},
|
||||||
expectedHostname: "myhost",
|
expectedHostname: "myhost",
|
||||||
expectedHeader: "foo",
|
expectedHeader: "foo",
|
||||||
|
expectedMethod: http.MethodGet,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "custom method",
|
||||||
|
serverURL: "http://backend1:80",
|
||||||
|
options: Options{
|
||||||
|
Path: "/",
|
||||||
|
Method: http.MethodHead,
|
||||||
|
},
|
||||||
|
expectedHostname: "backend1:80",
|
||||||
|
expectedMethod: http.MethodHead,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -353,11 +368,12 @@ func TestAddHeadersAndHost(t *testing.T) {
|
||||||
req, err := backend.newRequest(u)
|
req, err := backend.newRequest(u)
|
||||||
require.NoError(t, err, "failed to create new backend request")
|
require.NoError(t, err, "failed to create new backend request")
|
||||||
|
|
||||||
req = backend.addHeadersAndHost(req)
|
req = backend.setRequestOptions(req)
|
||||||
|
|
||||||
assert.Equal(t, "http://backend1:80/", req.URL.String())
|
assert.Equal(t, "http://backend1:80/", req.URL.String())
|
||||||
assert.Equal(t, test.expectedHostname, req.Host)
|
assert.Equal(t, test.expectedHostname, req.Host)
|
||||||
assert.Equal(t, test.expectedHeader, req.Header.Get("Custom-Header"))
|
assert.Equal(t, test.expectedHeader, req.Header.Get("Custom-Header"))
|
||||||
|
assert.Equal(t, test.expectedMethod, req.Method)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -360,6 +360,7 @@ func buildHealthCheckOptions(ctx context.Context, lb healthcheck.Balancer, backe
|
||||||
return &healthcheck.Options{
|
return &healthcheck.Options{
|
||||||
Scheme: hc.Scheme,
|
Scheme: hc.Scheme,
|
||||||
Path: hc.Path,
|
Path: hc.Path,
|
||||||
|
Method: hc.Method,
|
||||||
Port: hc.Port,
|
Port: hc.Port,
|
||||||
Interval: interval,
|
Interval: interval,
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
|
|
Loading…
Reference in a new issue