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

fix: expose as boolean value. #5

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package servicefabric

import "strings"

func hasServiceLabel(service ServiceItemExtended, key string) bool {
_, exists := service.Labels[key]
return exists
}

func getFuncBoolLabel(labelName string) func(service ServiceItemExtended) bool {
return func(service ServiceItemExtended) bool {
return getBoolLabel(service, labelName)
}
}

func getBoolLabel(service ServiceItemExtended, labelName string) bool {
value, exists := service.Labels[labelName]
return exists && strings.EqualFold(strings.TrimSpace(value), "true")
}

func getServiceLabelValue(service ServiceItemExtended, key string) string {
return service.Labels[key]
}
10 changes: 1 addition & 9 deletions servicefabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (p *Provider) updateConfig(configurationChan chan<- types.ConfigMessage, po
"getServiceLabelsWithPrefix": getServiceLabelsWithPrefix,
"getServicesWithLabelValueMap": getServicesWithLabelValueMap,
"getServicesWithLabelValue": getServicesWithLabelValue,
"isExposed": getFuncBoolLabel("expose"),
}

configuration, err := p.GetConfiguration(tmpl, sfFuncMap, templateObjects)
Expand Down Expand Up @@ -235,15 +236,6 @@ func getValidInstances(sfClient sfClient, app sf.ApplicationItem, service sf.Ser
return validInstances
}

func hasServiceLabel(service ServiceItemExtended, key string) bool {
_, exists := service.Labels[key]
return exists
}

func getServiceLabelValue(service ServiceItemExtended, key string) string {
return service.Labels[key]
}

func getServicesWithLabelValueMap(services []ServiceItemExtended, key string) map[string][]ServiceItemExtended {
result := map[string][]ServiceItemExtended{}
for _, service := range services {
Expand Down
12 changes: 7 additions & 5 deletions servicefabric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package servicefabric
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -85,7 +85,7 @@ func TestUpdateConfig(t *testing.T) {
}

labels := map[string]string{
"expose": "",
"expose": "true",
"frontend.rule.default": "Path: /",
"backend.loadbalancer.method": "wrr",
"backend.circuitbreaker": "NetworkErrorRatio() > 0.5",
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestUpdateConfig(t *testing.T) {
if err != nil {
res, _ := json.Marshal(actual)
t.Log(string(res))
t.Error("actual != expected")
t.Error(err)
}
case <-timeout:
t.Error("Provider failed to return configuration")
Expand Down Expand Up @@ -255,7 +255,7 @@ func compareConfigurations(actual, expected types.ConfigMessage) error {
expectedFrontendsStr := string(expectedFrontends)

if actualFrontendsStr != expectedFrontendsStr {
return errors.New("backend configuration differs from expected configuration")
return fmt.Errorf("backend configuration differs from expected configuration: got %q, want %q", actualFrontendsStr, expectedFrontendsStr)
}

actualBackends, err := json.Marshal(actual.Configuration.Backends)
Expand All @@ -274,7 +274,9 @@ func compareConfigurations(actual, expected types.ConfigMessage) error {
}
return nil
}
return fmt.Errorf("backends count differs from expected: got %+v, want %+v", actual.Configuration.Backends, expected.Configuration.Backends)
}
return fmt.Errorf("frontends count differs from expected: got %+v, want %+v", actual.Configuration.Frontends, expected.Configuration.Frontends)
}
return errors.New("provider name differs from expected")
return fmt.Errorf("provider name differs from expected: got %q, want %q", actual.ProviderName, expected.ProviderName)
}
3 changes: 1 addition & 2 deletions servicefabric_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,12 @@ const tmpl = `
{{end}}
{{end}}
{{range $service := .Services}}
{{if hasServiceLabel $service "expose"}}
{{if isExposed $service}}
{{if eq $service.ServiceKind "Stateless"}}

[frontends."{{$service.Name}}"]
backend = "{{$service.Name}}"


{{if hasServiceLabel $service "frontend.passHostHeader"}}
passHostHeader = {{getServiceLabelValue $service "frontend.passHostHeader" }}
{{end}}
Expand Down