-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
manager.go
296 lines (240 loc) · 7.17 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package controller
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/lock"
)
var (
// globalStatus is the global status of all controllers
globalStatus = NewManager()
)
type controllerMap map[string]*Controller
// 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 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) *Controller {
start := time.Now()
// ensure the callbacks are valid
if params.DoFunc == nil {
params.DoFunc = func(ctx context.Context) error {
return undefinedDoFunc(name)
}
}
if params.StopFunc == nil {
params.StopFunc = NoopFunc
}
m.mutex.Lock()
defer m.mutex.Unlock()
if m.controllers == nil {
m.controllers = controllerMap{}
}
ctrl, exists := m.controllers[name]
if exists {
ctrl.getLogger().Debug("Updating existing controller")
ctrl.mutex.Lock()
ctrl.updateParamsLocked(params)
ctrl.mutex.Unlock()
// Notify the goroutine of the params update.
select {
case ctrl.update <- struct{}{}:
default:
}
ctrl.getLogger().Debug("Controller update time: ", time.Since(start))
return ctrl
} else {
return m.createControllerLocked(name, params)
}
}
func (m *Manager) createControllerLocked(name string, params ControllerParams) *Controller {
ctrl := &Controller{
name: name,
uuid: uuid.New().String(),
stop: make(chan struct{}),
update: make(chan struct{}, 1),
trigger: make(chan struct{}, 1),
terminated: make(chan struct{}),
}
ctrl.updateParamsLocked(params)
ctrl.getLogger().Debug("Starting new controller")
if params.Context == nil {
ctrl.ctxDoFunc, ctrl.cancelDoFunc = context.WithCancel(context.Background())
} else {
ctrl.ctxDoFunc, ctrl.cancelDoFunc = context.WithCancel(params.Context)
}
m.controllers[ctrl.name] = ctrl
globalStatus.mutex.Lock()
globalStatus.controllers[ctrl.uuid] = ctrl
globalStatus.mutex.Unlock()
go ctrl.runController()
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 *Controller) {
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) *Controller {
m.mutex.RLock()
defer m.mutex.RUnlock()
if c, ok := m.controllers[name]; ok {
return c
}
return nil
}
func (m *Manager) removeAndReturnController(name string) (*Controller, 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
}
// TerminationChannel returns a channel that is closed after the controller has
// been terminated
func (m *Manager) TerminationChannel(name string) chan struct{} {
if c := m.lookup(name); c != nil {
return c.terminated
}
c := make(chan struct{})
close(c)
return c
}
func (m *Manager) removeAll() []*Controller {
ctrls := []*Controller{}
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) {
controller := m.lookup(name)
if controller == nil {
return
}
controller.Trigger()
}
// 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.
func FakeManager(failingControllers int) *Manager {
m := &Manager{
controllers: controllerMap{},
}
for i := 0; i < failingControllers; i++ {
ctrl := &Controller{
name: fmt.Sprintf("controller-%d", i),
uuid: fmt.Sprintf("%d", i),
stop: make(chan struct{}),
update: make(chan struct{}, 1),
trigger: make(chan struct{}, 1),
terminated: make(chan struct{}),
lastError: fmt.Errorf("controller failed"),
failureCount: 1,
consecutiveErrors: 1,
}
ctrl.ctxDoFunc, ctrl.cancelDoFunc = context.WithCancel(context.Background())
m.controllers[ctrl.name] = ctrl
}
return m
}