-
Notifications
You must be signed in to change notification settings - Fork 159
/
sync_services.go
655 lines (581 loc) · 27 KB
/
sync_services.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
package scyllacluster
import (
"context"
"fmt"
"regexp"
"strconv"
"time"
scyllav1 "github.com/scylladb/scylla-operator/pkg/api/scylla/v1"
"github.com/scylladb/scylla-operator/pkg/controllerhelpers"
"github.com/scylladb/scylla-operator/pkg/naming"
"github.com/scylladb/scylla-operator/pkg/pointer"
"github.com/scylladb/scylla-operator/pkg/resourceapply"
"github.com/scylladb/scylla-operator/pkg/scyllafeatures"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
)
var serviceOrdinalRegex = regexp.MustCompile("^.*-([0-9]+)$")
func (scc *Controller) makeServices(sc *scyllav1.ScyllaCluster, oldServices map[string]*corev1.Service, jobs map[string]*batchv1.Job) ([]*corev1.Service, error) {
services := []*corev1.Service{
IdentityService(sc),
}
for _, rack := range sc.Spec.Datacenter.Racks {
stsName := naming.StatefulSetNameForRack(rack, sc)
for ord := int32(0); ord < rack.Members; ord++ {
svcName := fmt.Sprintf("%s-%d", stsName, ord)
oldSvc := oldServices[svcName]
svc, err := MemberService(sc, rack.Name, svcName, oldSvc, jobs)
if err != nil {
return nil, fmt.Errorf("can't create member service for %d'th node: %w", ord, err)
}
services = append(services, svc)
}
}
return services, nil
}
func (scc *Controller) pruneServices(
ctx context.Context,
sc *scyllav1.ScyllaCluster,
requiredServices []*corev1.Service,
services map[string]*corev1.Service,
statefulSets map[string]*appsv1.StatefulSet,
) ([]metav1.Condition, error) {
var errs []error
var progressingConditions []metav1.Condition
for _, svc := range services {
if svc.DeletionTimestamp != nil {
continue
}
isRequired := false
for _, req := range requiredServices {
if svc.Name == req.Name {
isRequired = true
}
}
if isRequired {
continue
}
// Do not delete services for scale down.
rackName, ok := svc.Labels[naming.RackNameLabel]
if !ok {
errs = append(errs, fmt.Errorf("service %s/%s is missing %q label", svc.Namespace, svc.Name, naming.RackNameLabel))
continue
}
stsName := fmt.Sprintf("%s-%s-%s", sc.Name, sc.Spec.Datacenter.Name, rackName)
sts, ok := statefulSets[stsName]
if !ok {
errs = append(errs, fmt.Errorf("statefulset %s/%s is missing", sc.Namespace, stsName))
continue
}
// TODO: Label services with the ordinal instead of parsing.
// TODO: Move it to a function and unit test it.
svcOrdinalStrings := serviceOrdinalRegex.FindStringSubmatch(svc.Name)
if len(svcOrdinalStrings) != 2 {
errs = append(errs, fmt.Errorf("can't parse ordinal from service %s/%s", svc.Namespace, svc.Name))
continue
}
svcOrdinal, err := strconv.Atoi(svcOrdinalStrings[1])
if err != nil {
errs = append(errs, err)
continue
}
if int32(svcOrdinal) < *sts.Spec.Replicas {
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForAnotherService",
Message: fmt.Sprintf("Service %q is waiting for another service to be decommissioned first.", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
continue
}
// Do not delete services that weren't properly decommissioned.
if svc.Labels[naming.DecommissionedLabel] != naming.LabelValueTrue {
klog.Warningf("Refusing to cleanup service %s/%s whose member wasn't decommissioned.", svc.Namespace, svc.Name)
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForServiceDecommission",
Message: fmt.Sprintf("Waiting for service %q to be fully decommissioned", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
continue
}
// Because we also delete the PVC, we need to recheck the service state with a live call.
// We can't delete the PVC after the service because the deletion wouldn't be retried.
{
freshSvc, err := scc.kubeClient.CoreV1().Services(svc.Namespace).Get(ctx, svc.Name, metav1.GetOptions{})
if err != nil {
errs = append(errs, err)
continue
}
if freshSvc.UID != svc.UID {
klog.V(2).InfoS("Stale caches, won't delete the pvc because the service UIDs don't match.", "Service", klog.KObj(svc))
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForCaches",
Message: fmt.Sprintf("Service %q is outdated because live UID is different. Waiting on service caches to catch up.", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
continue
}
if freshSvc.Labels[naming.DecommissionedLabel] != naming.LabelValueTrue {
klog.V(2).InfoS("Stale caches, won't delete the pvc because the service is no longer decommissioned.", "Service", klog.KObj(svc))
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForCaches",
Message: fmt.Sprintf("Service %q is outdated because the service is no longed decommisioned. Waiting on service caches to catch up.", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
continue
}
}
backgroundPropagationPolicy := metav1.DeletePropagationBackground
controllerhelpers.AddGenericProgressingStatusCondition(&progressingConditions, serviceControllerProgressingCondition, &corev1.PersistentVolumeClaim{}, "delete", sc.Generation)
pvcName := naming.PVCNameForService(svc.Name)
err = scc.kubeClient.CoreV1().PersistentVolumeClaims(svc.Namespace).Delete(ctx, pvcName, metav1.DeleteOptions{
PropagationPolicy: &backgroundPropagationPolicy,
})
if err != nil && !apierrors.IsNotFound(err) {
errs = append(errs, err)
continue
}
controllerhelpers.AddGenericProgressingStatusCondition(&progressingConditions, serviceControllerProgressingCondition, svc, "delete", sc.Generation)
err = scc.kubeClient.CoreV1().Services(svc.Namespace).Delete(ctx, svc.Name, metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{
UID: &svc.UID,
ResourceVersion: &svc.ResourceVersion,
},
PropagationPolicy: &backgroundPropagationPolicy,
})
if err != nil && !apierrors.IsNotFound(err) {
errs = append(errs, err)
continue
}
}
return progressingConditions, utilerrors.NewAggregate(errs)
}
func (scc *Controller) syncServices(
ctx context.Context,
sc *scyllav1.ScyllaCluster,
status *scyllav1.ScyllaClusterStatus,
services map[string]*corev1.Service,
statefulSets map[string]*appsv1.StatefulSet,
jobs map[string]*batchv1.Job,
) ([]metav1.Condition, error) {
if sc.Spec.ExposeOptions != nil && sc.Spec.ExposeOptions.NodeService != nil && sc.Spec.ExposeOptions.NodeService.Type != scyllav1.NodeServiceTypeClusterIP {
supportsExposing, err := scyllafeatures.Supports(sc, scyllafeatures.ExposingScyllaClusterViaServiceOtherThanClusterIP)
if err != nil {
return nil, fmt.Errorf("can't determine if ScyllaDB version %q supports exposing via Service other than ClusterIP: %w", sc.Spec.Version, err)
}
if !supportsExposing {
scc.eventRecorder.Eventf(
sc,
corev1.EventTypeWarning,
"InvalidScyllaDBVersion",
fmt.Sprintf("Requested ScyllaDB version %q does not support exposing via Service other than ClusterIP, use the latest one", sc.Spec.Version),
)
return nil, fmt.Errorf("requested ScyllaDB version %q does not support exposing via Service other than ClusterIP, use the latest one", sc.Spec.Version)
}
}
requiredServices, err := scc.makeServices(sc, services, jobs)
if err != nil {
return nil, fmt.Errorf("can't make services: %w", err)
}
// Delete any excessive Services.
// Delete has to be the fist action to avoid getting stuck on quota.
progressingConditions, err := scc.pruneServices(ctx, sc, requiredServices, services, statefulSets)
if err != nil {
return nil, fmt.Errorf("can't delete Service(s): %w", err)
}
// We need to first propagate ReplaceAddressFirstBoot from status for the new service.
for _, svc := range requiredServices {
_, changed, err := resourceapply.ApplyService(ctx, scc.kubeClient.CoreV1(), scc.serviceLister, scc.eventRecorder, svc, resourceapply.ApplyOptions{})
if changed {
controllerhelpers.AddGenericProgressingStatusCondition(&progressingConditions, serviceControllerProgressingCondition, svc, "apply", sc.Generation)
}
if err != nil {
return progressingConditions, err
}
}
// Replace members.
for _, svc := range services {
_, ok := svc.Labels[naming.ReplaceLabel]
if !ok {
continue
}
klog.V(4).InfoS("Node is labelled with replacement label and will be replaced", "ScyllaCluster", klog.KObj(sc), "Service", klog.KObj(svc))
rackName, ok := svc.Labels[naming.RackNameLabel]
if !ok {
return progressingConditions, fmt.Errorf("service %s/%s is missing %q label", svc.Namespace, svc.Name, naming.RackNameLabel)
}
rackStatus := status.Racks[rackName]
controllerhelpers.SetRackCondition(&rackStatus, scyllav1.RackConditionTypeMemberReplacing)
status.Racks[rackName] = rackStatus
supportsReplaceUsingHostID, err := scyllafeatures.Supports(sc, scyllafeatures.ReplacingNodeUsingHostID)
if err != nil {
return progressingConditions, fmt.Errorf("can't determine if ScyllaCluster %q supports replacing using hostID: %w", naming.ObjRef(sc), err)
}
if supportsReplaceUsingHostID {
klog.V(4).InfoS("Replacing node using HostID", "ScyllaCluster", klog.KObj(sc), "Service", klog.KObj(svc))
pcs, err := scc.replaceNodeUsingHostID(ctx, sc, svc)
if err != nil {
return progressingConditions, fmt.Errorf("can't replace node using Host ID: %w", err)
}
progressingConditions = append(progressingConditions, pcs...)
} else {
klog.V(4).InfoS("Replacing node using ClusterIP", "ScyllaCluster", klog.KObj(sc), "Service", klog.KObj(svc))
pcs, err := scc.replaceNodeUsingClusterIP(ctx, sc, status, svc, rackName)
if err != nil {
return progressingConditions, fmt.Errorf("can't replace node using ClusterIP: %w", err)
}
progressingConditions = append(progressingConditions, pcs...)
}
}
return progressingConditions, nil
}
func (scc *Controller) replaceNodeUsingHostID(ctx context.Context, sc *scyllav1.ScyllaCluster, svc *corev1.Service) ([]metav1.Condition, error) {
var progressingConditions []metav1.Condition
nodeHostID, ok := svc.Annotations[naming.HostIDAnnotation]
if !ok {
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForHostIDAnnotationBeforeReplacement",
Message: fmt.Sprintf("Waiting to observe node Host ID annotation on service %q before node is replaced", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
return progressingConditions, nil
}
replacingNodeHostID, ok := svc.Labels[naming.ReplacingNodeHostIDLabel]
if !ok {
pcs, err := scc.initializeReplaceNodeUsingHostID(ctx, sc, svc, nodeHostID)
if err != nil {
return progressingConditions, fmt.Errorf("can't initialize replace using HostID: %w", err)
}
progressingConditions = append(progressingConditions, pcs...)
} else {
pcs, err := scc.finishOngoingReplaceNodeUsingHostID(ctx, sc, svc, nodeHostID, replacingNodeHostID)
if err != nil {
return progressingConditions, fmt.Errorf("can't finish replace using HostID: %w", err)
}
progressingConditions = append(progressingConditions, pcs...)
}
return progressingConditions, nil
}
func (scc *Controller) initializeReplaceNodeUsingHostID(ctx context.Context, sc *scyllav1.ScyllaCluster, svc *corev1.Service, nodeHostID string) ([]metav1.Condition, error) {
klog.V(2).InfoS("Initiating node replacement", "ScyllaCluster", klog.KObj(sc), "Service", klog.KObj(svc), "HostID", nodeHostID)
var progressingConditions []metav1.Condition
pcs, err := scc.removePodAndAssociatedPVC(ctx, sc, svc)
if err != nil {
return progressingConditions, fmt.Errorf("can't remove ScyllaCluster %q Pod %q and associated PVC: %w", naming.ObjRef(sc), naming.ObjRef(svc), err)
}
progressingConditions = append(progressingConditions, pcs...)
// Update the member Service.
klog.V(2).InfoS("Updating member service with Host ID of a node it is replacing",
"ScyllaCluster", klog.KObj(sc),
"Service", klog.KObj(svc),
"HostID", svc.Annotations[naming.HostIDAnnotation],
)
svcCopy := svc.DeepCopy()
svcCopy.Labels[naming.ReplacingNodeHostIDLabel] = nodeHostID
controllerhelpers.AddGenericProgressingStatusCondition(&progressingConditions, serviceControllerProgressingCondition, svcCopy, "update", sc.Generation)
_, err = scc.kubeClient.CoreV1().Services(svcCopy.Namespace).Update(ctx, svcCopy, metav1.UpdateOptions{})
resourceapply.ReportUpdateEvent(scc.eventRecorder, svc, err)
if err != nil {
return progressingConditions, err
}
// We have updated the service. Wait for re-queue.
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingToObserveUpdatedService",
Message: fmt.Sprintf("Waiting to observe updated service %q to proceeed with other services.", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
return progressingConditions, nil
}
func (scc *Controller) finishOngoingReplaceNodeUsingHostID(ctx context.Context, sc *scyllav1.ScyllaCluster, svc *corev1.Service, nodeHostID, replacingNodeHostID string) ([]metav1.Condition, error) {
klog.V(4).InfoS("Node is being replaced, awaiting node to become ready", "ScyllaCluster", klog.KObj(sc), "Service", klog.KObj(svc))
var progressingConditions []metav1.Condition
pod, err := scc.podLister.Pods(svc.Namespace).Get(svc.Name)
if err != nil {
if !apierrors.IsNotFound(err) {
return progressingConditions, err
}
klog.V(2).InfoS("Pod has not been recreated by the StatefulSet controller yet",
"ScyllaCluster", klog.KObj(sc),
"Pod", klog.KObj(svc),
)
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForPodRecreation",
Message: fmt.Sprintf("Service %q is waiting for StatefulSet controller to recreate pod.", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
return progressingConditions, nil
}
if nodeHostID == replacingNodeHostID {
klog.V(2).InfoS("Node is still replacing, Host ID annotation wasn't updated",
"ScyllaCluster", klog.KObj(sc),
"Service", klog.KObj(svc),
"NodeHostID", nodeHostID,
"ReplacingNodeHostID", replacingNodeHostID,
)
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForUpdatedHostIDAnnotation",
Message: fmt.Sprintf("Service %q is waiting updated Host ID annotation", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
return progressingConditions, nil
}
sc = scc.resolveScyllaClusterControllerThroughStatefulSet(pod)
if sc == nil {
return progressingConditions, fmt.Errorf("pod %q is not owned by us anymore", naming.ObjRef(pod))
}
// We could still see an old pod in the caches - verify with a live call.
podReady, pod, err := controllerhelpers.IsPodReadyWithPositiveLiveCheck(ctx, scc.kubeClient.CoreV1(), pod)
if err != nil {
return progressingConditions, err
}
if !podReady {
klog.V(2).InfoS("Pod isn't ready yet",
"ScyllaCluster", klog.KObj(sc),
"Pod", klog.KObj(pod),
)
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForPodReadiness",
Message: fmt.Sprintf("Service %q is waiting for Pod %q to become ready.", naming.ObjRef(svc), naming.ObjRef(pod)),
ObservedGeneration: sc.Generation,
})
return progressingConditions, nil
}
scc.eventRecorder.Eventf(svc, corev1.EventTypeNormal, "FinishedReplacingNode", "New pod %s/%s is ready.", pod.Namespace, pod.Name)
svcCopy := svc.DeepCopy()
delete(svcCopy.Labels, naming.ReplaceLabel)
delete(svcCopy.Labels, naming.ReplacingNodeHostIDLabel)
controllerhelpers.AddGenericProgressingStatusCondition(&progressingConditions, serviceControllerProgressingCondition, svcCopy, "update", sc.Generation)
_, err = scc.kubeClient.CoreV1().Services(svcCopy.Namespace).Update(ctx, svcCopy, metav1.UpdateOptions{})
resourceapply.ReportUpdateEvent(scc.eventRecorder, svc, err)
if err != nil {
return progressingConditions, err
}
return progressingConditions, nil
}
func (scc *Controller) replaceNodeUsingClusterIP(ctx context.Context, sc *scyllav1.ScyllaCluster, status *scyllav1.ScyllaClusterStatus, svc *corev1.Service, rackName string) ([]metav1.Condition, error) {
var progressingConditions []metav1.Condition
replaceAddr, ok := svc.Labels[naming.ReplaceLabel]
if !ok {
return progressingConditions, nil
}
if len(replaceAddr) == 0 {
pcs, err := scc.initializeReplaceNodeUsingClusterIP(ctx, sc, status, svc, rackName)
if err != nil {
return progressingConditions, fmt.Errorf("can't initialize replace using ClusterIP: %w", err)
}
progressingConditions = append(progressingConditions, pcs...)
} else {
pcs, err := scc.finishOngoingReplaceNodeUsingClusterIP(ctx, sc, status, svc, rackName)
if err != nil {
return progressingConditions, fmt.Errorf("can't finish replace using ClusterIP: %w", err)
}
progressingConditions = append(progressingConditions, pcs...)
}
return progressingConditions, nil
}
func (scc *Controller) initializeReplaceNodeUsingClusterIP(ctx context.Context, sc *scyllav1.ScyllaCluster, status *scyllav1.ScyllaClusterStatus, svc *corev1.Service, rackName string) ([]metav1.Condition, error) {
var progressingConditions []metav1.Condition
// Member needs to be replaced.
// Save replace address in RackStatus
if len(status.Racks[rackName].ReplaceAddressFirstBoot[svc.Name]) == 0 {
status.Racks[rackName].ReplaceAddressFirstBoot[svc.Name] = svc.Spec.ClusterIP
klog.V(2).InfoS("Adding member address to replace address list", "Member", svc.Name, "IP", svc.Spec.ClusterIP, "ReplaceAddresses", status.Racks[rackName].ReplaceAddressFirstBoot)
// Make sure the address is stored before proceeding further.
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingToPickUpReplaceAddress",
Message: fmt.Sprintf("Service %q is waiting to pick up stored replace address.", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
return progressingConditions, nil
}
pcs, err := scc.removePodAndAssociatedPVC(ctx, sc, svc)
if err != nil {
return progressingConditions, fmt.Errorf("can't remove ScyllaCluster %q Pod %q and associated PVC: %w", naming.ObjRef(sc), naming.ObjRef(svc), err)
}
progressingConditions = append(progressingConditions, pcs...)
// Delete the member Service.
klog.V(2).InfoS("Deleting the member service to replace member",
"ScyllaCluster", klog.KObj(sc),
"Service", klog.KObj(svc),
)
controllerhelpers.AddGenericProgressingStatusCondition(&progressingConditions, serviceControllerProgressingCondition, svc, "delete", sc.Generation)
err = scc.kubeClient.CoreV1().Services(svc.Namespace).Delete(ctx, svc.Name, metav1.DeleteOptions{
PropagationPolicy: pointer.Ptr(metav1.DeletePropagationBackground),
Preconditions: &metav1.Preconditions{
UID: &svc.UID,
// Delete only if its the version we see in cache. (Has the same replace label state.)
ResourceVersion: &svc.ResourceVersion,
},
})
if err != nil {
if apierrors.IsNotFound(err) {
klog.V(4).InfoS("Pod not found", "Pod", klog.ObjectRef{Namespace: svc.Namespace, Name: svc.Name})
} else {
resourceapply.ReportDeleteEvent(scc.eventRecorder, svc, err)
return progressingConditions, err
}
} else {
resourceapply.ReportDeleteEvent(scc.eventRecorder, svc, nil)
// FIXME: The pod could have been already up and read the old ClusterIP - make sure it will restart.
// We can't delete the pod here as it wouldn't retry failures.
// We have deleted the service. Wait for re-queue.
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingToObserveDeletedService",
Message: fmt.Sprintf("Waiting to observe service %q as deleted to proceeed with other services.", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
}
return progressingConditions, nil
}
func (scc *Controller) finishOngoingReplaceNodeUsingClusterIP(ctx context.Context, sc *scyllav1.ScyllaCluster, status *scyllav1.ScyllaClusterStatus, svc *corev1.Service, rackName string) ([]metav1.Condition, error) {
var progressingConditions []metav1.Condition
// Member is being replaced. Wait for readiness and clear the replace label.
pod, err := scc.podLister.Pods(svc.Namespace).Get(svc.Name)
if err != nil {
if !apierrors.IsNotFound(err) {
return progressingConditions, err
}
klog.V(2).InfoS("Pod has not been recreated by the StatefulSet controller yet",
"ScyllaCluster", klog.KObj(sc),
"Pod", klog.KObj(svc),
)
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForPodRecreation",
Message: fmt.Sprintf("Service %q is waiting for StatefulSet controller to recreate pod.", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
} else {
sc := scc.resolveScyllaClusterControllerThroughStatefulSet(pod)
if sc == nil {
return progressingConditions, fmt.Errorf("pod %q is not owned by us anymore", naming.ObjRef(pod))
}
// We could still see an old pod in the caches - verify with a live call.
podReady, pod, err := controllerhelpers.IsPodReadyWithPositiveLiveCheck(ctx, scc.kubeClient.CoreV1(), pod)
if err != nil {
return progressingConditions, err
}
if podReady {
scc.eventRecorder.Eventf(svc, corev1.EventTypeNormal, "FinishedReplacingNode", "New pod %s/%s is ready.", pod.Namespace, pod.Name)
svcCopy := svc.DeepCopy()
delete(svcCopy.Labels, naming.ReplaceLabel)
controllerhelpers.AddGenericProgressingStatusCondition(&progressingConditions, serviceControllerProgressingCondition, svcCopy, "update", sc.Generation)
_, err := scc.kubeClient.CoreV1().Services(svcCopy.Namespace).Update(ctx, svcCopy, metav1.UpdateOptions{})
resourceapply.ReportUpdateEvent(scc.eventRecorder, svc, err)
if err != nil {
return progressingConditions, err
}
delete(status.Racks[rackName].ReplaceAddressFirstBoot, svc.Name)
} else {
klog.V(2).InfoS("Pod isn't ready yet",
"ScyllaCluster", klog.KObj(sc),
"Pod", klog.KObj(pod),
)
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForPodReadiness",
Message: fmt.Sprintf("Service %q is waiting for Pod %q to become ready.", naming.ObjRef(svc), naming.ObjRef(pod)),
ObservedGeneration: sc.Generation,
})
}
}
return progressingConditions, nil
}
func (scc *Controller) removePodAndAssociatedPVC(ctx context.Context, sc *scyllav1.ScyllaCluster, svc *corev1.Service) ([]metav1.Condition, error) {
var progressingConditions []metav1.Condition
backgroundPropagationPolicy := metav1.DeletePropagationBackground
// First, Delete the PVC if it exists.
// The PVC has finalizer protection so it will wait for the pod to be deleted.
// We can't do it the other way around or the pod could be recreated before we delete the PVC.
pvcMeta := &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: svc.Namespace,
Name: naming.PVCNameForPod(svc.Name),
},
}
klog.V(2).InfoS("Deleting the PVC to replace member",
"ScyllaCluster", klog.KObj(sc),
"Service", klog.KObj(svc),
"PVC", klog.KObj(pvcMeta),
)
controllerhelpers.AddGenericProgressingStatusCondition(&progressingConditions, serviceControllerProgressingCondition, pvcMeta, "delete", sc.Generation)
err := scc.kubeClient.CoreV1().PersistentVolumeClaims(pvcMeta.Namespace).Delete(ctx, pvcMeta.Name, metav1.DeleteOptions{
PropagationPolicy: &backgroundPropagationPolicy,
})
if err != nil {
if !apierrors.IsNotFound(err) {
resourceapply.ReportDeleteEvent(scc.eventRecorder, pvcMeta, err)
return progressingConditions, err
}
klog.V(4).InfoS("PVC not found", "PVC", klog.KObj(pvcMeta))
}
resourceapply.ReportDeleteEvent(scc.eventRecorder, pvcMeta, nil)
// Hack: Sleeps are terrible thing to in sync logic but this compensates for a broken
// StatefulSet controller in Kubernetes. StatefulSet doesn't reconcile PVCs and decides
// it's presence only from its cache, and never recreates it if it's missing, only when it
// creates the pod. This gives StatefulSet controller a chance to see the PVC was deleted
// before deleting the pod.
progressingConditions = append(progressingConditions, metav1.Condition{
Type: serviceControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: "WaitingForStatefulSetToObservePVCDeletion",
Message: "Waiting for StatefulSet controller to observe PVC deletion.",
ObservedGeneration: sc.Generation,
})
time.Sleep(10 * time.Second)
// Evict the Pod if it exists.
podMeta := &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: svc.Namespace,
Name: svc.Name,
},
}
klog.V(2).InfoS("Deleting the Pod to replace member",
"ScyllaCluster", klog.KObj(sc),
"Service", klog.KObj(svc),
"Pod", klog.KObj(podMeta),
)
controllerhelpers.AddGenericProgressingStatusCondition(&progressingConditions, serviceControllerProgressingCondition, podMeta, "delete", sc.Generation)
// TODO: Revert back to eviction when it's fixed in kubernetes 1.19.z (#732)
// (https://github.com/kubernetes/kubernetes/issues/103970)
err = scc.kubeClient.CoreV1().Pods(podMeta.Namespace).Delete(ctx, podMeta.Name, metav1.DeleteOptions{
PropagationPolicy: &backgroundPropagationPolicy,
})
if err != nil {
if !apierrors.IsNotFound(err) {
resourceapply.ReportDeleteEvent(scc.eventRecorder, podMeta, err)
return progressingConditions, err
}
klog.V(4).InfoS("Pod not found", "Pod", klog.ObjectRef{Namespace: svc.Namespace, Name: svc.Name})
}
resourceapply.ReportDeleteEvent(scc.eventRecorder, podMeta, nil)
return progressingConditions, nil
}