From cfa1f4722657298dfa60c09e4bf3a3ddc6f34f40 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 3 Jan 2018 16:37:31 +0100 Subject: [PATCH] feat(kv): add frontend redirect configuration. --- provider/kv/kv_config.go | 19 +++++++++++ provider/kv/kv_config_test.go | 63 +++++++++++++++++++++++++++++++++++ provider/kv/kv_mock_test.go | 9 +++++ templates/kv.tmpl | 8 +++++ 4 files changed, 99 insertions(+) diff --git a/provider/kv/kv_config.go b/provider/kv/kv_config.go index a228d47fd..8fee729db 100644 --- a/provider/kv/kv_config.go +++ b/provider/kv/kv_config.go @@ -33,6 +33,8 @@ func (p *Provider) buildConfiguration() *types.Configuration { "Last": p.last, "Has": p.has, + // Frontend functions + "getRedirect": p.getRedirect, // Backend functions "getSticky": p.getSticky, "hasStickinessLabel": p.hasStickinessLabel, @@ -77,6 +79,23 @@ func (p *Provider) getStickinessCookieName(rootPath string) string { 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 { serverNames := p.list(backend, pathBackendServers) return fun.Filter(p.serverFilter, serverNames).([]string) diff --git a/provider/kv/kv_config_test.go b/provider/kv/kv_config_test.go index f6f340081..c4e3cc031 100644 --- a/provider/kv/kv_config_test.go +++ b/provider/kv/kv_config_test.go @@ -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) + }) + } +} diff --git a/provider/kv/kv_mock_test.go b/provider/kv/kv_mock_test.go index 878d385da..eab65d7df 100644 --- a/provider/kv/kv_mock_test.go +++ b/provider/kv/kv_mock_test.go @@ -7,6 +7,15 @@ import ( "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 type KvError struct { Get error diff --git a/templates/kv.tmpl b/templates/kv.tmpl index 846db7567..3d9dbda71 100644 --- a/templates/kv.tmpl +++ b/templates/kv.tmpl @@ -70,6 +70,14 @@ "{{.}}", {{end}}] + {{$redirect := getRedirect $frontend }} + {{ if $redirect }} + [frontends."{{$frontendName}}".redirect] + entryPoint = "{{ $redirect.EntryPoint }}" + regex = "{{ $redirect.Regex }}" + replacement = "{{ $redirect.Replacement }}" + {{end}} + {{range $route := List $frontend "/routes/"}} [frontends."{{$frontendName}}".routes."{{Last $route}}"] rule = "{{Get "" $route "/rule"}}"