Skip to content

Commit 5e3d91f

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

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

pkg/manager/internal.go

Lines changed: 10 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+
isLeader 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+
shouldStart := false
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.started && (cm.isLeader || cm.resourceLock == nil)
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)
@@ -347,6 +352,7 @@ func (cm *controllerManager) startLeaderElection() (err error) {
347352
RetryPeriod: cm.retryPeriod,
348353
Callbacks: leaderelection.LeaderCallbacks{
349354
OnStartedLeading: func(_ context.Context) {
355+
cm.isLeader = true
350356
cm.startLeaderElectionRunnables()
351357
},
352358
OnStoppedLeading: func() {

0 commit comments

Comments
 (0)