diff --git a/provider/docker/config.go b/provider/docker/config.go index f2f88d4c8..6e3421812 100644 --- a/provider/docker/config.go +++ b/provider/docker/config.go @@ -196,7 +196,6 @@ func (p *Provider) getFrontendRule(container dockerData, segmentLabels map[strin } func (p Provider) getIPAddress(container dockerData) string { - if value := label.GetStringValue(container.Labels, labelDockerNetwork, ""); value != "" { networkSettings := container.NetworkSettings if networkSettings.Networks != nil { @@ -248,6 +247,8 @@ func (p Provider) getIPAddress(container dockerData) string { for _, network := range container.NetworkSettings.Networks { return network.Addr } + + log.Warnf("Unable to find the IP address for the container %q.", container.Name) return "" } @@ -316,12 +317,17 @@ func (p *Provider) getServers(containers []dockerData) map[string]types.Server { var servers map[string]types.Server for i, container := range containers { + ip := p.getIPAddress(container) + if len(ip) == 0 { + log.Warnf("Unable to find the IP address for the container %q: the server is ignored.", container.Name) + continue + } + if servers == nil { servers = make(map[string]types.Server) } protocol := label.GetStringValue(container.SegmentLabels, label.TraefikProtocol, label.DefaultProtocol) - ip := p.getIPAddress(container) port := getPort(container) serverName := "server-" + container.SegmentName + "-" + container.Name diff --git a/provider/docker/config_container_docker_test.go b/provider/docker/config_container_docker_test.go index 90d910711..d8b4564bd 100644 --- a/provider/docker/config_container_docker_test.go +++ b/provider/docker/config_container_docker_test.go @@ -399,6 +399,7 @@ func TestDockerBuildConfiguration(t *testing.T) { test := test t.Run(test.desc, func(t *testing.T) { t.Parallel() + var dockerDataList []dockerData for _, cont := range test.containers { dData := parseContainer(cont) @@ -1019,3 +1020,122 @@ func TestDockerGetPort(t *testing.T) { }) } } + +func TestDockerGetServers(t *testing.T) { + p := &Provider{} + + testCases := []struct { + desc string + containers []docker.ContainerJSON + expected map[string]types.Server + }{ + { + desc: "no container", + expected: nil, + }, + { + desc: "with a simple container", + containers: []docker.ContainerJSON{ + containerJSON( + name("test1"), + withNetwork("testnet", ipv4("10.10.10.10")), + ports(nat.PortMap{ + "80/tcp": {}, + })), + }, + expected: map[string]types.Server{ + "server-test1": { + URL: "http://10.10.10.10:80", + Weight: 1, + }, + }, + }, + { + desc: "with several containers", + containers: []docker.ContainerJSON{ + containerJSON( + name("test1"), + withNetwork("testnet", ipv4("10.10.10.11")), + ports(nat.PortMap{ + "80/tcp": {}, + })), + containerJSON( + name("test2"), + withNetwork("testnet", ipv4("10.10.10.12")), + ports(nat.PortMap{ + "81/tcp": {}, + })), + containerJSON( + name("test3"), + withNetwork("testnet", ipv4("10.10.10.13")), + ports(nat.PortMap{ + "82/tcp": {}, + })), + }, + expected: map[string]types.Server{ + "server-test1": { + URL: "http://10.10.10.11:80", + Weight: 1, + }, + "server-test2": { + URL: "http://10.10.10.12:81", + Weight: 1, + }, + "server-test3": { + URL: "http://10.10.10.13:82", + Weight: 1, + }, + }, + }, + { + desc: "ignore one container because no ip address", + containers: []docker.ContainerJSON{ + containerJSON( + name("test1"), + withNetwork("testnet", ipv4("")), + ports(nat.PortMap{ + "80/tcp": {}, + })), + containerJSON( + name("test2"), + withNetwork("testnet", ipv4("10.10.10.12")), + ports(nat.PortMap{ + "81/tcp": {}, + })), + containerJSON( + name("test3"), + withNetwork("testnet", ipv4("10.10.10.13")), + ports(nat.PortMap{ + "82/tcp": {}, + })), + }, + expected: map[string]types.Server{ + "server-test2": { + URL: "http://10.10.10.12:81", + Weight: 1, + }, + "server-test3": { + URL: "http://10.10.10.13:82", + Weight: 1, + }, + }, + }, + } + + for _, test := range testCases { + test := test + t.Run(test.desc, func(t *testing.T) { + t.Parallel() + + var dockerDataList []dockerData + for _, cont := range test.containers { + dData := parseContainer(cont) + dockerDataList = append(dockerDataList, dData) + } + + servers := p.getServers(dockerDataList) + + assert.Equal(t, test.expected, servers) + }) + } +} diff --git a/provider/rancher/config.go b/provider/rancher/config.go index a24c4c191..6b59ca9fd 100644 --- a/provider/rancher/config.go +++ b/provider/rancher/config.go @@ -166,6 +166,11 @@ func getServers(service rancherData) map[string]types.Server { var servers map[string]types.Server for index, ip := range service.Containers { + if len(ip) == 0 { + log.Warnf("Unable to find the IP address for a container in the service %q: this container is ignored.", service.Name) + continue + } + if servers == nil { servers = make(map[string]types.Server) } diff --git a/provider/rancher/config_test.go b/provider/rancher/config_test.go index 599ae8de0..4154794ab 100644 --- a/provider/rancher/config_test.go +++ b/provider/rancher/config_test.go @@ -832,6 +832,18 @@ func TestGetServers(t *testing.T) { }, expected: nil, }, + { + desc: "should return nil when no server IPs", + service: rancherData{ + Labels: map[string]string{ + label.TraefikWeight: "7", + }, + Containers: []string{""}, + Health: "healthy", + State: "active", + }, + expected: nil, + }, { desc: "should use default weight when invalid weight value", service: rancherData{