forked from mittwald/kubernetes-loadwatcher
/
watcher_run.go
63 lines (52 loc) · 1.56 KB
/
watcher_run.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
package pressurecooker
import (
"fmt"
"github.com/golang/glog"
"time"
)
func (w *Watcher) SetAsHigh(high bool) {
w.isCurrentlyHigh = high
}
func (w *Watcher) Run(closeChan chan struct{}) (<-chan PressureThresholdEvent, <-chan PressureThresholdEvent, <-chan error) {
exceeded := make(chan PressureThresholdEvent)
deceeded := make(chan PressureThresholdEvent)
errs := make(chan error)
ticker := time.Tick(w.TickerInterval)
go func() {
defer func() {
close(exceeded)
close(deceeded)
close(errs)
}()
for {
select {
case <-ticker:
cpu, err := w.proc.PSIStatsForResource("cpu")
if err != nil {
errs <- err
continue
}
if cpu.Some == nil {
errs <- fmt.Errorf("could not load cpu pressure, got %v", cpu)
continue
}
glog.Infof("current state: high_load=%t avg10=%.2f avg60=%.2f avg300=%.2f threshold=%.2f",
w.isCurrentlyHigh, cpu.Some.Avg10, cpu.Some.Avg60, cpu.Some.Avg300, w.PressureThreshold)
if cpu.Some.Avg300 >= w.PressureThreshold {
if !w.isCurrentlyHigh {
w.isCurrentlyHigh = true
exceeded <- PressureThresholdEvent(*cpu.Some)
} else if cpu.Some.Avg60 >= w.PressureThreshold && cpu.Some.Avg10 >= w.PressureThreshold {
exceeded <- PressureThresholdEvent(*cpu.Some)
}
} else if cpu.Some.Avg300 < w.PressureThreshold && cpu.Some.Avg60 < w.PressureThreshold && cpu.Some.Avg10 < w.PressureThreshold {
w.isCurrentlyHigh = false
deceeded <- PressureThresholdEvent(*cpu.Some)
}
case <-closeChan:
return
}
}
}()
return exceeded, deceeded, errs
}