/
valueinrange.go
75 lines (64 loc) · 1.84 KB
/
valueinrange.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package anomalydetector
import (
"fmt"
"github.com/amadeusitgroup/kanary/pkg/pod"
kapiv1 "k8s.io/api/core/v1"
)
var _ AnomalyDetector = &ValueInRangeAnalyser{}
//inRangeByPodName true means in range
type inRangeByPodName map[string]bool
type valueInRangeAnalyser interface {
doAnalysis() (inRangeByPodName, error)
}
//ValueInRangeConfig Configuration for ValueInRangeAnalyser
type ValueInRangeConfig struct {
Min float64
Max float64
}
//ValueInRangeAnalyser anomalyDetector that check the deviation of a continous value compare to average
type ValueInRangeAnalyser struct {
ConfigSpecific ValueInRangeConfig
ConfigAnalyser Config
analyser valueInRangeAnalyser
}
//GetPodsOutOfBounds implements interface AnomalyDetector
func (d *ValueInRangeAnalyser) GetPodsOutOfBounds() ([]*kapiv1.Pod, error) {
listOfPods, err := d.ConfigAnalyser.PodLister.List(d.ConfigAnalyser.Selector)
if err != nil {
return nil, fmt.Errorf("can't list pods, error:%v", err)
}
listOfPods, err = pod.PurgeNotReadyPods(listOfPods)
if err != nil {
return nil, fmt.Errorf("can't purge not ready pods, error:%v", err)
}
podByName, podWithNoTraffic, err := PodByName(listOfPods, d.ConfigAnalyser.ExclusionFunc)
if err != nil {
return nil, err
}
result := []*kapiv1.Pod{}
inRangeByPods, err := d.analyser.doAnalysis()
if err != nil {
return nil, err
}
//check if the key is GlobalKeyQuery that means that the result is applicable to all pods
if len(inRangeByPods) == 1 {
if v, ok := inRangeByPods[GlobalQueryKey]; ok {
for _, pod := range podByName {
inRangeByPods[pod.Name] = v
}
delete(inRangeByPods, GlobalQueryKey)
}
}
for podName, inRange := range inRangeByPods {
_, found := podWithNoTraffic[podName]
if found {
continue
}
if !inRange {
if p, ok := podByName[podName]; ok {
result = append(result, p)
}
}
}
return result, nil
}