fix(docker): Network filter.

This commit is contained in:
Fernandez Ludovic 2017-10-23 10:33:02 +02:00 committed by Traefiker
parent 81f7aa9df2
commit e3cac7d0e5

View file

@ -25,6 +25,7 @@ import (
eventtypes "github.com/docker/docker/api/types/events" eventtypes "github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
swarmtypes "github.com/docker/docker/api/types/swarm" swarmtypes "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
"github.com/docker/go-connections/sockets" "github.com/docker/go-connections/sockets"
@ -560,10 +561,10 @@ func (p *Provider) getFrontendRule(container dockerData) string {
return label return label
} }
if labels, err := getLabels(container, []string{labelDockerComposeProject, labelDockerComposeService}); err == nil { if labels, err := getLabels(container, []string{labelDockerComposeProject, labelDockerComposeService}); err == nil {
return "Host:" + p.getSubDomain(labels[labelDockerComposeService]+"."+labels[labelDockerComposeProject]) + "." + p.Domain return "Host:" + getSubDomain(labels[labelDockerComposeService]+"."+labels[labelDockerComposeProject]) + "." + p.Domain
} }
if len(p.Domain) > 0 { if len(p.Domain) > 0 {
return "Host:" + p.getSubDomain(container.ServiceName) + "." + p.Domain return "Host:" + getSubDomain(container.ServiceName) + "." + p.Domain
} }
return "" return ""
} }
@ -818,7 +819,7 @@ func parseContainer(container dockertypes.ContainerJSON) dockerData {
} }
// Escape beginning slash "/", convert all others to dash "-", and convert underscores "_" to dash "-" // Escape beginning slash "/", convert all others to dash "-", and convert underscores "_" to dash "-"
func (p *Provider) getSubDomain(name string) string { func getSubDomain(name string) string {
return strings.Replace(strings.Replace(strings.TrimPrefix(name, "/"), "/", "-", -1), "_", "-", -1) return strings.Replace(strings.Replace(strings.TrimPrefix(name, "/"), "/", "-", -1), "_", "-", -1)
} }
@ -827,8 +828,16 @@ func (p *Provider) listServices(ctx context.Context, dockerClient client.APIClie
if err != nil { if err != nil {
return []dockerData{}, err return []dockerData{}, err
} }
serverVersion, err := dockerClient.ServerVersion(ctx)
networkListArgs := filters.NewArgs() networkListArgs := filters.NewArgs()
networkListArgs.Add("scope", "swarm") // https://docs.docker.com/engine/api/v1.29/#tag/Network (Docker 17.06)
if versions.GreaterThanOrEqualTo(serverVersion.APIVersion, "1.29") {
networkListArgs.Add("scope", "swarm")
} else {
networkListArgs.Add("driver", "overlay")
}
networkList, err := dockerClient.NetworkList(ctx, dockertypes.NetworkListOptions{Filters: networkListArgs}) networkList, err := dockerClient.NetworkList(ctx, dockertypes.NetworkListOptions{Filters: networkListArgs})