feat(kv): add frontend redirect configuration.
This commit is contained in:
parent
40b59da224
commit
cfa1f47226
4 changed files with 99 additions and 0 deletions
|
@ -33,6 +33,8 @@ func (p *Provider) buildConfiguration() *types.Configuration {
|
||||||
"Last": p.last,
|
"Last": p.last,
|
||||||
"Has": p.has,
|
"Has": p.has,
|
||||||
|
|
||||||
|
// Frontend functions
|
||||||
|
"getRedirect": p.getRedirect,
|
||||||
// Backend functions
|
// Backend functions
|
||||||
"getSticky": p.getSticky,
|
"getSticky": p.getSticky,
|
||||||
"hasStickinessLabel": p.hasStickinessLabel,
|
"hasStickinessLabel": p.hasStickinessLabel,
|
||||||
|
@ -77,6 +79,23 @@ func (p *Provider) getStickinessCookieName(rootPath string) string {
|
||||||
return p.get("", rootPath, pathBackendLoadBalancerStickinessCookieName)
|
return p.get("", rootPath, pathBackendLoadBalancerStickinessCookieName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Provider) getRedirect(rootPath string) *types.Redirect {
|
||||||
|
if p.has(rootPath, pathFrontendRedirectEntryPoint) {
|
||||||
|
return &types.Redirect{
|
||||||
|
EntryPoint: p.get("", rootPath, pathFrontendRedirectEntryPoint),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.has(rootPath, pathFrontendRedirectRegex) && p.has(rootPath, pathFrontendRedirectReplacement) {
|
||||||
|
return &types.Redirect{
|
||||||
|
Regex: p.get("", rootPath, pathFrontendRedirectRegex),
|
||||||
|
Replacement: p.get("", rootPath, pathFrontendRedirectReplacement),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Provider) listServers(backend string) []string {
|
func (p *Provider) listServers(backend string) []string {
|
||||||
serverNames := p.list(backend, pathBackendServers)
|
serverNames := p.list(backend, pathBackendServers)
|
||||||
return fun.Filter(p.serverFilter, serverNames).([]string)
|
return fun.Filter(p.serverFilter, serverNames).([]string)
|
||||||
|
|
|
@ -707,3 +707,66 @@ func TestProviderHasStickinessLabel(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProviderGetRedirect(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
rootPath string
|
||||||
|
kvPairs []*store.KVPair
|
||||||
|
expected *types.Redirect
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "should use entry point when entry point key is valued in the store",
|
||||||
|
rootPath: "traefik/frontends/foo",
|
||||||
|
kvPairs: filler("traefik",
|
||||||
|
frontend("foo",
|
||||||
|
withPair(pathFrontendRedirectEntryPoint, "https"))),
|
||||||
|
expected: &types.Redirect{
|
||||||
|
EntryPoint: "https",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "should use regex when regex keys are valued in the store",
|
||||||
|
rootPath: "traefik/frontends/foo",
|
||||||
|
kvPairs: filler("traefik",
|
||||||
|
frontend("foo",
|
||||||
|
withPair(pathFrontendRedirectRegex, "(.*)"),
|
||||||
|
withPair(pathFrontendRedirectReplacement, "$1"))),
|
||||||
|
expected: &types.Redirect{
|
||||||
|
Regex: "(.*)",
|
||||||
|
Replacement: "$1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "should only use entry point when entry point and regex base are valued in the store",
|
||||||
|
rootPath: "traefik/frontends/foo",
|
||||||
|
kvPairs: filler("traefik",
|
||||||
|
frontend("foo",
|
||||||
|
withPair(pathFrontendRedirectEntryPoint, "https"),
|
||||||
|
withPair(pathFrontendRedirectRegex, "nope"),
|
||||||
|
withPair(pathFrontendRedirectReplacement, "nope"))),
|
||||||
|
expected: &types.Redirect{
|
||||||
|
EntryPoint: "https",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "should return when redirect keys are not valued in the store",
|
||||||
|
rootPath: "traefik/frontends/foo",
|
||||||
|
kvPairs: filler("traefik", frontend("foo")),
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range testCases {
|
||||||
|
test := test
|
||||||
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
p := newProviderMock(test.kvPairs)
|
||||||
|
|
||||||
|
actual := p.getRedirect(test.rootPath)
|
||||||
|
|
||||||
|
assert.Equal(t, test.expected, actual)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,15 @@ import (
|
||||||
"github.com/docker/libkv/store"
|
"github.com/docker/libkv/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func newProviderMock(kvPairs []*store.KVPair) *Provider {
|
||||||
|
return &Provider{
|
||||||
|
Prefix: "traefik",
|
||||||
|
kvClient: &Mock{
|
||||||
|
KVPairs: kvPairs,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Override Get/List to return a error
|
// Override Get/List to return a error
|
||||||
type KvError struct {
|
type KvError struct {
|
||||||
Get error
|
Get error
|
||||||
|
|
|
@ -70,6 +70,14 @@
|
||||||
"{{.}}",
|
"{{.}}",
|
||||||
{{end}}]
|
{{end}}]
|
||||||
|
|
||||||
|
{{$redirect := getRedirect $frontend }}
|
||||||
|
{{ if $redirect }}
|
||||||
|
[frontends."{{$frontendName}}".redirect]
|
||||||
|
entryPoint = "{{ $redirect.EntryPoint }}"
|
||||||
|
regex = "{{ $redirect.Regex }}"
|
||||||
|
replacement = "{{ $redirect.Replacement }}"
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{range $route := List $frontend "/routes/"}}
|
{{range $route := List $frontend "/routes/"}}
|
||||||
[frontends."{{$frontendName}}".routes."{{Last $route}}"]
|
[frontends."{{$frontendName}}".routes."{{Last $route}}"]
|
||||||
rule = "{{Get "" $route "/rule"}}"
|
rule = "{{Get "" $route "/rule"}}"
|
||||||
|
|
Loading…
Reference in a new issue