Skip to content

Commit 4053c22

Browse files
author
Kate Osborn
committed
Return error when leader lease is lost
1 parent 7820c2b commit 4053c22

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

internal/mode/static/leader.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package static
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"time"
78

@@ -43,10 +44,21 @@ type leaderElectorRunnable struct {
4344
le *leaderelection.LeaderElector
4445
}
4546

46-
// Start runs the leaderelection.LeaderElector and blocks until the context is canceled or Run returns.
47+
// Start runs the leaderelection.LeaderElector and blocks until the context is canceled or the leader lease is lost.
48+
// If the leader lease is lost, Start returns an error, and the controller-runtime manager will exit, causing the Pod
49+
// to restart. This is necessary otherwise components that need leader election might continue to run after the leader
50+
// lease was lost.
4751
func (l *leaderElectorRunnable) Start(ctx context.Context) error {
4852
l.le.Run(ctx)
49-
return nil
53+
54+
// Run exits if the context is canceled or the leader lease is lost. We only want to return an error if the
55+
// context is not canceled.
56+
select {
57+
case <-ctx.Done():
58+
return nil
59+
default:
60+
return errors.New("leader election lost")
61+
}
5062
}
5163

5264
// IsLeader returns if the Pod is the current leader.

0 commit comments

Comments
 (0)