Merge pull request #930 from Juliens/defaultFuncMap

feat: Add defaultFuncMap to template
This commit is contained in:
Emile Vauge 2016-12-14 18:41:47 +01:00 committed by GitHub
commit 913a297e8d
6 changed files with 76 additions and 6 deletions

View file

@ -258,7 +258,6 @@ func (provider *Docker) loadDockerConfig(containersInspected []dockerData) *type
"getMaxConnAmount": provider.getMaxConnAmount,
"getMaxConnExtractorFunc": provider.getMaxConnExtractorFunc,
"getSticky": provider.getSticky,
"replace": replace,
}
// filter containers

View file

@ -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,

View file

@ -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,

View file

@ -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)

View file

@ -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)

View file

@ -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")
}
}