Skip to content

Commit

Permalink
Merge pull request #176 from zalando-incubator/ingress-weights-float64
Browse files Browse the repository at this point in the history
Correctly treat ingress weight values as float64
  • Loading branch information
mikkeloscar authored Jul 15, 2020
2 parents 4df21ae + 801e5d7 commit 4bdce4d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
16 changes: 10 additions & 6 deletions pkg/collector/skipper_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ func NewSkipperCollector(client kubernetes.Interface, plugin CollectorPlugin, hp
}, nil
}

func getAnnotationWeight(backendWeights string, backend string) float64 {
var weightsMap map[string]int
func getAnnotationWeight(backendWeights string, backend string) (float64, error) {
var weightsMap map[string]float64
err := json.Unmarshal([]byte(backendWeights), &weightsMap)
if err != nil {
return 0
return 0, err
}
if weight, ok := weightsMap[backend]; ok {
return float64(weight) / 100
return float64(weight) / 100, nil
}
return 0
return 0, nil
}

func getWeights(ingressAnnotations map[string]string, backendAnnotations []string, backend string) (float64, error) {
Expand All @@ -107,7 +107,11 @@ func getWeights(ingressAnnotations map[string]string, backendAnnotations []strin
for _, anno := range backendAnnotations {
if weightsMap, ok := ingressAnnotations[anno]; ok {
annotationsPresent = true
maxWeight = math.Max(maxWeight, getAnnotationWeight(weightsMap, backend))
weight, err := getAnnotationWeight(weightsMap, backend)
if err != nil {
return 0.0, err
}
maxWeight = math.Max(maxWeight, weight)
}
}

Expand Down
32 changes: 16 additions & 16 deletions pkg/collector/skipper_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestSkipperCollector(t *testing.T) {
expectError bool
fakedAverage bool
namespace string
backendWeights map[string]map[string]int
backendWeights map[string]map[string]float64
replicas int32
readyReplicas int32
backendAnnotations []string
Expand All @@ -138,7 +138,7 @@ func TestSkipperCollector(t *testing.T) {
collectedMetric: 1000,
namespace: "default",
backend: "backend1",
backendWeights: map[string]map[string]int{testBackendWeightsAnnotation: {"backend2": 60, "backend1": 40}},
backendWeights: map[string]map[string]float64{testBackendWeightsAnnotation: {"backend2": 60.0, "backend1": 40}},
replicas: 1,
readyReplicas: 1,
backendAnnotations: []string{testBackendWeightsAnnotation},
Expand All @@ -152,7 +152,7 @@ func TestSkipperCollector(t *testing.T) {
collectedMetric: 1000,
namespace: "default",
backend: "backend1",
backendWeights: map[string]map[string]int{testBackendWeightsAnnotation: {"backend2": 60, "backend1": 40}},
backendWeights: map[string]map[string]float64{testBackendWeightsAnnotation: {"backend2": 60, "backend1": 40}},
replicas: 1,
readyReplicas: 1,
backendAnnotations: []string{testBackendWeightsAnnotation},
Expand All @@ -167,7 +167,7 @@ func TestSkipperCollector(t *testing.T) {
fakedAverage: true,
namespace: "default",
backend: "backend1",
backendWeights: map[string]map[string]int{testBackendWeightsAnnotation: {"backend2": 50, "backend1": 50}},
backendWeights: map[string]map[string]float64{testBackendWeightsAnnotation: {"backend2": 50, "backend1": 50}},
replicas: 5,
readyReplicas: 5,
backendAnnotations: []string{testBackendWeightsAnnotation},
Expand All @@ -181,7 +181,7 @@ func TestSkipperCollector(t *testing.T) {
collectedMetric: 1500,
namespace: "default",
backend: "backend1",
backendWeights: map[string]map[string]int{testBackendWeightsAnnotation: {"backend2": 50, "backend1": 50}},
backendWeights: map[string]map[string]float64{testBackendWeightsAnnotation: {"backend2": 50, "backend1": 50}},
replicas: 5, // this is not taken into account
readyReplicas: 5,
backendAnnotations: []string{testBackendWeightsAnnotation},
Expand All @@ -195,7 +195,7 @@ func TestSkipperCollector(t *testing.T) {
collectedMetric: 0,
namespace: "default",
backend: "backend1",
backendWeights: map[string]map[string]int{testBackendWeightsAnnotation: {"backend2": 100, "backend1": 0}},
backendWeights: map[string]map[string]float64{testBackendWeightsAnnotation: {"backend2": 100, "backend1": 0}},
replicas: 5,
readyReplicas: 5,
backendAnnotations: []string{testBackendWeightsAnnotation},
Expand All @@ -210,7 +210,7 @@ func TestSkipperCollector(t *testing.T) {
fakedAverage: true,
namespace: "default",
backend: "backend1",
backendWeights: map[string]map[string]int{
backendWeights: map[string]map[string]float64{
testBackendWeightsAnnotation: {"backend2": 20, "backend1": 80},
testStacksetWeightsAnnotation: {"backend2": 0, "backend1": 100},
},
Expand All @@ -227,7 +227,7 @@ func TestSkipperCollector(t *testing.T) {
collectedMetric: 1500,
namespace: "default",
backend: "backend1",
backendWeights: map[string]map[string]int{
backendWeights: map[string]map[string]float64{
testBackendWeightsAnnotation: {"backend2": 20, "backend1": 80},
testStacksetWeightsAnnotation: {"backend2": 0, "backend1": 100},
},
Expand All @@ -244,7 +244,7 @@ func TestSkipperCollector(t *testing.T) {
collectedMetric: 0,
namespace: "default",
backend: "backend3",
backendWeights: map[string]map[string]int{testBackendWeightsAnnotation: {"backend2": 100, "backend1": 0}},
backendWeights: map[string]map[string]float64{testBackendWeightsAnnotation: {"backend2": 100, "backend1": 0}},
replicas: 1,
readyReplicas: 1,
backendAnnotations: []string{testBackendWeightsAnnotation},
Expand All @@ -258,7 +258,7 @@ func TestSkipperCollector(t *testing.T) {
collectedMetric: 1500,
namespace: "default",
backend: "backend3",
backendWeights: map[string]map[string]int{},
backendWeights: map[string]map[string]float64{},
replicas: 1,
readyReplicas: 1,
backendAnnotations: []string{testBackendWeightsAnnotation},
Expand All @@ -272,7 +272,7 @@ func TestSkipperCollector(t *testing.T) {
expectError: true,
namespace: "default",
backend: "",
backendWeights: map[string]map[string]int{testBackendWeightsAnnotation: {"backend2": 100, "backend1": 0}},
backendWeights: map[string]map[string]float64{testBackendWeightsAnnotation: {"backend2": 100, "backend1": 0}},
replicas: 1,
readyReplicas: 1,
backendAnnotations: []string{testBackendWeightsAnnotation},
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestSkipperCollector(t *testing.T) {
fakedAverage: true,
namespace: "default",
backend: "backend2",
backendWeights: map[string]map[string]int{
backendWeights: map[string]map[string]float64{
testBackendWeightsAnnotation: {"backend2": 20, "backend1": 80},
testStacksetWeightsAnnotation: {"backend1": 100},
},
Expand All @@ -314,12 +314,12 @@ func TestSkipperCollector(t *testing.T) {
metric: 1500,
ingressName: "dummy-ingress",
hostnames: []string{"example.org"},
expectedQuery: `scalar(sum(rate(skipper_serve_host_duration_seconds_count{host=~"example_org"}[1m])) * 0.2000)`,
expectedQuery: `scalar(sum(rate(skipper_serve_host_duration_seconds_count{host=~"example_org"}[1m])) * 0.2050)`,
collectedMetric: 1500,
namespace: "default",
backend: "backend2",
backendWeights: map[string]map[string]int{
testBackendWeightsAnnotation: {"backend2": 20, "backend1": 80},
backendWeights: map[string]map[string]float64{
testBackendWeightsAnnotation: {"backend2": 20.5, "backend1": 79.5},
testStacksetWeightsAnnotation: {"backend1": 100},
},
replicas: 5,
Expand Down Expand Up @@ -352,7 +352,7 @@ func TestSkipperCollector(t *testing.T) {
}
}

func makeIngress(client kubernetes.Interface, namespace, ingressName, backend string, hostnames []string, backendWeights map[string]map[string]int) error {
func makeIngress(client kubernetes.Interface, namespace, ingressName, backend string, hostnames []string, backendWeights map[string]map[string]float64) error {
annotations := make(map[string]string)
for anno, weights := range backendWeights {
sWeights, err := json.Marshal(weights)
Expand Down

0 comments on commit 4bdce4d

Please sign in to comment.