Skip to content

Commit

Permalink
fix bug when set renewDeadline greater than leaseDuration the control…
Browse files Browse the repository at this point in the history
…ler will hang and no error will be return

but actually the error should be throw and exit

Signed-off-by: yuzhipeng <[email protected]>
  • Loading branch information
yuzp1996 committed Jan 3, 2022
1 parent f236f03 commit ef93ca1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
23 changes: 14 additions & 9 deletions pkg/manager/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,9 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {

// Start the leader election and all required runnables.
{
ctx, cancel := context.WithCancel(context.Background())
cm.leaderElectionCancel = cancel
go func() {
if cm.resourceLock != nil {
if err := cm.startLeaderElection(ctx); err != nil {
if err := cm.startLeaderElection(); err != nil {
cm.errChan <- err
}
} else {
Expand Down Expand Up @@ -552,13 +550,16 @@ func (cm *controllerManager) engageStopProcedure(stopComplete <-chan struct{}) e
defer cm.recorderProvider.Stop(cm.shutdownCtx)
defer func() {
// Cancel leader election only after we waited. It will os.Exit() the app for safety.
if cm.resourceLock != nil {
// After asking the context to be cancelled, make sure
// we wait for the leader stopped channel to be closed, otherwise
// we might encounter race conditions between this code
// and the event recorder, which is used within leader election code.
// After asking the context to be cancelled, make sure
// we wait for the leader stopped channel to be closed, otherwise
// we might encounter race conditions between this code
// and the event recorder, which is used within leader election code.
if cm.leaderElectionCancel != nil {
cm.leaderElectionCancel()
}
if cm.leaderElectionStopped != nil {
<-cm.leaderElectionStopped

}
}()

Expand Down Expand Up @@ -604,7 +605,7 @@ func (cm *controllerManager) startLeaderElectionRunnables() error {
return cm.runnables.LeaderElection.Start(cm.internalCtx)
}

func (cm *controllerManager) startLeaderElection(ctx context.Context) (err error) {
func (cm *controllerManager) startLeaderElection() (err error) {
l, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: cm.resourceLock,
LeaseDuration: cm.leaseDuration,
Expand Down Expand Up @@ -637,6 +638,10 @@ func (cm *controllerManager) startLeaderElection(ctx context.Context) (err error
return err
}

ctx, cancel := context.WithCancel(context.Background())
cm.leaderElectionCancel = cancel
cm.leaderElectionStopped = make(chan struct{})

// Start the leader elector process
go func() {
l.Run(ctx)
Expand Down
1 change: 0 additions & 1 deletion pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ func New(config *rest.Config, options Options) (Manager, error) {
livenessEndpointName: options.LivenessEndpointName,
gracefulShutdownTimeout: *options.GracefulShutdownTimeout,
internalProceduresStop: make(chan struct{}),
leaderElectionStopped: make(chan struct{}),
leaderElectionReleaseOnCancel: options.LeaderElectionReleaseOnCancel,
}, nil
}
Expand Down

0 comments on commit ef93ca1

Please sign in to comment.