diff --git a/provider/docker.go b/provider/docker.go index 27c35760d..e93d603fd 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -258,7 +258,6 @@ func (provider *Docker) loadDockerConfig(containersInspected []dockerData) *type "getMaxConnAmount": provider.getMaxConnAmount, "getMaxConnExtractorFunc": provider.getMaxConnExtractorFunc, "getSticky": provider.getSticky, - "replace": replace, } // filter containers diff --git a/provider/eureka.go b/provider/eureka.go index 50b5f9c6a..4b6ff8932 100644 --- a/provider/eureka.go +++ b/provider/eureka.go @@ -84,8 +84,6 @@ func (provider *Eureka) Provide(configurationChan chan<- types.ConfigMessage, po // Build the configuration from Eureka server func (provider *Eureka) buildConfiguration() (*types.Configuration, error) { var EurekaFuncMap = template.FuncMap{ - "replace": replace, - "tolower": strings.ToLower, "getPort": provider.getPort, "getProtocol": provider.getProtocol, "getWeight": provider.getWeight, diff --git a/provider/marathon.go b/provider/marathon.go index 3e3ef8d3a..5e901f37c 100644 --- a/provider/marathon.go +++ b/provider/marathon.go @@ -139,7 +139,6 @@ func (provider *Marathon) loadMarathonConfig() *types.Configuration { "getEntryPoints": provider.getEntryPoints, "getFrontendRule": provider.getFrontendRule, "getFrontendBackend": provider.getFrontendBackend, - "replace": replace, "hasCircuitBreakerLabels": provider.hasCircuitBreakerLabels, "hasLoadBalancerLabels": provider.hasLoadBalancerLabels, "hasMaxConnLabels": provider.hasMaxConnLabels, diff --git a/provider/mesos.go b/provider/mesos.go index 1822a3e60..b6db4f597 100644 --- a/provider/mesos.go +++ b/provider/mesos.go @@ -135,7 +135,6 @@ func (provider *Mesos) loadMesosConfig() *types.Configuration { "getFrontendBackend": provider.getFrontendBackend, "getID": provider.getID, "getFrontEndName": provider.getFrontEndName, - "replace": replace, } t := records.NewRecordGenerator(time.Duration(provider.StateTimeoutSecond) * time.Second) diff --git a/provider/provider.go b/provider/provider.go index 09982bbec..641c70fc8 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -58,7 +58,19 @@ func (p *BaseProvider) getConfiguration(defaultTemplateFile string, funcMap temp err error ) configuration := new(types.Configuration) - tmpl := template.New(p.Filename).Funcs(funcMap) + var defaultFuncMap = template.FuncMap{ + "replace": replace, + "tolower": strings.ToLower, + "normalize": normalize, + "split": split, + "contains": contains, + } + + for funcID, funcElement := range funcMap { + defaultFuncMap[funcID] = funcElement + } + + tmpl := template.New(p.Filename).Funcs(defaultFuncMap) if len(p.Filename) > 0 { buf, err = ioutil.ReadFile(p.Filename) if err != nil { @@ -93,6 +105,14 @@ 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) +} + func normalize(name string) string { fargs := func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) diff --git a/provider/provider_test.go b/provider/provider_test.go index f7f321989..2e25aba9e 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -325,3 +325,58 @@ func TestMatchingConstraints(t *testing.T) { } } } + +func TestDefaultFuncMap(t *testing.T) { + templateFile, err := ioutil.TempFile("", "provider-configuration") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(templateFile.Name()) + data := []byte(` + [backends] + [backends.{{ "backend-1" | replace "-" "" }}] + [backends.{{ "BACKEND1" | tolower }}.circuitbreaker] + expression = "NetworkErrorRatio() > 0.5" + [backends.servers.server1] + url = "http://172.17.0.2:80" + weight = 10 + [backends.backend1.servers.server2] + url = "http://172.17.0.3:80" + weight = 1 + +[frontends] + [frontends.{{normalize "frontend/1"}}] + {{ $backend := "backend1/test/value" | split "/" }} + {{ $backendid := index $backend 1 }} + {{ if "backend1" | contains "backend" }} + backend = "backend1" + {{end}} + 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.Fatalf("Configuration should not be nil, but was") + } + if _, ok := configuration.Backends["backend1"]; !ok { + t.Fatalf("backend1 should exists, but it not") + } + if _, ok := configuration.Frontends["frontend-1"]; !ok { + t.Fatalf("Frontend frontend-1 should exists, but it not") + } +}