From f84d947115eaed124419227a1d07a544ee9b009e Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Mon, 20 Jan 2020 15:56:05 +0100 Subject: [PATCH] Use the calculated port when useBindPortIP is enabled --- pkg/provider/docker/config.go | 28 ++++-------- pkg/provider/docker/config_test.go | 68 ++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 23 deletions(-) diff --git a/pkg/provider/docker/config.go b/pkg/provider/docker/config.go index 61cb9928f..5ffc00798 100644 --- a/pkg/provider/docker/config.go +++ b/pkg/provider/docker/config.go @@ -150,10 +150,12 @@ func (p *Provider) addServerTCP(ctx context.Context, container dockerData, loadB return errors.New("load-balancer is not defined") } - serverPort := "" + var serverPort string if len(loadBalancer.Servers) > 0 { serverPort = loadBalancer.Servers[0].Port + loadBalancer.Servers[0].Port = "" } + ip, port, err := p.getIPPort(ctx, container, serverPort) if err != nil { return err @@ -165,11 +167,6 @@ func (p *Provider) addServerTCP(ctx context.Context, container dockerData, loadB loadBalancer.Servers = []dynamic.TCPServer{server} } - if serverPort != "" { - port = serverPort - loadBalancer.Servers[0].Port = "" - } - if port == "" { return errors.New("port is missing") } @@ -183,7 +180,12 @@ func (p *Provider) addServer(ctx context.Context, container dockerData, loadBala return errors.New("load-balancer is not defined") } - serverPort := getLBServerPort(loadBalancer) + var serverPort string + if len(loadBalancer.Servers) > 0 { + serverPort = loadBalancer.Servers[0].Port + loadBalancer.Servers[0].Port = "" + } + ip, port, err := p.getIPPort(ctx, container, serverPort) if err != nil { return err @@ -196,11 +198,6 @@ func (p *Provider) addServer(ctx context.Context, container dockerData, loadBala loadBalancer.Servers = []dynamic.Server{server} } - if serverPort != "" { - port = serverPort - loadBalancer.Servers[0].Port = "" - } - if port == "" { return errors.New("port is missing") } @@ -302,13 +299,6 @@ func (p *Provider) getPortBinding(container dockerData, serverPort string) (*nat return nil, fmt.Errorf("unable to find the external IP:Port for the container %q", container.Name) } -func getLBServerPort(loadBalancer *dynamic.ServersLoadBalancer) string { - if loadBalancer != nil && len(loadBalancer.Servers) > 0 { - return loadBalancer.Servers[0].Port - } - return "" -} - func getPort(container dockerData, serverPort string) string { if len(serverPort) > 0 { return serverPort diff --git a/pkg/provider/docker/config_test.go b/pkg/provider/docker/config_test.go index c52e8b161..e999c54c1 100644 --- a/pkg/provider/docker/config_test.go +++ b/pkg/provider/docker/config_test.go @@ -339,10 +339,11 @@ func TestDefaultRule(t *testing.T) { func Test_buildConfiguration(t *testing.T) { testCases := []struct { - desc string - containers []dockerData - constraints string - expected *dynamic.Configuration + desc string + containers []dockerData + useBindPortIP bool + constraints string + expected *dynamic.Configuration }{ { desc: "invalid HTTP service definition", @@ -2515,6 +2516,64 @@ func Test_buildConfiguration(t *testing.T) { }, }, }, + { + desc: "useBindPortIP with LblPort | ExtIp:ExtPort:LblPort => ExtIp:ExtPort", + containers: []dockerData{ + { + ServiceName: "Test", + Name: "Test", + Labels: map[string]string{ + "traefik.http.services.Test.loadbalancer.server.port": "80", + }, + NetworkSettings: networkSettings{ + Ports: nat.PortMap{ + nat.Port("79/tcp"): []nat.PortBinding{{ + HostIP: "192.168.0.1", + HostPort: "8080", + }}, + nat.Port("80/tcp"): []nat.PortBinding{{ + HostIP: "192.168.0.1", + HostPort: "8081", + }}, + }, + Networks: map[string]*networkData{ + "bridge": { + Name: "bridge", + Addr: "127.0.0.1", + }, + }, + }, + }, + }, + useBindPortIP: true, + expected: &dynamic.Configuration{ + TCP: &dynamic.TCPConfiguration{ + Routers: map[string]*dynamic.TCPRouter{}, + Services: map[string]*dynamic.TCPService{}, + }, + HTTP: &dynamic.HTTPConfiguration{ + Routers: map[string]*dynamic.Router{ + "Test": { + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + }, + }, + Middlewares: map[string]*dynamic.Middleware{}, + Services: map[string]*dynamic.Service{ + "Test": { + LoadBalancer: &dynamic.ServersLoadBalancer{ + Servers: []dynamic.Server{ + { + URL: "http://192.168.0.1:8081", + }, + }, + PassHostHeader: Bool(true), + }, + }, + }, + }, + }, + }, } for _, test := range testCases { @@ -2526,6 +2585,7 @@ func Test_buildConfiguration(t *testing.T) { p := Provider{ ExposedByDefault: true, DefaultRule: "Host(`{{ normalize .Name }}.traefik.wtf`)", + UseBindPortIP: test.useBindPortIP, } p.Constraints = test.constraints