-
Notifications
You must be signed in to change notification settings - Fork 211
/
zookeepercluster_controller.go
619 lines (580 loc) · 20 KB
/
zookeepercluster_controller.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
/**
* Copyright (c) 2018 Dell Inc., or its subsidiaries. All Rights Reserved.
*
* 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
*/
package zookeepercluster
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"github.com/pravega/zookeeper-operator/pkg/utils"
"github.com/pravega/zookeeper-operator/pkg/yamlexporter"
"k8s.io/apimachinery/pkg/labels"
"github.com/go-logr/logr"
"github.com/pravega/zookeeper-operator/pkg/zk"
corev1 "k8s.io/api/core/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
zookeeperv1beta1 "github.com/pravega/zookeeper-operator/pkg/apis/zookeeper/v1beta1"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/source"
)
// ReconcileTime is the delay between reconciliations
const ReconcileTime = 30 * time.Second
var log = logf.Log.WithName("controller_zookeepercluster")
// AddZookeeperReconciler creates a new ZookeeperCluster Controller and adds it
// to the Manager. The Manager will set fields on the Controller and Start it
// when the Manager is Started.
func AddZookeeperReconciler(mgr manager.Manager) error {
return add(mgr, newZookeeperClusterReconciler(mgr))
}
// newReconciler returns a new reconcile.Reconciler
func newZookeeperClusterReconciler(mgr manager.Manager) reconcile.Reconciler {
return &ReconcileZookeeperCluster{client: mgr.GetClient(), scheme: mgr.GetScheme()}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("zookeepercluster-controller", mgr,
controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource ZookeeperCluster
err = c.Watch(&source.Kind{Type: &zookeeperv1beta1.ZookeeperCluster{}},
&handler.EnqueueRequestForObject{})
if err != nil {
return err
}
// Watch for changes to zookeeper stateful-set secondary resources
err = c.Watch(&source.Kind{Type: &appsv1.StatefulSet{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &zookeeperv1beta1.ZookeeperCluster{},
})
if err != nil {
return err
}
// Watch for changes to zookeeper service secondary resources
err = c.Watch(&source.Kind{Type: &corev1.Service{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &zookeeperv1beta1.ZookeeperCluster{},
})
if err != nil {
return err
}
// Watch for changes to zookeeper pod secondary resources
err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &zookeeperv1beta1.ZookeeperCluster{},
})
if err != nil {
return err
}
return nil
}
var _ reconcile.Reconciler = &ReconcileZookeeperCluster{}
// ReconcileZookeeperCluster reconciles a ZookeeperCluster object
type ReconcileZookeeperCluster struct {
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver
client client.Client
scheme *runtime.Scheme
log logr.Logger
skipSTSReconcile int
}
type reconcileFun func(cluster *zookeeperv1beta1.ZookeeperCluster) error
// Reconcile reads that state of the cluster for a ZookeeperCluster object and
// makes changes based on the state read and what is in the ZookeeperCluster.Spec
func (r *ReconcileZookeeperCluster) Reconcile(request reconcile.Request) (reconcile.Result, error) {
r.log = log.WithValues(
"Request.Namespace", request.Namespace,
"Request.Name", request.Name)
r.log.Info("Reconciling ZookeeperCluster")
// Fetch the ZookeeperCluster instance
instance := &zookeeperv1beta1.ZookeeperCluster{}
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile
// request. Owned objects are automatically garbage collected. For
// additional cleanup logic use finalizers.
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
changed := instance.WithDefaults()
if changed {
r.log.Info("Setting default settings for zookeeper-cluster")
if err := r.client.Update(context.TODO(), instance); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{Requeue: true}, nil
}
for _, fun := range []reconcileFun{
r.reconcileFinalizers,
r.reconcileConfigMap,
r.reconcileStatefulSet,
r.reconcileClientService,
r.reconcileHeadlessService,
r.reconcilePodDisruptionBudget,
r.reconcileClusterStatus,
} {
if err = fun(instance); err != nil {
return reconcile.Result{}, err
}
}
// Recreate any missing resources every 'ReconcileTime'
return reconcile.Result{RequeueAfter: ReconcileTime}, nil
}
func (r *ReconcileZookeeperCluster) reconcileStatefulSet(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
sts := zk.MakeStatefulSet(instance)
if err = controllerutil.SetControllerReference(instance, sts, r.scheme); err != nil {
return err
}
foundSts := &appsv1.StatefulSet{}
err = r.client.Get(context.TODO(), types.NamespacedName{
Name: sts.Name,
Namespace: sts.Namespace,
}, foundSts)
if err != nil && errors.IsNotFound(err) {
r.log.Info("Creating a new Zookeeper StatefulSet",
"StatefulSet.Namespace", sts.Namespace,
"StatefulSet.Name", sts.Name)
err = r.client.Create(context.TODO(), sts)
if err != nil {
return err
}
return nil
} else if err != nil {
return err
} else {
foundSTSSize := *foundSts.Spec.Replicas
newSTSSize := *sts.Spec.Replicas
if newSTSSize < foundSTSSize {
// We're dealing with STS scale down
if !r.isConfigMapInSync(instance) {
r.log.Info("Skipping StatefulSet reconcile as ConfigMap not updated yet.")
return nil
}
/*
After updating ConfigMap we need to wait for changes to sync to the volume,
failing which `zookeeperTeardown.sh` won't get invoked for the pods that are being scaled down
and these will stay in the ensemble config forever.
For details see:
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically
*/
r.skipSTSReconcile++
if r.skipSTSReconcile < 6 {
r.log.Info("Waiting for Config Map update to sync...Skipping STS Reconcile")
return nil
}
}
return r.updateStatefulSet(instance, foundSts, sts)
}
return nil
}
func (r *ReconcileZookeeperCluster) updateStatefulSet(instance *zookeeperv1beta1.ZookeeperCluster, foundSts *appsv1.StatefulSet, sts *appsv1.StatefulSet) (err error) {
r.log.Info("Updating StatefulSet",
"StatefulSet.Namespace", foundSts.Namespace,
"StatefulSet.Name", foundSts.Name)
zk.SyncStatefulSet(foundSts, sts)
err = r.client.Update(context.TODO(), foundSts)
if err != nil {
return err
}
instance.Status.Replicas = foundSts.Status.Replicas
instance.Status.ReadyReplicas = foundSts.Status.ReadyReplicas
r.skipSTSReconcile = 0
return nil
}
func (r *ReconcileZookeeperCluster) isConfigMapInSync(instance *zookeeperv1beta1.ZookeeperCluster) bool {
cm := zk.MakeConfigMap(instance)
foundCm := &corev1.ConfigMap{}
err := r.client.Get(context.TODO(), types.NamespacedName{
Name: cm.Name,
Namespace: cm.Namespace,
}, foundCm)
if err != nil {
r.log.Error(err, "Error getting config map.")
return false
} else {
// found config map, now check number of replicas in configMap
envStr := foundCm.Data["env.sh"]
splitSlice := strings.Split(envStr, "CLUSTER_SIZE=")
if len(splitSlice) < 2 {
r.log.Error(err, "Error: Could not find cluster size in configmap.")
return false
}
cs := strings.TrimSpace(splitSlice[1])
clusterSize, _ := strconv.Atoi(cs)
return (int32(clusterSize) == instance.Spec.Replicas)
}
return false
}
func (r *ReconcileZookeeperCluster) reconcileClientService(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
svc := zk.MakeClientService(instance)
if err = controllerutil.SetControllerReference(instance, svc, r.scheme); err != nil {
return err
}
foundSvc := &corev1.Service{}
err = r.client.Get(context.TODO(), types.NamespacedName{
Name: svc.Name,
Namespace: svc.Namespace,
}, foundSvc)
if err != nil && errors.IsNotFound(err) {
r.log.Info("Creating new client service",
"Service.Namespace", svc.Namespace,
"Service.Name", svc.Name)
err = r.client.Create(context.TODO(), svc)
if err != nil {
return err
}
return nil
} else if err != nil {
return err
} else {
r.log.Info("Updating existing client service",
"Service.Namespace", foundSvc.Namespace,
"Service.Name", foundSvc.Name)
zk.SyncService(foundSvc, svc)
err = r.client.Update(context.TODO(), foundSvc)
if err != nil {
return err
}
port := instance.ZookeeperPorts().Client
instance.Status.InternalClientEndpoint = fmt.Sprintf("%s:%d",
foundSvc.Spec.ClusterIP, port)
if foundSvc.Spec.Type == "LoadBalancer" {
for _, i := range foundSvc.Status.LoadBalancer.Ingress {
if i.IP != "" {
instance.Status.ExternalClientEndpoint = fmt.Sprintf("%s:%d",
i.IP, port)
}
}
} else {
instance.Status.ExternalClientEndpoint = "N/A"
}
}
return nil
}
func (r *ReconcileZookeeperCluster) reconcileHeadlessService(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
svc := zk.MakeHeadlessService(instance)
if err = controllerutil.SetControllerReference(instance, svc, r.scheme); err != nil {
return err
}
foundSvc := &corev1.Service{}
err = r.client.Get(context.TODO(), types.NamespacedName{
Name: svc.Name,
Namespace: svc.Namespace,
}, foundSvc)
if err != nil && errors.IsNotFound(err) {
r.log.Info("Creating new headless service",
"Service.Namespace", svc.Namespace,
"Service.Name", svc.Name)
err = r.client.Create(context.TODO(), svc)
if err != nil {
return err
}
return nil
} else if err != nil {
return err
} else {
r.log.Info("Updating existing headless service",
"Service.Namespace", foundSvc.Namespace,
"Service.Name", foundSvc.Name)
zk.SyncService(foundSvc, svc)
err = r.client.Update(context.TODO(), foundSvc)
if err != nil {
return err
}
}
return nil
}
func (r *ReconcileZookeeperCluster) reconcilePodDisruptionBudget(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
pdb := zk.MakePodDisruptionBudget(instance)
if err = controllerutil.SetControllerReference(instance, pdb, r.scheme); err != nil {
return err
}
foundPdb := &policyv1beta1.PodDisruptionBudget{}
err = r.client.Get(context.TODO(), types.NamespacedName{
Name: pdb.Name,
Namespace: pdb.Namespace,
}, foundPdb)
if err != nil && errors.IsNotFound(err) {
r.log.Info("Creating new pod-disruption-budget",
"PodDisruptionBudget.Namespace", pdb.Namespace,
"PodDisruptionBudget.Name", pdb.Name)
err = r.client.Create(context.TODO(), pdb)
if err != nil {
return err
}
return nil
} else if err != nil {
return err
}
return nil
}
func (r *ReconcileZookeeperCluster) reconcileConfigMap(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
cm := zk.MakeConfigMap(instance)
if err = controllerutil.SetControllerReference(instance, cm, r.scheme); err != nil {
return err
}
foundCm := &corev1.ConfigMap{}
err = r.client.Get(context.TODO(), types.NamespacedName{
Name: cm.Name,
Namespace: cm.Namespace,
}, foundCm)
if err != nil && errors.IsNotFound(err) {
r.log.Info("Creating a new Zookeeper Config Map",
"ConfigMap.Namespace", cm.Namespace,
"ConfigMap.Name", cm.Name)
err = r.client.Create(context.TODO(), cm)
if err != nil {
return err
}
return nil
} else if err != nil {
return err
} else {
r.log.Info("Updating existing config-map",
"ConfigMap.Namespace", foundCm.Namespace,
"ConfigMap.Name", foundCm.Name)
zk.SyncConfigMap(foundCm, cm)
err = r.client.Update(context.TODO(), foundCm)
if err != nil {
return err
}
}
return nil
}
func (r *ReconcileZookeeperCluster) reconcileClusterStatus(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
foundPods := &corev1.PodList{}
labelSelector := labels.SelectorFromSet(map[string]string{"app": instance.GetName()})
listOps := &client.ListOptions{
Namespace: instance.Namespace,
LabelSelector: labelSelector,
}
err = r.client.List(context.TODO(), listOps, foundPods)
if err != nil {
return err
}
var (
readyMembers []string
unreadyMembers []string
)
for _, p := range foundPods.Items {
ready := true
for _, c := range p.Status.ContainerStatuses {
if !c.Ready {
ready = false
}
}
if ready {
readyMembers = append(readyMembers, p.Name)
} else {
unreadyMembers = append(unreadyMembers, p.Name)
}
}
instance.Status.Members.Ready = readyMembers
instance.Status.Members.Unready = unreadyMembers
r.log.Info("Updating zookeeper status",
"StatefulSet.Namespace", instance.Namespace,
"StatefulSet.Name", instance.Name)
return r.client.Status().Update(context.TODO(), instance)
}
// YAMLExporterReconciler returns a fake Reconciler which is being used for generating YAML files
func YAMLExporterReconciler(zookeepercluster *zookeeperv1beta1.ZookeeperCluster) *ReconcileZookeeperCluster {
var scheme = scheme.Scheme
scheme.AddKnownTypes(zookeeperv1beta1.SchemeGroupVersion, zookeepercluster)
return &ReconcileZookeeperCluster{
client: fake.NewFakeClient(zookeepercluster),
scheme: scheme,
}
}
// GenerateYAML generated secondary resource of ZookeeperCluster resources YAML files
func (r *ReconcileZookeeperCluster) GenerateYAML(inst *zookeeperv1beta1.ZookeeperCluster) error {
if inst.WithDefaults() {
fmt.Println("set default values")
}
for _, fun := range []reconcileFun{
r.yamlConfigMap,
r.yamlStatefulSet,
r.yamlClientService,
r.yamlHeadlessService,
r.yamlPodDisruptionBudget,
} {
if err := fun(inst); err != nil {
return err
}
}
return nil
}
// yamlStatefulSet will generates YAML file for StatefulSet
func (r *ReconcileZookeeperCluster) yamlStatefulSet(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
sts := zk.MakeStatefulSet(instance)
if err = controllerutil.SetControllerReference(instance, sts, r.scheme); err != nil {
return err
}
subdir, err := yamlexporter.CreateOutputSubDir(sts.OwnerReferences[0].Kind, sts.Labels["component"])
return yamlexporter.GenerateOutputYAMLFile(subdir, sts.Kind, sts)
}
// yamlClientService will generates YAML file for zookeeper client service
func (r *ReconcileZookeeperCluster) yamlClientService(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
svc := zk.MakeClientService(instance)
if err = controllerutil.SetControllerReference(instance, svc, r.scheme); err != nil {
return err
}
subdir, err := yamlexporter.CreateOutputSubDir(svc.OwnerReferences[0].Kind, "client")
if err != nil {
return err
}
return yamlexporter.GenerateOutputYAMLFile(subdir, svc.Kind, svc)
}
// yamlHeadlessService will generates YAML file for zookeeper headless service
func (r *ReconcileZookeeperCluster) yamlHeadlessService(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
svc := zk.MakeHeadlessService(instance)
if err = controllerutil.SetControllerReference(instance, svc, r.scheme); err != nil {
return err
}
subdir, err := yamlexporter.CreateOutputSubDir(svc.OwnerReferences[0].Kind, "headless")
if err != nil {
return err
}
return yamlexporter.GenerateOutputYAMLFile(subdir, svc.Kind, svc)
}
// yamlPodDisruptionBudget will generates YAML file for zookeeper PDB
func (r *ReconcileZookeeperCluster) yamlPodDisruptionBudget(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
pdb := zk.MakePodDisruptionBudget(instance)
if err = controllerutil.SetControllerReference(instance, pdb, r.scheme); err != nil {
return err
}
subdir, err := yamlexporter.CreateOutputSubDir(pdb.OwnerReferences[0].Kind, "pdb")
if err != nil {
return err
}
return yamlexporter.GenerateOutputYAMLFile(subdir, pdb.Kind, pdb)
}
// yamlConfigMap will generates YAML file for Zookeeper configmap
func (r *ReconcileZookeeperCluster) yamlConfigMap(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
cm := zk.MakeConfigMap(instance)
if err = controllerutil.SetControllerReference(instance, cm, r.scheme); err != nil {
return err
}
subdir, err := yamlexporter.CreateOutputSubDir(cm.OwnerReferences[0].Kind, "config")
if err != nil {
return err
}
return yamlexporter.GenerateOutputYAMLFile(subdir, cm.Kind, cm)
}
func (r *ReconcileZookeeperCluster) reconcileFinalizers(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
if instance.Spec.Persistence.VolumeReclaimPolicy != zookeeperv1beta1.VolumeReclaimPolicyDelete {
return nil
}
if instance.DeletionTimestamp.IsZero() {
if !utils.ContainsString(instance.ObjectMeta.Finalizers, utils.ZkFinalizer) {
instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers, utils.ZkFinalizer)
if err = r.client.Update(context.TODO(), instance); err != nil {
return err
}
}
return r.cleanupOrphanPVCs(instance)
} else {
if utils.ContainsString(instance.ObjectMeta.Finalizers, utils.ZkFinalizer) {
if err = r.cleanUpAllPVCs(instance); err != nil {
return err
}
instance.ObjectMeta.Finalizers = utils.RemoveString(instance.ObjectMeta.Finalizers, utils.ZkFinalizer)
if err = r.client.Update(context.TODO(), instance); err != nil {
return err
}
}
}
return nil
}
func (r *ReconcileZookeeperCluster) getPVCCount(instance *zookeeperv1beta1.ZookeeperCluster) (pvcCount int, err error) {
pvcList, err := r.getPVCList(instance)
if err != nil {
return -1, err
}
pvcCount = len(pvcList.Items)
return pvcCount, nil
}
func (r *ReconcileZookeeperCluster) cleanupOrphanPVCs(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
// this check should make sure we do not delete the PVCs before the STS has scaled down
if instance.Status.ReadyReplicas == instance.Spec.Replicas {
pvcCount, err := r.getPVCCount(instance)
if err != nil {
return err
}
r.log.Info("cleanupOrphanPVCs", "PVC Count", pvcCount, "ReadyReplicas Count", instance.Status.ReadyReplicas)
if pvcCount > int(instance.Spec.Replicas) {
pvcList, err := r.getPVCList(instance)
if err != nil {
return err
}
for _, pvcItem := range pvcList.Items {
// delete only Orphan PVCs
if utils.IsPVCOrphan(pvcItem.Name, instance.Spec.Replicas) {
r.deletePVC(pvcItem)
}
}
}
}
return nil
}
func (r *ReconcileZookeeperCluster) getPVCList(instance *zookeeperv1beta1.ZookeeperCluster) (pvList corev1.PersistentVolumeClaimList, err error) {
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{"app": instance.GetName()},
})
pvclistOps := &client.ListOptions{
Namespace: instance.Namespace,
LabelSelector: selector,
}
pvcList := &corev1.PersistentVolumeClaimList{}
err = r.client.List(context.TODO(), pvclistOps, pvcList)
return *pvcList, err
}
func (r *ReconcileZookeeperCluster) cleanUpAllPVCs(instance *zookeeperv1beta1.ZookeeperCluster) (err error) {
pvcList, err := r.getPVCList(instance)
if err != nil {
return err
}
for _, pvcItem := range pvcList.Items {
r.deletePVC(pvcItem)
}
return nil
}
func (r *ReconcileZookeeperCluster) deletePVC(pvcItem corev1.PersistentVolumeClaim) {
pvcDelete := &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvcItem.Name,
Namespace: pvcItem.Namespace,
},
}
r.log.Info("Deleting PVC", "With Name", pvcItem.Name)
err := r.client.Delete(context.TODO(), pvcDelete)
if err != nil {
r.log.Error(err, "Error deleteing PVC.", "Name", pvcDelete.Name)
}
}