From d145c407d6516602a857bed53c953fd0801b6c9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vasseur?= Date: Mon, 10 Jan 2022 18:15:57 +0100 Subject: [PATCH] Don't ignore unhealthy containers in docker provider 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. --- pkg/provider/docker/config.go | 26 +++++++++++++++++++++----- pkg/provider/docker/config_test.go | 17 ++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/pkg/provider/docker/config.go b/pkg/provider/docker/config.go index fe1e6c655e..a2d77c85b2 100644 --- a/pkg/provider/docker/config.go +++ b/pkg/provider/docker/config.go @@ -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 { @@ -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) @@ -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 { @@ -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) @@ -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 { @@ -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) @@ -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 } diff --git a/pkg/provider/docker/config_test.go b/pkg/provider/docker/config_test.go index c02e316839..49cc01e209 100644 --- a/pkg/provider/docker/config_test.go +++ b/pkg/provider/docker/config_test.go @@ -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{}, }, },