From a731b43b52b8199c5f4f800ee2540e6e15203f02 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Wed, 28 Mar 2018 17:18:04 +0200 Subject: [PATCH] Frontend rule and segment labels. --- autogen/gentemplates/gen.go | 6 +++--- provider/docker/config.go | 8 ++++---- provider/docker/config_container_docker_test.go | 7 ++----- provider/docker/config_container_swarm_test.go | 5 +---- provider/label/segment.go | 4 +++- provider/rancher/config.go | 10 +++++----- provider/rancher/config_test.go | 11 +++++++++-- provider/rancher/deprecated_config.go | 11 +++++++++-- provider/rancher/deprecated_config_test.go | 2 +- templates/docker.tmpl | 2 +- templates/rancher.tmpl | 4 ++-- 11 files changed, 40 insertions(+), 30 deletions(-) diff --git a/autogen/gentemplates/gen.go b/autogen/gentemplates/gen.go index 8b5745066..f64d7b52d 100644 --- a/autogen/gentemplates/gen.go +++ b/autogen/gentemplates/gen.go @@ -631,7 +631,7 @@ var _templatesDockerTmpl = []byte(`{{$backendServers := .Servers}} {{end}} [frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"] - rule = "{{ getFrontendRule $container }}" + rule = "{{ getFrontendRule $container $container.SegmentLabels }}" {{end}} `) @@ -2011,8 +2011,8 @@ var _templatesRancherTmpl = []byte(`{{ $backendServers := .Backends }} {{end}} {{end}} - [frontends."frontend-{{$frontendName}}".routes."route-frontend-{{$frontendName}}"] - rule = "{{getFrontendRule $service}}" + [frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"] + rule = "{{ getFrontendRule $service.Name $service.SegmentLabels }}" {{end}} `) diff --git a/provider/docker/config.go b/provider/docker/config.go index 6d747bb7d..4615030f5 100644 --- a/provider/docker/config.go +++ b/provider/docker/config.go @@ -116,7 +116,7 @@ func (p *Provider) containerFilter(container dockerData) bool { for segmentName, labels := range segmentProperties { errPort = checkSegmentPort(labels, segmentName) - if len(p.getFrontendRule(container)) == 0 { + if len(p.getFrontendRule(container, labels)) == 0 { log.Debugf("Filtering container with empty frontend rule %s %s", container.Name, segmentName) return false } @@ -160,14 +160,14 @@ func (p *Provider) getFrontendName(container dockerData, idx int) string { if len(container.SegmentName) > 0 { name = getBackendName(container) } else { - name = p.getFrontendRule(container) + "-" + strconv.Itoa(idx) + name = p.getFrontendRule(container, container.SegmentLabels) + "-" + strconv.Itoa(idx) } return provider.Normalize(name) } -func (p *Provider) getFrontendRule(container dockerData) string { - if value := label.GetStringValue(container.SegmentLabels, label.TraefikFrontendRule, ""); len(value) != 0 { +func (p *Provider) getFrontendRule(container dockerData, segmentLabels map[string]string) string { + if value := label.GetStringValue(segmentLabels, label.TraefikFrontendRule, ""); len(value) != 0 { return value } diff --git a/provider/docker/config_container_docker_test.go b/provider/docker/config_container_docker_test.go index 7c78a84d6..d6b96f3b1 100644 --- a/provider/docker/config_container_docker_test.go +++ b/provider/docker/config_container_docker_test.go @@ -634,9 +634,6 @@ func TestDockerTraefikFilter(t *testing.T) { t.Parallel() dData := parseContainer(test.container) - segmentProperties := label.ExtractTraefikLabels(dData.Labels) - dData.SegmentLabels = segmentProperties[""] - actual := test.provider.containerFilter(dData) if actual != test.expected { t.Errorf("expected %v for %+v, got %+v", test.expected, test, actual) @@ -746,12 +743,12 @@ func TestDockerGetFrontendRule(t *testing.T) { dData := parseContainer(test.container) segmentProperties := label.ExtractTraefikLabels(dData.Labels) - dData.SegmentLabels = segmentProperties[""] provider := &Provider{ Domain: "docker.localhost", } - actual := provider.getFrontendRule(dData) + + actual := provider.getFrontendRule(dData, segmentProperties[""]) assert.Equal(t, test.expected, actual) }) } diff --git a/provider/docker/config_container_swarm_test.go b/provider/docker/config_container_swarm_test.go index 4623dafd9..5cab4c4a3 100644 --- a/provider/docker/config_container_swarm_test.go +++ b/provider/docker/config_container_swarm_test.go @@ -469,8 +469,6 @@ func TestSwarmTraefikFilter(t *testing.T) { t.Parallel() dData := parseService(test.service, test.networks) - segmentProperties := label.ExtractTraefikLabels(dData.Labels) - dData.SegmentLabels = segmentProperties[""] actual := test.provider.containerFilter(dData) if actual != test.expected { @@ -583,14 +581,13 @@ func TestSwarmGetFrontendRule(t *testing.T) { dData := parseService(test.service, test.networks) segmentProperties := label.ExtractTraefikLabels(dData.Labels) - dData.SegmentLabels = segmentProperties[""] provider := &Provider{ Domain: "docker.localhost", SwarmMode: true, } - actual := provider.getFrontendRule(dData) + actual := provider.getFrontendRule(dData, segmentProperties[""]) assert.Equal(t, test.expected, actual) }) } diff --git a/provider/label/segment.go b/provider/label/segment.go index 8274e4490..6eee9857c 100644 --- a/provider/label/segment.go +++ b/provider/label/segment.go @@ -128,7 +128,9 @@ func ExtractTraefikLabels(originLabels map[string]string) SegmentProperties { allLabels[segmentName][Prefix+propertyName] = value } } - log.Debug(originLabels, allLabels) + + log.Debug("originLabels", originLabels) + log.Debug("allLabels", allLabels) allLabels.mergeDefault() diff --git a/provider/rancher/config.go b/provider/rancher/config.go index 29fb6d84e..110e6db94 100644 --- a/provider/rancher/config.go +++ b/provider/rancher/config.go @@ -88,7 +88,7 @@ func (p *Provider) serviceFilter(service rancherData) bool { return false } - if len(p.getFrontendRule(service)) == 0 { + if len(p.getFrontendRule(service.Name, labels)) == 0 { log.Debugf("Filtering container with empty frontend rule %s %s", service.Name, segmentName) return false } @@ -123,9 +123,9 @@ func (p *Provider) serviceFilter(service rancherData) bool { return true } -func (p *Provider) getFrontendRule(service rancherData) string { - defaultRule := "Host:" + strings.ToLower(strings.Replace(service.Name, "/", ".", -1)) + "." + p.Domain - return label.GetStringValue(service.Labels, label.TraefikFrontendRule, defaultRule) +func (p *Provider) getFrontendRule(serviceName string, labels map[string]string) string { + defaultRule := "Host:" + strings.ToLower(strings.Replace(serviceName, "/", ".", -1)) + "." + p.Domain + return label.GetStringValue(labels, label.TraefikFrontendRule, defaultRule) } func (p *Provider) getFrontendName(service rancherData) string { @@ -133,7 +133,7 @@ func (p *Provider) getFrontendName(service rancherData) string { if len(service.SegmentName) > 0 { name = getBackendName(service) } else { - name = p.getFrontendRule(service) + name = p.getFrontendRule(service.Name, service.SegmentLabels) } return provider.Normalize(name) diff --git a/provider/rancher/config_test.go b/provider/rancher/config_test.go index 19da23e6e..88aca6fb6 100644 --- a/provider/rancher/config_test.go +++ b/provider/rancher/config_test.go @@ -260,6 +260,7 @@ func TestProviderBuildConfiguration(t *testing.T) { label.Prefix + "sauternes." + label.SuffixProtocol: "https", label.Prefix + "sauternes." + label.SuffixWeight: "12", + label.Prefix + "sauternes." + label.SuffixFrontendRule: "Host:traefik.wtf", label.Prefix + "sauternes." + label.SuffixFrontendAuthBasic: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0", label.Prefix + "sauternes." + label.SuffixFrontendEntryPoints: "http,https", label.Prefix + "sauternes." + label.SuffixFrontendPassHostHeader: "true", @@ -319,7 +320,7 @@ func TestProviderBuildConfiguration(t *testing.T) { Backend: "backend-sauternes", Routes: map[string]types.Route{ "route-frontend-sauternes": { - Rule: "Host:.rancher.localhost", + Rule: "Host:traefik.wtf", }, }, PassHostHeader: true, @@ -697,6 +698,9 @@ func TestProviderGetFrontendName(t *testing.T) { t.Run(test.desc, func(t *testing.T) { t.Parallel() + segmentProperties := label.ExtractTraefikLabels(test.service.Labels) + test.service.SegmentLabels = segmentProperties[""] + actual := provider.getFrontendName(test.service) assert.Equal(t, test.expected, actual) }) @@ -762,7 +766,10 @@ func TestProviderGetFrontendRule(t *testing.T) { t.Run(test.desc, func(t *testing.T) { t.Parallel() - actual := provider.getFrontendRule(test.service) + segmentProperties := label.ExtractTraefikLabels(test.service.Labels) + test.service.SegmentLabels = segmentProperties[""] + + actual := provider.getFrontendRule(test.service.Name, test.service.SegmentLabels) assert.Equal(t, test.expected, actual) }) } diff --git a/provider/rancher/deprecated_config.go b/provider/rancher/deprecated_config.go index 003eb4a33..fa30b3fad 100644 --- a/provider/rancher/deprecated_config.go +++ b/provider/rancher/deprecated_config.go @@ -1,6 +1,7 @@ package rancher import ( + "strings" "text/template" "github.com/BurntSushi/ty/fun" @@ -31,7 +32,7 @@ func (p *Provider) buildConfigurationV1(services []rancherData) *types.Configura // Frontend functions "getBackend": getBackendNameV1, - "getFrontendRule": p.getFrontendRule, + "getFrontendRule": p.getFrontendRuleV1, "getPriority": getFuncIntV1(label.TraefikFrontendPriority, label.DefaultFrontendPriorityInt), "getPassHostHeader": getFuncBoolV1(label.TraefikFrontendPassHostHeader, label.DefaultPassHostHeaderBool), "getEntryPoints": getFuncSliceStringV1(label.TraefikFrontendEntryPoints), @@ -109,9 +110,15 @@ func (p *Provider) serviceFilterV1(service rancherData) bool { return true } +// Deprecated +func (p *Provider) getFrontendRuleV1(service rancherData) string { + defaultRule := "Host:" + strings.ToLower(strings.Replace(service.Name, "/", ".", -1)) + "." + p.Domain + return label.GetStringValue(service.Labels, label.TraefikFrontendRule, defaultRule) +} + // Deprecated func (p *Provider) getFrontendNameV1(service rancherData) string { - return provider.Normalize(p.getFrontendRule(service)) + return provider.Normalize(p.getFrontendRuleV1(service)) } // Deprecated diff --git a/provider/rancher/deprecated_config_test.go b/provider/rancher/deprecated_config_test.go index c0d6d9836..ab9d6d4af 100644 --- a/provider/rancher/deprecated_config_test.go +++ b/provider/rancher/deprecated_config_test.go @@ -459,7 +459,7 @@ func TestProviderGetFrontendRuleV1(t *testing.T) { t.Run(test.desc, func(t *testing.T) { t.Parallel() - actual := provider.getFrontendRule(test.service) + actual := provider.getFrontendRule(test.service.Name, test.service.Labels) assert.Equal(t, test.expected, actual) }) } diff --git a/templates/docker.tmpl b/templates/docker.tmpl index 06a47c663..307275914 100644 --- a/templates/docker.tmpl +++ b/templates/docker.tmpl @@ -171,6 +171,6 @@ {{end}} [frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"] - rule = "{{ getFrontendRule $container }}" + rule = "{{ getFrontendRule $container $container.SegmentLabels }}" {{end}} diff --git a/templates/rancher.tmpl b/templates/rancher.tmpl index 32c30edbf..e78e5d9aa 100644 --- a/templates/rancher.tmpl +++ b/templates/rancher.tmpl @@ -169,7 +169,7 @@ {{end}} {{end}} - [frontends."frontend-{{$frontendName}}".routes."route-frontend-{{$frontendName}}"] - rule = "{{getFrontendRule $service}}" + [frontends."frontend-{{ $frontendName }}".routes."route-frontend-{{ $frontendName }}"] + rule = "{{ getFrontendRule $service.Name $service.SegmentLabels }}" {{end}}