Merge pull request #438 from samber/TRAEFIK-311--support-docker-backend
feat(constraints): Supports constraints for docker backend
This commit is contained in:
commit
29e647763a
3 changed files with 24 additions and 4 deletions
|
@ -203,6 +203,7 @@ Træfɪk filters services according to service attributes/tags set in your confi
|
|||
|
||||
Supported backends:
|
||||
|
||||
- Docker
|
||||
- Consul Catalog
|
||||
|
||||
Supported filters:
|
||||
|
@ -574,6 +575,14 @@ watch = true
|
|||
# cert = "/etc/ssl/docker.crt"
|
||||
# key = "/etc/ssl/docker.key"
|
||||
# insecureskipverify = true
|
||||
|
||||
# Constraint on Docker tags
|
||||
#
|
||||
# Optional
|
||||
#
|
||||
# constraints = ["tag==api", "tag==he*ld"]
|
||||
# Matching with containers having the label "traefik.tags" set to "api,helloworld"
|
||||
# ex: $ docker run -d -P --label traefik.tags=api,helloworld emilevauge/whoami
|
||||
```
|
||||
|
||||
Labels can be used on containers to override default behaviour:
|
||||
|
|
|
@ -177,11 +177,12 @@ func (provider *Docker) loadDockerConfig(containersInspected []dockertypes.Conta
|
|||
}
|
||||
|
||||
// filter containers
|
||||
filteredContainers := fun.Filter(containerFilter, containersInspected).([]dockertypes.ContainerJSON)
|
||||
filteredContainers := fun.Filter(provider.ContainerFilter, containersInspected).([]dockertypes.ContainerJSON)
|
||||
|
||||
frontends := map[string][]dockertypes.ContainerJSON{}
|
||||
for _, container := range filteredContainers {
|
||||
frontends[provider.getFrontendName(container)] = append(frontends[provider.getFrontendName(container)], container)
|
||||
frontendName := provider.getFrontendName(container)
|
||||
frontends[frontendName] = append(frontends[frontendName], container)
|
||||
}
|
||||
|
||||
templateObjects := struct {
|
||||
|
@ -201,7 +202,8 @@ func (provider *Docker) loadDockerConfig(containersInspected []dockertypes.Conta
|
|||
return configuration
|
||||
}
|
||||
|
||||
func containerFilter(container dockertypes.ContainerJSON) bool {
|
||||
// ContainerFilter checks if container have to be exposed
|
||||
func (provider *Docker) ContainerFilter(container dockertypes.ContainerJSON) bool {
|
||||
_, err := strconv.Atoi(container.Config.Labels["traefik.port"])
|
||||
if len(container.NetworkSettings.Ports) == 0 && err != nil {
|
||||
log.Debugf("Filtering container without port and no traefik.port label %s", container.Name)
|
||||
|
@ -217,6 +219,14 @@ func containerFilter(container dockertypes.ContainerJSON) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
constraintTags := strings.Split(container.Config.Labels["traefik.tags"], ",")
|
||||
if ok, failingConstraint := provider.MatchConstraints(constraintTags); !ok {
|
||||
if failingConstraint != nil {
|
||||
log.Debugf("Container %v pruned by '%v' constraint", container.Name, failingConstraint.String())
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -621,6 +621,7 @@ func TestDockerGetLabels(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestDockerTraefikFilter(t *testing.T) {
|
||||
provider := Docker{}
|
||||
containers := []struct {
|
||||
container docker.ContainerJSON
|
||||
expected bool
|
||||
|
@ -792,7 +793,7 @@ func TestDockerTraefikFilter(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, e := range containers {
|
||||
actual := containerFilter(e.container)
|
||||
actual := provider.ContainerFilter(e.container)
|
||||
if actual != e.expected {
|
||||
t.Fatalf("expected %v for %+v, got %+v", e.expected, e, actual)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue