Skip to content

Commit 6bfd811

Browse files
committed
Check Orphan PVC before updating STS
- Move Status.Repicas and Status.ReadyReplicas update to reconcileClusterStatus - Use StatefulSet Replicas when checking orphan PVC - Check need for PVC cleanup before updating STS Signed-off-by: hoyhbx <hoyhbx@gmail.com>
1 parent cd732b9 commit 6bfd811

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

controllers/zookeepercluster_controller.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@ func (r *ZookeeperClusterReconciler) reconcileStatefulSet(instance *zookeeperv1b
211211
} else if err != nil {
212212
return err
213213
} 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+
214228
// check whether zookeeperCluster is updated before updating the sts
215229
cmp := compareResourceVersion(instance, foundSts)
216230
if cmp < 0 {
@@ -258,8 +272,6 @@ func (r *ZookeeperClusterReconciler) updateStatefulSet(instance *zookeeperv1beta
258272
if err != nil {
259273
return err
260274
}
261-
instance.Status.Replicas = foundSts.Status.Replicas
262-
instance.Status.ReadyReplicas = foundSts.Status.ReadyReplicas
263275
return nil
264276
}
265277

@@ -558,6 +570,15 @@ func (r *ZookeeperClusterReconciler) reconcileClusterStatus(instance *zookeeperv
558570
instance.Status.Members.Ready = readyMembers
559571
instance.Status.Members.Unready = unreadyMembers
560572

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+
561582
// If Cluster is in a ready state...
562583
if instance.Spec.Replicas == instance.Status.ReadyReplicas && (!instance.Status.MetaRootCreated) {
563584
r.Log.Info("Cluster is Ready, Creating ZK Metadata...")
@@ -707,21 +728,35 @@ func (r *ZookeeperClusterReconciler) getPVCCount(instance *zookeeperv1beta1.Zook
707728
}
708729

709730
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+
return err
739+
}
740+
710741
// 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 {
742+
if foundSts.Status.ReadyReplicas == foundSts.Status.Replicas {
712743
pvcCount, err := r.getPVCCount(instance)
713744
if err != nil {
714745
return err
715746
}
716-
r.Log.Info("cleanupOrphanPVCs", "PVC Count", pvcCount, "ReadyReplicas Count", instance.Status.ReadyReplicas)
717-
if pvcCount > int(instance.Spec.Replicas) {
747+
748+
r.Log.Info("cleanupOrphanPVCs",
749+
"PVC Count", pvcCount,
750+
"Replicas Count", foundSts.Spec.Replicas)
751+
if pvcCount > int(*foundSts.Spec.Replicas) {
718752
pvcList, err := r.getPVCList(instance)
719753
if err != nil {
720754
return err
721755
}
722756
for _, pvcItem := range pvcList.Items {
723757
// delete only Orphan PVCs
724-
if utils.IsPVCOrphan(pvcItem.Name, instance.Spec.Replicas) {
758+
if utils.IsPVCOrphan(pvcItem.Name, *foundSts.Spec.Replicas) {
759+
r.Log.Info("cleanupOrphanPVCs", "Deleting Orphan PVC", pvcItem.Name)
725760
r.deletePVC(pvcItem)
726761
}
727762
}

0 commit comments

Comments
 (0)