feat(kv): add error pages configuration.

This commit is contained in:
Fernandez Ludovic 2018-01-03 16:39:37 +01:00 committed by Traefiker
parent cfa1f47226
commit 51390aa874
4 changed files with 98 additions and 0 deletions

View file

@ -81,6 +81,18 @@ func withPair(key string, value string) func(map[string]string) {
}
}
func withErrorPage(name string, backend, query, status string) func(map[string]string) {
return func(pairs map[string]string) {
if len(name) == 0 {
return
}
withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesBackend, backend)(pairs)
withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesQuery, query)(pairs)
withPair(pathFrontendErrorPages+name+pathFrontendErrorPagesStatus, status)(pairs)
}
}
func TestFiller(t *testing.T) {
expected := []*store.KVPair{
{Key: "traefik/backends/backend.with.dot.too", Value: []byte("")},

View file

@ -35,6 +35,8 @@ func (p *Provider) buildConfiguration() *types.Configuration {
// Frontend functions
"getRedirect": p.getRedirect,
"getErrorPages": p.getErrorPages,
// Backend functions
"getSticky": p.getSticky,
"hasStickinessLabel": p.hasStickinessLabel,
@ -96,6 +98,28 @@ func (p *Provider) getRedirect(rootPath string) *types.Redirect {
return nil
}
func (p *Provider) getErrorPages(rootPath string) map[string]*types.ErrorPage {
var errorPages map[string]*types.ErrorPage
pathErrors := p.list(rootPath, pathFrontendErrorPages)
for _, pathPage := range pathErrors {
if errorPages == nil {
errorPages = make(map[string]*types.ErrorPage)
}
pageName := p.last(pathPage)
errorPages[pageName] = &types.ErrorPage{
Backend: p.get("", pathPage, pathFrontendErrorPagesBackend),
Query: p.get("", pathPage, pathFrontendErrorPagesQuery),
Status: p.splitGet(pathPage, pathFrontendErrorPagesStatus),
}
}
return errorPages
}
func (p *Provider) listServers(backend string) []string {
serverNames := p.list(backend, pathBackendServers)
return fun.Filter(p.serverFilter, serverNames).([]string)

View file

@ -770,3 +770,52 @@ func TestProviderGetRedirect(t *testing.T) {
})
}
}
func TestProviderGetErrorPages(t *testing.T) {
testCases := []struct {
desc string
rootPath string
kvPairs []*store.KVPair
expected map[string]*types.ErrorPage
}{
{
desc: "2 errors pages",
rootPath: "traefik/frontends/foo",
kvPairs: filler("traefik",
frontend("foo",
withErrorPage("foo", "error", "/test1", "500-501, 503-599"),
withErrorPage("bar", "error", "/test2", "400-405"))),
expected: map[string]*types.ErrorPage{
"foo": {
Backend: "error",
Query: "/test1",
Status: []string{"500-501", "503-599"},
},
"bar": {
Backend: "error",
Query: "/test2",
Status: []string{"400-405"},
},
},
},
{
desc: "return nil when no errors pages",
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.getErrorPages(test.rootPath)
assert.Equal(t, test.expected, actual)
})
}
}

View file

@ -78,6 +78,19 @@
replacement = "{{ $redirect.Replacement }}"
{{end}}
{{ $errorPages := getErrorPages $frontend }}
{{ if $errorPages }}
[frontends."{{$frontendName}}".errors]
{{ range $pageName, $page := $errorPages }}
[frontends."{{$frontendName}}".errors.{{ $pageName }}]
status = [{{range $page.Status}}
"{{.}}",
{{end}}]
backend = "{{$page.Backend}}"
query = "{{$page.Query}}"
{{end}}
{{end}}
{{range $route := List $frontend "/routes/"}}
[frontends."{{$frontendName}}".routes."{{Last $route}}"]
rule = "{{Get "" $route "/rule"}}"