feat(consulcatalog): add frontend redirect tags.
This commit is contained in:
parent
97ce77169a
commit
170fc13e02
3 changed files with 90 additions and 0 deletions
|
@ -48,6 +48,7 @@ func (p *CatalogProvider) buildConfiguration(catalog []catalogUpdate) *types.Con
|
|||
"getPassHostHeader": p.getFuncBoolAttribute(label.SuffixFrontendPassHostHeader, true),
|
||||
"getPassTLSCert": p.getFuncBoolAttribute(label.SuffixFrontendPassTLSCert, label.DefaultPassTLSCert),
|
||||
"getWhitelistSourceRange": p.getFuncSliceAttribute(label.SuffixFrontendWhitelistSourceRange),
|
||||
"getRedirect": p.getRedirect,
|
||||
}
|
||||
|
||||
var allNodes []*api.ServiceEntry
|
||||
|
@ -284,6 +285,23 @@ func (p *CatalogProvider) getHealthCheck(tags []string) *types.HealthCheck {
|
|||
}
|
||||
}
|
||||
|
||||
func (p *CatalogProvider) getRedirect(tags []string) *types.Redirect {
|
||||
if p.hasAttribute(label.SuffixFrontendRedirectEntryPoint, tags) {
|
||||
return &types.Redirect{
|
||||
EntryPoint: p.getAttribute(label.SuffixFrontendRedirectEntryPoint, tags, ""),
|
||||
}
|
||||
}
|
||||
|
||||
if p.hasAttribute(label.SuffixFrontendRedirectRegex, tags) && p.hasAttribute(label.SuffixFrontendRedirectReplacement, tags) {
|
||||
return &types.Redirect{
|
||||
Regex: p.getAttribute(label.SuffixFrontendRedirectRegex, tags, ""),
|
||||
Replacement: p.getAttribute(label.SuffixFrontendRedirectReplacement, tags, ""),
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Base functions
|
||||
|
||||
func (p *CatalogProvider) getFuncStringAttribute(name string, defaultValue string) func(tags []string) string {
|
||||
|
@ -364,6 +382,10 @@ func (p *CatalogProvider) getBoolAttribute(name string, tags []string, defaultVa
|
|||
return value
|
||||
}
|
||||
|
||||
func (p *CatalogProvider) hasAttribute(name string, tags []string) bool {
|
||||
return hasTag(p.getPrefixedName(name), tags)
|
||||
}
|
||||
|
||||
func (p *CatalogProvider) getAttribute(name string, tags []string, defaultValue string) string {
|
||||
return getTag(p.getPrefixedName(name), tags, defaultValue)
|
||||
}
|
||||
|
|
|
@ -996,3 +996,63 @@ func TestCatalogProviderGetHealthCheck(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCatalogProviderGetRedirect(t *testing.T) {
|
||||
p := &CatalogProvider{
|
||||
Prefix: "traefik",
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
tags []string
|
||||
expected *types.Redirect
|
||||
}{
|
||||
{
|
||||
desc: "should return nil when no tags",
|
||||
tags: []string{},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
desc: "should use only entry point tag when mix regex redirect and entry point redirect",
|
||||
tags: []string{
|
||||
label.TraefikFrontendRedirectEntryPoint + "=https",
|
||||
label.TraefikFrontendRedirectRegex + "=(.*)",
|
||||
label.TraefikFrontendRedirectReplacement + "=$1",
|
||||
},
|
||||
expected: &types.Redirect{
|
||||
EntryPoint: "https",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "should return a struct when entry point redirect tag",
|
||||
tags: []string{
|
||||
label.TraefikFrontendRedirectEntryPoint + "=https",
|
||||
},
|
||||
expected: &types.Redirect{
|
||||
EntryPoint: "https",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "should return a struct when regex redirect tags",
|
||||
tags: []string{
|
||||
label.TraefikFrontendRedirectRegex + "=(.*)",
|
||||
label.TraefikFrontendRedirectReplacement + "=$1",
|
||||
},
|
||||
expected: &types.Redirect{
|
||||
Regex: "(.*)",
|
||||
Replacement: "$1",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result := p.getRedirect(test.tags)
|
||||
|
||||
assert.Equal(t, test.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,6 +67,14 @@
|
|||
"{{.}}",
|
||||
{{end}}]
|
||||
|
||||
{{ $redirect := getRedirect $service.Attributes }}
|
||||
{{if $redirect }}
|
||||
[frontends."frontend-{{ $service.ServiceName }}".redirect]
|
||||
entryPoint = "{{ $redirect.EntryPoint }}"
|
||||
regex = "{{ $redirect.Regex }}"
|
||||
replacement = "{{ $redirect.Replacement }}"
|
||||
{{end}}
|
||||
|
||||
[frontends."frontend-{{$service.ServiceName}}".routes."route-host-{{$service.ServiceName}}"]
|
||||
rule = "{{ getFrontendRule $service }}"
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue