From c01c1ef60ed612c5e42c1ceae0c6f92e67619cc3 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 13 Dec 2017 16:56:24 +0100 Subject: [PATCH] refactor: generic template functions. --- labels.go | 30 +++++++++++++----------------- servicefabric.go | 41 +++++++++++++++-------------------------- servicefabric_tmpl.go | 30 +++++++++++++++--------------- 3 files changed, 43 insertions(+), 58 deletions(-) diff --git a/labels.go b/labels.go index 23cda73..f2878d2 100644 --- a/labels.go +++ b/labels.go @@ -2,6 +2,7 @@ package servicefabric import ( "strconv" + "strings" ) func getFuncBoolLabel(labelName string, defaultValue bool) func(service ServiceItemExtended) bool { @@ -10,32 +11,27 @@ func getFuncBoolLabel(labelName string, defaultValue bool) func(service ServiceI } } -// ------ - -// Deprecated -func getFuncStringLabelWithDefault() func(service ServiceItemExtended, labelName string, defaultValue string) string { - return func(service ServiceItemExtended, labelName string, defaultValue string) string { - return getStringValue(service.Labels, labelName, defaultValue) - } +func getFuncServiceStringLabel(service ServiceItemExtended, labelName string, defaultValue string) string { + return getStringValue(service.Labels, labelName, defaultValue) } -// Deprecated -func hasFunc() func(service ServiceItemExtended, labelName string) bool { - return func(service ServiceItemExtended, labelName string) bool { - return hasLabelNew(service.Labels, labelName) - } +func hasFuncService(service ServiceItemExtended, labelName string) bool { + return hasLabel(service.Labels, labelName) } -// Deprecated -func getFuncStringLabel(defaultValue string) func(service ServiceItemExtended, labelName string) string { - return func(service ServiceItemExtended, labelName string) string { - return getStringValue(service.Labels, labelName, defaultValue) +func getServiceLabelsWithPrefix(service ServiceItemExtended, prefix string) map[string]string { + results := make(map[string]string) + for k, v := range service.Labels { + if strings.HasPrefix(k, prefix) { + results[k] = v + } } + return results } // must be replace by label.Has() // Deprecated -func hasLabelNew(labels map[string]string, labelName string) bool { +func hasLabel(labels map[string]string, labelName string) bool { value, ok := labels[labelName] return ok && len(value) > 0 } diff --git a/servicefabric.go b/servicefabric.go index a6b7819..8183d94 100644 --- a/servicefabric.go +++ b/servicefabric.go @@ -95,19 +95,18 @@ func (p *Provider) updateConfig(configurationChan chan<- types.ConfigMessage, po func (p *Provider) buildConfiguration(sfClient sfClient) (*types.Configuration, error) { var sfFuncMap = template.FuncMap{ - "isPrimary": isPrimary, - "getDefaultEndpoint": getDefaultEndpoint, - "getNamedEndpoint": getNamedEndpoint, - "getApplicationParameter": getApplicationParameter, - "doesAppParamContain": doesAppParamContain, - "hasLabel": hasFunc(), - "getLabelValue": getFuncStringLabel(""), - "getLabelValueWithDefault": getFuncStringLabelWithDefault(), - "getLabelsWithPrefix": getLabelsWithPrefix, - "getServicesWithLabelValueMap": getServicesWithLabelValueMap, - "getServicesWithLabelValue": getServicesWithLabelValue, // FIXME unused - "isExposed": getFuncBoolLabel("expose", false), - "getBackendName": getBackendName, + "getServices": getServices, + "hasLabel": hasFuncService, + "getLabelValue": getFuncServiceStringLabel, + "getLabelsWithPrefix": getServiceLabelsWithPrefix, + "isPrimary": isPrimary, + "isExposed": getFuncBoolLabel("expose", false), + "getBackendName": getBackendName, + "getDefaultEndpoint": getDefaultEndpoint, + "getNamedEndpoint": getNamedEndpoint, // FIXME unused + "getApplicationParameter": getApplicationParameter, // FIXME unused + "doesAppParamContain": doesAppParamContain, // FIXME unused + "filterServicesByLabelValue": filterServicesByLabelValue, // FIXME unused } services, err := getClusterServices(sfClient) @@ -118,7 +117,7 @@ func (p *Provider) buildConfiguration(sfClient sfClient) (*types.Configuration, templateObjects := struct { Services []ServiceItemExtended }{ - services, + Services: services, } return p.GetConfiguration(tmpl, sfFuncMap, templateObjects) @@ -273,7 +272,7 @@ func getValidInstances(sfClient sfClient, app sf.ApplicationItem, service sf.Ser return validInstances } -func getServicesWithLabelValueMap(services []ServiceItemExtended, key string) map[string][]ServiceItemExtended { +func getServices(services []ServiceItemExtended, key string) map[string][]ServiceItemExtended { result := map[string][]ServiceItemExtended{} for _, service := range services { if value, exists := service.Labels[key]; exists { @@ -287,7 +286,7 @@ func getServicesWithLabelValueMap(services []ServiceItemExtended, key string) ma return result } -func getServicesWithLabelValue(services []ServiceItemExtended, key, expectedValue string) []ServiceItemExtended { +func filterServicesByLabelValue(services []ServiceItemExtended, key, expectedValue string) []ServiceItemExtended { var srvWithLabel []ServiceItemExtended for _, service := range services { value, exists := service.Labels[key] @@ -298,16 +297,6 @@ func getServicesWithLabelValue(services []ServiceItemExtended, key, expectedValu return srvWithLabel } -func getLabelsWithPrefix(service ServiceItemExtended, prefix string) map[string]string { - results := make(map[string]string) - for k, v := range service.Labels { - if strings.HasPrefix(k, prefix) { - results[k] = v - } - } - return results -} - func isPrimary(instance replicaInstance) bool { _, data := instance.GetReplicaData() return data.ReplicaRole == "Primary" diff --git a/servicefabric_tmpl.go b/servicefabric_tmpl.go index f5df364..74be503 100644 --- a/servicefabric_tmpl.go +++ b/servicefabric_tmpl.go @@ -1,8 +1,8 @@ package servicefabric const tmpl = ` +{{$groupedServiceMap := getServices .Services "backend.group.name"}} [backends] - {{$groupedServiceMap := getServicesWithLabelValueMap .Services "backend.group.name"}} {{range $aggName, $aggServices := $groupedServiceMap }} [backends."{{$aggName}}"] {{range $service := $aggServices}} @@ -10,7 +10,7 @@ const tmpl = ` {{range $instance := $partition.Instances}} [backends."{{$aggName}}".servers."{{$service.ID}}-{{$instance.ID}}"] url = "{{getDefaultEndpoint $instance}}" - weight = {{getLabelValueWithDefault $service "backend.group.weight" "1"}} + weight = {{getLabelValue $service "backend.group.weight" "1"}} {{end}} {{end}} {{end}} @@ -21,15 +21,15 @@ const tmpl = ` [backends."{{$service.Name}}"] [backends."{{$service.Name}}".LoadBalancer] {{if hasLabel $service "backend.loadbalancer.method"}} - method = "{{getLabelValue $service "backend.loadbalancer.method" }}" + method = "{{getLabelValue $service "backend.loadbalancer.method" "" }}" {{else}} method = "drr" {{end}} {{if hasLabel $service "backend.healthcheck"}} [backends."{{$service.Name}}".healthcheck] - path = "{{getLabelValue $service "backend.healthcheck"}}" - interval = "{{getLabelValueWithDefault $service "backend.healthcheck.interval" "10s"}}" + path = "{{getLabelValue $service "backend.healthcheck" ""}}" + interval = "{{getLabelValue $service "backend.healthcheck.interval" "10s"}}" {{end}} {{if hasLabel $service "backend.loadbalancer.stickiness"}} @@ -38,21 +38,21 @@ const tmpl = ` {{if hasLabel $service "backend.circuitbreaker"}} [backends."{{$service.Name}}".circuitbreaker] - expression = "{{getLabelValue $service "backend.circuitbreaker"}}" + expression = "{{getLabelValue $service "backend.circuitbreaker" ""}}" {{end}} {{if hasLabel $service "backend.maxconn.amount"}} [backends."{{$service.Name}}".maxconn] - amount = {{getLabelValue $service "backend.maxconn.amount"}} + amount = {{getLabelValue $service "backend.maxconn.amount" ""}} {{if hasLabel $service "backend.maxconn.extractorfunc"}} - extractorfunc = "{{getLabelValue $service "backend.maxconn.extractorfunc"}}" + extractorfunc = "{{getLabelValue $service "backend.maxconn.extractorfunc" ""}}" {{end}} {{end}} {{range $instance := $partition.Instances}} [backends."{{$service.Name}}".servers."{{$instance.ID}}"] url = "{{getDefaultEndpoint $instance}}" - weight = {{getLabelValueWithDefault $service "backend.weight" "1"}} + weight = {{getLabelValue $service "backend.weight" "1"}} {{end}} {{else if eq $partition.ServiceKind "Stateful"}} {{range $replica := $partition.Replicas}} @@ -98,23 +98,23 @@ const tmpl = ` backend = "{{$service.Name}}" {{if hasLabel $service "frontend.passHostHeader"}} - passHostHeader = {{getLabelValue $service "frontend.passHostHeader" }} + passHostHeader = {{getLabelValue $service "frontend.passHostHeader" ""}} {{end}} {{if hasLabel $service "frontend.whitelistSourceRange"}} - whitelistSourceRange = {{getLabelValue $service "frontend.whitelistSourceRange" }} + whitelistSourceRange = {{getLabelValue $service "frontend.whitelistSourceRange" ""}} {{end}} {{if hasLabel $service "frontend.priority"}} - priority = {{getLabelValue $service "frontend.priority"}} + priority = {{getLabelValue $service "frontend.priority" ""}} {{end}} {{if hasLabel $service "frontend.basicAuth"}} - basicAuth = {{getLabelValue $service "frontend.basicAuth"}} + basicAuth = {{getLabelValue $service "frontend.basicAuth" ""}} {{end}} {{if hasLabel $service "frontend.entryPoints"}} - entryPoints = {{getLabelValue $service "frontend.entryPoints"}} + entryPoints = {{getLabelValue $service "frontend.entryPoints" ""}} {{end}} {{range $key, $value := getLabelsWithPrefix $service "frontend.rule"}} @@ -130,7 +130,7 @@ const tmpl = ` [frontends."{{$service.Name}}/{{$partitionId}}"] backend = "{{getBackendName $service.Name $partition}}" [frontends."{{$service.Name}}/{{$partitionId}}".routes.default] - rule = {{getLabelValue $service "frontend.rule.partition.$partitionId"}} + rule = {{getLabelValue $service "frontend.rule.partition.$partitionId" ""}} {{end}} {{end}}