From 1284f15d627ce8d6903553b81c63f9f12980ba27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=A5=96=E5=BB=BA?= Date: Mon, 15 Aug 2022 11:24:37 +0800 Subject: [PATCH] abort kube-ovn-controller on leader change (#1797) --- pkg/controller/election.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/controller/election.go b/pkg/controller/election.go index 2144cff51a5..73815335e76 100644 --- a/pkg/controller/election.go +++ b/pkg/controller/election.go @@ -25,6 +25,7 @@ type leaderElectionConfig struct { Client clientset.Interface ElectionID string + WasLeader bool OnStartedLeading func(chan struct{}) OnStoppedLeading func() @@ -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 @@ -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) @@ -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) }