Skip to content

Commit 28262f4

Browse files
authored
Merge pull request #487 from adohe/fix_runnable_twice
🐛 fix runnable run twice issue
2 parents b5e34e4 + e63210a commit 28262f4

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

pkg/manager/internal.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ type controllerManager struct {
9090
// metricsListener is used to serve prometheus metrics
9191
metricsListener net.Listener
9292

93-
mu sync.Mutex
94-
started bool
95-
errChan chan error
93+
mu sync.Mutex
94+
started bool
95+
startedLeader bool
96+
errChan chan error
9697

9798
// internalStop is the stop channel *actually* used by everything involved
9899
// with the manager as a stop channel, so that we can pass a stop channel
@@ -134,14 +135,18 @@ func (cm *controllerManager) Add(r Runnable) error {
134135
return err
135136
}
136137

138+
var shouldStart bool
139+
137140
// Add the runnable to the leader election or the non-leaderelection list
138141
if leRunnable, ok := r.(LeaderElectionRunnable); ok && !leRunnable.NeedLeaderElection() {
142+
shouldStart = cm.started
139143
cm.nonLeaderElectionRunnables = append(cm.nonLeaderElectionRunnables, r)
140144
} else {
145+
shouldStart = cm.startedLeader
141146
cm.leaderElectionRunnables = append(cm.leaderElectionRunnables, r)
142147
}
143148

144-
if cm.started {
149+
if shouldStart {
145150
// If already started, start the controller
146151
go func() {
147152
cm.errChan <- r.Start(cm.internalStop)
@@ -314,6 +319,8 @@ func (cm *controllerManager) startLeaderElectionRunnables() {
314319
cm.errChan <- ctrl.Start(cm.internalStop)
315320
}()
316321
}
322+
323+
cm.startedLeader = true
317324
}
318325

319326
func (cm *controllerManager) waitForCache() {

0 commit comments

Comments
 (0)