Handle condition when backend weights only sometimes present (#33)

Signed-off-by: Arjun Naik <arjun.rn@gmail.com>
This commit is contained in:
Arjun 2019-02-01 10:59:42 +01:00 committed by GitHub
parent 7258cb7800
commit f4efa2898b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 9 deletions

View File

@ -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

View File

@ -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()