From c7df82e695b3e994e51eba0af58a96dc095aa9c6 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Tue, 23 Oct 2018 17:36:05 +0200 Subject: [PATCH] Remove the trailing dot if the domain is not defined. --- provider/consulcatalog/config.go | 2 +- provider/consulcatalog/config_test.go | 16 +++++++++++++++- provider/docker/config.go | 7 +++++-- provider/ecs/config.go | 6 +++++- provider/ecs/config_test.go | 12 ++++++------ provider/marathon/config.go | 7 +++++-- provider/mesos/config.go | 5 ++++- provider/rancher/config.go | 6 +++++- 8 files changed, 46 insertions(+), 15 deletions(-) diff --git a/provider/consulcatalog/config.go b/provider/consulcatalog/config.go index f148a73a0..22cc99651 100644 --- a/provider/consulcatalog/config.go +++ b/provider/consulcatalog/config.go @@ -111,7 +111,7 @@ func (p *Provider) getFrontendRule(service serviceUpdate) string { return "" } - return buffer.String() + return strings.TrimSuffix(buffer.String(), ".") } func (p *Provider) getServer(node *api.ServiceEntry) types.Server { diff --git a/provider/consulcatalog/config_test.go b/provider/consulcatalog/config_test.go index 9485b925c..80d1c5fa3 100644 --- a/provider/consulcatalog/config_test.go +++ b/provider/consulcatalog/config_test.go @@ -1029,6 +1029,7 @@ func TestProviderGetFrontendRule(t *testing.T) { testCases := []struct { desc string service serviceUpdate + domain string expected string }{ { @@ -1037,8 +1038,18 @@ func TestProviderGetFrontendRule(t *testing.T) { ServiceName: "foo", Attributes: []string{}, }, + domain: "localhost", expected: "Host:foo.localhost", }, + { + desc: "When no domain should return default host foo", + service: serviceUpdate{ + ServiceName: "foo", + Attributes: []string{}, + }, + domain: "", + expected: "Host:foo", + }, { desc: "Should return host *.example.com", service: serviceUpdate{ @@ -1047,6 +1058,7 @@ func TestProviderGetFrontendRule(t *testing.T) { "traefik.frontend.rule=Host:*.example.com", }, }, + domain: "localhost", expected: "Host:*.example.com", }, { @@ -1057,6 +1069,7 @@ func TestProviderGetFrontendRule(t *testing.T) { "traefik.frontend.rule=Host:{{.ServiceName}}.example.com", }, }, + domain: "localhost", expected: "Host:foo.example.com", }, { @@ -1068,6 +1081,7 @@ func TestProviderGetFrontendRule(t *testing.T) { "contextPath=/bar", }, }, + domain: "localhost", expected: "PathPrefix:/bar", }, } @@ -1078,7 +1092,7 @@ func TestProviderGetFrontendRule(t *testing.T) { t.Parallel() p := &Provider{ - Domain: "localhost", + Domain: test.domain, Prefix: "traefik", FrontEndRule: "Host:{{.ServiceName}}.{{.Domain}}", frontEndRuleTemplate: template.New("consul catalog frontend rule"), diff --git a/provider/docker/config.go b/provider/docker/config.go index ab996b384..576a848c1 100644 --- a/provider/docker/config.go +++ b/provider/docker/config.go @@ -186,13 +186,16 @@ func (p *Provider) getFrontendRule(container dockerData, segmentLabels map[strin } domain := label.GetStringValue(segmentLabels, label.TraefikDomain, p.Domain) + if len(domain) > 0 { + domain = "." + domain + } if values, err := label.GetStringMultipleStrict(container.Labels, labelDockerComposeProject, labelDockerComposeService); err == nil { - return "Host:" + getSubDomain(values[labelDockerComposeService]+"."+values[labelDockerComposeProject]) + "." + domain + return "Host:" + getSubDomain(values[labelDockerComposeService]+"."+values[labelDockerComposeProject]) + domain } if len(domain) > 0 { - return "Host:" + getSubDomain(container.ServiceName) + "." + domain + return "Host:" + getSubDomain(container.ServiceName) + domain } return "" diff --git a/provider/ecs/config.go b/provider/ecs/config.go index fcc0ab1ee..ccc25e8bf 100644 --- a/provider/ecs/config.go +++ b/provider/ecs/config.go @@ -141,7 +141,11 @@ func (p *Provider) getFrontendRule(i ecsInstance) string { } domain := label.GetStringValue(i.SegmentLabels, label.TraefikDomain, p.Domain) - defaultRule := "Host:" + strings.ToLower(strings.Replace(i.Name, "_", "-", -1)) + "." + domain + if len(domain) > 0 { + domain = "." + domain + } + + defaultRule := "Host:" + strings.ToLower(strings.Replace(i.Name, "_", "-", -1)) + domain return label.GetStringValue(i.TraefikLabels, label.TraefikFrontendRule, defaultRule) } diff --git a/provider/ecs/config_test.go b/provider/ecs/config_test.go index f120b109f..0a4a169c8 100644 --- a/provider/ecs/config_test.go +++ b/provider/ecs/config_test.go @@ -52,7 +52,7 @@ func TestBuildConfiguration(t *testing.T) { Backend: "backend-instance", Routes: map[string]types.Route{ "route-frontend-instance": { - Rule: "Host:instance.", + Rule: "Host:instance", }, }, PassHostHeader: true, @@ -99,7 +99,7 @@ func TestBuildConfiguration(t *testing.T) { Backend: "backend-instance", Routes: map[string]types.Route{ "route-frontend-instance": { - Rule: "Host:instance.", + Rule: "Host:instance", }, }, PassHostHeader: true, @@ -144,7 +144,7 @@ func TestBuildConfiguration(t *testing.T) { Backend: "backend-instance", Routes: map[string]types.Route{ "route-frontend-instance": { - Rule: "Host:instance.", + Rule: "Host:instance", }, }, Auth: &types.Auth{ @@ -195,7 +195,7 @@ func TestBuildConfiguration(t *testing.T) { Backend: "backend-instance", Routes: map[string]types.Route{ "route-frontend-instance": { - Rule: "Host:instance.", + Rule: "Host:instance", }, }, Auth: &types.Auth{ @@ -246,7 +246,7 @@ func TestBuildConfiguration(t *testing.T) { Backend: "backend-instance", Routes: map[string]types.Route{ "route-frontend-instance": { - Rule: "Host:instance.", + Rule: "Host:instance", }, }, Auth: &types.Auth{ @@ -305,7 +305,7 @@ func TestBuildConfiguration(t *testing.T) { Backend: "backend-instance", Routes: map[string]types.Route{ "route-frontend-instance": { - Rule: "Host:instance.", + Rule: "Host:instance", }, }, Auth: &types.Auth{ diff --git a/provider/marathon/config.go b/provider/marathon/config.go index 0194ba4c6..5e892cb4a 100644 --- a/provider/marathon/config.go +++ b/provider/marathon/config.go @@ -214,11 +214,14 @@ func (p *Provider) getFrontendRule(app appData) string { } domain := label.GetStringValue(app.SegmentLabels, label.TraefikDomain, p.Domain) + if len(domain) > 0 { + domain = "." + domain + } if len(app.SegmentName) > 0 { - return "Host:" + strings.ToLower(provider.Normalize(app.SegmentName)) + "." + p.getSubDomain(app.ID) + "." + domain + return "Host:" + strings.ToLower(provider.Normalize(app.SegmentName)) + "." + p.getSubDomain(app.ID) + domain } - return "Host:" + p.getSubDomain(app.ID) + "." + domain + return "Host:" + p.getSubDomain(app.ID) + domain } func getPort(task marathon.Task, app appData) string { diff --git a/provider/mesos/config.go b/provider/mesos/config.go index 47a5ef9ce..61f415e88 100644 --- a/provider/mesos/config.go +++ b/provider/mesos/config.go @@ -222,8 +222,11 @@ func (p *Provider) getFrontendRule(task taskData) string { } domain := label.GetStringValue(task.TraefikLabels, label.TraefikDomain, p.Domain) + if len(domain) > 0 { + domain = "." + domain + } - return "Host:" + p.getSegmentSubDomain(task) + "." + domain + return "Host:" + p.getSegmentSubDomain(task) + domain } func (p *Provider) getServers(tasks []taskData) map[string]types.Server { diff --git a/provider/rancher/config.go b/provider/rancher/config.go index ccd8a43d1..d6232b47a 100644 --- a/provider/rancher/config.go +++ b/provider/rancher/config.go @@ -128,7 +128,11 @@ func (p *Provider) serviceFilter(service rancherData) bool { func (p *Provider) getFrontendRule(serviceName string, labels map[string]string) string { domain := label.GetStringValue(labels, label.TraefikDomain, p.Domain) - defaultRule := "Host:" + strings.ToLower(strings.Replace(serviceName, "/", ".", -1)) + "." + domain + if len(domain) > 0 { + domain = "." + domain + } + + defaultRule := "Host:" + strings.ToLower(strings.Replace(serviceName, "/", ".", -1)) + domain return label.GetStringValue(labels, label.TraefikFrontendRule, defaultRule) }