From f4efa2898be17cc4482702a363136f0778551869 Mon Sep 17 00:00:00 2001 From: Arjun Date: Fri, 1 Feb 2019 10:59:42 +0100 Subject: [PATCH] Handle condition when backend weights only sometimes present (#33) Signed-off-by: Arjun Naik --- pkg/collector/skipper_collector.go | 20 ++++++++++++-------- pkg/collector/skipper_collector_test.go | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/pkg/collector/skipper_collector.go b/pkg/collector/skipper_collector.go index 751a695..4c60c6a 100644 --- a/pkg/collector/skipper_collector.go +++ b/pkg/collector/skipper_collector.go @@ -81,29 +81,33 @@ func NewSkipperCollector(client kubernetes.Interface, plugin CollectorPlugin, hp }, nil } -func getAnnotationWeight(backendWeights string, backend string) float64 { +func getAnnotationWeight(backendWeights string, backend string) (float64, bool) { var weightsMap map[string]int err := json.Unmarshal([]byte(backendWeights), &weightsMap) if err != nil { - return 1 + return 0, false } if weight, ok := weightsMap[backend]; ok { - return float64(weight) / 100 + return float64(weight) / 100, true } - return 1 + return 0, false } func getWeights(ingressAnnotations map[string]string, backendAnnotations []string, backend string) float64 { var maxWeight float64 = -1 + weightSet := false for _, anno := range backendAnnotations { if weightsMap, ok := ingressAnnotations[anno]; ok { - weight := getAnnotationWeight(weightsMap, backend) - if weight > maxWeight { - maxWeight = weight + weight, isPresent := getAnnotationWeight(weightsMap, backend) + if isPresent { + weightSet = true + if weight > maxWeight { + maxWeight = weight + } } } } - if maxWeight >= 0 { + if weightSet { return maxWeight } return 1.0 diff --git a/pkg/collector/skipper_collector_test.go b/pkg/collector/skipper_collector_test.go index 40281fa..f465433 100644 --- a/pkg/collector/skipper_collector_test.go +++ b/pkg/collector/skipper_collector_test.go @@ -180,12 +180,27 @@ func TestSkipperCollector(t *testing.T) { ingressName: "dummy-ingress", collectedMetric: 1500, namespace: "default", - backend: "", + backend: "backend3", backendWeights: map[string]map[string]int{testBackendWeightsAnnotation: {"backend2": 100, "backend1": 0}}, replicas: 1, readyReplicas: 1, backendAnnotations: []string{testBackendWeightsAnnotation}, }, + { + msg: "test partial backend annotations", + metrics: []int{100, 1500, 700}, + ingressName: "dummy-ingress", + collectedMetric: 60, + namespace: "default", + backend: "backend2", + backendWeights: map[string]map[string]int{ + testBackendWeightsAnnotation: {"backend2": 20, "backend1": 80}, + testStacksetWeightsAnnotation: {"backend1": 100}, + }, + replicas: 5, + readyReplicas: 5, + backendAnnotations: []string{testBackendWeightsAnnotation, testStacksetWeightsAnnotation}, + }, } { t.Run(tc.msg, func(tt *testing.T) { client := fake.NewSimpleClientset()