Fix label segmentation when using custom prefix
This commit is contained in:
parent
057498ed01
commit
a47d770e71
2 changed files with 110 additions and 3 deletions
|
@ -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) {
|
func TestGetTag(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
|
|
|
@ -578,7 +578,7 @@ func (p *Provider) generateFrontends(service *serviceUpdate) []*serviceUpdate {
|
||||||
})
|
})
|
||||||
|
|
||||||
// loop over children of <prefix>.frontends.*
|
// loop over children of <prefix>.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{
|
frontends = append(frontends, &serviceUpdate{
|
||||||
ServiceName: service.ServiceName + "-" + frontend.Name,
|
ServiceName: service.ServiceName + "-" + frontend.Name,
|
||||||
ParentServiceName: service.ServiceName,
|
ParentServiceName: service.ServiceName,
|
||||||
|
@ -589,6 +589,7 @@ func (p *Provider) generateFrontends(service *serviceUpdate) []*serviceUpdate {
|
||||||
|
|
||||||
return frontends
|
return frontends
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSegments(path string, prefix string, tree map[string]string) []*frontendSegment {
|
func getSegments(path string, prefix string, tree map[string]string) []*frontendSegment {
|
||||||
segments := make([]*frontendSegment, 0)
|
segments := make([]*frontendSegment, 0)
|
||||||
// find segment names
|
// 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
|
segmentNames[strings.SplitN(strings.TrimPrefix(key, path+"."), ".", 2)[0]] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get labels for each segment found
|
// get labels for each segment found
|
||||||
for segment := range segmentNames {
|
for segment := range segmentNames {
|
||||||
labels := make(map[string]string)
|
labels := make(map[string]string)
|
||||||
for key, value := range tree {
|
for key, value := range tree {
|
||||||
if strings.HasPrefix(key, path+"."+segment) {
|
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{
|
segments = append(segments, &frontendSegment{
|
||||||
|
|
Loading…
Reference in a new issue