Skip to content

Commit

Permalink
Don't ignore unhealthy containers in docker provider
Browse files Browse the repository at this point in the history
Don't ignore configuration from labels on healthy containers and instead
generate a service without servers.

This will make such case generate 503 for unhealthy services instead of
404.
  • Loading branch information
jvasseur committed Jan 10, 2022
1 parent ba822ac commit d145c40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
26 changes: 21 additions & 5 deletions pkg/provider/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func (p *Provider) buildConfiguration(ctx context.Context, containersInspected [
}

func (p *Provider) buildTCPServiceConfiguration(ctx context.Context, container dockerData, configuration *dynamic.TCPConfiguration) error {
logger := log.FromContext(ctx)

serviceName := getServiceName(container)

if len(configuration.Services) == 0 {
Expand All @@ -100,6 +102,11 @@ func (p *Provider) buildTCPServiceConfiguration(ctx context.Context, container d
}
}

if container.Health != "" && container.Health != "healthy" {
logger.Debug("Filtering unhealthy or starting container")
return nil
}

for name, service := range configuration.Services {
ctxSvc := log.With(ctx, log.Str(log.ServiceName, name))
err := p.addServerTCP(ctxSvc, container, service.LoadBalancer)
Expand All @@ -112,6 +119,8 @@ func (p *Provider) buildTCPServiceConfiguration(ctx context.Context, container d
}

func (p *Provider) buildUDPServiceConfiguration(ctx context.Context, container dockerData, configuration *dynamic.UDPConfiguration) error {
logger := log.FromContext(ctx)

serviceName := getServiceName(container)

if len(configuration.Services) == 0 {
Expand All @@ -122,6 +131,11 @@ func (p *Provider) buildUDPServiceConfiguration(ctx context.Context, container d
}
}

if container.Health != "" && container.Health != "healthy" {
logger.Debug("Filtering unhealthy or starting container")
return nil
}

for name, service := range configuration.Services {
ctxSvc := log.With(ctx, log.Str(log.ServiceName, name))
err := p.addServerUDP(ctxSvc, container, service.LoadBalancer)
Expand All @@ -134,6 +148,8 @@ func (p *Provider) buildUDPServiceConfiguration(ctx context.Context, container d
}

func (p *Provider) buildServiceConfiguration(ctx context.Context, container dockerData, configuration *dynamic.HTTPConfiguration) error {
logger := log.FromContext(ctx)

serviceName := getServiceName(container)

if len(configuration.Services) == 0 {
Expand All @@ -145,6 +161,11 @@ func (p *Provider) buildServiceConfiguration(ctx context.Context, container dock
}
}

if container.Health != "" && container.Health != "healthy" {
logger.Debug("Filtering unhealthy or starting container")
return nil
}

for name, service := range configuration.Services {
ctxSvc := log.With(ctx, log.Str(log.ServiceName, name))
err := p.addServer(ctxSvc, container, service.LoadBalancer)
Expand Down Expand Up @@ -174,11 +195,6 @@ func (p *Provider) keepContainer(ctx context.Context, container dockerData) bool
return false
}

if container.Health != "" && container.Health != "healthy" {
logger.Debug("Filtering unhealthy or starting container")
return false
}

return true
}

Expand Down
17 changes: 14 additions & 3 deletions pkg/provider/docker/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2265,9 +2265,20 @@ func Test_buildConfiguration(t *testing.T) {
Services: map[string]*dynamic.UDPService{},
},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{},
Routers: map[string]*dynamic.Router{
"Test": {
Service: "Test",
Rule: "Host(`Test.traefik.wtf`)",
},
},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{
"Test": {
LoadBalancer: &dynamic.ServersLoadBalancer{
PassHostHeader: Bool(true),
},
},
},
ServersTransports: map[string]*dynamic.ServersTransport{},
},
},
Expand Down

0 comments on commit d145c40

Please sign in to comment.