From 08503655d9da2a2d56b0134dbd0c5952800f3929 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Mon, 16 Oct 2017 17:38:03 +0200 Subject: [PATCH] Backward compatibility for sticky --- docs/basics.md | 16 ++++---- provider/consul/consul_catalog.go | 15 ++++--- provider/consul/consul_catalog_test.go | 32 ++------------- provider/docker/docker.go | 25 ++++++------ provider/docker/docker_test.go | 40 +------------------ provider/ecs/ecs.go | 21 ++++++---- provider/kubernetes/kubernetes.go | 10 +++-- provider/kubernetes/kubernetes_test.go | 6 +-- provider/kv/kv.go | 18 +++++---- provider/kv/kv_test.go | 42 -------------------- provider/marathon/marathon.go | 21 +++++----- provider/marathon/marathon_test.go | 54 ++++++++++++++------------ provider/rancher/rancher.go | 26 +++++++------ provider/rancher/rancher_test.go | 46 +--------------------- server/server.go | 3 +- templates/consul_catalog.tmpl | 4 +- templates/docker.tmpl | 3 +- templates/ecs.tmpl | 3 +- templates/kubernetes.tmpl | 5 ++- templates/kv.tmpl | 4 +- templates/marathon.tmpl | 3 +- templates/rancher.tmpl | 3 +- 22 files changed, 140 insertions(+), 260 deletions(-) diff --git a/docs/basics.md b/docs/basics.md index 79ecf9398..f117e554c 100644 --- a/docs/basics.md +++ b/docs/basics.md @@ -354,21 +354,19 @@ The default cookie name is an abbreviation of a sha1 (ex: `_1d52e`). On subsequent requests, the client will be directed to the backend stored in the cookie if it is still healthy. If not, a new backend will be assigned. -To activate sticky session: ```toml [backends] [backends.backend1] + # Enable sticky session [backends.backend1.loadbalancer.stickiness] -``` -To customize the cookie name: - -```toml -[backends] - [backends.backend1] - [backends.backend1.loadbalancer.stickiness] - cookieName = "my_cookie" + # Customize the cookie name + # + # Optional + # Default: a sha1 (6 chars) + # + # cookieName = "my_cookie" ``` The deprecated way: diff --git a/provider/consul/consul_catalog.go b/provider/consul/consul_catalog.go index 868bcd6b4..db9c7bbc2 100644 --- a/provider/consul/consul_catalog.go +++ b/provider/consul/consul_catalog.go @@ -397,17 +397,19 @@ func (p *CatalogProvider) getBasicAuth(tags []string) []string { return []string{} } -func (p *CatalogProvider) hasStickinessLabel(tags []string) bool { - stickinessTag := p.getTag(types.LabelBackendLoadbalancerStickiness, tags, "") - +func (p *CatalogProvider) getSticky(tags []string) string { stickyTag := p.getTag(types.LabelBackendLoadbalancerSticky, tags, "") if len(stickyTag) > 0 { log.Warnf("Deprecated configuration found: %s. Please use %s.", types.LabelBackendLoadbalancerSticky, types.LabelBackendLoadbalancerStickiness) + } else { + stickyTag = "false" } + return stickyTag +} - stickiness := len(stickinessTag) > 0 && strings.EqualFold(strings.TrimSpace(stickinessTag), "true") - sticky := len(stickyTag) > 0 && strings.EqualFold(strings.TrimSpace(stickyTag), "true") - return stickiness || sticky +func (p *CatalogProvider) hasStickinessLabel(tags []string) bool { + stickinessTag := p.getTag(types.LabelBackendLoadbalancerStickiness, tags, "") + return len(stickinessTag) > 0 && strings.EqualFold(strings.TrimSpace(stickinessTag), "true") } func (p *CatalogProvider) getStickinessCookieName(tags []string) string { @@ -465,6 +467,7 @@ func (p *CatalogProvider) buildConfig(catalog []catalogUpdate) *types.Configurat "getBackendName": p.getBackendName, "getBackendAddress": p.getBackendAddress, "getBasicAuth": p.getBasicAuth, + "getSticky": p.getSticky, "hasStickinessLabel": p.hasStickinessLabel, "getStickinessCookieName": p.getStickinessCookieName, "getAttribute": p.getAttribute, diff --git a/provider/consul/consul_catalog_test.go b/provider/consul/consul_catalog_test.go index 7b8ce4a7b..8ffdd3e0d 100644 --- a/provider/consul/consul_catalog_test.go +++ b/provider/consul/consul_catalog_test.go @@ -904,43 +904,19 @@ func TestConsulCatalogHasStickinessLabel(t *testing.T) { tags: []string{}, expected: false, }, - { - desc: "sticky=true", - tags: []string{ - "traefik.backend.loadbalancer.sticky=true", - }, - expected: true, - }, { desc: "stickiness=true", tags: []string{ - "traefik.backend.loadbalancer.stickiness=true", + types.LabelBackendLoadbalancerStickiness + "=true", }, expected: true, }, { - desc: "sticky=true and stickiness=true", + desc: "stickiness=false", tags: []string{ - "traefik.backend.loadbalancer.sticky=true", - "traefik.backend.loadbalancer.stickiness=true", + types.LabelBackendLoadbalancerStickiness + "=false", }, - expected: true, - }, - { - desc: "sticky=false and stickiness=true", - tags: []string{ - "traefik.backend.loadbalancer.sticky=true", - "traefik.backend.loadbalancer.stickiness=false", - }, - expected: true, - }, - { - desc: "sticky=true and stickiness=false", - tags: []string{ - "traefik.backend.loadbalancer.sticky=true", - "traefik.backend.loadbalancer.stickiness=false", - }, - expected: true, + expected: false, }, } diff --git a/provider/docker/docker.go b/provider/docker/docker.go index b1570e81c..2b7f80454 100644 --- a/provider/docker/docker.go +++ b/provider/docker/docker.go @@ -275,6 +275,7 @@ func (p *Provider) loadDockerConfig(containersInspected []dockerData) *types.Con "hasMaxConnLabels": p.hasMaxConnLabels, "getMaxConnAmount": p.getMaxConnAmount, "getMaxConnExtractorFunc": p.getMaxConnExtractorFunc, + "getSticky": p.getSticky, "getStickinessCookieName": p.getStickinessCookieName, "hasStickinessLabel": p.hasStickinessLabel, "getIsBackendLBSwarm": p.getIsBackendLBSwarm, @@ -465,10 +466,10 @@ func (p *Provider) getServiceProtocol(container dockerData, serviceName string) func (p *Provider) hasLoadBalancerLabel(container dockerData) bool { _, errMethod := getLabel(container, types.LabelBackendLoadbalancerMethod) _, errSticky := getLabel(container, types.LabelBackendLoadbalancerSticky) - if errMethod != nil && errSticky != nil { - return false - } - return true + _, errStickiness := getLabel(container, types.LabelBackendLoadbalancerStickiness) + _, errCookieName := getLabel(container, types.LabelBackendLoadbalancerStickinessCookieName) + + return errMethod == nil || errSticky == nil || errStickiness == nil || errCookieName == nil } func (p *Provider) hasMaxConnLabels(container dockerData) bool { @@ -646,15 +647,17 @@ func (p *Provider) getWeight(container dockerData) string { func (p *Provider) hasStickinessLabel(container dockerData) bool { labelStickiness, errStickiness := getLabel(container, types.LabelBackendLoadbalancerStickiness) + return errStickiness == nil && len(labelStickiness) > 0 && strings.EqualFold(strings.TrimSpace(labelStickiness), "true") +} - labelSticky, errSticky := getLabel(container, types.LabelBackendLoadbalancerSticky) - if len(labelSticky) > 0 { - log.Warnf("Deprecated configuration found: %s. Please use %s.", types.LabelBackendLoadbalancerSticky, types.LabelBackendLoadbalancerStickiness) +func (p *Provider) getSticky(container dockerData) string { + if label, err := getLabel(container, types.LabelBackendLoadbalancerSticky); err == nil { + if len(label) > 0 { + log.Warnf("Deprecated configuration found: %s. Please use %s.", types.LabelBackendLoadbalancerSticky, types.LabelBackendLoadbalancerStickiness) + } + return label } - - stickiness := errStickiness == nil && len(labelStickiness) > 0 && strings.EqualFold(strings.TrimSpace(labelStickiness), "true") - sticky := errSticky == nil && len(labelSticky) > 0 && strings.EqualFold(strings.TrimSpace(labelSticky), "true") - return stickiness || sticky + return "false" } func (p *Provider) getStickinessCookieName(container dockerData) string { diff --git a/provider/docker/docker_test.go b/provider/docker/docker_test.go index 0701dc2e2..718890e3e 100644 --- a/provider/docker/docker_test.go +++ b/provider/docker/docker_test.go @@ -1060,24 +1060,10 @@ func TestDockerHasStickinessLabel(t *testing.T) { expected bool }{ { - desc: "no sticky/stickiness-label", + desc: "no stickiness-label", container: containerJSON(), expected: false, }, - { - desc: "sticky true", - container: containerJSON(labels(map[string]string{ - types.LabelBackendLoadbalancerSticky: "true", - })), - expected: true, - }, - { - desc: "sticky false", - container: containerJSON(labels(map[string]string{ - types.LabelBackendLoadbalancerSticky: "false", - })), - expected: false, - }, { desc: "stickiness true", container: containerJSON(labels(map[string]string{ @@ -1092,30 +1078,6 @@ func TestDockerHasStickinessLabel(t *testing.T) { })), expected: false, }, - { - desc: "sticky true + stickiness false", - container: containerJSON(labels(map[string]string{ - types.LabelBackendLoadbalancerSticky: "true", - types.LabelBackendLoadbalancerStickiness: "false", - })), - expected: true, - }, - { - desc: "sticky false + stickiness true", - container: containerJSON(labels(map[string]string{ - types.LabelBackendLoadbalancerSticky: "false", - types.LabelBackendLoadbalancerStickiness: "true", - })), - expected: true, - }, - { - desc: "sticky false + stickiness false", - container: containerJSON(labels(map[string]string{ - types.LabelBackendLoadbalancerSticky: "false", - types.LabelBackendLoadbalancerStickiness: "false", - })), - expected: false, - }, } for _, test := range testCases { diff --git a/provider/ecs/ecs.go b/provider/ecs/ecs.go index d76e3f939..9b5eab4b7 100644 --- a/provider/ecs/ecs.go +++ b/provider/ecs/ecs.go @@ -184,6 +184,7 @@ func (p *Provider) loadECSConfig(ctx context.Context, client *awsClient) (*types "getFrontendRule": p.getFrontendRule, "getBasicAuth": p.getBasicAuth, "getLoadBalancerMethod": p.getLoadBalancerMethod, + "getLoadBalancerSticky": p.getLoadBalancerSticky, "hasStickinessLabel": p.hasStickinessLabel, "getStickinessCookieName": p.getStickinessCookieName, } @@ -485,16 +486,20 @@ func getFirstInstanceLabel(instances []ecsInstance, labelName string) string { return "" } +func (p *Provider) getLoadBalancerSticky(instances []ecsInstance) string { + if len(instances) > 0 { + label := getFirstInstanceLabel(instances, types.LabelBackendLoadbalancerSticky) + if label != "" { + log.Warnf("Deprecated configuration found: %s. Please use %s.", types.LabelBackendLoadbalancerSticky, types.LabelBackendLoadbalancerStickiness) + return label + } + } + return "false" +} + func (p *Provider) hasStickinessLabel(instances []ecsInstance) bool { stickinessLabel := getFirstInstanceLabel(instances, types.LabelBackendLoadbalancerStickiness) - - stickyLabel := getFirstInstanceLabel(instances, types.LabelBackendLoadbalancerSticky) - if len(stickyLabel) > 0 { - log.Warnf("Deprecated configuration found: %s. Please use %s.", types.LabelBackendLoadbalancerSticky, types.LabelBackendLoadbalancerStickiness) - } - stickiness := len(stickinessLabel) > 0 && strings.EqualFold(strings.TrimSpace(stickinessLabel), "true") - sticky := len(stickyLabel) > 0 && strings.EqualFold(strings.TrimSpace(stickyLabel), "true") - return stickiness || sticky + return len(stickinessLabel) > 0 && strings.EqualFold(strings.TrimSpace(stickinessLabel), "true") } func (p *Provider) getStickinessCookieName(instances []ecsInstance) string { diff --git a/provider/kubernetes/kubernetes.go b/provider/kubernetes/kubernetes.go index 5d38b5af7..05b9382c1 100644 --- a/provider/kubernetes/kubernetes.go +++ b/provider/kubernetes/kubernetes.go @@ -248,13 +248,15 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error) templateObjects.Backends[r.Host+pa.Path].LoadBalancer.Method = "drr" } - if len(service.Annotations[types.LabelBackendLoadbalancerSticky]) > 0 { + if sticky := service.Annotations[types.LabelBackendLoadbalancerSticky]; len(sticky) > 0 { log.Warnf("Deprecated configuration found: %s. Please use %s.", types.LabelBackendLoadbalancerSticky, types.LabelBackendLoadbalancerStickiness) + templateObjects.Backends[r.Host+pa.Path].LoadBalancer.Sticky = strings.EqualFold(strings.TrimSpace(sticky), "true") } - if service.Annotations[types.LabelBackendLoadbalancerSticky] == "true" || service.Annotations[types.LabelBackendLoadbalancerStickiness] == "true" { - templateObjects.Backends[r.Host+pa.Path].LoadBalancer.Stickiness = &types.Stickiness{ - CookieName: r.Host + pa.Path, + if service.Annotations[types.LabelBackendLoadbalancerStickiness] == "true" { + templateObjects.Backends[r.Host+pa.Path].LoadBalancer.Stickiness = &types.Stickiness{} + if cookieName := service.Annotations[types.LabelBackendLoadbalancerStickinessCookieName]; len(cookieName) > 0 { + templateObjects.Backends[r.Host+pa.Path].LoadBalancer.Stickiness.CookieName = cookieName } } diff --git a/provider/kubernetes/kubernetes_test.go b/provider/kubernetes/kubernetes_test.go index 5e0a66bf9..cdd5fe9bc 100644 --- a/provider/kubernetes/kubernetes_test.go +++ b/provider/kubernetes/kubernetes_test.go @@ -1325,9 +1325,7 @@ func TestServiceAnnotations(t *testing.T) { CircuitBreaker: nil, LoadBalancer: &types.LoadBalancer{ Method: "wrr", - Stickiness: &types.Stickiness{ - CookieName: "bar", - }, + Sticky: true, }, }, }, @@ -1356,7 +1354,7 @@ func TestServiceAnnotations(t *testing.T) { }, } - assert.Equal(t, expected, actual) + assert.EqualValues(t, expected, actual) } func TestIngressAnnotations(t *testing.T) { diff --git a/provider/kv/kv.go b/provider/kv/kv.go index c75d2f231..1ebb4298f 100644 --- a/provider/kv/kv.go +++ b/provider/kv/kv.go @@ -144,6 +144,7 @@ func (p *Provider) loadConfig() *types.Configuration { "Get": p.get, "SplitGet": p.splitGet, "Last": p.last, + "getSticky": p.getSticky, "hasStickinessLabel": p.hasStickinessLabel, "getStickinessCookieName": p.getStickinessCookieName, } @@ -242,18 +243,19 @@ func (p *Provider) checkConstraints(keys ...string) bool { return true } -func (p *Provider) hasStickinessLabel(rootPath string) bool { - stickyValue := p.get("false", rootPath, "/loadbalancer", "/sticky") - - sticky := len(stickyValue) != 0 && strings.EqualFold(strings.TrimSpace(stickyValue), "true") - if sticky { +func (p *Provider) getSticky(rootPath string) string { + stickyValue := p.get("", rootPath, "/loadbalancer", "/sticky") + if len(stickyValue) > 0 { log.Warnf("Deprecated configuration found: %s. Please use %s.", "loadbalancer/sticky", "loadbalancer/stickiness") + } else { + stickyValue = "false" } + return stickyValue +} +func (p *Provider) hasStickinessLabel(rootPath string) bool { stickinessValue := p.get("false", rootPath, "/loadbalancer", "/stickiness") - stickiness := len(stickinessValue) > 0 && strings.EqualFold(strings.TrimSpace(stickinessValue), "true") - - return stickiness || sticky + return len(stickinessValue) > 0 && strings.EqualFold(strings.TrimSpace(stickinessValue), "true") } func (p *Provider) getStickinessCookieName(rootPath string) string { diff --git a/provider/kv/kv_test.go b/provider/kv/kv_test.go index 91aa64de8..a78c0ac90 100644 --- a/provider/kv/kv_test.go +++ b/provider/kv/kv_test.go @@ -399,48 +399,6 @@ func TestKVHasStickinessLabel(t *testing.T) { }, expected: true, }, - { - desc: "stickiness=true and sticky=true", - KVPairs: []*store.KVPair{ - { - Key: "loadbalancer/stickiness", - Value: []byte("true"), - }, - { - Key: "loadbalancer/sticky", - Value: []byte("true"), - }, - }, - expected: true, - }, - { - desc: "stickiness=false and sticky=true", - KVPairs: []*store.KVPair{ - { - Key: "loadbalancer/stickiness", - Value: []byte("false"), - }, - { - Key: "loadbalancer/sticky", - Value: []byte("true"), - }, - }, - expected: true, - }, - { - desc: "stickiness=true and sticky=false", - KVPairs: []*store.KVPair{ - { - Key: "loadbalancer/stickiness", - Value: []byte("true"), - }, - { - Key: "loadbalancer/sticky", - Value: []byte("false"), - }, - }, - expected: true, - }, } for _, test := range testCases { diff --git a/provider/marathon/marathon.go b/provider/marathon/marathon.go index 982306bf7..41e61a703 100644 --- a/provider/marathon/marathon.go +++ b/provider/marathon/marathon.go @@ -188,8 +188,9 @@ func (p *Provider) loadMarathonConfig() *types.Configuration { "getMaxConnAmount": p.getMaxConnAmount, "getLoadBalancerMethod": p.getLoadBalancerMethod, "getCircuitBreakerExpression": p.getCircuitBreakerExpression, - "getStickinessCookieName": p.getStickinessCookieName, + "getSticky": p.getSticky, "hasStickinessLabel": p.hasStickinessLabel, + "getStickinessCookieName": p.getStickinessCookieName, "hasHealthCheckLabels": p.hasHealthCheckLabels, "getHealthCheckPath": p.getHealthCheckPath, "getHealthCheckInterval": p.getHealthCheckInterval, @@ -429,17 +430,17 @@ func (p *Provider) getProtocol(application marathon.Application, serviceName str return "http" } +func (p *Provider) getSticky(application marathon.Application) string { + if sticky, ok := p.getAppLabel(application, types.LabelBackendLoadbalancerSticky); ok { + log.Warnf("Deprecated configuration found: %s. Please use %s.", types.LabelBackendLoadbalancerSticky, types.LabelBackendLoadbalancerStickiness) + return sticky + } + return "false" +} + func (p *Provider) hasStickinessLabel(application marathon.Application) bool { labelStickiness, okStickiness := p.getAppLabel(application, types.LabelBackendLoadbalancerStickiness) - - labelSticky, okSticky := p.getAppLabel(application, types.LabelBackendLoadbalancerSticky) - if len(labelSticky) > 0 { - log.Warnf("Deprecated configuration found: %s. Please use %s.", types.LabelBackendLoadbalancerSticky, types.LabelBackendLoadbalancerStickiness) - } - - stickiness := okStickiness && len(labelStickiness) > 0 && strings.EqualFold(strings.TrimSpace(labelStickiness), "true") - sticky := okSticky && len(labelSticky) > 0 && strings.EqualFold(strings.TrimSpace(labelSticky), "true") - return stickiness || sticky + return okStickiness && len(labelStickiness) > 0 && strings.EqualFold(strings.TrimSpace(labelStickiness), "true") } func (p *Provider) getStickinessCookieName(application marathon.Application) string { diff --git a/provider/marathon/marathon_test.go b/provider/marathon/marathon_test.go index 145cefe5e..4c58ab62e 100644 --- a/provider/marathon/marathon_test.go +++ b/provider/marathon/marathon_test.go @@ -854,6 +854,36 @@ func TestMarathonGetProtocol(t *testing.T) { }) } } +func TestMarathonGetSticky(t *testing.T) { + testCases := []struct { + desc string + application marathon.Application + expected string + }{ + { + desc: "label missing", + application: application(), + expected: "false", + }, + { + desc: "label existing", + application: application(label(types.LabelBackendLoadbalancerSticky, "true")), + expected: "true", + }, + } + + for _, test := range testCases { + test := test + t.Run(test.desc, func(t *testing.T) { + t.Parallel() + provider := &Provider{} + actual := provider.getSticky(test.application) + if actual != test.expected { + t.Errorf("actual %q, expected %q", actual, test.expected) + } + }) + } +} func TestMarathonHasStickinessLabel(t *testing.T) { testCases := []struct { @@ -866,16 +896,6 @@ func TestMarathonHasStickinessLabel(t *testing.T) { application: application(), expected: false, }, - { - desc: "sticky=true (deprecated)", - application: application(label(types.LabelBackendLoadbalancerSticky, "true")), - expected: true, - }, - { - desc: "sticky=false (deprecated)", - application: application(label(types.LabelBackendLoadbalancerSticky, "false")), - expected: false, - }, { desc: "stickiness=true", application: application(label(types.LabelBackendLoadbalancerStickiness, "true")), @@ -886,20 +906,6 @@ func TestMarathonHasStickinessLabel(t *testing.T) { application: application(label(types.LabelBackendLoadbalancerStickiness, "true")), expected: true, }, - { - desc: "sticky=false stickiness=true ", - application: application( - label(types.LabelBackendLoadbalancerStickiness, "true"), - label(types.LabelBackendLoadbalancerSticky, "false")), - expected: true, - }, - { - desc: "sticky=true stickiness=false ", - application: application( - label(types.LabelBackendLoadbalancerStickiness, "false"), - label(types.LabelBackendLoadbalancerSticky, "true")), - expected: true, - }, } for _, test := range testCases { diff --git a/provider/rancher/rancher.go b/provider/rancher/rancher.go index 948c3c30b..341a285cc 100644 --- a/provider/rancher/rancher.go +++ b/provider/rancher/rancher.go @@ -92,10 +92,10 @@ func (p *Provider) getLoadBalancerMethod(service rancherData) string { func (p *Provider) hasLoadBalancerLabel(service rancherData) bool { _, errMethod := getServiceLabel(service, types.LabelBackendLoadbalancerMethod) _, errSticky := getServiceLabel(service, types.LabelBackendLoadbalancerSticky) - if errMethod != nil && errSticky != nil { - return false - } - return true + _, errStickiness := getServiceLabel(service, types.LabelBackendLoadbalancerStickiness) + _, errCookieName := getServiceLabel(service, types.LabelBackendLoadbalancerStickinessCookieName) + + return errMethod == nil || errSticky == nil || errStickiness == nil || errCookieName == nil } func (p *Provider) hasCircuitBreakerLabel(service rancherData) bool { @@ -112,17 +112,18 @@ func (p *Provider) getCircuitBreakerExpression(service rancherData) string { return "NetworkErrorRatio() > 1" } +func (p *Provider) getSticky(service rancherData) string { + if _, err := getServiceLabel(service, types.LabelBackendLoadbalancerSticky); err == nil { + log.Warnf("Deprecated configuration found: %s. Please use %s.", types.LabelBackendLoadbalancerSticky, types.LabelBackendLoadbalancerStickiness) + return "true" + } + return "false" +} + func (p *Provider) hasStickinessLabel(service rancherData) bool { labelStickiness, errStickiness := getServiceLabel(service, types.LabelBackendLoadbalancerStickiness) - labelSticky, errSticky := getServiceLabel(service, types.LabelBackendLoadbalancerSticky) - if len(labelSticky) > 0 { - log.Warnf("Deprecated configuration found: %s. Please use %s.", types.LabelBackendLoadbalancerSticky, types.LabelBackendLoadbalancerStickiness) - } - - stickiness := errStickiness == nil && len(labelStickiness) > 0 && strings.EqualFold(strings.TrimSpace(labelStickiness), "true") - sticky := errSticky == nil && len(labelSticky) > 0 && strings.EqualFold(strings.TrimSpace(labelSticky), "true") - return stickiness || sticky + return errStickiness == nil && len(labelStickiness) > 0 && strings.EqualFold(strings.TrimSpace(labelStickiness), "true") } func (p *Provider) getStickinessCookieName(service rancherData, backendName string) string { @@ -235,6 +236,7 @@ func (p *Provider) loadRancherConfig(services []rancherData) *types.Configuratio "hasMaxConnLabels": p.hasMaxConnLabels, "getMaxConnAmount": p.getMaxConnAmount, "getMaxConnExtractorFunc": p.getMaxConnExtractorFunc, + "getSticky": p.getSticky, "hasStickinessLabel": p.hasStickinessLabel, "getStickinessCookieName": p.getStickinessCookieName, } diff --git a/provider/rancher/rancher_test.go b/provider/rancher/rancher_test.go index d56b552e9..92426eef4 100644 --- a/provider/rancher/rancher_test.go +++ b/provider/rancher/rancher_test.go @@ -616,16 +616,6 @@ func TestRancherHasStickinessLabel(t *testing.T) { }, expected: false, }, - { - desc: "sticky=true", - service: rancherData{ - Name: "test-service", - Labels: map[string]string{ - types.LabelBackendLoadbalancerSticky: "true", - }, - }, - expected: true, - }, { desc: "stickiness=true", service: rancherData{ @@ -637,49 +627,15 @@ func TestRancherHasStickinessLabel(t *testing.T) { expected: true, }, { - desc: "sticky=true and stickiness=true", + desc: "stickiness=true", service: rancherData{ Name: "test-service", Labels: map[string]string{ - types.LabelBackendLoadbalancerSticky: "true", - types.LabelBackendLoadbalancerStickiness: "true", - }, - }, - expected: true, - }, - { - desc: "sticky=false and stickiness=false", - service: rancherData{ - Name: "test-service", - Labels: map[string]string{ - types.LabelBackendLoadbalancerSticky: "false", types.LabelBackendLoadbalancerStickiness: "false", }, }, expected: false, }, - { - desc: "sticky=true and stickiness=false", - service: rancherData{ - Name: "test-service", - Labels: map[string]string{ - types.LabelBackendLoadbalancerSticky: "true", - types.LabelBackendLoadbalancerStickiness: "false", - }, - }, - expected: true, - }, - { - desc: "sticky=false and stickiness=true", - service: rancherData{ - Name: "test-service", - Labels: map[string]string{ - types.LabelBackendLoadbalancerSticky: "false", - types.LabelBackendLoadbalancerStickiness: "true", - }, - }, - expected: true, - }, } for _, test := range testCases { diff --git a/server/server.go b/server/server.go index cfcd91411..7a8893d06 100644 --- a/server/server.go +++ b/server/server.go @@ -1172,7 +1172,8 @@ func (server *Server) configureFrontends(frontends map[string]*types.Frontend) { } func (*Server) configureBackends(backends map[string]*types.Backend) { - for backendName, backend := range backends { + for backendName := range backends { + backend := backends[backendName] if backend.LoadBalancer != nil && backend.LoadBalancer.Sticky { log.Warnf("Deprecated configuration found: %s. Please use %s.", "backend.LoadBalancer.Sticky", "backend.LoadBalancer.Stickiness") } diff --git a/templates/consul_catalog.tmpl b/templates/consul_catalog.tmpl index 93777c55c..1d5861735 100644 --- a/templates/consul_catalog.tmpl +++ b/templates/consul_catalog.tmpl @@ -18,9 +18,9 @@ [backends."backend-{{$service}}".loadbalancer] method = "{{getAttribute "backend.loadbalancer" .Attributes "wrr"}}" - sticky = {{getAttribute "backend.loadbalancer.sticky" .Attributes "false"}} + sticky = {{getSticky .Attributes}} {{if hasStickinessLabel .Attributes}} - [Backends."backend-{{$service}}".LoadBalancer.Stickiness] + [backends."backend-{{$service}}".loadbalancer.stickiness] cookieName = "{{getStickinessCookieName .Attributes}}" {{end}} diff --git a/templates/docker.tmpl b/templates/docker.tmpl index ef6f69526..c2a7cf54b 100644 --- a/templates/docker.tmpl +++ b/templates/docker.tmpl @@ -8,8 +8,9 @@ {{if hasLoadBalancerLabel $backend}} [backends.backend-{{$backendName}}.loadbalancer] method = "{{getLoadBalancerMethod $backend}}" + sticky = {{getSticky $backend}} {{if hasStickinessLabel $backend}} - [Backends."{{$backendName}}".LoadBalancer.Stickiness] + [backends.backend-{{$backendName}}.loadBalancer.stickiness] cookieName = "{{getStickinessCookieName $backend}}" {{end}} {{end}} diff --git a/templates/ecs.tmpl b/templates/ecs.tmpl index f42642ad4..74c96504a 100644 --- a/templates/ecs.tmpl +++ b/templates/ecs.tmpl @@ -1,8 +1,9 @@ [backends]{{range $serviceName, $instances := .Services}} [backends.backend-{{ $serviceName }}.loadbalancer] method = "{{ getLoadBalancerMethod $instances}}" + sticky = {{ getLoadBalancerSticky $instances}} {{if hasStickinessLabel $instances}} - [Backends.backend-{{ $serviceName }}.LoadBalancer.Stickiness] + [backends.backend-{{ $serviceName }}.loadbalancer.stickiness] cookieName = "{{getStickinessCookieName $instances}}" {{end}} diff --git a/templates/kubernetes.tmpl b/templates/kubernetes.tmpl index bab94ae19..8f31c3e79 100644 --- a/templates/kubernetes.tmpl +++ b/templates/kubernetes.tmpl @@ -6,8 +6,11 @@ {{end}} [backends."{{$backendName}}".loadbalancer] method = "{{$backend.LoadBalancer.Method}}" + {{if $backend.LoadBalancer.Sticky}} + sticky = true + {{end}} {{if $backend.LoadBalancer.Stickiness}} - [Backends."{{$backendName}}".LoadBalancer.Stickiness] + [backends."{{$backendName}}".loadbalancer.stickiness] cookieName = "{{$backend.LoadBalancer.Stickiness.CookieName}}" {{end}} {{range $serverName, $server := $backend.Servers}} diff --git a/templates/kv.tmpl b/templates/kv.tmpl index ebbbb39c2..291a14d38 100644 --- a/templates/kv.tmpl +++ b/templates/kv.tmpl @@ -16,9 +16,9 @@ {{with $loadBalancer}} [backends."{{$backendName}}".loadBalancer] method = "{{$loadBalancer}}" - sticky = {{ Get "false" . "/loadbalancer/" "sticky" }} + sticky = {{ getSticky . }} {{if hasStickinessLabel $backend}} - [Backends."{{$backendName}}".LoadBalancer.Stickiness] + [backends."{{$backendName}}".loadBalancer.stickiness] cookieName = {{getStickinessCookieName $backend}} {{end}} {{end}} diff --git a/templates/marathon.tmpl b/templates/marathon.tmpl index aaa7f2e6a..e03fd601c 100644 --- a/templates/marathon.tmpl +++ b/templates/marathon.tmpl @@ -20,8 +20,9 @@ {{ if hasLoadBalancerLabels $app }} [backends."backend{{getBackend $app $serviceName }}".loadbalancer] method = "{{getLoadBalancerMethod $app }}" + sticky = {{getSticky $app}} {{if hasStickinessLabel $app}} - [Backends."backend{{getBackend $app $serviceName }}".LoadBalancer.Stickiness] + [backends."backend{{getBackend $app $serviceName }}".loadbalancer.stickiness] cookieName = "{{getStickinessCookieName $app}}" {{end}} {{end}} diff --git a/templates/rancher.tmpl b/templates/rancher.tmpl index 1f911e5b2..a5355c252 100644 --- a/templates/rancher.tmpl +++ b/templates/rancher.tmpl @@ -8,8 +8,9 @@ {{if hasLoadBalancerLabel $backend}} [backends.backend-{{$backendName}}.loadbalancer] method = "{{getLoadBalancerMethod $backend}}" + sticky = {{getSticky $backend}} {{if hasStickinessLabel $backend}} - [Backends."{{$backendName}}".LoadBalancer.Stickiness] + [backends.backend-{{$backendName}}.loadbalancer.stickiness] cookieName = "{{getStickinessCookieName $backend}}" {{end}} {{end}}