Add the sprig functions in the template engine

This commit is contained in:
thomasbach76 2017-08-10 20:42:39 +02:00 committed by Ludovic Fernandez
parent ff11467022
commit 7ff6c32452
42 changed files with 5671 additions and 50 deletions

View file

@ -351,7 +351,7 @@ func (p *Provider) getBackend(application marathon.Application) string {
if label, ok := p.getLabel(application, types.LabelBackend); ok {
return label
}
return provider.Replace("/", "-", application.ID)
return strings.Replace(application.ID, "/", "-", -1)
}
func (p *Provider) getSubDomain(name string) string {

View file

@ -12,6 +12,7 @@ import (
"unicode"
"github.com/BurntSushi/toml"
"github.com/Masterminds/sprig"
"github.com/containous/traefik/autogen"
"github.com/containous/traefik/log"
"github.com/containous/traefik/safe"
@ -59,14 +60,12 @@ func (p *BaseProvider) GetConfiguration(defaultTemplateFile string, funcMap temp
err error
)
configuration := new(types.Configuration)
var defaultFuncMap = template.FuncMap{
"replace": Replace,
"tolower": strings.ToLower,
"normalize": Normalize,
"split": split,
"contains": contains,
}
var defaultFuncMap = sprig.TxtFuncMap()
// tolower is deprecated in favor of sprig's lower function
defaultFuncMap["tolower"] = strings.ToLower
defaultFuncMap["normalize"] = Normalize
defaultFuncMap["split"] = split
for funcID, funcElement := range funcMap {
defaultFuncMap[funcID] = funcElement
}
@ -102,15 +101,6 @@ func (p *BaseProvider) GetConfiguration(defaultTemplateFile string, funcMap temp
return configuration, nil
}
// Replace is an alias for strings.Replace
func Replace(s1 string, s2 string, s3 string) string {
return strings.Replace(s3, s1, s2, -1)
}
func contains(substr, s string) bool {
return strings.Contains(s, substr)
}
func split(sep, s string) []string {
return strings.Split(s, sep)
}

View file

@ -145,37 +145,6 @@ func TestGetConfiguration(t *testing.T) {
}
}
func TestReplace(t *testing.T) {
cases := []struct {
str string
expected string
}{
{
str: "",
expected: "",
},
{
str: "foo",
expected: "bar",
},
{
str: "foo foo",
expected: "bar bar",
},
{
str: "somethingfoo",
expected: "somethingbar",
},
}
for _, c := range cases {
actual := Replace("foo", "bar", c.str)
if actual != c.expected {
t.Fatalf("expected %q, got %q, for %q", c.expected, actual, c.str)
}
}
}
func TestGetConfigurationReturnsCorrectMaxConnConfiguration(t *testing.T) {
templateFile, err := ioutil.TempFile("", "provider-configuration")
if err != nil {
@ -380,3 +349,51 @@ func TestDefaultFuncMap(t *testing.T) {
t.Fatal("Frontend frontend-1 should exists, but it not")
}
}
func TestSprigFunctions(t *testing.T) {
templateFile, err := ioutil.TempFile("", "provider-configuration")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(templateFile.Name())
data := []byte(`
{{$backend_name := trimAll "-" uuidv4}}
[backends]
[backends.{{$backend_name}}]
[backends.{{$backend_name}}.circuitbreaker]
[backends.{{$backend_name}}.servers.server2]
url = "http://172.17.0.3:80"
weight = 1
[frontends]
[frontends.{{normalize "frontend/1"}}]
backend = "{{$backend_name}}"
passHostHeader = true
[frontends.frontend-1.routes.test_2]
rule = "Path"
value = "/test"`)
err = ioutil.WriteFile(templateFile.Name(), data, 0700)
if err != nil {
t.Fatal(err)
}
provider := &myProvider{
BaseProvider{
Filename: templateFile.Name(),
},
nil,
}
configuration, err := provider.GetConfiguration(templateFile.Name(), nil, nil)
if err != nil {
t.Fatalf("Shouldn't have error out, got %v", err)
}
if configuration == nil {
t.Fatal("Configuration should not be nil, but was")
}
if len(configuration.Backends) != 1 {
t.Fatal("one backend should be defined, but it's not")
}
if _, ok := configuration.Frontends["frontend-1"]; !ok {
t.Fatal("Frontend frontend-1 should exists, but it not")
}
}