Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
refactor: generic template functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Dec 14, 2017
1 parent bbaf273 commit c01c1ef
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 58 deletions.
30 changes: 13 additions & 17 deletions labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package servicefabric

import (
"strconv"
"strings"
)

func getFuncBoolLabel(labelName string, defaultValue bool) func(service ServiceItemExtended) bool {
Expand All @@ -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
}
Expand Down
41 changes: 15 additions & 26 deletions servicefabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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]
Expand All @@ -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"
Expand Down
30 changes: 15 additions & 15 deletions servicefabric_tmpl.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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}}
{{range $partition := $service.Partitions}}
{{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}}
Expand All @@ -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"}}
Expand All @@ -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}}
Expand Down Expand Up @@ -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"}}
Expand All @@ -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}}
Expand Down

0 comments on commit c01c1ef

Please sign in to comment.