@@ -58,8 +58,12 @@ type controllerManager struct {
58
58
// to scheme.scheme.
59
59
scheme * runtime.Scheme
60
60
61
- // runnables is the set of Controllers that the controllerManager injects deps into and Starts.
62
- runnables []Runnable
61
+ // leaderElectionRunnables is the set of Controllers that the controllerManager injects deps into and Starts.
62
+ // These Runnables are managed by lead election.
63
+ leaderElectionRunnables []Runnable
64
+ // nonLeaderElectionRunnables is the set of webhook servers that the controllerManager injects deps into and Starts.
65
+ // These Runnables are in HA mode (not blocked by lead election)
66
+ nonLeaderElectionRunnables []Runnable
63
67
64
68
cache cache.Cache
65
69
@@ -121,7 +125,7 @@ type controllerManager struct {
121
125
retryPeriod time.Duration
122
126
}
123
127
124
- // Add sets dependencies on i, and adds it to the list of runnables to start.
128
+ // Add sets dependencies on i, and adds it to the list of Runnables to start.
125
129
func (cm * controllerManager ) Add (r Runnable ) error {
126
130
cm .mu .Lock ()
127
131
defer cm .mu .Unlock ()
@@ -131,8 +135,13 @@ func (cm *controllerManager) Add(r Runnable) error {
131
135
return err
132
136
}
133
137
134
- // Add the runnable to the list
135
- cm .runnables = append (cm .runnables , r )
138
+ // Add the runnable to the leader election or the non-leaderelection list
139
+ if leRunnable , ok := r .(LeaderElectionRunnable ); ok && ! leRunnable .NeedLeaderElection () {
140
+ cm .nonLeaderElectionRunnables = append (cm .nonLeaderElectionRunnables , r )
141
+ } else {
142
+ cm .leaderElectionRunnables = append (cm .leaderElectionRunnables , r )
143
+ }
144
+
136
145
if cm .started {
137
146
// If already started, start the controller
138
147
go func () {
@@ -254,13 +263,15 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
254
263
go cm .serveMetrics (cm .internalStop )
255
264
}
256
265
266
+ go cm .startNonLeaderElectionRunnables ()
267
+
257
268
if cm .resourceLock != nil {
258
269
err := cm .startLeaderElection ()
259
270
if err != nil {
260
271
return err
261
272
}
262
273
} else {
263
- go cm .start ()
274
+ go cm .startLeaderElectionRunnables ()
264
275
}
265
276
266
277
select {
@@ -273,11 +284,11 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
273
284
}
274
285
}
275
286
276
- func (cm * controllerManager ) start () {
287
+ func (cm * controllerManager ) startNonLeaderElectionRunnables () {
277
288
cm .mu .Lock ()
278
289
defer cm .mu .Unlock ()
279
290
280
- // Start the Cache. Allow the function to start the cache to be mocked out for testing
291
+ // Start the Cache. Allow the function to startLeaderElectionRunnables the cache to be mocked out for testing
281
292
if cm .startCache == nil {
282
293
cm .startCache = cm .cache .Start
283
294
}
@@ -291,8 +302,26 @@ func (cm *controllerManager) start() {
291
302
// TODO(community): Check the return value and write a test
292
303
cm .cache .WaitForCacheSync (cm .internalStop )
293
304
294
- // Start the runnables after the cache has synced
295
- for _ , c := range cm .runnables {
305
+ // Start the non-leaderelection Runnables after the cache has synced
306
+ for _ , c := range cm .nonLeaderElectionRunnables {
307
+ // Controllers block, but we want to return an error if any have an error starting.
308
+ // Write any Start errors to a channel so we can return them
309
+ ctrl := c
310
+ go func () {
311
+ cm .errChan <- ctrl .Start (cm .internalStop )
312
+ }()
313
+ }
314
+
315
+ cm .started = true
316
+ }
317
+
318
+ func (cm * controllerManager ) startLeaderElectionRunnables () {
319
+ // Wait for the caches to sync.
320
+ // TODO(community): Check the return value and write a test
321
+ cm .cache .WaitForCacheSync (cm .internalStop )
322
+
323
+ // Start the leader election Runnables after the cache has synced
324
+ for _ , c := range cm .leaderElectionRunnables {
296
325
// Controllers block, but we want to return an error if any have an error starting.
297
326
// Write any Start errors to a channel so we can return them
298
327
ctrl := c
@@ -312,7 +341,7 @@ func (cm *controllerManager) startLeaderElection() (err error) {
312
341
RetryPeriod : cm .retryPeriod ,
313
342
Callbacks : leaderelection.LeaderCallbacks {
314
343
OnStartedLeading : func (_ context.Context ) {
315
- cm .start ()
344
+ cm .startLeaderElectionRunnables ()
316
345
},
317
346
OnStoppedLeading : func () {
318
347
// Most implementations of leader election log.Fatal() here.
0 commit comments