refacto(constraint-kv): Move constraint checking out of template
This commit is contained in:
parent
013808956c
commit
ced69b8397
2 changed files with 16 additions and 11 deletions
|
@ -153,11 +153,11 @@ func (provider *Kv) loadConfig() *types.Configuration {
|
||||||
}
|
}
|
||||||
|
|
||||||
var KvFuncMap = template.FuncMap{
|
var KvFuncMap = template.FuncMap{
|
||||||
"List": provider.list,
|
"List": provider.list,
|
||||||
"Get": provider.get,
|
"ListServers": provider.listServers,
|
||||||
"SplitGet": provider.splitGet,
|
"Get": provider.get,
|
||||||
"Last": provider.last,
|
"SplitGet": provider.splitGet,
|
||||||
"CheckConstraints": provider.checkConstraints,
|
"Last": provider.last,
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration, err := provider.getConfiguration("templates/kv.tmpl", KvFuncMap, templateObjects)
|
configuration, err := provider.getConfiguration("templates/kv.tmpl", KvFuncMap, templateObjects)
|
||||||
|
@ -189,6 +189,13 @@ func (provider *Kv) list(keys ...string) []string {
|
||||||
return fun.Values(directoryKeys).([]string)
|
return fun.Values(directoryKeys).([]string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (provider *Kv) listServers(backend string) []string {
|
||||||
|
serverNames := provider.list(backend, "/servers/")
|
||||||
|
return fun.Filter(func(serverName string) bool {
|
||||||
|
return provider.checkConstraints(serverName, "/tags")
|
||||||
|
}, serverNames).([]string)
|
||||||
|
}
|
||||||
|
|
||||||
func (provider *Kv) get(defaultValue string, keys ...string) string {
|
func (provider *Kv) get(defaultValue string, keys ...string) string {
|
||||||
joinedKeys := strings.Join(keys, "")
|
joinedKeys := strings.Join(keys, "")
|
||||||
keyPair, err := provider.kvclient.Get(strings.TrimPrefix(joinedKeys, "/"))
|
keyPair, err := provider.kvclient.Get(strings.TrimPrefix(joinedKeys, "/"))
|
||||||
|
@ -220,7 +227,7 @@ func (provider *Kv) last(key string) string {
|
||||||
return splittedKey[len(splittedKey)-1]
|
return splittedKey[len(splittedKey)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *Kv) checkConstraints(keys ...string) string {
|
func (provider *Kv) checkConstraints(keys ...string) bool {
|
||||||
joinedKeys := strings.Join(keys, "")
|
joinedKeys := strings.Join(keys, "")
|
||||||
keyPair, err := provider.kvclient.Get(joinedKeys)
|
keyPair, err := provider.kvclient.Get(joinedKeys)
|
||||||
|
|
||||||
|
@ -235,7 +242,7 @@ func (provider *Kv) checkConstraints(keys ...string) string {
|
||||||
if failingConstraint != nil {
|
if failingConstraint != nil {
|
||||||
log.Debugf("Constraint %v not matching with following tags: %v", failingConstraint.String(), value)
|
log.Debugf("Constraint %v not matching with following tags: %v", failingConstraint.String(), value)
|
||||||
}
|
}
|
||||||
return "false"
|
return false
|
||||||
}
|
}
|
||||||
return "true"
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
[backends]{{range $backends}}
|
[backends]{{range $backends}}
|
||||||
{{$backend := .}}
|
{{$backend := .}}
|
||||||
{{$servers := List $backend "/servers/" }}
|
{{$servers := ListServers $backend }}
|
||||||
|
|
||||||
{{$circuitBreaker := Get "" . "/circuitbreaker/" "expression"}}
|
{{$circuitBreaker := Get "" . "/circuitbreaker/" "expression"}}
|
||||||
{{with $circuitBreaker}}
|
{{with $circuitBreaker}}
|
||||||
|
@ -28,13 +28,11 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{range $servers}}
|
{{range $servers}}
|
||||||
{{if ne (CheckConstraints "" . "/tags") "false"}}
|
|
||||||
[backends."{{Last $backend}}".servers."{{Last .}}"]
|
[backends."{{Last $backend}}".servers."{{Last .}}"]
|
||||||
url = "{{Get "" . "/url"}}"
|
url = "{{Get "" . "/url"}}"
|
||||||
weight = {{Get "" . "/weight"}}
|
weight = {{Get "" . "/weight"}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
|
||||||
|
|
||||||
[frontends]{{range $frontends}}
|
[frontends]{{range $frontends}}
|
||||||
{{$frontend := Last .}}
|
{{$frontend := Last .}}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue