@@ -50,8 +50,11 @@ type controllerManager struct {
50
50
// to scheme.scheme.
51
51
scheme * runtime.Scheme
52
52
53
- // runnables is the set of Controllers that the controllerManager injects deps into and Starts.
53
+ // runnables is the set of Controllers and (or) webhook server that the controllerManager injects deps into and Starts.
54
54
runnables []Runnable
55
+ // haRunnables is the set of webhook servers that the controllerManager injects deps into and Starts.
56
+ // These Runnables are in HA mode (not blocked by lead election)
57
+ haRunnables []Runnable
55
58
56
59
cache cache.Cache
57
60
@@ -104,7 +107,7 @@ type controllerManager struct {
104
107
}
105
108
106
109
// Add sets dependencies on i, and adds it to the list of runnables to start.
107
- func (cm * controllerManager ) Add (r Runnable ) error {
110
+ func (cm * controllerManager ) add (r Runnable , leaderElection bool ) error {
108
111
cm .mu .Lock ()
109
112
defer cm .mu .Unlock ()
110
113
@@ -114,7 +117,11 @@ func (cm *controllerManager) Add(r Runnable) error {
114
117
}
115
118
116
119
// Add the runnable to the list
117
- cm .runnables = append (cm .runnables , r )
120
+ if leaderElection {
121
+ cm .runnables = append (cm .runnables , r )
122
+ } else {
123
+ cm .haRunnables = append (cm .haRunnables , r )
124
+ }
118
125
if cm .started {
119
126
// If already started, start the controller
120
127
go func () {
@@ -125,6 +132,16 @@ func (cm *controllerManager) Add(r Runnable) error {
125
132
return nil
126
133
}
127
134
135
+ // Add sets dependencies on i, and adds it to the list of runnables to start.
136
+ func (cm * controllerManager ) Add (r Runnable ) error {
137
+ return cm .add (r , true )
138
+ }
139
+
140
+ // AddHA sets dependencies on i, and adds it to the list of runnables to start in HA mode.
141
+ func (cm * controllerManager ) AddHA (r Runnable ) error {
142
+ return cm .add (r , false )
143
+ }
144
+
128
145
func (cm * controllerManager ) SetFields (i interface {}) error {
129
146
if _ , err := inject .ConfigInto (cm .config , i ); err != nil {
130
147
return err
@@ -235,13 +252,15 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
235
252
go cm .serveMetrics (cm .internalStop )
236
253
}
237
254
255
+ go cm .start ()
256
+
238
257
if cm .resourceLock != nil {
239
258
err := cm .startLeaderElection ()
240
259
if err != nil {
241
260
return err
242
261
}
243
262
} else {
244
- go cm .start ()
263
+ go cm .startLeaderElected ()
245
264
}
246
265
247
266
select {
@@ -272,6 +291,24 @@ func (cm *controllerManager) start() {
272
291
// TODO(community): Check the return value and write a test
273
292
cm .cache .WaitForCacheSync (cm .internalStop )
274
293
294
+ // Start the HA runnables after the cache has synced
295
+ for _ , c := range cm .haRunnables {
296
+ // Controllers block, but we want to return an error if any have an error starting.
297
+ // Write any Start errors to a channel so we can return them
298
+ ctrl := c
299
+ go func () {
300
+ cm .errChan <- ctrl .Start (cm .internalStop )
301
+ }()
302
+ }
303
+
304
+ cm .started = true
305
+ }
306
+
307
+ func (cm * controllerManager ) startLeaderElected () {
308
+ // Wait for the caches to sync.
309
+ // TODO(community): Check the return value and write a test
310
+ cm .cache .WaitForCacheSync (cm .internalStop )
311
+
275
312
// Start the runnables after the cache has synced
276
313
for _ , c := range cm .runnables {
277
314
// Controllers block, but we want to return an error if any have an error starting.
@@ -295,7 +332,7 @@ func (cm *controllerManager) startLeaderElection() (err error) {
295
332
RetryPeriod : 2 * time .Second ,
296
333
Callbacks : leaderelection.LeaderCallbacks {
297
334
OnStartedLeading : func (_ context.Context ) {
298
- cm .start ()
335
+ cm .startLeaderElected ()
299
336
},
300
337
OnStoppedLeading : func () {
301
338
// Most implementations of leader election log.Fatal() here.
0 commit comments