Skip to content

Commit

Permalink
abort kube-ovn-controller on leader change (#1797)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Aug 15, 2022
1 parent 5bf8de0 commit 1284f15
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/controller/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type leaderElectionConfig struct {
Client clientset.Interface

ElectionID string
WasLeader bool

OnStartedLeading func(chan struct{})
OnStoppedLeading func()
Expand All @@ -36,13 +37,14 @@ func (c *Controller) isLeader() bool {
}

func (c *Controller) leaderElection() {
elector := setupLeaderElection(&leaderElectionConfig{
config := &leaderElectionConfig{
Client: c.config.KubeClient,
ElectionID: "ovn-config",
PodName: c.config.PodName,
PodNamespace: c.config.PodNamespace,
})
c.elector = elector
}

c.elector = setupLeaderElection(config)
for {
if c.isLeader() {
return
Expand All @@ -58,6 +60,7 @@ func setupLeaderElection(config *leaderElectionConfig) *leaderelection.LeaderEle
OnStartedLeading: func(ctx context.Context) {
klog.Infof("I am the new leader")
stopCh = make(chan struct{})
config.WasLeader = true

if config.OnStartedLeading != nil {
config.OnStartedLeading(stopCh)
Expand All @@ -74,6 +77,9 @@ func setupLeaderElection(config *leaderElectionConfig) *leaderelection.LeaderEle
},
OnNewLeader: func(identity string) {
klog.Infof("new leader elected: %v", identity)
if config.WasLeader && identity != config.PodName {
klog.Fatal("I am not leader anymore")
}
if config.OnNewLeader != nil {
config.OnNewLeader(identity)
}
Expand Down

0 comments on commit 1284f15

Please sign in to comment.