Fix Docker filter empty rule
Signed-off-by: Emile Vauge <emile@vauge.com>
This commit is contained in:
parent
ec245d604a
commit
bf3f6e2029
3 changed files with 352 additions and 144 deletions
|
@ -523,6 +523,11 @@ func (p *Provider) containerFilter(container dockerData) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
if len(p.getFrontendRule(container)) == 0 {
|
||||
log.Debugf("Filtering container with empty frontend rule %s", container.Name)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -537,12 +542,11 @@ func (p *Provider) getFrontendRule(container dockerData) string {
|
|||
if label, err := getLabel(container, "traefik.frontend.rule"); err == nil {
|
||||
return label
|
||||
}
|
||||
if labels, err := getLabels(container, []string{"com.docker.compose.project", "com.docker.compose.service"}); err == nil {
|
||||
return "Host:" + p.getSubDomain(labels["com.docker.compose.service"]+"."+labels["com.docker.compose.project"]) + "." + p.Domain
|
||||
}
|
||||
|
||||
if len(p.Domain) > 0 {
|
||||
return "Host:" + p.getSubDomain(container.ServiceName) + "." + p.Domain
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (p *Provider) getBackend(container dockerData) string {
|
||||
if label, err := getLabel(container, "traefik.backend"); err == nil {
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/containous/traefik/types"
|
||||
docker "github.com/docker/engine-api/types"
|
||||
"github.com/docker/engine-api/types/container"
|
||||
"github.com/docker/go-connections/nat"
|
||||
)
|
||||
|
||||
|
@ -489,125 +490,298 @@ func TestDockerGetLabels(t *testing.T) {
|
|||
func TestDockerTraefikFilter(t *testing.T) {
|
||||
containers := []struct {
|
||||
container docker.ContainerJSON
|
||||
exposedByDefault bool
|
||||
expected bool
|
||||
provider *Provider
|
||||
}{
|
||||
{
|
||||
container: containerJSON(),
|
||||
exposedByDefault: true,
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{},
|
||||
NetworkSettings: &docker.NetworkSettings{},
|
||||
},
|
||||
expected: false,
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
container: containerJSON(
|
||||
labels(map[string]string{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{
|
||||
Labels: map[string]string{
|
||||
"traefik.enable": "false",
|
||||
}),
|
||||
ports(nat.PortMap{
|
||||
},
|
||||
},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
}),
|
||||
),
|
||||
exposedByDefault: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
container: containerJSON(
|
||||
labels(map[string]string{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{
|
||||
Labels: map[string]string{
|
||||
"traefik.frontend.rule": "Host:foo.bar",
|
||||
}),
|
||||
ports(nat.PortMap{
|
||||
},
|
||||
},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
}),
|
||||
),
|
||||
exposedByDefault: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
container: containerJSON(
|
||||
ports(nat.PortMap{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container-multi-ports",
|
||||
},
|
||||
Config: &container.Config{},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
"443/tcp": {},
|
||||
}),
|
||||
),
|
||||
exposedByDefault: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
container: containerJSON(
|
||||
ports(nat.PortMap{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
}),
|
||||
),
|
||||
exposedByDefault: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
container: containerJSON(
|
||||
labels(map[string]string{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{
|
||||
Labels: map[string]string{
|
||||
"traefik.port": "80",
|
||||
}),
|
||||
ports(nat.PortMap{
|
||||
},
|
||||
},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
"443/tcp": {},
|
||||
}),
|
||||
),
|
||||
exposedByDefault: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
container: containerJSON(
|
||||
labels(map[string]string{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{
|
||||
Labels: map[string]string{
|
||||
"traefik.enable": "true",
|
||||
}),
|
||||
ports(nat.PortMap{
|
||||
},
|
||||
},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
}),
|
||||
),
|
||||
exposedByDefault: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
container: containerJSON(
|
||||
labels(map[string]string{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{
|
||||
Labels: map[string]string{
|
||||
"traefik.enable": "anything",
|
||||
}),
|
||||
ports(nat.PortMap{
|
||||
},
|
||||
},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
}),
|
||||
),
|
||||
exposedByDefault: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
container: containerJSON(
|
||||
labels(map[string]string{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{
|
||||
Labels: map[string]string{
|
||||
"traefik.frontend.rule": "Host:foo.bar",
|
||||
}),
|
||||
ports(nat.PortMap{
|
||||
},
|
||||
},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
}),
|
||||
),
|
||||
exposedByDefault: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
container: containerJSON(
|
||||
ports(nat.PortMap{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
}),
|
||||
),
|
||||
exposedByDefault: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: false,
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
container: containerJSON(
|
||||
labels(map[string]string{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{
|
||||
Labels: map[string]string{
|
||||
"traefik.enable": "true",
|
||||
}),
|
||||
ports(nat.PortMap{
|
||||
},
|
||||
},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
}),
|
||||
),
|
||||
exposedByDefault: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
Domain: "test",
|
||||
ExposedByDefault: false,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{
|
||||
Labels: map[string]string{
|
||||
"traefik.enable": "true",
|
||||
},
|
||||
},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
ExposedByDefault: false,
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
container: docker.ContainerJSON{
|
||||
ContainerJSONBase: &docker.ContainerJSONBase{
|
||||
Name: "container",
|
||||
},
|
||||
Config: &container.Config{
|
||||
Labels: map[string]string{
|
||||
"traefik.enable": "true",
|
||||
"traefik.frontend.rule": "Host:i.love.this.host",
|
||||
},
|
||||
},
|
||||
NetworkSettings: &docker.NetworkSettings{
|
||||
NetworkSettingsBase: docker.NetworkSettingsBase{
|
||||
Ports: nat.PortMap{
|
||||
"80/tcp": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
provider: &Provider{
|
||||
ExposedByDefault: false,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
}
|
||||
|
@ -616,12 +790,10 @@ func TestDockerTraefikFilter(t *testing.T) {
|
|||
e := e
|
||||
t.Run(strconv.Itoa(containerID), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
provider := Provider{}
|
||||
provider.ExposedByDefault = e.exposedByDefault
|
||||
dockerData := parseContainer(e.container)
|
||||
actual := provider.containerFilter(dockerData)
|
||||
actual := e.provider.containerFilter(dockerData)
|
||||
if actual != e.expected {
|
||||
t.Errorf("expected %v for %+v (%+v, %+v), got %+v", e.expected, e.container, e.container.NetworkSettings, e.container.ContainerJSONBase, actual)
|
||||
t.Errorf("expected %v for %+v, got %+v", e.expected, e, actual)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -504,85 +504,121 @@ func TestSwarmGetLabels(t *testing.T) {
|
|||
func TestSwarmTraefikFilter(t *testing.T) {
|
||||
services := []struct {
|
||||
service swarm.Service
|
||||
exposedByDefault bool
|
||||
expected bool
|
||||
networks map[string]*docker.NetworkResource
|
||||
provider *Provider
|
||||
}{
|
||||
{
|
||||
service: swarmService(),
|
||||
exposedByDefault: true,
|
||||
expected: false,
|
||||
networks: map[string]*docker.NetworkResource{},
|
||||
provider: &Provider{
|
||||
SwarmMode: true,
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
service: swarmService(serviceLabels(map[string]string{
|
||||
"traefik.enable": "false",
|
||||
"traefik.port": "80",
|
||||
})),
|
||||
exposedByDefault: true,
|
||||
expected: false,
|
||||
networks: map[string]*docker.NetworkResource{},
|
||||
provider: &Provider{
|
||||
SwarmMode: true,
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
service: swarmService(serviceLabels(map[string]string{
|
||||
"traefik.frontend.rule": "Host:foo.bar",
|
||||
"traefik.port": "80",
|
||||
})),
|
||||
exposedByDefault: true,
|
||||
expected: true,
|
||||
networks: map[string]*docker.NetworkResource{},
|
||||
provider: &Provider{
|
||||
SwarmMode: true,
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
service: swarmService(serviceLabels(map[string]string{
|
||||
"traefik.port": "80",
|
||||
})),
|
||||
exposedByDefault: true,
|
||||
expected: true,
|
||||
networks: map[string]*docker.NetworkResource{},
|
||||
provider: &Provider{
|
||||
SwarmMode: true,
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
service: swarmService(serviceLabels(map[string]string{
|
||||
"traefik.enable": "true",
|
||||
"traefik.port": "80",
|
||||
})),
|
||||
exposedByDefault: true,
|
||||
expected: true,
|
||||
networks: map[string]*docker.NetworkResource{},
|
||||
provider: &Provider{
|
||||
SwarmMode: true,
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
service: swarmService(serviceLabels(map[string]string{
|
||||
"traefik.enable": "anything",
|
||||
"traefik.port": "80",
|
||||
})),
|
||||
exposedByDefault: true,
|
||||
expected: true,
|
||||
networks: map[string]*docker.NetworkResource{},
|
||||
provider: &Provider{
|
||||
SwarmMode: true,
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
service: swarmService(serviceLabels(map[string]string{
|
||||
"traefik.frontend.rule": "Host:foo.bar",
|
||||
"traefik.port": "80",
|
||||
})),
|
||||
exposedByDefault: true,
|
||||
expected: true,
|
||||
networks: map[string]*docker.NetworkResource{},
|
||||
provider: &Provider{
|
||||
SwarmMode: true,
|
||||
Domain: "test",
|
||||
ExposedByDefault: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
service: swarmService(serviceLabels(map[string]string{
|
||||
"traefik.port": "80",
|
||||
})),
|
||||
exposedByDefault: false,
|
||||
expected: false,
|
||||
networks: map[string]*docker.NetworkResource{},
|
||||
provider: &Provider{
|
||||
SwarmMode: true,
|
||||
Domain: "test",
|
||||
ExposedByDefault: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
service: swarmService(serviceLabels(map[string]string{
|
||||
"traefik.enable": "true",
|
||||
"traefik.port": "80",
|
||||
})),
|
||||
exposedByDefault: false,
|
||||
expected: true,
|
||||
networks: map[string]*docker.NetworkResource{},
|
||||
provider: &Provider{
|
||||
SwarmMode: true,
|
||||
Domain: "test",
|
||||
ExposedByDefault: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -591,11 +627,7 @@ func TestSwarmTraefikFilter(t *testing.T) {
|
|||
t.Run(strconv.Itoa(serviceID), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
dockerData := parseService(e.service, e.networks)
|
||||
provider := &Provider{
|
||||
SwarmMode: true,
|
||||
}
|
||||
provider.ExposedByDefault = e.exposedByDefault
|
||||
actual := provider.containerFilter(dockerData)
|
||||
actual := e.provider.containerFilter(dockerData)
|
||||
if actual != e.expected {
|
||||
t.Errorf("expected %v for %+v, got %+v", e.expected, e, actual)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue