1
0
mirror of https://github.com/containous/traefik.git synced 2024-12-22 13:34:03 +03:00

Remove the trailing dot if the domain is not defined.

This commit is contained in:
Ludovic Fernandez 2018-10-23 17:36:05 +02:00 committed by Traefiker Bot
parent 638960284e
commit c7df82e695
8 changed files with 46 additions and 15 deletions

View File

@ -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 {

View File

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

View File

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

View File

@ -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)
}

View File

@ -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{

View File

@ -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 {

View File

@ -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 {

View File

@ -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)
}