-
Notifications
You must be signed in to change notification settings - Fork 3
/
election.go
126 lines (105 loc) · 3.26 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
123
124
125
126
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package election
import (
"context"
"sync"
"time"
"github.com/azure/peerd/pkg/k8s"
"github.com/multiformats/go-multiaddr"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
)
// LeaderElection provides an interface to elect a leader in a kubernetes cluster.
type LeaderElection interface {
// RunOrDie runs the leader election.
RunOrDie(ctx context.Context, id string) error
// Leader gets the address of the elected leader.
Leader() (multiaddr.Multiaddr, error)
}
// leaderElection provides an implementation of LeaderElection.
type leaderElection struct {
// ns is the namespace in which to run the leader election.
ns string
// name is the name of the leader election resource in the cluster.
name string
// id is the id of the elected leader.
id string
cs kubernetes.Interface
initChan chan interface{}
mx sync.RWMutex
}
var _ LeaderElection = &leaderElection{}
// Leader gets the address of the elected leader.
func (le *leaderElection) Leader() (multiaddr.Multiaddr, error) {
<-le.initChan
le.mx.RLock()
defer le.mx.RUnlock()
addr, err := multiaddr.NewMultiaddr(le.id)
if err != nil {
return nil, err
}
return addr, nil
}
// RunOrDie runs the leader election.
func (le *leaderElection) RunOrDie(ctx context.Context, id string) error {
lockCfg := resourcelock.ResourceLockConfig{
Identity: id,
}
rl, err := resourcelock.New(resourcelock.LeasesResourceLock, le.ns, le.name, le.cs.CoreV1(), le.cs.CoordinationV1(), lockCfg)
if err != nil {
return err
}
go leaderelection.RunOrDie(ctx, le.leaderElectionConfig(rl))
return nil
}
// leaderElectionConfig creates a new configuration for the leader election.
func (le *leaderElection) leaderElectionConfig(rl resourcelock.Interface) leaderelection.LeaderElectionConfig {
return leaderelection.LeaderElectionConfig{
Lock: rl,
ReleaseOnCancel: true,
LeaseDuration: 10 * time.Second,
RenewDeadline: 5 * time.Second,
RetryPeriod: 2 * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(_ context.Context) {},
OnStoppedLeading: func() {},
OnNewLeader: le.onNewLeader,
},
}
}
// onNewLeader is called when a new leader is elected.
// It updates the leaderElection instance with the new leader's identity.
func (le *leaderElection) onNewLeader(identity string) {
if identity == resourcelock.UnknownLeader {
return
}
select {
case <-le.initChan:
break
default:
// A leader has been elected.
close(le.initChan)
}
le.mx.Lock()
defer le.mx.Unlock()
le.id = identity
}
// New build a new LeaderElection instance in the given namespace, with the given name.
// The kubeConfigPath is used to create the kubernetes interface. It may be empty if the runtime environment is a pod.
func New(namespace, name, kubeConfigPath string) LeaderElection {
cs, err := k8s.NewKubernetesInterface(kubeConfigPath)
if err != nil {
panic(err)
}
return newLeaderElection(namespace, name, cs)
}
func newLeaderElection(namespace, name string, cs kubernetes.Interface) *leaderElection {
return &leaderElection{
ns: namespace,
name: name,
cs: cs,
initChan: make(chan interface{}),
}
}