Skip to content

Commit 4f40fca

Browse files
authored
Exit NGF container when leader lease is lost (#1130)
Problem: If NGF Pod loses connection to the API server and cannot renew the leader election lease, it stops leading and cannot become the leader again. After it stops leading, it will not report any statuses until it is restarted. Solution: Update the leader elector Start method to return an error if the leader lease is lost. This will cause the controller-runtime manager to exit, and the NGF container will restart. The new container will then attempt to become the leader again. This aligns with how the controller-runtime library handles losing a leader lease.
1 parent 7820c2b commit 4f40fca

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

internal/mode/static/leader.go

+14-2
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)