diff --git a/docker.go b/docker.go index 913f08f..a237bd1 100644 --- a/docker.go +++ b/docker.go @@ -65,10 +65,10 @@ func findContainerByServiceName(dc client.APIClient, svcType string, svcName str return types.ContainerJSON{}, errors.Wrapf(err, "failed to inspect container %s", c.ID) } // check labels - svcNeedle := fmt.Sprintf("traefik.%s.services.%s", svcType, svcName) - routerNeedle := fmt.Sprintf("traefik.%s.routers.%s", svcType, routerName) + svcNeedle := fmt.Sprintf("traefik.%s.services.%s.", svcType, svcName) + routerNeedle := fmt.Sprintf("traefik.%s.routers.%s.", svcType, routerName) for k := range container.Config.Labels { - if strings.Contains(k, svcNeedle) || (routerName != "" && strings.Contains(k, routerNeedle)) { + if strings.HasPrefix(k, svcNeedle) || (routerName != "" && strings.HasPrefix(k, routerNeedle)) { logrus.Debugf("found container '%s' (%s) for service '%s'", container.Name, container.ID, svcName) return container, nil } diff --git a/docker_test.go b/docker_test.go index bb89349..0c1c8e4 100644 --- a/docker_test.go +++ b/docker_test.go @@ -124,3 +124,14 @@ func Test_helloWorldNoCert(t *testing.T) { // assert.Fail(t, "TODO: check for no cert") } + +func Test_samePrefix(t *testing.T) { + store := doTest(t, "prefix.yml") + + // Two services `hello` and `hello-test`. + // The former's name is a prefix of the latter. Ensure the matching does not mix them up. + assertServiceIPs(t, store, []svc{ + {"hello", "http", "http://192.168.100.100:5555"}, + {"hello-test", "http", "http://192.168.100.100:5566"}, + }) +} diff --git a/fixtures/prefix.yml b/fixtures/prefix.yml new file mode 100644 index 0000000..5bf3731 --- /dev/null +++ b/fixtures/prefix.yml @@ -0,0 +1,29 @@ + +services: + hello: + image: helloworld + restart: unless-stopped + ports: + - 5555:5555 + labels: + - "traefik.enable=true" + - "traefik.http.routers.hello.rule=Host(`hello.local`)" + - "traefik.http.routers.hello.service=hello" + - "traefik.http.routers.hello.tls=true" + - "traefik.http.routers.hello.tls.certresolver=default" + - "traefik.http.services.hello.loadbalancer.server.scheme=http" + - "traefik.http.services.hello.loadbalancer.server.port=5555" + + hello-test: + image: helloworld + restart: unless-stopped + ports: + - 5566:5566 + labels: + - "traefik.enable=true" + - "traefik.http.routers.hello-test.rule=Host(`hello-test.local`)" + - "traefik.http.routers.hello-test.service=hello-test" + - "traefik.http.routers.hello-test.tls=true" + - "traefik.http.routers.hello-test.tls.certresolver=default" + - "traefik.http.services.hello-test.loadbalancer.server.scheme=http" + - "traefik.http.services.hello-test.loadbalancer.server.port=5566"