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

@ -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",
},
},
},