-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod_leader.go
319 lines (269 loc) · 9.66 KB
/
pod_leader.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//
// DISCLAIMER
//
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
//
// Licensed 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package resources
import (
"context"
"strings"
"sync"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
api "github.com/dalet-oss/arangodb-operator/pkg/apis/deployment/v1"
"github.com/dalet-oss/arangodb-operator/pkg/deployment/features"
"github.com/dalet-oss/arangodb-operator/pkg/deployment/patch"
"github.com/dalet-oss/arangodb-operator/pkg/util/arangod"
"github.com/dalet-oss/arangodb-operator/pkg/util/errors"
"github.com/dalet-oss/arangodb-operator/pkg/util/globals"
"github.com/dalet-oss/arangodb-operator/pkg/util/k8sutil"
inspectorInterface "github.com/dalet-oss/arangodb-operator/pkg/util/k8sutil/inspector"
"github.com/dalet-oss/arangodb-operator/pkg/util/k8sutil/kerrors"
"github.com/dalet-oss/arangodb-operator/pkg/util/k8sutil/patcher"
)
// EnsureLeader creates leader label on the pod's agency and creates service to it.
// When agency leader is not known then all agencies' pods should not have leader label, and
// consequentially service will not point to any pod.
// It works only in active fail-over mode.
func (r *Resources) EnsureLeader(ctx context.Context, cachedStatus inspectorInterface.Inspector) error {
if r.context.GetSpec().GetMode() != api.DeploymentModeActiveFailover {
return nil
}
log := r.log.Str("section", "pod")
cache, ok := r.context.GetAgencyHealth()
if !ok {
return nil
}
leaderID := cache.LeaderID()
status := r.context.GetStatus()
noLeader := len(leaderID) == 0
changed := false
group := api.ServerGroupAgents
for _, e := range status.Members.AsListInGroup(group) {
pod, exist := cachedStatus.Pod().V1().GetSimple(e.Member.Pod.GetName())
if !exist {
continue
}
labels := pod.GetLabels()
if noLeader || e.Member.ID != leaderID {
// Unset a leader when:
// - leader is unknown.
// - leader does not belong to the current pod.
if _, ok := labels[k8sutil.LabelKeyArangoLeader]; ok {
delete(labels, k8sutil.LabelKeyArangoLeader)
err := r.context.ApplyPatchOnPod(ctx, pod, patch.ItemReplace(patch.NewPath("metadata", "labels"), labels))
if err != nil {
log.Err(err).Error("Unable to remove leader label")
return err
}
log.Warn("leader label is removed from \"%s\" member", e.Member.ID)
changed = true
}
continue
}
// From here on it is known that there is a leader, and it should be attached to the current pod.
if value, ok := labels[k8sutil.LabelKeyArangoLeader]; !ok {
labels = addLabel(labels, k8sutil.LabelKeyArangoLeader, "true")
} else if value != "true" {
labels = addLabel(labels, k8sutil.LabelKeyArangoLeader, "true")
} else {
// A pod is already a leader, so nothing to change.
continue
}
err := r.context.ApplyPatchOnPod(ctx, pod, patch.ItemReplace(patch.NewPath("metadata", "labels"), labels))
if err != nil {
log.Err(err).Error("Unable to update leader label")
return err
}
log.Warn("leader label is set on \"%s\" member", e.Member.ID)
changed = true
}
if changed {
return errors.Reconcile()
}
if noLeader {
// There is no leader agency so service may not exist, or it can exist with empty list of endpoints.
return nil
}
leaderAgentSvcName := k8sutil.CreateAgentLeaderServiceName(r.context.GetAPIObject().GetName())
deploymentName := r.context.GetAPIObject().GetName()
ports := []core.ServicePort{CreateServerServicePort()}
selector := k8sutil.LabelsForLeaderMember(deploymentName, group.AsRole(), leaderID)
if s, ok := cachedStatus.Service().V1().GetSimple(leaderAgentSvcName); ok {
if c, err := patcher.ServicePatcher(ctx, cachedStatus.ServicesModInterface().V1(), s, meta.PatchOptions{}, patcher.PatchServiceSelector(selector), patcher.PatchServicePorts(ports)); err != nil {
return err
} else {
if !c {
return r.ensureSingleServerLeader(ctx, cachedStatus)
}
return errors.Reconcile()
}
}
s := r.createService(leaderAgentSvcName, r.context.GetNamespace(), "", core.ServiceTypeClusterIP, r.context.GetAPIObject().AsOwner(), ports, selector)
err := globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
_, err := cachedStatus.ServicesModInterface().V1().Create(ctxChild, s, meta.CreateOptions{})
return err
})
if err != nil {
if !kerrors.IsConflict(err) {
return err
}
}
// The service has been created.
return errors.Reconcile()
}
// getSingleServerLeaderID returns ids of a single server leaders.
func (r *Resources) getSingleServerLeaderID(ctx context.Context) ([]string, error) {
status := r.context.GetStatus()
var mutex sync.Mutex
var leaderIDs []string
var anyError error
ctxCancel, cancel := context.WithCancel(ctx)
defer cancel()
var wg sync.WaitGroup
for _, m := range status.Members.Single {
wg.Add(1)
go func(id string) {
defer wg.Done()
err := globals.GetGlobalTimeouts().ArangoD().RunWithTimeout(ctxCancel, func(ctxChild context.Context) error {
c, err := r.context.GetMembersState().GetMemberClient(id)
if err != nil {
return err
}
if available, err := arangod.IsServerAvailable(ctxChild, c); err != nil {
return err
} else if !available {
return errors.New("not available")
}
mutex.Lock()
leaderIDs = append(leaderIDs, id)
mutex.Unlock()
return nil
})
if err != nil {
mutex.Lock()
anyError = err
mutex.Unlock()
}
}(m.ID)
}
wg.Wait()
if len(leaderIDs) > 0 {
return leaderIDs, nil
}
if anyError != nil {
return nil, errors.WithMessagef(anyError, "unable to get a leader")
}
return nil, errors.New("unable to get a leader")
}
// setSingleServerLeadership adds or removes leadership label on a single server pod.
func (r *Resources) ensureSingleServerLeader(ctx context.Context, cachedStatus inspectorInterface.Inspector) error {
changed := false
enabled := features.FailoverLeadership().Enabled()
var leaderID string
if enabled {
leaderIDs, err := r.getSingleServerLeaderID(ctx)
if err != nil {
return err
}
if len(leaderIDs) == 1 {
leaderID = leaderIDs[0]
} else if len(leaderIDs) > 1 {
r.log.Error("multiple leaders found: %s. Blocking traffic to the deployment services", strings.Join(leaderIDs, ", "))
}
}
status := r.context.GetStatus()
for _, m := range status.Members.Single {
pod, exist := cachedStatus.Pod().V1().GetSimple(m.Pod.GetName())
if !exist {
continue
}
labels := pod.GetLabels()
if enabled && m.ID == leaderID {
if value, ok := labels[k8sutil.LabelKeyArangoLeader]; ok && value == "true" {
// Single server is available, and it has a leader label.
continue
}
labels = addLabel(labels, k8sutil.LabelKeyArangoLeader, "true")
} else {
if _, ok := labels[k8sutil.LabelKeyArangoLeader]; !ok {
// Single server is not available, and it does not have a leader label.
continue
}
delete(labels, k8sutil.LabelKeyArangoLeader)
}
err := r.context.ApplyPatchOnPod(ctx, pod, patch.ItemReplace(patch.NewPath("metadata", "labels"), labels))
if err != nil {
return errors.WithMessagef(err, "unable to change leader label for pod %s", m.Pod.GetName())
}
changed = true
}
if changed {
return errors.Reconcile()
}
return r.ensureSingleServerLeaderServices(ctx, cachedStatus)
}
// ensureSingleServerLeaderServices adds a leadership label to deployment service and external deployment service.
func (r *Resources) ensureSingleServerLeaderServices(ctx context.Context, cachedStatus inspectorInterface.Inspector) error {
// Add a leadership label to deployment service and external deployment service.
deploymentName := r.context.GetAPIObject().GetName()
changed := false
services := []string{
k8sutil.CreateDatabaseClientServiceName(deploymentName),
k8sutil.CreateDatabaseExternalAccessServiceName(deploymentName),
}
enabled := features.FailoverLeadership().Enabled()
for _, svcName := range services {
svc, exists := cachedStatus.Service().V1().GetSimple(svcName)
if !exists {
// It will be created later with a leadership label.
continue
}
selector := svc.Spec.Selector
if enabled {
if v, ok := selector[k8sutil.LabelKeyArangoLeader]; ok && v == "true" {
// It is already OK.
continue
}
selector = addLabel(selector, k8sutil.LabelKeyArangoLeader, "true")
} else {
if _, ok := selector[k8sutil.LabelKeyArangoLeader]; !ok {
// Service does not have a leader label, and it should not have.
continue
}
delete(selector, k8sutil.LabelKeyArangoLeader)
}
parser := patch.Patch([]patch.Item{patch.ItemReplace(patch.NewPath("spec", "selector"), selector)})
data, err := parser.Marshal()
if err != nil {
return errors.WithMessagef(err, "unable to marshal labels for service %s", svcName)
}
err = globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
_, err := cachedStatus.ServicesModInterface().V1().Patch(ctxChild, svcName, types.JSONPatchType, data, meta.PatchOptions{})
return err
})
if err != nil {
return errors.WithMessagef(err, "unable to patch labels for service %s", svcName)
}
changed = true
}
if changed {
return errors.Reconcile()
}
return nil
}