1
0
Fork 0

refactor(consulcatalog): reorganize code.

This commit is contained in:
Fernandez Ludovic 2018-01-01 00:02:18 +01:00 committed by Traefiker
parent 6e23454202
commit 586b5714a7
4 changed files with 669 additions and 652 deletions

View file

@ -16,19 +16,23 @@ import (
func (p *CatalogProvider) buildConfiguration(catalog []catalogUpdate) *types.Configuration {
var FuncMap = template.FuncMap{
"getAttribute": p.getAttribute,
"getTag": getTag,
"hasTag": hasTag,
// Backend functions
"getBackend": getBackend,
"getFrontendRule": p.getFrontendRule,
"getBackendName": getBackendName,
"getBackendAddress": getBackendAddress,
"getBasicAuth": p.getBasicAuth,
"hasMaxconnAttributes": p.hasMaxConnAttributes,
"getSticky": p.getSticky,
"hasStickinessLabel": p.hasStickinessLabel,
"getStickinessCookieName": p.getStickinessCookieName,
"getAttribute": p.getAttribute,
"getTag": getTag,
"hasTag": hasTag,
"getEntryPoints": getEntryPoints,
"hasMaxconnAttributes": p.hasMaxConnAttributes,
// Frontend functions
"getBackendName": getBackendName,
"getFrontendRule": p.getFrontendRule,
"getBasicAuth": p.getBasicAuth,
"getEntryPoints": getEntryPoints,
}
var allNodes []*api.ServiceEntry
@ -58,7 +62,7 @@ func (p *CatalogProvider) buildConfiguration(catalog []catalogUpdate) *types.Con
return configuration
}
func (p *CatalogProvider) setupFrontEndTemplate() {
func (p *CatalogProvider) setupFrontEndRuleTemplate() {
var FuncMap = template.FuncMap{
"getAttribute": p.getAttribute,
"getTag": getTag,
@ -68,6 +72,8 @@ func (p *CatalogProvider) setupFrontEndTemplate() {
p.frontEndRuleTemplate = tmpl
}
// Specific functions
func (p *CatalogProvider) getFrontendRule(service serviceUpdate) string {
customFrontendRule := p.getAttribute(label.SuffixFrontendRule, service.Attributes, "")
if customFrontendRule == "" {
@ -102,19 +108,16 @@ func (p *CatalogProvider) getFrontendRule(service serviceUpdate) string {
}
func (p *CatalogProvider) getBasicAuth(tags []string) []string {
list := p.getAttribute(label.SuffixFrontendAuthBasic, tags, "")
if list != "" {
return strings.Split(list, ",")
}
return []string{}
return p.getSliceAttribute(label.SuffixFrontendAuthBasic, tags)
}
func (p *CatalogProvider) hasMaxConnAttributes(attributes []string) bool {
amount := p.getAttribute(label.SuffixBackendMaxConnAmount, attributes, "")
extractorfunc := p.getAttribute(label.SuffixBackendMaxConnExtractorFunc, attributes, "")
return amount != "" && extractorfunc != ""
extractorFunc := p.getAttribute(label.SuffixBackendMaxConnExtractorFunc, attributes, "")
return amount != "" && extractorFunc != ""
}
// Deprecated
func getEntryPoints(list string) []string {
return strings.Split(list, ",")
}
@ -146,7 +149,8 @@ func getBackendName(node *api.ServiceEntry, index int) string {
}
// TODO: Deprecated
// Deprecated replaced by Stickiness
// replaced by Stickiness
// Deprecated
func (p *CatalogProvider) getSticky(tags []string) string {
stickyTag := p.getAttribute(label.SuffixBackendLoadBalancerSticky, tags, "")
if len(stickyTag) > 0 {
@ -165,3 +169,77 @@ func (p *CatalogProvider) hasStickinessLabel(tags []string) bool {
func (p *CatalogProvider) getStickinessCookieName(tags []string) string {
return p.getAttribute(label.SuffixBackendLoadBalancerStickinessCookieName, tags, "")
}
// Base functions
func (p *CatalogProvider) getSliceAttribute(name string, tags []string) []string {
rawValue := getTag(p.getPrefixedName(name), tags, "")
if len(rawValue) == 0 {
return nil
}
return label.SplitAndTrimString(rawValue, ",")
}
func (p *CatalogProvider) getBoolAttribute(name string, tags []string, defaultValue bool) bool {
rawValue := getTag(p.getPrefixedName(name), tags, "")
if len(rawValue) == 0 {
return defaultValue
}
value, err := strconv.ParseBool(rawValue)
if err != nil {
log.Errorf("Invalid value for %s: %s", name, rawValue)
return defaultValue
}
return value
}
func (p *CatalogProvider) getAttribute(name string, tags []string, defaultValue string) string {
return getTag(p.getPrefixedName(name), tags, defaultValue)
}
func (p *CatalogProvider) getPrefixedName(name string) string {
if len(p.Prefix) > 0 && len(name) > 0 {
return p.Prefix + "." + name
}
return name
}
func hasTag(name string, tags []string) bool {
lowerName := strings.ToLower(name)
for _, tag := range tags {
lowerTag := strings.ToLower(tag)
// Given the nature of Consul tags, which could be either singular markers, or key=value pairs
if strings.HasPrefix(lowerTag, lowerName+"=") || lowerTag == lowerName {
return true
}
}
return false
}
func getTag(name string, tags []string, defaultValue string) string {
lowerName := strings.ToLower(name)
for _, tag := range tags {
lowerTag := strings.ToLower(tag)
// Given the nature of Consul tags, which could be either singular markers, or key=value pairs
if strings.HasPrefix(lowerTag, lowerName+"=") || lowerTag == lowerName {
// In case, where a tag might be a key=value, try to split it by the first '='
kv := strings.SplitN(tag, "=", 2)
// If the returned result is a key=value pair, return the 'value' component
if len(kv) == 2 {
return kv[1]
}
// If the returned result is a singular marker, return the 'key' component
return kv[0]
}
}
return defaultValue
}