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

@ -50,11 +50,9 @@ func TestRouterManager_Get(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
},
@ -89,11 +87,9 @@ func TestRouterManager_Get(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
},
@ -114,11 +110,9 @@ func TestRouterManager_Get(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
},
@ -140,11 +134,9 @@ func TestRouterManager_Get(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
},
@ -183,11 +175,9 @@ func TestRouterManager_Get(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
},
@ -225,11 +215,9 @@ func TestRouterManager_Get(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
},
@ -250,11 +238,9 @@ func TestRouterManager_Get(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
},
@ -276,11 +262,9 @@ func TestRouterManager_Get(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
},
@ -373,11 +357,9 @@ func TestAccessLog(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
},
@ -403,11 +385,9 @@ func TestAccessLog(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
},
@ -470,15 +450,12 @@ func TestRuntimeConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://127.0.0.1:8085",
Weight: 1,
URL: "http://127.0.0.1:8085",
},
{
URL: "http://127.0.0.1:8086",
Weight: 1,
URL: "http://127.0.0.1:8086",
},
},
Method: "wrr",
HealthCheck: &config.HealthCheck{
Interval: "500ms",
Path: "/health",
@ -507,11 +484,9 @@ func TestRuntimeConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://127.0.0.1",
Weight: 1,
URL: "http://127.0.0.1",
},
},
Method: "wrr",
},
},
},
@ -536,11 +511,9 @@ func TestRuntimeConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://127.0.0.1",
Weight: 1,
URL: "http://127.0.0.1",
},
},
Method: "wrr",
},
},
},
@ -565,11 +538,9 @@ func TestRuntimeConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://127.0.0.1",
Weight: 1,
URL: "http://127.0.0.1",
},
},
Method: "wrr",
},
},
},
@ -610,11 +581,9 @@ func TestRuntimeConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://127.0.0.1",
Weight: 1,
URL: "http://127.0.0.1",
},
},
Method: "wrr",
},
},
},
@ -652,11 +621,9 @@ func TestRuntimeConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://127.0.0.1",
Weight: 1,
URL: "http://127.0.0.1",
},
},
Method: "wrr",
},
},
},
@ -685,11 +652,9 @@ func TestRuntimeConfiguration(t *testing.T) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://127.0.0.1",
Weight: 1,
URL: "http://127.0.0.1",
},
},
Method: "wrr",
},
},
},
@ -786,11 +751,9 @@ func BenchmarkRouterServe(b *testing.B) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server.URL,
Weight: 1,
URL: server.URL,
},
},
Method: "wrr",
},
},
}
@ -832,11 +795,9 @@ func BenchmarkService(b *testing.B) {
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "tchouck",
Weight: 1,
URL: "tchouck",
},
},
Method: "wrr",
},
},
}

View file

@ -32,7 +32,6 @@ func TestRuntimeConfiguration(t *testing.T) {
Port: "8086",
},
},
Method: "wrr",
},
},
},
@ -67,7 +66,6 @@ func TestRuntimeConfiguration(t *testing.T) {
Address: "127.0.0.1:80",
},
},
Method: "wrr",
},
},
},
@ -100,10 +98,8 @@ func TestRuntimeConfiguration(t *testing.T) {
Servers: []config.TCPServer{
{
Address: "127.0.0.1:80",
Weight: 1,
},
},
Method: "wrr",
},
},
},
@ -135,10 +131,8 @@ func TestRuntimeConfiguration(t *testing.T) {
Servers: []config.TCPServer{
{
Address: "127.0.0.1:80",
Weight: 1,
},
},
Method: "wrr",
},
},
},

View file

@ -40,7 +40,6 @@ func TestReuseService(t *testing.T) {
th.WithBasicAuth(&config.BasicAuth{Users: []string{"foo:bar"}}),
)),
th.WithLoadBalancerServices(th.WithService("bar",
th.WithLBMethod("wrr"),
th.WithServers(th.WithServer(testServer.URL))),
),
)

View file

@ -161,7 +161,6 @@ func TestServerResponseEmptyBackend(t *testing.T) {
th.WithRule(routeRule)),
),
th.WithLoadBalancerServices(th.WithService("bar",
th.WithLBMethod("wrr"),
th.WithServers(th.WithServer(testServerURL))),
),
)
@ -176,7 +175,21 @@ func TestServerResponseEmptyBackend(t *testing.T) {
expectedStatusCode: http.StatusNotFound,
},
{
desc: "Empty Backend LB-Drr",
desc: "Empty Backend LB",
config: func(testServerURL string) *config.HTTPConfiguration {
return th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo",
th.WithEntryPoints("http"),
th.WithServiceName("bar"),
th.WithRule(routeRule)),
),
th.WithLoadBalancerServices(th.WithService("bar")),
)
},
expectedStatusCode: http.StatusServiceUnavailable,
},
{
desc: "Empty Backend LB Sticky",
config: func(testServerURL string) *config.HTTPConfiguration {
return th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo",
@ -185,14 +198,14 @@ func TestServerResponseEmptyBackend(t *testing.T) {
th.WithRule(routeRule)),
),
th.WithLoadBalancerServices(th.WithService("bar",
th.WithLBMethod("drr")),
th.WithStickiness("test")),
),
)
},
expectedStatusCode: http.StatusServiceUnavailable,
},
{
desc: "Empty Backend LB-Drr Sticky",
desc: "Empty Backend LB",
config: func(testServerURL string) *config.HTTPConfiguration {
return th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo",
@ -200,15 +213,13 @@ func TestServerResponseEmptyBackend(t *testing.T) {
th.WithServiceName("bar"),
th.WithRule(routeRule)),
),
th.WithLoadBalancerServices(th.WithService("bar",
th.WithLBMethod("drr"), th.WithStickiness("test")),
),
th.WithLoadBalancerServices(th.WithService("bar")),
)
},
expectedStatusCode: http.StatusServiceUnavailable,
},
{
desc: "Empty Backend LB-Wrr",
desc: "Empty Backend LB Sticky",
config: func(testServerURL string) *config.HTTPConfiguration {
return th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo",
@ -217,23 +228,7 @@ func TestServerResponseEmptyBackend(t *testing.T) {
th.WithRule(routeRule)),
),
th.WithLoadBalancerServices(th.WithService("bar",
th.WithLBMethod("wrr")),
),
)
},
expectedStatusCode: http.StatusServiceUnavailable,
},
{
desc: "Empty Backend LB-Wrr Sticky",
config: func(testServerURL string) *config.HTTPConfiguration {
return th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo",
th.WithEntryPoints("http"),
th.WithServiceName("bar"),
th.WithRule(routeRule)),
),
th.WithLoadBalancerServices(th.WithService("bar",
th.WithLBMethod("wrr"), th.WithStickiness("test")),
th.WithStickiness("test")),
),
)
},

View file

