Merge pull request #930 from Juliens/defaultFuncMap
feat: Add defaultFuncMap to template
This commit is contained in:
commit
913a297e8d
6 changed files with 76 additions and 6 deletions
|
@ -258,7 +258,6 @@ func (provider *Docker) loadDockerConfig(containersInspected []dockerData) *type
|
||||||
"getMaxConnAmount": provider.getMaxConnAmount,
|
"getMaxConnAmount": provider.getMaxConnAmount,
|
||||||
"getMaxConnExtractorFunc": provider.getMaxConnExtractorFunc,
|
"getMaxConnExtractorFunc": provider.getMaxConnExtractorFunc,
|
||||||
"getSticky": provider.getSticky,
|
"getSticky": provider.getSticky,
|
||||||
"replace": replace,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter containers
|
// filter containers
|
||||||
|
|
|
@ -84,8 +84,6 @@ func (provider *Eureka) Provide(configurationChan chan<- types.ConfigMessage, po
|
||||||
// Build the configuration from Eureka server
|
// Build the configuration from Eureka server
|
||||||
func (provider *Eureka) buildConfiguration() (*types.Configuration, error) {
|
func (provider *Eureka) buildConfiguration() (*types.Configuration, error) {
|
||||||
var EurekaFuncMap = template.FuncMap{
|
var EurekaFuncMap = template.FuncMap{
|
||||||
"replace": replace,
|
|
||||||
"tolower": strings.ToLower,
|
|
||||||
"getPort": provider.getPort,
|
"getPort": provider.getPort,
|
||||||
"getProtocol": provider.getProtocol,
|
"getProtocol": provider.getProtocol,
|
||||||
"getWeight": provider.getWeight,
|
"getWeight": provider.getWeight,
|
||||||
|
|
|
@ -139,7 +139,6 @@ func (provider *Marathon) loadMarathonConfig() *types.Configuration {
|
||||||
"getEntryPoints": provider.getEntryPoints,
|
"getEntryPoints": provider.getEntryPoints,
|
||||||
"getFrontendRule": provider.getFrontendRule,
|
"getFrontendRule": provider.getFrontendRule,
|
||||||
"getFrontendBackend": provider.getFrontendBackend,
|
"getFrontendBackend": provider.getFrontendBackend,
|
||||||
"replace": replace,
|
|
||||||
"hasCircuitBreakerLabels": provider.hasCircuitBreakerLabels,
|
"hasCircuitBreakerLabels": provider.hasCircuitBreakerLabels,
|
||||||
"hasLoadBalancerLabels": provider.hasLoadBalancerLabels,
|
"hasLoadBalancerLabels": provider.hasLoadBalancerLabels,
|
||||||
"hasMaxConnLabels": provider.hasMaxConnLabels,
|
"hasMaxConnLabels": provider.hasMaxConnLabels,
|
||||||
|
|
|
@ -135,7 +135,6 @@ func (provider *Mesos) loadMesosConfig() *types.Configuration {
|
||||||
"getFrontendBackend": provider.getFrontendBackend,
|
"getFrontendBackend": provider.getFrontendBackend,
|
||||||
"getID": provider.getID,
|
"getID": provider.getID,
|
||||||
"getFrontEndName": provider.getFrontEndName,
|
"getFrontEndName": provider.getFrontEndName,
|
||||||
"replace": replace,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t := records.NewRecordGenerator(time.Duration(provider.StateTimeoutSecond) * time.Second)
|
t := records.NewRecordGenerator(time.Duration(provider.StateTimeoutSecond) * time.Second)
|
||||||
|
|
|
@ -58,7 +58,19 @@ func (p *BaseProvider) getConfiguration(defaultTemplateFile string, funcMap temp
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
configuration := new(types.Configuration)
|
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 {
|
if len(p.Filename) > 0 {
|
||||||
buf, err = ioutil.ReadFile(p.Filename)
|
buf, err = ioutil.ReadFile(p.Filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -93,6 +105,14 @@ func replace(s1 string, s2 string, s3 string) string {
|
||||||
return strings.Replace(s3, s1, s2, -1)
|
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 {
|
func normalize(name string) string {
|
||||||
fargs := func(c rune) bool {
|
fargs := func(c rune) bool {
|
||||||
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
|
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
|
||||||
|
|
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue