/
leaderelection.go
191 lines (165 loc) · 5.15 KB
/
leaderelection.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// Config is put into a different package to prevent cyclic imports in case
// it is needed in several locations
package uniqueness
import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/gofrs/uuid"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "k8s.io/client-go/kubernetes"
le "k8s.io/client-go/tools/leaderelection"
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
)
type LeaderelectionManager struct {
log *logp.Logger
leader *le.LeaderElector
wg *sync.WaitGroup
cancelFunc context.CancelFunc
kubeClient k8s.Interface
}
func NewLeaderElector(log *logp.Logger, client k8s.Interface) Manager {
if client == nil {
log.Errorf("NewLeaderElector returned default client")
return &DefaultUniqueManager{}
}
wg := &sync.WaitGroup{}
return &LeaderelectionManager{
log: log,
leader: nil,
wg: wg,
cancelFunc: nil,
kubeClient: client,
}
}
func (m *LeaderelectionManager) IsLeader() bool {
return m.leader.IsLeader()
}
// Run leader election is blocking until a FirstLeaderDeadline timeout has reached.
func (m *LeaderelectionManager) Run(ctx context.Context) error {
newCtx, cancel := context.WithCancel(ctx)
m.cancelFunc = cancel
leConfig, err := m.buildConfig(newCtx)
if err != nil {
m.log.Errorf("Fail building leader election config: %v", err)
return err
}
m.leader, err = le.NewLeaderElector(leConfig)
if err != nil {
m.log.Errorf("Fail to create a new leader elector: %v", err)
return err
}
go m.leader.Run(newCtx)
m.wg.Add(1)
m.log.Infof("Started leader election")
time.Sleep(FirstLeaderDeadline)
m.log.Debugf("Stop waiting after %s for a leader to be elected", FirstLeaderDeadline)
return nil
}
func (m *LeaderelectionManager) Stop() {
if m.cancelFunc != nil {
m.log.Info("Stopping leader election manager")
m.cancelFunc()
m.wg.Wait()
return
}
m.log.Warn("Leader election manager is not initialized")
}
func (m *LeaderelectionManager) buildConfig(ctx context.Context) (le.LeaderElectionConfig, error) {
podId, err := m.currentPodID()
if err != nil {
return le.LeaderElectionConfig{}, err
}
id := fmt.Sprintf("%s_%s", LeaderLeaseName, podId)
ns, err := kubernetes.InClusterNamespace()
if err != nil {
ns = v1.NamespaceDefault
}
lease := metav1.ObjectMeta{
Name: LeaderLeaseName,
Namespace: ns,
}
return le.LeaderElectionConfig{
Lock: &rl.LeaseLock{
LeaseMeta: lease,
Client: m.kubeClient.CoordinationV1(),
LockConfig: rl.ResourceLockConfig{
Identity: id,
},
},
ReleaseOnCancel: true,
LeaseDuration: LeaseDuration,
RenewDeadline: RenewDeadline,
RetryPeriod: RetryPeriod,
Callbacks: le.LeaderCallbacks{
OnStartedLeading: func(_ context.Context) {
m.log.Infof("Leader election lock GAINED, id: %v", id)
},
OnStoppedLeading: func() {
// OnStoppedLeading gets called even if cloudbeat wasn't the leader, for example, if the context is canceled due to reconfiguration from fleet.
// We re-run the manager to keep following leader status except for context cancellation events.
m.log.Infof("Leader election lock LOST, id: %v", id)
defer m.wg.Done()
select {
case <-ctx.Done():
m.log.Info("Leader election is canceled")
return
default:
go m.leader.Run(ctx)
m.wg.Add(1)
m.log.Infof("Re-running leader elector")
}
},
OnNewLeader: func(identity string) {
if identity == id {
m.log.Infof("Leader election lock has been acquired by this pod, id: %v", identity)
} else {
m.log.Infof("Leader election lock has been acquired by another pod, id: %v", identity)
}
},
},
}, nil
}
func (m *LeaderelectionManager) currentPodID() (string, error) {
pod, found := os.LookupEnv(PodNameEnvar)
if !found {
m.log.Warnf("Env var %s wasn't found", PodNameEnvar)
return m.generateUUID()
}
return m.lastPart(pod)
}
func (m *LeaderelectionManager) lastPart(s string) (string, error) {
parts := strings.Split(s, "-")
if len(parts) == 0 {
m.log.Warnf("Failed to find id for pod_name: %s", s)
return m.generateUUID()
}
return parts[len(parts)-1], nil
}
func (m *LeaderelectionManager) generateUUID() (string, error) {
result, err := uuid.NewV4()
m.log.Warnf("Generating uuid as an identifier, UUID: %s", result.String())
return result.String(), err
}