Skip to content

Commit e63210a

Browse files
committed
🐛 fix runnable run twice issue
1 parent f99287c commit e63210a

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
@@ -91,9 +91,10 @@ type controllerManager struct {
9191
// metricsListener is used to serve prometheus metrics
9292
metricsListener net.Listener
9393

94-
mu sync.Mutex
95-
started bool
96-
errChan chan error
94+
mu sync.Mutex
95+
started bool
96+
startedLeader bool
97+
errChan chan error
9798

9899
// internalStop is the stop channel *actually* used by everything involved
99100
// 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 {
135136
return err
136137
}
137138

139+
var shouldStart bool
140+
138141
// Add the runnable to the leader election or the non-leaderelection list
139142
if leRunnable, ok := r.(LeaderElectionRunnable); ok && !leRunnable.NeedLeaderElection() {
143+
shouldStart = cm.started
140144
cm.nonLeaderElectionRunnables = append(cm.nonLeaderElectionRunnables, r)
141145
} else {
146+
shouldStart = cm.startedLeader
142147
cm.leaderElectionRunnables = append(cm.leaderElectionRunnables, r)
143148
}
144149

145-
if cm.started {
150+
if shouldStart {
146151
// If already started, start the controller
147152
go func() {
148153
cm.errChan <- r.Start(cm.internalStop)
@@ -316,6 +321,8 @@ func (cm *controllerManager) startLeaderElectionRunnables() {
316321
cm.errChan <- ctrl.Start(cm.internalStop)
317322
}()
318323
}
324+
325+
cm.startedLeader = true
319326
}
320327

321328
func (cm *controllerManager) waitForCache() {

0 commit comments

Comments
 (0)