Skip to content

Commit 7e77a55

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 <[email protected]>
1 parent e05e5c1 commit 7e77a55

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

controllers/zookeepercluster_controller.go

+44-6
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,38 @@ 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+
if errors.IsNotFound(err) {
739+
return nil
740+
}
741+
return err
742+
}
743+
710744
// 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 {
712746
pvcCount, err := r.getPVCCount(instance)
713747
if err != nil {
714748
return err
715749
}
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) {
718755
pvcList, err := r.getPVCList(instance)
719756
if err != nil {
720757
return err
721758
}
722759
for _, pvcItem := range pvcList.Items {
723760
// 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)
725763
r.deletePVC(pvcItem)
726764
}
727765
}

0 commit comments

Comments
 (0)