Skip to content

Commit a5bde5d

Browse files
committed
fix tests
1 parent ca34c4f commit a5bde5d

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

pkg/manager/internal.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -351,39 +351,6 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
351351
// Initialize the internal context.
352352
cm.internalCtx, cm.internalCancel = context.WithCancel(ctx)
353353

354-
leaderElector, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
355-
Lock: cm.resourceLock,
356-
LeaseDuration: cm.leaseDuration,
357-
RenewDeadline: cm.renewDeadline,
358-
RetryPeriod: cm.retryPeriod,
359-
Callbacks: leaderelection.LeaderCallbacks{
360-
OnStartedLeading: func(_ context.Context) {
361-
if err := cm.startLeaderElectionRunnables(); err != nil {
362-
cm.errChan <- err
363-
return
364-
}
365-
close(cm.elected)
366-
},
367-
OnStoppedLeading: func() {
368-
if cm.onStoppedLeading != nil {
369-
cm.onStoppedLeading()
370-
}
371-
// Make sure graceful shutdown is skipped if we lost the leader lock without
372-
// intending to.
373-
cm.gracefulShutdownTimeout = time.Duration(0)
374-
// Most implementations of leader election log.Fatal() here.
375-
// Since Start is wrapped in log.Fatal when called, we can just return
376-
// an error here which will cause the program to exit.
377-
cm.errChan <- errors.New("leader election lost")
378-
},
379-
},
380-
ReleaseOnCancel: cm.leaderElectionReleaseOnCancel,
381-
Name: cm.leaderElectionID,
382-
})
383-
if err != nil {
384-
return err
385-
}
386-
387354
// This chan indicates that stop is complete, in other words all runnables have returned or timeout on stop request
388355
stopComplete := make(chan struct{})
389356
defer close(stopComplete)
@@ -467,6 +434,39 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
467434
ctx, cancel := context.WithCancel(context.Background())
468435
cm.leaderElectionCancel = cancel
469436
if cm.resourceLock != nil {
437+
leaderElector, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
438+
Lock: cm.resourceLock,
439+
LeaseDuration: cm.leaseDuration,
440+
RenewDeadline: cm.renewDeadline,
441+
RetryPeriod: cm.retryPeriod,
442+
Callbacks: leaderelection.LeaderCallbacks{
443+
OnStartedLeading: func(_ context.Context) {
444+
if err := cm.startLeaderElectionRunnables(); err != nil {
445+
cm.errChan <- err
446+
return
447+
}
448+
close(cm.elected)
449+
},
450+
OnStoppedLeading: func() {
451+
if cm.onStoppedLeading != nil {
452+
cm.onStoppedLeading()
453+
}
454+
// Make sure graceful shutdown is skipped if we lost the leader lock without
455+
// intending to.
456+
cm.gracefulShutdownTimeout = time.Duration(0)
457+
// Most implementations of leader election log.Fatal() here.
458+
// Since Start is wrapped in log.Fatal when called, we can just return
459+
// an error here which will cause the program to exit.
460+
cm.errChan <- errors.New("leader election lost")
461+
},
462+
},
463+
ReleaseOnCancel: cm.leaderElectionReleaseOnCancel,
464+
Name: cm.leaderElectionID,
465+
})
466+
if err != nil {
467+
return fmt.Errorf("failed during initialization leader election process: %w", err)
468+
}
469+
470470
// Start the leader elector process
471471
go func() {
472472
leaderElector.Run(ctx)

0 commit comments

Comments
 (0)