-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
manager.go
374 lines (307 loc) · 9.43 KB
/
manager.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package controller
import (
"context"
"fmt"
"github.com/go-openapi/strfmt"
"github.com/google/uuid"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/time"
)
var (
// globalStatus is the global status of all controllers
globalStatus = NewManager()
)
type controllerMap map[string]*managedController
// Manager is a list of controllers
type Manager struct {
controllers controllerMap
mutex lock.RWMutex
}
// NewManager allocates a new manager
func NewManager() *Manager {
return &Manager{
controllers: controllerMap{},
}
}
// GetGlobalStatus returns the status of all controllers
func GetGlobalStatus() models.ControllerStatuses {
return globalStatus.GetStatusModel()
}
// UpdateController installs or updates a controller in the
// manager. A controller is primarily identified by its name.
// If a controller with the name already exists, the controller
// will be shut down and replaced with the provided controller.
//
// Updating a controller will cause the DoFunc to be run immediately regardless
// of any previous conditions. It will also cause any statistics to be reset.
func (m *Manager) UpdateController(name string, params ControllerParams) {
m.updateController(name, params)
}
func (m *Manager) updateController(name string, params ControllerParams) *managedController {
start := time.Now()
m.mutex.Lock()
defer m.mutex.Unlock()
if m.controllers == nil {
m.controllers = controllerMap{}
}
if params.Group.Name == "" {
log.Errorf(
"Controller initialized with unpopulated group information. " +
"Metrics will not be exported for this controller.")
}
ctrl, exists := m.controllers[name]
if exists {
ctrl.getLogger().Debug("Updating existing controller")
ctrl.updateParamsLocked(params)
// Notify the goroutine of the params update.
select {
case ctrl.update <- ctrl.params:
default:
}
ctrl.getLogger().Debug("Controller update time: ", time.Since(start))
} else {
return m.createControllerLocked(name, params)
}
return ctrl
}
func (m *Manager) createControllerLocked(name string, params ControllerParams) *managedController {
ctrl := &managedController{
controller: controller{
name: name,
group: params.Group,
uuid: uuid.New().String(),
stop: make(chan struct{}),
update: make(chan ControllerParams, 1),
trigger: make(chan struct{}, 1),
terminated: make(chan struct{}),
},
}
ctrl.updateParamsLocked(params)
ctrl.getLogger().Debug("Starting new controller")
m.controllers[ctrl.name] = ctrl
globalStatus.mutex.Lock()
globalStatus.controllers[ctrl.uuid] = ctrl
globalStatus.mutex.Unlock()
go ctrl.runController(ctrl.params)
return ctrl
}
// CreateController installs a new controller in the
// manager. If a controller with the name already exists
// this method returns false without triggering, otherwise
// creates the controller and runs it immediately.
func (m *Manager) CreateController(name string, params ControllerParams) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.controllers != nil {
if _, exists := m.controllers[name]; exists {
return false
}
} else {
m.controllers = controllerMap{}
}
m.createControllerLocked(name, params)
return true
}
func (m *Manager) removeController(ctrl *managedController) {
ctrl.stopController()
delete(m.controllers, ctrl.name)
globalStatus.mutex.Lock()
delete(globalStatus.controllers, ctrl.uuid)
globalStatus.mutex.Unlock()
ctrl.getLogger().Debug("Removed controller")
}
func (m *Manager) lookup(name string) *managedController {
m.mutex.RLock()
defer m.mutex.RUnlock()
if c, ok := m.controllers[name]; ok {
return c
}
return nil
}
func (m *Manager) removeAndReturnController(name string) (*managedController, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.controllers == nil {
return nil, fmt.Errorf("empty controller map")
}
oldCtrl, ok := m.controllers[name]
if !ok {
return nil, fmt.Errorf("unable to find controller %s", name)
}
m.removeController(oldCtrl)
return oldCtrl, nil
}
// RemoveController stops and removes a controller from the manager. If DoFunc
// is currently running, DoFunc is allowed to complete in the background.
func (m *Manager) RemoveController(name string) error {
_, err := m.removeAndReturnController(name)
return err
}
// RemoveControllerAndWait stops and removes a controller using
// RemoveController() and then waits for it to run to completion.
func (m *Manager) RemoveControllerAndWait(name string) error {
oldCtrl, err := m.removeAndReturnController(name)
if err == nil {
<-oldCtrl.terminated
}
return err
}
func (m *Manager) removeAll() []*managedController {
ctrls := []*managedController{}
m.mutex.Lock()
defer m.mutex.Unlock()
if m.controllers == nil {
return ctrls
}
for _, ctrl := range m.controllers {
m.removeController(ctrl)
ctrls = append(ctrls, ctrl)
}
return ctrls
}
// RemoveAll stops and removes all controllers of the manager
func (m *Manager) RemoveAll() {
m.removeAll()
}
// RemoveAllAndWait stops and removes all controllers of the manager and then
// waits for all controllers to exit
func (m *Manager) RemoveAllAndWait() {
ctrls := m.removeAll()
for _, ctrl := range ctrls {
<-ctrl.terminated
}
}
// GetStatusModel returns the status of all controllers as models.ControllerStatuses
func (m *Manager) GetStatusModel() models.ControllerStatuses {
// Create a copy of pointers to current controller so we can unlock the
// manager mutex quickly again
controllers := controllerMap{}
m.mutex.RLock()
for key, c := range m.controllers {
controllers[key] = c
}
m.mutex.RUnlock()
statuses := models.ControllerStatuses{}
for _, c := range controllers {
statuses = append(statuses, c.GetStatusModel())
}
return statuses
}
// TriggerController triggers the controller with the specified name.
func (m *Manager) TriggerController(name string) {
ctrl := m.lookup(name)
if ctrl == nil {
return
}
select {
case ctrl.trigger <- struct{}{}:
default:
}
}
// FakeManager returns a fake controller manager with the specified number of
// failing controllers. The returned manager is identical in any regard except
// for internal pointers.
// Used for testing only.
func FakeManager(failingControllers int) *Manager {
m := &Manager{
controllers: controllerMap{},
}
for i := 0; i < failingControllers; i++ {
ctrl := &managedController{
controller: controller{
name: fmt.Sprintf("controller-%d", i),
uuid: fmt.Sprintf("%d", i),
stop: make(chan struct{}),
update: make(chan ControllerParams, 1),
trigger: make(chan struct{}, 1),
terminated: make(chan struct{}),
lastError: fmt.Errorf("controller failed"),
failureCount: 1,
consecutiveErrors: 1,
},
}
ctrl.params.Context, ctrl.cancelDoFunc = context.WithCancel(context.Background())
m.controllers[ctrl.name] = ctrl
}
return m
}
type managedController struct {
controller
params ControllerParams
cancelDoFunc context.CancelFunc
}
// updateParamsLocked sanitizes and sets the controller's parameters.
//
// If the RunInterval exceeds ControllerMaxInterval, it will be capped.
//
// Manager's mutex must be held
func (c *managedController) updateParamsLocked(params ControllerParams) {
// ensure the callbacks are valid
if params.DoFunc == nil {
params.DoFunc = func(ctx context.Context) error {
return undefinedDoFunc(c.name)
}
}
if params.StopFunc == nil {
params.StopFunc = NoopFunc
}
// Enforce max controller interval
maxInterval := time.Duration(option.Config.MaxControllerInterval) * time.Second
if maxInterval > 0 && params.RunInterval > maxInterval {
c.getLogger().Infof("Limiting interval to %s", maxInterval)
params.RunInterval = maxInterval
}
// Save current context on update if not canceling
ctx := c.params.Context
// Check if the current context needs to be cancelled
if c.params.CancelDoFuncOnUpdate && c.cancelDoFunc != nil {
c.cancelDoFunc()
c.params.Context = nil
}
// (re)set the context as the previous might have been cancelled
if c.params.Context == nil {
if params.Context == nil {
ctx, c.cancelDoFunc = context.WithCancel(context.Background())
} else {
ctx, c.cancelDoFunc = context.WithCancel(params.Context)
}
}
c.params = params
c.params.Context = ctx
}
func (c *managedController) stopController() {
if c.cancelDoFunc != nil {
c.cancelDoFunc()
}
close(c.stop)
}
// GetStatusModel returns a models.ControllerStatus representing the
// controller's configuration & status
func (c *managedController) GetStatusModel() *models.ControllerStatus {
c.mutex.RLock()
defer c.mutex.RUnlock()
status := &models.ControllerStatus{
Name: c.name,
UUID: strfmt.UUID(c.uuid),
Configuration: &models.ControllerStatusConfiguration{
ErrorRetry: !c.params.NoErrorRetry,
ErrorRetryBase: strfmt.Duration(c.params.ErrorRetryBaseDuration),
Interval: strfmt.Duration(c.params.RunInterval),
},
Status: &models.ControllerStatusStatus{
SuccessCount: int64(c.successCount),
LastSuccessTimestamp: strfmt.DateTime(c.lastSuccessStamp),
FailureCount: int64(c.failureCount),
LastFailureTimestamp: strfmt.DateTime(c.lastErrorStamp),
ConsecutiveFailureCount: int64(c.consecutiveErrors),
},
}
if c.lastError != nil {
status.Status.LastFailureMsg = c.lastError.Error()
}
return status
}