@@ -91,9 +91,10 @@ type controllerManager struct {
91
91
// metricsListener is used to serve prometheus metrics
92
92
metricsListener net.Listener
93
93
94
- mu sync.Mutex
95
- started bool
96
- errChan chan error
94
+ mu sync.Mutex
95
+ started bool
96
+ isLeader bool
97
+ errChan chan error
97
98
98
99
// internalStop is the stop channel *actually* used by everything involved
99
100
// with the manager as a stop channel, so that we can pass a stop channel
@@ -135,14 +136,18 @@ func (cm *controllerManager) Add(r Runnable) error {
135
136
return err
136
137
}
137
138
139
+ shouldStart := false
140
+
138
141
// Add the runnable to the leader election or the non-leaderelection list
139
142
if leRunnable , ok := r .(LeaderElectionRunnable ); ok && ! leRunnable .NeedLeaderElection () {
143
+ shouldStart = cm .started
140
144
cm .nonLeaderElectionRunnables = append (cm .nonLeaderElectionRunnables , r )
141
145
} else {
146
+ shouldStart = cm .started && (cm .isLeader || cm .resourceLock == nil )
142
147
cm .leaderElectionRunnables = append (cm .leaderElectionRunnables , r )
143
148
}
144
149
145
- if cm . started {
150
+ if shouldStart {
146
151
// If already started, start the controller
147
152
go func () {
148
153
cm .errChan <- r .Start (cm .internalStop )
@@ -347,6 +352,7 @@ func (cm *controllerManager) startLeaderElection() (err error) {
347
352
RetryPeriod : cm .retryPeriod ,
348
353
Callbacks : leaderelection.LeaderCallbacks {
349
354
OnStartedLeading : func (_ context.Context ) {
355
+ cm .isLeader = true
350
356
cm .startLeaderElectionRunnables ()
351
357
},
352
358
OnStoppedLeading : func () {
0 commit comments