-
Notifications
You must be signed in to change notification settings - Fork 53
/
monitor.go
210 lines (194 loc) · 4.68 KB
/
monitor.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package health
import (
"context"
"sync"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/pkg/health/annotations"
"github.com/rancher/opni/pkg/util"
"go.uber.org/zap"
)
type Monitor struct {
MonitorOptions
mu sync.Mutex
currentHealth map[string]*corev1.Health
currentStatus map[string]*corev1.Status
healthListeners []chan *corev1.ClusterHealth
statusListeners []chan *corev1.ClusterStatus
}
type MonitorOptions struct {
lg *zap.SugaredLogger
}
type MonitorOption func(*MonitorOptions)
func (o *MonitorOptions) apply(opts ...MonitorOption) {
for _, op := range opts {
op(o)
}
}
func WithLogger(lg *zap.SugaredLogger) MonitorOption {
return func(o *MonitorOptions) {
o.lg = lg
}
}
func NewMonitor(opts ...MonitorOption) *Monitor {
options := MonitorOptions{
lg: zap.NewNop().Sugar(),
}
options.apply(opts...)
return &Monitor{
MonitorOptions: options,
currentHealth: make(map[string]*corev1.Health),
currentStatus: make(map[string]*corev1.Status),
}
}
func (m *Monitor) Run(ctx context.Context, updater HealthStatusUpdater) {
defer func() {
m.mu.Lock()
defer m.mu.Unlock()
m.currentHealth = make(map[string]*corev1.Health)
m.currentStatus = make(map[string]*corev1.Status)
for _, l := range m.healthListeners {
close(l)
}
for _, s := range m.statusListeners {
close(s)
}
m.healthListeners = nil
m.statusListeners = nil
}()
for {
select {
case <-ctx.Done():
return
case update, ok := <-updater.HealthC():
if !ok {
m.lg.Debug("health update channel closed")
return
}
m.mu.Lock()
m.lg.With(
"id", update.ID,
"ready", update.Health.Ready,
"conditions", update.Health.Conditions,
).With(
annotations.KeyValuePairs(update.Health.GetAnnotations())...,
).Info("received health update")
m.currentHealth[update.ID] = update.Health
for _, ch := range m.healthListeners {
ch <- &corev1.ClusterHealth{
Cluster: &corev1.Reference{
Id: update.ID,
},
Health: util.ProtoClone(update.Health),
}
}
m.mu.Unlock()
case update, ok := <-updater.StatusC():
if !ok {
m.lg.Debug("status update channel closed")
return
}
m.mu.Lock()
m.lg.With(
"id", update.ID,
"connected", update.Status.Connected,
).Info("received status update")
m.currentStatus[update.ID] = update.Status
for _, ch := range m.statusListeners {
ch <- &corev1.ClusterStatus{
Cluster: &corev1.Reference{
Id: update.ID,
},
Status: util.ProtoClone(update.Status),
}
}
m.mu.Unlock()
}
}
}
func (m *Monitor) GetHealthStatus(id string) *corev1.HealthStatus {
m.mu.Lock()
defer m.mu.Unlock()
return &corev1.HealthStatus{
Health: util.ProtoClone(m.currentHealth[id]),
Status: util.ProtoClone(m.currentStatus[id]),
}
}
func (m *Monitor) WatchHealthStatus(ctx context.Context) <-chan *corev1.ClusterHealthStatus {
m.mu.Lock()
ch := make(chan *corev1.ClusterHealthStatus, 100)
hl := make(chan *corev1.ClusterHealth, 10)
sl := make(chan *corev1.ClusterStatus, 10)
m.healthListeners = append(m.healthListeners, hl)
m.statusListeners = append(m.statusListeners, sl)
curHealth := make(map[string]*corev1.Health, len(m.currentHealth))
curStatus := make(map[string]*corev1.Status, len(m.currentStatus))
for k, v := range m.currentHealth {
curHealth[k] = util.ProtoClone(v)
}
for k, v := range m.currentStatus {
curStatus[k] = util.ProtoClone(v)
}
m.mu.Unlock()
go func() {
for id, h := range curHealth {
s := curStatus[id]
ch <- &corev1.ClusterHealthStatus{
Cluster: &corev1.Reference{
Id: id,
},
HealthStatus: &corev1.HealthStatus{
Health: util.ProtoClone(h),
Status: util.ProtoClone(s),
},
}
}
LOOP:
for {
var ref *corev1.Reference
select {
case <-ctx.Done():
break LOOP
case health, ok := <-hl:
if !ok {
break LOOP
}
ref = health.Cluster
curHealth[ref.Id] = health.Health
case status, ok := <-sl:
if !ok {
break LOOP
}
ref = status.Cluster
curStatus[ref.Id] = status.Status
}
hs := &corev1.ClusterHealthStatus{
Cluster: ref,
HealthStatus: &corev1.HealthStatus{
Health: util.ProtoClone(curHealth[ref.Id]),
Status: util.ProtoClone(curStatus[ref.Id]),
},
}
select {
case <-ctx.Done():
case ch <- hs:
}
}
m.mu.Lock()
defer m.mu.Unlock()
for i, h := range m.healthListeners {
if h == hl {
m.healthListeners = append(m.healthListeners[:i], m.healthListeners[i+1:]...)
close(hl)
break
}
}
for i, s := range m.statusListeners {
if s == sl {
m.statusListeners = append(m.statusListeners[:i], m.statusListeners[i+1:]...)
close(sl)
break
}
}
}()
return ch
}