forked from flant/shell-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
338 lines (282 loc) · 9.35 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package kube_events_manager
import (
"context"
"fmt"
"sort"
log "github.com/sirupsen/logrus"
klient "github.com/flant/kube-client/client"
. "github.com/flant/shell-operator/pkg/kube_events_manager/types"
"github.com/flant/shell-operator/pkg/metric_storage"
utils "github.com/flant/shell-operator/pkg/utils/labels"
)
type Monitor interface {
WithContext(ctx context.Context)
WithKubeClient(client klient.Client)
WithMetricStorage(mstor *metric_storage.MetricStorage)
WithConfig(config *MonitorConfig)
WithKubeEventCb(eventCb func(KubeEvent))
CreateInformers() error
Start(context.Context)
Stop()
PauseHandleEvents()
Snapshot() []ObjectAndFilterResult
EnableKubeEventCb()
GetConfig() *MonitorConfig
SnapshotOperations() (total *CachedObjectsInfo, last *CachedObjectsInfo)
}
// Monitor holds informers for resources and a namespace informer
type monitor struct {
Name string
Config *MonitorConfig
KubeClient klient.Client
// Static list of informers
ResourceInformers []ResourceInformer
// Namespace informer to get new namespaces
NamespaceInformer NamespaceInformer
// map of dynamically starting informers
VaryingInformers map[string][]ResourceInformer
eventCb func(KubeEvent)
eventsEnabled bool
// Index of namespaces statically defined in monitor configuration
staticNamespaces map[string]bool
cancelForNs map[string]context.CancelFunc
ctx context.Context
cancel context.CancelFunc
metricStorage *metric_storage.MetricStorage
}
var NewMonitor = func() Monitor {
return &monitor{
ResourceInformers: make([]ResourceInformer, 0),
VaryingInformers: make(map[string][]ResourceInformer),
cancelForNs: make(map[string]context.CancelFunc),
staticNamespaces: make(map[string]bool),
}
}
func (m *monitor) WithContext(ctx context.Context) {
m.ctx, m.cancel = context.WithCancel(ctx)
}
func (m *monitor) WithKubeClient(client klient.Client) {
m.KubeClient = client
}
func (m *monitor) WithMetricStorage(mstor *metric_storage.MetricStorage) {
m.metricStorage = mstor
}
func (m *monitor) WithConfig(config *MonitorConfig) {
m.Config = config
}
func (m *monitor) GetConfig() *MonitorConfig {
return m.Config
}
func (m *monitor) WithKubeEventCb(eventCb func(KubeEvent)) {
m.eventCb = eventCb
}
// CreateInformers creates all informers and
// a namespace informer if namespace.labelSelector is defined.
// If MonitorConfig.NamespaceSelector.MatchNames is defined, then
// multiple informers are created for each namespace.
// If no NamespaceSelector defined, then one informer is created.
func (m *monitor) CreateInformers() error {
logEntry := log.
WithFields(utils.LabelsToLogFields(m.Config.Metadata.LogLabels)).
WithField("binding.name", m.Config.Metadata.DebugName)
if m.Config.Kind == "" && m.Config.ApiVersion == "" {
logEntry.Debugf("Create Informers for Config with empty kind and apiVersion: %+v", m.Config)
return nil
}
logEntry.Debugf("Create Informers Config: %+v", m.Config)
nsNames := m.Config.Namespaces()
if len(nsNames) > 0 {
logEntry.Debugf("create static ResourceInformers")
// create informers for each specified object name in each specified namespace
// This list of informers is static.
for _, nsName := range nsNames {
if nsName != "" {
m.staticNamespaces[nsName] = true
}
informers, err := m.CreateInformersForNamespace(nsName)
if err != nil {
return err
}
m.ResourceInformers = append(m.ResourceInformers, informers...)
}
}
if m.Config.NamespaceSelector != nil && m.Config.NamespaceSelector.LabelSelector != nil {
logEntry.Debugf("Create NamespaceInformer for namespace.labelSelector")
m.NamespaceInformer = NewNamespaceInformer(m.Config)
m.NamespaceInformer.WithContext(m.ctx)
m.NamespaceInformer.WithKubeClient(m.KubeClient)
err := m.NamespaceInformer.CreateSharedInformer(
func(nsName string) {
// Added/Modified event: check, create and run informers for Ns
// ignore event if namespace is already has static ResourceInformers
if _, ok := m.staticNamespaces[nsName]; ok {
return
}
// ignore already started informers
_, ok := m.VaryingInformers[nsName]
if ok {
return
}
logEntry.Infof("got ns/%s, create dynamic ResourceInformers", nsName)
var err error
m.VaryingInformers[nsName], err = m.CreateInformersForNamespace(nsName)
if err != nil {
logEntry.Errorf("create ResourceInformers for ns/%s: %v", nsName, err)
}
var ctx context.Context
ctx, m.cancelForNs[nsName] = context.WithCancel(m.ctx)
for _, informer := range m.VaryingInformers[nsName] {
informer.WithContext(ctx)
if m.eventsEnabled {
informer.EnableKubeEventCb()
}
informer.Start()
}
},
func(nsName string) {
// Delete event: check, stop and remove informers for Ns
logEntry.Infof("deleted ns/%s, stop dynamic ResourceInformers", nsName)
// ignore statically specified namespaces
if _, ok := m.staticNamespaces[nsName]; ok {
return
}
// ignore already stopped informers
_, ok := m.cancelForNs[nsName]
if !ok {
return
}
m.cancelForNs[nsName]()
// TODO wait
delete(m.VaryingInformers, nsName)
delete(m.cancelForNs, nsName)
},
)
if err != nil {
return fmt.Errorf("create namespace informer: %v", err)
}
for nsName := range m.NamespaceInformer.GetExistedObjects() {
logEntry.Infof("got ns/%s, create dynamic ResourceInformers", nsName)
// ignore event if namespace is already has static ResourceInformers
if _, ok := m.staticNamespaces[nsName]; ok {
continue
}
var err error
m.VaryingInformers[nsName], err = m.CreateInformersForNamespace(nsName)
if err != nil {
logEntry.Errorf("create ResourceInformers for ns/%s: %v", nsName, err)
}
}
}
return nil
}
// Snapshot returns all existed objects from all created informers
func (m *monitor) Snapshot() []ObjectAndFilterResult {
objects := make([]ObjectAndFilterResult, 0)
for _, informer := range m.ResourceInformers {
objects = append(objects, informer.CachedObjects()...)
}
for nsName := range m.VaryingInformers {
for _, informer := range m.VaryingInformers[nsName] {
objects = append(objects, informer.CachedObjects()...)
}
}
// Sort objects by namespace and name
sort.Sort(ByNamespaceAndName(objects))
return objects
}
// EnableKubeEventCb allows execution of event callback for all informers.
// Also executes eventCb for events accumulated during "Synchronization" phase.
func (m *monitor) EnableKubeEventCb() {
for _, informer := range m.ResourceInformers {
informer.EnableKubeEventCb()
}
for nsName := range m.VaryingInformers {
for _, informer := range m.VaryingInformers[nsName] {
informer.EnableKubeEventCb()
}
}
// Enable events for future VaryingInformers.
m.eventsEnabled = true
}
// CreateInformersForNamespace creates informers bounded to the namespace. If no matchName is specified,
// it is only one informer. If matchName is specified, then multiple informers are created.
//
// If namespace is empty, then informer is bounded to all namespaces.
func (m *monitor) CreateInformersForNamespace(namespace string) (informers []ResourceInformer, err error) {
informers = make([]ResourceInformer, 0)
objNames := []string{""}
if len(m.Config.Names()) > 0 {
objNames = m.Config.Names()
}
for _, objName := range objNames {
informer := NewResourceInformer(m.Config)
informer.WithKubeClient(m.KubeClient)
informer.WithMetricStorage(m.metricStorage)
informer.WithNamespace(namespace)
informer.WithName(objName)
informer.WithKubeEventCb(m.eventCb)
err := informer.CreateSharedInformer()
if err != nil {
return nil, err
}
informers = append(informers, informer)
}
return informers, nil
}
// Start calls Run on all informers.
func (m *monitor) Start(parentCtx context.Context) {
m.ctx, m.cancel = context.WithCancel(parentCtx)
for _, informer := range m.ResourceInformers {
informer.WithContext(m.ctx)
informer.Start()
}
for nsName := range m.VaryingInformers {
var ctx context.Context
ctx, m.cancelForNs[nsName] = context.WithCancel(m.ctx)
for _, informer := range m.VaryingInformers[nsName] {
informer.WithContext(ctx)
informer.Start()
}
}
if m.NamespaceInformer != nil {
m.NamespaceInformer.WithContext(m.ctx)
m.NamespaceInformer.Start()
}
}
// Stop stops all informers
func (m *monitor) Stop() {
if m.cancel != nil {
m.cancel()
}
}
// PauseHandleEvents set flags for all informers to ignore incoming events.
// Useful for shutdown without panicking.
// Calling cancel() leads to a race and panicking, see https://github.com/kubernetes/kubernetes/issues/59822
func (m *monitor) PauseHandleEvents() {
for _, informer := range m.ResourceInformers {
informer.PauseHandleEvents()
}
for _, informers := range m.VaryingInformers {
for _, informer := range informers {
informer.PauseHandleEvents()
}
}
if m.NamespaceInformer != nil {
m.NamespaceInformer.PauseHandleEvents()
}
}
func (m *monitor) SnapshotOperations() (total *CachedObjectsInfo, last *CachedObjectsInfo) {
total = &CachedObjectsInfo{}
last = &CachedObjectsInfo{}
for _, informer := range m.ResourceInformers {
total.Add(informer.CachedObjectsInfo())
last.Add(informer.CachedObjectsInfoIncrement())
}
for nsName := range m.VaryingInformers {
for _, informer := range m.VaryingInformers[nsName] {
total.Add(informer.CachedObjectsInfo())
last.Add(informer.CachedObjectsInfoIncrement())
}
}
return total, last
}