From 6619a787a32d00711eda2de7ee7e86db69e3a25a Mon Sep 17 00:00:00 2001 From: NicoMen Date: Mon, 20 Nov 2017 15:16:03 +0100 Subject: [PATCH] Fix problems about duplicated and missing Docker backends/frontends. --- provider/docker/docker.go | 14 ++++++++++---- provider/docker/service_test.go | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/provider/docker/docker.go b/provider/docker/docker.go index cc5598c37..a8b65ea99 100644 --- a/provider/docker/docker.go +++ b/provider/docker/docker.go @@ -302,9 +302,15 @@ func (p *Provider) loadDockerConfig(containersInspected []dockerData) *types.Con frontends := map[string][]dockerData{} backends := map[string]dockerData{} servers := map[string][]dockerData{} + serviceNames := make(map[string]struct{}) for idx, container := range filteredContainers { - frontendName := p.getFrontendName(container, idx) - frontends[frontendName] = append(frontends[frontendName], container) + if _, exists := serviceNames[container.ServiceName]; !exists { + frontendName := p.getFrontendName(container, idx) + frontends[frontendName] = append(frontends[frontendName], container) + if len(container.ServiceName) > 0 { + serviceNames[container.ServiceName] = struct{}{} + } + } backendName := p.getBackend(container) backends[backendName] = container servers[backendName] = append(servers[backendName], container) @@ -427,9 +433,9 @@ func (p *Provider) getServicePriority(container dockerData, serviceName string) // Extract backend from labels for a given service and a given docker container func (p *Provider) getServiceBackend(container dockerData, serviceName string) string { if value, ok := getContainerServiceLabel(container, serviceName, "frontend.backend"); ok { - return value + return container.ServiceName + "-" + value } - return p.getBackend(container) + "-" + provider.Normalize(serviceName) + return strings.TrimPrefix(container.ServiceName, "/") + "-" + p.getBackend(container) + "-" + provider.Normalize(serviceName) } // Extract rule from labels for a given service and a given docker container diff --git a/provider/docker/service_test.go b/provider/docker/service_test.go index 6198921f3..08febf216 100644 --- a/provider/docker/service_test.go +++ b/provider/docker/service_test.go @@ -171,19 +171,19 @@ func TestDockerGetServiceBackend(t *testing.T) { }{ { container: containerJSON(name("foo")), - expected: "foo-myservice", + expected: "foo-foo-myservice", }, { container: containerJSON(labels(map[string]string{ types.LabelBackend: "another-backend", })), - expected: "another-backend-myservice", + expected: "fake-another-backend-myservice", }, { container: containerJSON(labels(map[string]string{ "traefik.myservice.frontend.backend": "custom-backend", })), - expected: "custom-backend", + expected: "fake-custom-backend", }, } @@ -341,8 +341,8 @@ func TestDockerLoadDockerServiceConfig(t *testing.T) { ), }, expectedFrontends: map[string]*types.Frontend{ - "frontend-foo-service": { - Backend: "backend-foo-service", + "frontend-foo-foo-service": { + Backend: "backend-foo-foo-service", PassHostHeader: true, EntryPoints: []string{"http", "https"}, BasicAuth: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"}, @@ -354,7 +354,7 @@ func TestDockerLoadDockerServiceConfig(t *testing.T) { }, }, expectedBackends: map[string]*types.Backend{ - "backend-foo-service": { + "backend-foo-foo-service": { Servers: map[string]types.Server{ "service-0": { URL: "http://127.0.0.1:2503", @@ -399,8 +399,8 @@ func TestDockerLoadDockerServiceConfig(t *testing.T) { ), }, expectedFrontends: map[string]*types.Frontend{ - "frontend-foobar": { - Backend: "backend-foobar", + "frontend-test1-foobar": { + Backend: "backend-test1-foobar", PassHostHeader: false, Priority: 5000, EntryPoints: []string{"http", "https", "ws"}, @@ -411,8 +411,8 @@ func TestDockerLoadDockerServiceConfig(t *testing.T) { }, }, }, - "frontend-test2-anotherservice": { - Backend: "backend-test2-anotherservice", + "frontend-test2-test2-anotherservice": { + Backend: "backend-test2-test2-anotherservice", PassHostHeader: true, EntryPoints: []string{}, BasicAuth: []string{}, @@ -424,7 +424,7 @@ func TestDockerLoadDockerServiceConfig(t *testing.T) { }, }, expectedBackends: map[string]*types.Backend{ - "backend-foobar": { + "backend-test1-foobar": { Servers: map[string]types.Server{ "service-0": { URL: "https://127.0.0.1:2503", @@ -433,7 +433,7 @@ func TestDockerLoadDockerServiceConfig(t *testing.T) { }, CircuitBreaker: nil, }, - "backend-test2-anotherservice": { + "backend-test2-test2-anotherservice": { Servers: map[string]types.Server{ "service-0": { URL: "http://127.0.0.1:8079",