Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the calculated port when useBindPortIP is enabled #6199

Merged
merged 2 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 9 additions & 19 deletions pkg/provider/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
Expand All @@ -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
Expand All @@ -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")
}
Expand Down Expand Up @@ -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
Expand Down
68 changes: 64 additions & 4 deletions pkg/provider/docker/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand All @@ -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

Expand Down