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 0e0ea3c commit b669c67
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 @@ -24,6 +24,7 @@ type leaderElectionConfig struct {
Client clientset.Interface

ElectionID string
WasLeader bool

OnStartedLeading func(chan struct{})
OnStoppedLeading func()
Expand All @@ -35,13 +36,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 @@ -57,6 +59,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 @@ -73,6 +76,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 b669c67

Please sign in to comment.