diff --git a/old/provider/consulcatalog/config_test.go b/old/provider/consulcatalog/config_test.go index 64b2f7acb..3449b2825 100644 --- a/old/provider/consulcatalog/config_test.go +++ b/old/provider/consulcatalog/config_test.go @@ -829,6 +829,113 @@ func TestProviderBuildConfiguration(t *testing.T) { } } +func TestProviderBuildConfigurationCustomPrefix(t *testing.T) { + prefix := "traefik-test" + p := &Provider{ + Domain: "localhost", + Prefix: prefix, + ExposedByDefault: false, + FrontEndRule: "Host:{{.ServiceName}}.{{.Domain}}", + frontEndRuleTemplate: template.New("consul catalog frontend rule"), + } + + testCases := []struct { + desc string + nodes []catalogUpdate + expectedFrontends map[string]*types.Frontend + expectedBackends map[string]*types.Backend + }{ + { + desc: "Should build config which contains three frontends and one backend", + nodes: []catalogUpdate{ + { + Service: &serviceUpdate{ + ServiceName: "test", + Attributes: []string{ + "random.foo=bar", + prefix + ".frontend.rule=Host:A", + prefix + ".frontends.test1.rule=Host:B", + prefix + ".frontends.test2.rule=Host:C", + }, + }, + Nodes: []*api.ServiceEntry{ + { + Service: &api.AgentService{ + Service: "test", + Address: "127.0.0.1", + Port: 80, + Tags: []string{ + "random.foo=bar", + }, + }, + Node: &api.Node{ + Node: "localhost", + Address: "127.0.0.1", + }, + }, + }, + }, + }, + expectedFrontends: map[string]*types.Frontend{ + "frontend-test": { + Backend: "backend-test", + PassHostHeader: true, + Routes: map[string]types.Route{ + "route-host-test": { + Rule: "Host:A", + }, + }, + EntryPoints: []string{}, + }, + "frontend-test-test1": { + Backend: "backend-test", + PassHostHeader: true, + Routes: map[string]types.Route{ + "route-host-test-test1": { + Rule: "Host:B", + }, + }, + EntryPoints: []string{}, + }, + "frontend-test-test2": { + Backend: "backend-test", + PassHostHeader: true, + Routes: map[string]types.Route{ + "route-host-test-test2": { + Rule: "Host:C", + }, + }, + EntryPoints: []string{}, + }, + }, + expectedBackends: map[string]*types.Backend{ + "backend-test": { + Servers: map[string]types.Server{ + "test-0-O0Tnh-SwzY69M6SurTKP3wNKkzI": { + URL: "http://127.0.0.1:80", + Weight: 1, + }, + }, + }, + }, + }, + } + + for _, test := range testCases { + test := test + t.Run(test.desc, func(t *testing.T) { + t.Parallel() + + nodes := fakeLoadTraefikLabelsSlice(test.nodes, p.Prefix) + + actualConfig := p.buildConfiguration(nodes) + assert.NotNil(t, actualConfig) + assert.Equal(t, test.expectedBackends, actualConfig.Backends) + assert.Equal(t, test.expectedFrontends, actualConfig.Frontends) + }) + } +} + func TestGetTag(t *testing.T) { testCases := []struct { desc string diff --git a/old/provider/consulcatalog/consul_catalog.go b/old/provider/consulcatalog/consul_catalog.go index 2a9943ccf..3e87ca939 100644 --- a/old/provider/consulcatalog/consul_catalog.go +++ b/old/provider/consulcatalog/consul_catalog.go @@ -578,7 +578,7 @@ func (p *Provider) generateFrontends(service *serviceUpdate) []*serviceUpdate { }) // loop over children of .frontends.* - for _, frontend := range getSegments(p.Prefix+".frontends", p.Prefix, service.TraefikLabels) { + for _, frontend := range getSegments(label.Prefix+"frontends", label.Prefix, service.TraefikLabels) { frontends = append(frontends, &serviceUpdate{ ServiceName: service.ServiceName + "-" + frontend.Name, ParentServiceName: service.ServiceName, @@ -589,6 +589,7 @@ func (p *Provider) generateFrontends(service *serviceUpdate) []*serviceUpdate { return frontends } + func getSegments(path string, prefix string, tree map[string]string) []*frontendSegment { segments := make([]*frontendSegment, 0) // find segment names @@ -598,13 +599,12 @@ func getSegments(path string, prefix string, tree map[string]string) []*frontend segmentNames[strings.SplitN(strings.TrimPrefix(key, path+"."), ".", 2)[0]] = true } } - // get labels for each segment found for segment := range segmentNames { labels := make(map[string]string) for key, value := range tree { if strings.HasPrefix(key, path+"."+segment) { - labels[prefix+".frontend"+strings.TrimPrefix(key, path+"."+segment)] = value + labels[prefix+"frontend"+strings.TrimPrefix(key, path+"."+segment)] = value } } segments = append(segments, &frontendSegment{