@@ -211,6 +211,20 @@ func (r *ZookeeperClusterReconciler) reconcileStatefulSet(instance *zookeeperv1b
211
211
} else if err != nil {
212
212
return err
213
213
} else {
214
+ // check whether orphans PVCs need to be deleted before updating the sts
215
+ if instance .Spec .Persistence != nil &&
216
+ instance .Spec .Persistence .VolumeReclaimPolicy == zookeeperv1beta1 .VolumeReclaimPolicyDelete {
217
+ pvcCount , err := r .getPVCCount (instance )
218
+ if err != nil {
219
+ return err
220
+ }
221
+ r .Log .Info ("PVC count" , "count" , pvcCount , "replicas" , foundSts .Status .Replicas , "cr replicas" , instance .Spec .Replicas )
222
+ if pvcCount > int (foundSts .Status .Replicas ) {
223
+ r .Log .Info ("Deleting PVCs" , "count" , pvcCount , "replicas" , instance .Status .Replicas )
224
+ return nil
225
+ }
226
+ }
227
+
214
228
// check whether zookeeperCluster is updated before updating the sts
215
229
cmp := compareResourceVersion (instance , foundSts )
216
230
if cmp < 0 {
@@ -258,8 +272,6 @@ func (r *ZookeeperClusterReconciler) updateStatefulSet(instance *zookeeperv1beta
258
272
if err != nil {
259
273
return err
260
274
}
261
- instance .Status .Replicas = foundSts .Status .Replicas
262
- instance .Status .ReadyReplicas = foundSts .Status .ReadyReplicas
263
275
return nil
264
276
}
265
277
@@ -558,6 +570,15 @@ func (r *ZookeeperClusterReconciler) reconcileClusterStatus(instance *zookeeperv
558
570
instance .Status .Members .Ready = readyMembers
559
571
instance .Status .Members .Unready = unreadyMembers
560
572
573
+ foundSts := & appsv1.StatefulSet {}
574
+ err = r .Client .Get (context .TODO (), types.NamespacedName {
575
+ Name : instance .GetName (),
576
+ Namespace : instance .Namespace ,
577
+ }, foundSts )
578
+
579
+ instance .Status .Replicas = foundSts .Status .Replicas
580
+ instance .Status .ReadyReplicas = foundSts .Status .ReadyReplicas
581
+
561
582
// If Cluster is in a ready state...
562
583
if instance .Spec .Replicas == instance .Status .ReadyReplicas && (! instance .Status .MetaRootCreated ) {
563
584
r .Log .Info ("Cluster is Ready, Creating ZK Metadata..." )
@@ -707,21 +728,38 @@ func (r *ZookeeperClusterReconciler) getPVCCount(instance *zookeeperv1beta1.Zook
707
728
}
708
729
709
730
func (r * ZookeeperClusterReconciler ) cleanupOrphanPVCs (instance * zookeeperv1beta1.ZookeeperCluster ) (err error ) {
731
+ // get the up to date STS
732
+ foundSts := & appsv1.StatefulSet {}
733
+ err = r .Client .Get (context .TODO (), types.NamespacedName {
734
+ Name : instance .GetName (),
735
+ Namespace : instance .Namespace ,
736
+ }, foundSts )
737
+ if err != nil {
738
+ if errors .IsNotFound (err ) {
739
+ return nil
740
+ }
741
+ return err
742
+ }
743
+
710
744
// this check should make sure we do not delete the PVCs before the STS has scaled down
711
- if instance .Status .ReadyReplicas == instance . Spec .Replicas {
745
+ if foundSts .Status .ReadyReplicas == foundSts . Status .Replicas {
712
746
pvcCount , err := r .getPVCCount (instance )
713
747
if err != nil {
714
748
return err
715
749
}
716
- r .Log .Info ("cleanupOrphanPVCs" , "PVC Count" , pvcCount , "ReadyReplicas Count" , instance .Status .ReadyReplicas )
717
- if pvcCount > int (instance .Spec .Replicas ) {
750
+
751
+ r .Log .Info ("cleanupOrphanPVCs" ,
752
+ "PVC Count" , pvcCount ,
753
+ "Replicas Count" , foundSts .Spec .Replicas )
754
+ if pvcCount > int (* foundSts .Spec .Replicas ) {
718
755
pvcList , err := r .getPVCList (instance )
719
756
if err != nil {
720
757
return err
721
758
}
722
759
for _ , pvcItem := range pvcList .Items {
723
760
// delete only Orphan PVCs
724
- if utils .IsPVCOrphan (pvcItem .Name , instance .Spec .Replicas ) {
761
+ if utils .IsPVCOrphan (pvcItem .Name , * foundSts .Spec .Replicas ) {
762
+ r .Log .Info ("cleanupOrphanPVCs" , "Deleting Orphan PVC" , pvcItem .Name )
725
763
r .deletePVC (pvcItem )
726
764
}
727
765
}
0 commit comments