1
0
Fork 0

rework loadbalancer support

This commit is contained in:
Julien Salleyron 2019-06-05 22:18:06 +02:00 committed by Traefiker Bot
parent b143101f82
commit 518a37e776
86 changed files with 339 additions and 1055 deletions

View file

@ -131,7 +131,6 @@ func (p *Provider) buildTCPServiceConfiguration(ctx context.Context, app maratho
if len(conf.Services) == 0 {
conf.Services = make(map[string]*config.TCPService)
lb := &config.TCPLoadBalancerService{}
lb.SetDefaults()
conf.Services[appName] = &config.TCPService{
LoadBalancer: lb,
}
@ -141,7 +140,6 @@ func (p *Provider) buildTCPServiceConfiguration(ctx context.Context, app maratho
var servers []config.TCPServer
defaultServer := config.TCPServer{}
defaultServer.SetDefaults()
if len(service.LoadBalancer.Servers) > 0 {
defaultServer = service.LoadBalancer.Servers[0]
@ -212,7 +210,6 @@ func (p *Provider) getTCPServer(app marathon.Application, task marathon.Task, ex
server := config.TCPServer{
Address: net.JoinHostPort(host, port),
Weight: 1,
}
return server, nil
@ -230,8 +227,7 @@ func (p *Provider) getServer(app marathon.Application, task marathon.Task, extra
}
server := config.Server{
URL: fmt.Sprintf("%s://%s", defaultServer.Scheme, net.JoinHostPort(host, port)),
Weight: 1,
URL: fmt.Sprintf("%s://%s", defaultServer.Scheme, net.JoinHostPort(host, port)),
}
return server, nil

View file

@ -61,11 +61,9 @@ func TestBuildConfiguration(t *testing.T) {
"app": {LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
}},
},
@ -117,11 +115,9 @@ func TestBuildConfiguration(t *testing.T) {
"app": {LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
}},
},
@ -165,11 +161,9 @@ func TestBuildConfiguration(t *testing.T) {
"app": {LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
}},
},
@ -211,15 +205,12 @@ func TestBuildConfiguration(t *testing.T) {
"Service1": {LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:8080",
Weight: 1,
URL: "http://localhost:8080",
},
{
URL: "http://localhost:8081",
Weight: 1,
URL: "http://localhost:8081",
},
},
Method: "wrr",
PassHostHeader: true,
}},
},
@ -263,23 +254,18 @@ func TestBuildConfiguration(t *testing.T) {
"Service1": {LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:8080",
Weight: 1,
URL: "http://localhost:8080",
},
{
URL: "http://localhost:8081",
Weight: 1,
URL: "http://localhost:8081",
},
{
URL: "http://localhost:8082",
Weight: 1,
URL: "http://localhost:8082",
},
{
URL: "http://localhost:8083",
Weight: 1,
URL: "http://localhost:8083",
},
},
Method: "wrr",
PassHostHeader: true,
}},
},
@ -319,21 +305,17 @@ func TestBuildConfiguration(t *testing.T) {
"foo": {LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:8080",
Weight: 1,
URL: "http://localhost:8080",
},
},
Method: "wrr",
PassHostHeader: true,
}},
"bar": {LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:8081",
Weight: 1,
URL: "http://localhost:8081",
},
},
Method: "wrr",
PassHostHeader: true,
}},
},
@ -366,15 +348,12 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
{
URL: "http://localhost:81",
Weight: 1,
URL: "http://localhost:81",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -389,7 +368,7 @@ func TestBuildConfiguration(t *testing.T) {
appID("/app"),
appPorts(80),
withTasks(localhostTask(taskPorts(80))),
withLabel("traefik.http.services.Service1.loadbalancer.method", "drr"),
withLabel("traefik.http.services.Service1.loadbalancer.passhostheader", "true"),
)),
expected: &config.Configuration{
TCP: &config.TCPConfiguration{
@ -408,11 +387,9 @@ func TestBuildConfiguration(t *testing.T) {
"Service1": {LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "drr",
PassHostHeader: true,
}},
},
@ -426,7 +403,7 @@ func TestBuildConfiguration(t *testing.T) {
appID("/app"),
appPorts(80, 81),
withTasks(localhostTask(taskPorts(80, 81))),
withLabel("traefik.http.services.Service1.loadbalancer.method", "wrr"),
withLabel("traefik.http.services.Service1.loadbalancer.passhostheader", "true"),
withLabel("traefik.http.routers.Router1.rule", "Host(`foo.com`)"),
withLabel("traefik.http.routers.Router1.service", "Service1"),
)),
@ -448,11 +425,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -481,11 +456,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -507,7 +480,7 @@ func TestBuildConfiguration(t *testing.T) {
appPorts(80, 81),
withTasks(localhostTask(taskPorts(80, 81))),
withLabel("traefik.http.routers.Router1.rule", "Host(`foo.com`)"),
withLabel("traefik.http.services.Service1.loadbalancer.method", "wrr"),
withLabel("traefik.http.services.Service1.loadbalancer.passhostheader", "true"),
)),
expected: &config.Configuration{
TCP: &config.TCPConfiguration{
@ -527,11 +500,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -547,8 +518,8 @@ func TestBuildConfiguration(t *testing.T) {
appPorts(80, 81),
withTasks(localhostTask(taskPorts(80, 81))),
withLabel("traefik.http.routers.Router1.rule", "Host(`foo.com`)"),
withLabel("traefik.http.services.Service1.loadbalancer.method", "wrr"),
withLabel("traefik.http.services.Service2.loadbalancer.method", "wrr"),
withLabel("traefik.http.services.Service1.loadbalancer.passhostheader", "true"),
withLabel("traefik.http.services.Service2.loadbalancer.passhostheader", "true"),
)),
expected: &config.Configuration{
TCP: &config.TCPConfiguration{
@ -563,11 +534,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -575,11 +544,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -588,19 +555,19 @@ func TestBuildConfiguration(t *testing.T) {
},
},
{
desc: "two apps with same service name and different LB methods",
desc: "two apps with same service name and different passhostheader",
applications: withApplications(
application(
appID("/app"),
appPorts(80, 81),
withTasks(localhostTask(taskPorts(80, 81))),
withLabel("traefik.http.services.Service1.loadbalancer.method", "wrr"),
withLabel("traefik.http.services.Service1.loadbalancer.passhostheader", "false"),
),
application(
appID("/app2"),
appPorts(80, 81),
withTasks(localhostTask(taskPorts(80, 81))),
withLabel("traefik.http.services.Service1.loadbalancer.method", "drr"),
withLabel("traefik.http.services.Service1.loadbalancer.passhostheader", "true"),
)),
expected: &config.Configuration{
TCP: &config.TCPConfiguration{
@ -667,11 +634,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -679,11 +644,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -728,11 +691,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -740,11 +701,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -780,11 +739,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -792,11 +749,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -812,14 +767,14 @@ func TestBuildConfiguration(t *testing.T) {
appPorts(80, 81),
withTasks(localhostTask(taskPorts(80, 81))),
withLabel("traefik.http.routers.Router1.rule", "Host(`foo.com`)"),
withLabel("traefik.http.services.Service1.LoadBalancer.method", "wrr"),
withLabel("traefik.http.services.Service1.LoadBalancer.passhostheader", "true"),
),
application(
appID("/app2"),
appPorts(80, 81),
withTasks(localhostTask(taskPorts(80, 81))),
withLabel("traefik.http.routers.Router1.rule", "Host(`foo.com`)"),
withLabel("traefik.http.services.Service1.LoadBalancer.method", "wrr"),
withLabel("traefik.http.services.Service1.LoadBalancer.passhostheader", "true"),
)),
expected: &config.Configuration{
TCP: &config.TCPConfiguration{
@ -839,15 +794,12 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -883,11 +835,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -895,11 +845,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -934,11 +882,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -974,11 +920,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "h2c://localhost:90",
Weight: 1,
URL: "h2c://localhost:90",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -1009,11 +953,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -1021,11 +963,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:8080",
Weight: 1,
URL: "http://localhost:8080",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -1208,11 +1148,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -1255,11 +1193,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -1294,11 +1230,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "wrr",
PassHostHeader: true,
},
},
@ -1331,10 +1265,8 @@ func TestBuildConfiguration(t *testing.T) {
Servers: []config.TCPServer{
{
Address: "localhost:80",
Weight: 1,
},
},
Method: "wrr",
},
},
},
@ -1364,10 +1296,8 @@ func TestBuildConfiguration(t *testing.T) {
Servers: []config.TCPServer{
{
Address: "localhost:80",
Weight: 1,
},
},
Method: "wrr",
},
},
},
@ -1405,10 +1335,8 @@ func TestBuildConfiguration(t *testing.T) {
Servers: []config.TCPServer{
{
Address: "localhost:8080",
Weight: 1,
},
},
Method: "wrr",
},
},
},
@ -1430,7 +1358,7 @@ func TestBuildConfiguration(t *testing.T) {
withLabel("traefik.tcp.routers.foo.rule", "HostSNI(`foo.bar`)"),
withLabel("traefik.tcp.routers.foo.tls", "true"),
withLabel("traefik.tcp.services.foo.loadbalancer.server.port", "8080"),
withLabel("traefik.http.services.bar.loadbalancer.method", "drr"),
withLabel("traefik.http.services.bar.loadbalancer.passhostheader", "true"),
)),
expected: &config.Configuration{
TCP: &config.TCPConfiguration{
@ -1447,10 +1375,8 @@ func TestBuildConfiguration(t *testing.T) {
Servers: []config.TCPServer{
{
Address: "localhost:8080",
Weight: 1,
},
},
Method: "wrr",
},
},
},
@ -1468,11 +1394,9 @@ func TestBuildConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
Method: "drr",
PassHostHeader: true,
},
},
@ -1591,12 +1515,10 @@ func TestGetServer(t *testing.T) {
extraConf: configuration{},
defaultServer: config.Server{
Scheme: "http",
Weight: 1,
},
expected: expected{
server: config.Server{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
},
@ -1611,7 +1533,6 @@ func TestGetServer(t *testing.T) {
extraConf: configuration{},
defaultServer: config.Server{
Scheme: "http",
Weight: 1,
},
expected: expected{
error: "unable to process ports for /app taskID: no port found",
@ -1629,12 +1550,10 @@ func TestGetServer(t *testing.T) {
defaultServer: config.Server{
Scheme: "http",
Port: "88",
Weight: 1,
},
expected: expected{
server: config.Server{
URL: "http://localhost:88",
Weight: 1,
URL: "http://localhost:88",
},
},
},
@ -1650,7 +1569,6 @@ func TestGetServer(t *testing.T) {
defaultServer: config.Server{
Scheme: "http",
Port: "aaaa",
Weight: 1,
},
expected: expected{
error: `unable to process ports for /app taskID: strconv.Atoi: parsing "aaaa": invalid syntax`,
@ -1668,7 +1586,6 @@ func TestGetServer(t *testing.T) {
defaultServer: config.Server{
Scheme: "http",
Port: "-6",
Weight: 1,
},
expected: expected{
error: `unable to process ports for /app taskID: explicitly specified port -6 must be greater than zero`,
@ -1686,12 +1603,10 @@ func TestGetServer(t *testing.T) {
defaultServer: config.Server{
Scheme: "http",
Port: "index:1",
Weight: 1,
},
expected: expected{
server: config.Server{
URL: "http://localhost:81",
Weight: 1,
URL: "http://localhost:81",
},
},
},
@ -1707,7 +1622,6 @@ func TestGetServer(t *testing.T) {
defaultServer: config.Server{
Scheme: "http",
Port: "index:2",
Weight: 1,
},
expected: expected{
error: "unable to process ports for /app taskID: index 2 must be within range (0, 1)",
@ -1725,7 +1639,6 @@ func TestGetServer(t *testing.T) {
defaultServer: config.Server{
Scheme: "http",
Port: "index:aaa",
Weight: 1,
},
expected: expected{
error: `unable to process ports for /app taskID: strconv.Atoi: parsing "aaa": invalid syntax`,
@ -1743,12 +1656,10 @@ func TestGetServer(t *testing.T) {
extraConf: configuration{},
defaultServer: config.Server{
Scheme: "http",
Weight: 1,
},
expected: expected{
server: config.Server{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
},
@ -1764,12 +1675,10 @@ func TestGetServer(t *testing.T) {
extraConf: configuration{},
defaultServer: config.Server{
Scheme: "http",
Weight: 1,
},
expected: expected{
server: config.Server{
URL: "http://127.0.0.1:88",
Weight: 1,
URL: "http://127.0.0.1:88",
},
},
},
@ -1785,12 +1694,10 @@ func TestGetServer(t *testing.T) {
extraConf: configuration{},
defaultServer: config.Server{
Scheme: "http",
Weight: 1,
},
expected: expected{
server: config.Server{
URL: "http://127.0.0.1:80",
Weight: 1,
URL: "http://127.0.0.1:80",
},
},
},
@ -1806,12 +1713,10 @@ func TestGetServer(t *testing.T) {
extraConf: configuration{},
defaultServer: config.Server{
Scheme: "http",
Weight: 1,
},
expected: expected{
server: config.Server{
URL: "http://localhost:80",
Weight: 1,
URL: "http://localhost:80",
},
},
},
@ -1837,12 +1742,10 @@ func TestGetServer(t *testing.T) {
},
defaultServer: config.Server{
Scheme: "http",
Weight: 1,
},
expected: expected{
server: config.Server{
URL: "http://127.0.0.1:88",
Weight: 1,
URL: "http://127.0.0.1:88",
},
},
},
@ -1867,7 +1770,6 @@ func TestGetServer(t *testing.T) {
},
defaultServer: config.Server{
Scheme: "http",
Weight: 1,
},
expected: expected{
error: "found 2 task IP addresses but missing IP address index for Marathon application /app on task taskID",
@ -1894,7 +1796,6 @@ func TestGetServer(t *testing.T) {
},
defaultServer: config.Server{
Scheme: "http",
Weight: 1,
},
expected: expected{
error: "cannot use IP address index to select from 2 task IP addresses for Marathon application /app on task taskID",
@ -1916,7 +1817,6 @@ func TestGetServer(t *testing.T) {
extraConf: configuration{},
defaultServer: config.Server{
Scheme: "http",
Weight: 1,
},
expected: expected{
error: "missing IP address for Marathon application /app on task taskID",