From 920e82f11a7d048b9480956ffd023206436fe584 Mon Sep 17 00:00:00 2001 From: Alessandro Chitolina Date: Mon, 9 Nov 2020 17:12:05 +0100 Subject: [PATCH] fix: translate configured server port into correct mapped host port --- pkg/provider/ecs/config.go | 7 ++ pkg/provider/ecs/config_test.go | 135 ++++++++++++++++++++++++++++++-- 2 files changed, 134 insertions(+), 8 deletions(-) diff --git a/pkg/provider/ecs/config.go b/pkg/provider/ecs/config.go index 225669408..02684a07f 100644 --- a/pkg/provider/ecs/config.go +++ b/pkg/provider/ecs/config.go @@ -307,6 +307,13 @@ func (p Provider) getIPAddress(instance ecsInstance) string { func getPort(instance ecsInstance, serverPort string) string { if len(serverPort) > 0 { + for _, port := range instance.machine.ports { + containerPort := strconv.FormatInt(port.containerPort, 10) + if serverPort == containerPort { + return strconv.FormatInt(port.hostPort, 10) + } + } + return serverPort } diff --git a/pkg/provider/ecs/config_test.go b/pkg/provider/ecs/config_test.go index 56aab1869..ad2506694 100644 --- a/pkg/provider/ecs/config_test.go +++ b/pkg/provider/ecs/config_test.go @@ -1721,13 +1721,13 @@ func Test_buildConfiguration(t *testing.T) { name("Test"), labels(map[string]string{ "traefik.http.services.Service1.LoadBalancer.server.scheme": "h2c", - "traefik.http.services.Service1.LoadBalancer.server.port": "8080", + "traefik.http.services.Service1.LoadBalancer.server.port": "80", }), iMachine( mState(ec2.InstanceStateNameRunning), mPrivateIP("127.0.0.1"), mPorts( - mPort(0, 80, "tcp"), + mPort(80, 8080, "tcp"), ), ), ), @@ -1764,6 +1764,125 @@ func Test_buildConfiguration(t *testing.T) { }, }, }, + { + desc: "one container with label port not exposed by container", + containers: []ecsInstance{ + instance( + name("Test"), + labels(map[string]string{ + "traefik.http.services.Service1.LoadBalancer.server.scheme": "h2c", + "traefik.http.services.Service1.LoadBalancer.server.port": "8040", + }), + iMachine( + mState(ec2.InstanceStateNameRunning), + mPrivateIP("127.0.0.1"), + mPorts( + mPort(80, 8080, "tcp"), + ), + ), + ), + }, + expected: &dynamic.Configuration{ + TCP: &dynamic.TCPConfiguration{ + Routers: map[string]*dynamic.TCPRouter{}, + Services: map[string]*dynamic.TCPService{}, + }, + UDP: &dynamic.UDPConfiguration{ + Routers: map[string]*dynamic.UDPRouter{}, + Services: map[string]*dynamic.UDPService{}, + }, + HTTP: &dynamic.HTTPConfiguration{ + Routers: map[string]*dynamic.Router{ + "Test": { + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + }, + }, + Middlewares: map[string]*dynamic.Middleware{}, + Services: map[string]*dynamic.Service{ + "Service1": { + LoadBalancer: &dynamic.ServersLoadBalancer{ + Servers: []dynamic.Server{ + { + URL: "h2c://127.0.0.1:8040", + }, + }, + PassHostHeader: Bool(true), + }, + }, + }, + }, + }, + }, + { + desc: "one container with label and multiple ports", + containers: []ecsInstance{ + instance( + name("Test"), + labels(map[string]string{ + "traefik.http.routers.Test.rule": "Host(`Test.traefik.wtf`)", + "traefik.http.routers.Test.service": "Service1", + "traefik.http.services.Service1.LoadBalancer.server.port": "4445", + "traefik.http.routers.Test2.rule": "Host(`Test.traefik.local`)", + "traefik.http.routers.Test2.service": "Service2", + "traefik.http.services.Service2.LoadBalancer.server.port": "4444", + }), + iMachine( + mState(ec2.InstanceStateNameRunning), + mPrivateIP("127.0.0.1"), + mPorts( + mPort(4444, 32123, "tcp"), + mPort(4445, 32124, "tcp"), + ), + ), + ), + }, + expected: &dynamic.Configuration{ + TCP: &dynamic.TCPConfiguration{ + Routers: map[string]*dynamic.TCPRouter{}, + Services: map[string]*dynamic.TCPService{}, + }, + UDP: &dynamic.UDPConfiguration{ + Routers: map[string]*dynamic.UDPRouter{}, + Services: map[string]*dynamic.UDPService{}, + }, + HTTP: &dynamic.HTTPConfiguration{ + Routers: map[string]*dynamic.Router{ + "Test": { + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + }, + "Test2": { + Service: "Service2", + Rule: "Host(`Test.traefik.local`)", + }, + }, + Middlewares: map[string]*dynamic.Middleware{}, + Services: map[string]*dynamic.Service{ + "Service1": { + LoadBalancer: &dynamic.ServersLoadBalancer{ + Servers: []dynamic.Server{ + { + URL: "http://127.0.0.1:32124", + }, + }, + PassHostHeader: Bool(true), + }, + }, + "Service2": { + LoadBalancer: &dynamic.ServersLoadBalancer{ + Servers: []dynamic.Server{ + { + URL: "http://127.0.0.1:32123", + }, + }, + PassHostHeader: Bool(true), + }, + }, + }, + }, + }, + }, { desc: "one container with label port on two services", containers: []ecsInstance{ @@ -2274,13 +2393,13 @@ func Test_buildConfiguration(t *testing.T) { labels(map[string]string{ "traefik.tcp.routers.foo.rule": "HostSNI(`foo.bar`)", "traefik.tcp.routers.foo.tls.options": "foo", - "traefik.tcp.services.foo.loadbalancer.server.port": "8080", + "traefik.tcp.services.foo.loadbalancer.server.port": "80", }), iMachine( mState(ec2.InstanceStateNameRunning), mPrivateIP("127.0.0.1"), mPorts( - mPort(0, 80, "tcp"), + mPort(80, 8080, "tcp"), ), ), ), @@ -2327,13 +2446,13 @@ func Test_buildConfiguration(t *testing.T) { name("Test"), labels(map[string]string{ "traefik.udp.routers.foo.entrypoints": "mydns", - "traefik.udp.services.foo.loadbalancer.server.port": "8080", + "traefik.udp.services.foo.loadbalancer.server.port": "80", }), iMachine( mState(ec2.InstanceStateNameRunning), mPrivateIP("127.0.0.1"), mPorts( - mPort(0, 80, "udp"), + mPort(80, 8080, "udp"), ), ), ), @@ -2506,14 +2625,14 @@ func Test_buildConfiguration(t *testing.T) { instance( name("Test"), labels(map[string]string{ - "traefik.tcp.services.foo.loadbalancer.server.port": "8080", + "traefik.tcp.services.foo.loadbalancer.server.port": "80", "traefik.tcp.services.foo.loadbalancer.terminationdelay": "200", }), iMachine( mState(ec2.InstanceStateNameRunning), mPrivateIP("127.0.0.1"), mPorts( - mPort(0, 80, "tcp"), + mPort(80, 8080, "tcp"), ), ), ),