-
Notifications
You must be signed in to change notification settings - Fork 438
/
election.go
122 lines (106 loc) · 2.93 KB
/
election.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package controller
import (
"context"
"os"
"time"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"github.com/kubeovn/kube-ovn/pkg/util"
)
const ovnLeaderElector = "ovn-controller-leader-elector"
type leaderElectionConfig struct {
PodName string
PodNamespace string
Client clientset.Interface
ElectionID string
WasLeader bool
OnStartedLeading func(chan struct{})
OnStoppedLeading func()
OnNewLeader func(identity string)
}
func (c *Controller) isLeader() bool {
return c.elector.IsLeader()
}
func (c *Controller) leaderElection() {
config := &leaderElectionConfig{
Client: c.config.KubeClient,
ElectionID: "kube-ovn-controller",
PodName: c.config.PodName,
PodNamespace: c.config.PodNamespace,
}
c.elector = setupLeaderElection(config)
var flag bool
for {
if c.isLeader() {
config.WasLeader = true
return
}
if !flag {
klog.Info("waiting for becoming a leader")
flag = true
}
time.Sleep(1 * time.Second)
}
}
func setupLeaderElection(config *leaderElectionConfig) *leaderelection.LeaderElector {
var stopCh chan struct{}
callbacks := leaderelection.LeaderCallbacks{
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)
}
},
OnStoppedLeading: func() {
klog.Info("I am not leader anymore")
close(stopCh)
if config.OnStoppedLeading != nil {
config.OnStoppedLeading()
}
util.LogFatalAndExit(nil, "leaderelection lost")
},
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)
}
},
}
broadcaster := record.NewBroadcaster()
hostname := os.Getenv(util.HostnameEnv)
recorder := broadcaster.NewRecorder(scheme.Scheme, apiv1.EventSource{
Component: ovnLeaderElector,
Host: hostname,
})
lock := &resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{Namespace: config.PodNamespace, Name: config.ElectionID},
Client: config.Client.CoordinationV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: config.PodName,
EventRecorder: recorder,
},
}
elector, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: lock,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Callbacks: callbacks,
})
if err != nil {
util.LogFatalAndExit(err, "failed to create leader elector")
}
go elector.Run(context.Background())
return elector
}