1
0
Fork 0

Add a status option to the service health check

This commit is contained in:
Ali Afsharzadeh 2022-11-24 14:10:05 +03:30 committed by GitHub
parent 61325d7b91
commit 46c266661c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 97 additions and 2 deletions

View file

@ -237,6 +237,7 @@ type ServerHealthCheck struct {
Mode string `json:"mode,omitempty" toml:"mode,omitempty" yaml:"mode,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"`
Status int `json:"status,omitempty" toml:"status,omitempty" yaml:"status,omitempty" export:"true"`
Port int `json:"port,omitempty" toml:"port,omitempty,omitzero" yaml:"port,omitempty" export:"true"`
Interval ptypes.Duration `json:"interval,omitempty" toml:"interval,omitempty" yaml:"interval,omitempty" export:"true"`
Timeout ptypes.Duration `json:"timeout,omitempty" toml:"timeout,omitempty" yaml:"timeout,omitempty" export:"true"`

View file

@ -151,6 +151,7 @@ func TestDecodeConfiguration(t *testing.T) {
"traefik.http.services.Service0.loadbalancer.healthcheck.interval": "1s",
"traefik.http.services.Service0.loadbalancer.healthcheck.path": "foobar",
"traefik.http.services.Service0.loadbalancer.healthcheck.method": "foobar",
"traefik.http.services.Service0.loadbalancer.healthcheck.status": "401",
"traefik.http.services.Service0.loadbalancer.healthcheck.port": "42",
"traefik.http.services.Service0.loadbalancer.healthcheck.scheme": "foobar",
"traefik.http.services.Service0.loadbalancer.healthcheck.mode": "foobar",
@ -168,6 +169,7 @@ func TestDecodeConfiguration(t *testing.T) {
"traefik.http.services.Service1.loadbalancer.healthcheck.interval": "1s",
"traefik.http.services.Service1.loadbalancer.healthcheck.path": "foobar",
"traefik.http.services.Service1.loadbalancer.healthcheck.method": "foobar",
"traefik.http.services.Service1.loadbalancer.healthcheck.status": "401",
"traefik.http.services.Service1.loadbalancer.healthcheck.port": "42",
"traefik.http.services.Service1.loadbalancer.healthcheck.scheme": "foobar",
"traefik.http.services.Service1.loadbalancer.healthcheck.mode": "foobar",
@ -655,6 +657,7 @@ func TestDecodeConfiguration(t *testing.T) {
Mode: "foobar",
Path: "foobar",
Method: "foobar",
Status: 401,
Port: 42,
Interval: ptypes.Duration(time.Second),
Timeout: ptypes.Duration(time.Second),
@ -684,6 +687,7 @@ func TestDecodeConfiguration(t *testing.T) {
Mode: "foobar",
Path: "foobar",
Method: "foobar",
Status: 401,
Port: 42,
Interval: ptypes.Duration(time.Second),
Timeout: ptypes.Duration(time.Second),
@ -1147,6 +1151,7 @@ func TestEncodeConfiguration(t *testing.T) {
Scheme: "foobar",
Path: "foobar",
Method: "foobar",
Status: 401,
Port: 42,
Interval: ptypes.Duration(time.Second),
Timeout: ptypes.Duration(time.Second),
@ -1174,6 +1179,7 @@ func TestEncodeConfiguration(t *testing.T) {
Scheme: "foobar",
Path: "foobar",
Method: "foobar",
Status: 401,
Port: 42,
Interval: ptypes.Duration(time.Second),
Timeout: ptypes.Duration(time.Second),
@ -1335,6 +1341,7 @@ func TestEncodeConfiguration(t *testing.T) {
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Interval": "1000000000",
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Path": "foobar",
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Method": "foobar",
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Status": "401",
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Port": "42",
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Scheme": "foobar",
"traefik.HTTP.Services.Service0.LoadBalancer.HealthCheck.Timeout": "1000000000",
@ -1351,6 +1358,7 @@ func TestEncodeConfiguration(t *testing.T) {
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Interval": "1000000000",
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Path": "foobar",
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Method": "foobar",
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Status": "401",
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Port": "42",
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Scheme": "foobar",
"traefik.HTTP.Services.Service1.LoadBalancer.HealthCheck.Timeout": "1000000000",

View file

@ -173,10 +173,14 @@ func (shc *ServiceHealthChecker) checkHealthHTTP(ctx context.Context, target *ur
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
if shc.config.Status == 0 && (resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest) {
return fmt.Errorf("received error status code: %v", resp.StatusCode)
}
if shc.config.Status != 0 && shc.config.Status != resp.StatusCode {
return fmt.Errorf("received error status code: %v expected status code: %v", resp.StatusCode, shc.config.Status)
}
return nil
}

View file

@ -249,6 +249,7 @@ func TestServiceHealthChecker_Launch(t *testing.T) {
testCases := []struct {
desc string
mode string
status int
server StartTestServer
expNumRemovedServers int
expNumUpsertedServers int
@ -263,6 +264,15 @@ func TestServiceHealthChecker_Launch(t *testing.T) {
expGaugeValue: 1,
targetStatus: runtime.StatusUp,
},
{
desc: "healthy server staying healthy, with custom code status check",
server: newHTTPServer(http.StatusNotFound),
status: http.StatusNotFound,
expNumRemovedServers: 0,
expNumUpsertedServers: 1,
expGaugeValue: 1,
targetStatus: runtime.StatusUp,
},
{
desc: "healthy server staying healthy (StatusNoContent)",
server: newHTTPServer(http.StatusNoContent),
@ -287,6 +297,15 @@ func TestServiceHealthChecker_Launch(t *testing.T) {
expGaugeValue: 0,
targetStatus: runtime.StatusDown,
},
{
desc: "healthy server becoming sick, with custom code status check",
server: newHTTPServer(http.StatusOK),
status: http.StatusServiceUnavailable,
expNumRemovedServers: 1,
expNumUpsertedServers: 0,
expGaugeValue: 0,
targetStatus: runtime.StatusDown,
},
{
desc: "healthy server toggling to sick and back to healthy",
server: newHTTPServer(http.StatusServiceUnavailable, http.StatusOK),
@ -348,6 +367,7 @@ func TestServiceHealthChecker_Launch(t *testing.T) {
config := &dynamic.ServerHealthCheck{
Mode: test.mode,
Status: test.status,
Path: "/path",
Interval: ptypes.Duration(500 * time.Millisecond),
Timeout: ptypes.Duration(499 * time.Millisecond),