@ -185,58 +185,20 @@ func buildHealthCheckOptions(ctx context.Context, lb healthcheck.BalancerHandler
func (m *Manager) getLoadBalancer(ctx context.Context, serviceName string, service *config.LoadBalancerService, fwd http.Handler) (healthcheck.BalancerHandler, error) {
logger := log.FromContext(ctx)
logger.Debug("Creating load-balancer")
var options []roundrobin.LBOption
var stickySession *roundrobin.StickySession
var cookieName string
if stickiness := service.Stickiness; stickiness != nil {
cookieName = cookie.GetName(stickiness.CookieName, serviceName)
stickySession = roundrobin.NewStickySession(cookieName)
options = append(options, roundrobin.EnableStickySession(roundrobin.NewStickySession(cookieName)))
logger.Debugf("Sticky session cookie name: %v", cookieName)
}
var lb healthcheck.BalancerHandler
if service.Method == "drr" {
logger.Debug("Creating drr load-balancer")
rr, err := roundrobin.New(fwd)
if err != nil {
return nil, err
}
if stickySession != nil {
logger.Debugf("Sticky session cookie name: %v", cookieName)
lb, err = roundrobin.NewRebalancer(rr, roundrobin.RebalancerStickySession(stickySession))
if err != nil {
return nil, err
}
} else {
lb, err = roundrobin.NewRebalancer(rr)
if err != nil {
return nil, err
}
}
} else {
if service.Method != "wrr" {
logger.Warnf("Invalid load-balancing method %q, fallback to 'wrr' method", service.Method)
}
logger.Debug("Creating wrr load-balancer")
if stickySession != nil {
logger.Debugf("Sticky session cookie name: %v", cookieName)
var err error
lb, err = roundrobin.New(fwd, roundrobin.EnableStickySession(stickySession))
if err != nil {
return nil, err
}
} else {
var err error
lb, err = roundrobin.New(fwd)
if err != nil {
return nil, err
}
}
lb, err := roundrobin.New(fwd, options...)
if err != nil {
return nil, err
}
lbsu := healthcheck.NewLBStatusUpdater(lb, m.configs[serviceName])
@ -256,9 +218,9 @@ func (m *Manager) upsertServers(ctx context.Context, lb healthcheck.BalancerHand
return fmt.Errorf("error parsing server URL %s: %v", srv.URL, err)
}
logger.WithField(log.ServerName, name).Debugf("Creating server %d at %s with weight %d", name, u, srv.Weight)
logger.WithField(log.ServerName, name).Debugf("Creating server %d %s", name, u)
if err := lb.UpsertServer(u, roundrobin.Weight(srv.Weight)); err != nil {
if err := lb.UpsertServer(u, roundrobin.Weight(1)); err != nil {
return fmt.Errorf("error adding server %s to load balancer: %v", srv.URL, err)
}

View file

@ -35,8 +35,7 @@ func TestGetLoadBalancer(t *testing.T) {
service: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: ":",
Weight: 0,
URL: ":",
},
},
},
@ -122,15 +121,12 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
service: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: server1.URL,
Weight: 50,
URL: server1.URL,
},
{
URL: server2.URL,
Weight: 50,
URL: server2.URL,
},
},
Method: "wrr",
},
expected: []ExpectedResult{
{
@ -149,11 +145,9 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
service: &config.LoadBalancerService{
Servers: []config.Server{
{
URL: "http://foo",
Weight: 1,
URL: "http://foo",
},
},
Method: "wrr",
},
expected: []ExpectedResult{
{
@ -166,7 +160,6 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
serviceName: "test",
service: &config.LoadBalancerService{
Servers: []config.Server{},
Method: "wrr",
},
expected: []ExpectedResult{
{
@ -181,15 +174,12 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
Stickiness: &config.Stickiness{},
Servers: []config.Server{
{
URL: server1.URL,
Weight: 1,
URL: server1.URL,
},
{
URL: server2.URL,
Weight: 1,
URL: server2.URL,
},
},
Method: "wrr",
},
expected: []ExpectedResult{
{
@ -210,11 +200,9 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
PassHostHeader: true,
Servers: []config.Server{
{
URL: serverPassHost.URL,
Weight: 1,
URL: serverPassHost.URL,
},
},
Method: "wrr",
},
expected: []ExpectedResult{
{
@ -230,11 +218,9 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
Stickiness: &config.Stickiness{},
Servers: []config.Server{
{
URL: serverPassHostFalse.URL,
Weight: 1,
URL: serverPassHostFalse.URL,
},
},
Method: "wrr",
},
expected: []ExpectedResult{
{
@ -284,7 +270,7 @@ func TestManager_Build(t *testing.T) {
configs: map[string]*config.ServiceInfo{
"serviceName": {
Service: &config.Service{
LoadBalancer: &config.LoadBalancerService{Method: "wrr"},
LoadBalancer: &config.LoadBalancerService{},
},
},
},
@ -295,7 +281,7 @@ func TestManager_Build(t *testing.T) {
configs: map[string]*config.ServiceInfo{
"provider-1.serviceName": {
Service: &config.Service{
LoadBalancer: &config.LoadBalancerService{Method: "wrr"},
LoadBalancer: &config.LoadBalancerService{},
},
},
},
@ -306,7 +292,7 @@ func TestManager_Build(t *testing.T) {
configs: map[string]*config.ServiceInfo{
"provider-1.serviceName": {
Service: &config.Service{
LoadBalancer: &config.LoadBalancerService{Method: "wrr"},
LoadBalancer: &config.LoadBalancerService{},
},
},
},

View file

@ -70,7 +70,7 @@ func TestManager_BuildTCP(t *testing.T) {
configs: map[string]*config.TCPServiceInfo{
"serviceName": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{Method: "wrr"},
LoadBalancer: &config.TCPLoadBalancerService{},
},
},
},
@ -81,7 +81,7 @@ func TestManager_BuildTCP(t *testing.T) {
configs: map[string]*config.TCPServiceInfo{
"provider-1.serviceName": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{Method: "wrr"},
LoadBalancer: &config.TCPLoadBalancerService{},
},
},
},
@ -92,7 +92,7 @@ func TestManager_BuildTCP(t *testing.T) {
configs: map[string]*config.TCPServiceInfo{
"provider-1.serviceName": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{Method: "wrr"},
LoadBalancer: &config.TCPLoadBalancerService{},
},
},
},
@ -110,7 +110,6 @@ func TestManager_BuildTCP(t *testing.T) {
Address: "foobar.com:80",
},
},
Method: "wrr",
},
},
},
@ -129,7 +128,6 @@ func TestManager_BuildTCP(t *testing.T) {
Address: "192.168.0.12:80",
},
},
Method: "wrr",
},
},
},
@ -148,7 +146,6 @@ func TestManager_BuildTCP(t *testing.T) {
Address: "foobar.com",
},
},
Method: "wrr",
},
},
},
@ -167,7 +164,6 @@ func TestManager_BuildTCP(t *testing.T) {
Address: "192.168.0.12",
},
},
Method: "wrr",
},
},
},