Skip to content

Commit

Permalink
Merge pull request #292 from yersan/issue-291
Browse files Browse the repository at this point in the history
[291] Cleanup scaled down pod status
  • Loading branch information
yersan authored Feb 7, 2024
2 parents eb2f1df + d306d01 commit db216f8
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions controllers/wildflyserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ func (r *WildFlyServerReconciler) Reconcile(ctx context.Context, request ctrl.Re
numberOfDeployedPods := int32(len(podList.Items))
numberOfPodsToScaleDown := statefulsetSpecSize - wildflyServerSpecSize // difference between desired pod count and the current number of pods

// If there are pods clean and have been removed, remove the status now
if updated, err := r.cleanUpPodStatus(wildflyServer, podList); err != nil {
log.Error(err, "Failed to clean up pod status after scaling down", "WildFlyServer.Name", wildflyServer.Name)
return reconcile.Result{}, err
} else if updated {
return reconcile.Result{Requeue: true}, nil
}

// if the number of desired replica size (aka. WildflyServer.Spec.Replicas) is different from the number of active pods
// and the statefulset replica size was already changed to follow the value defined by the wildflyserver spec then wait for sts to reconcile
if statefulsetSpecSize == wildflyServerSpecSize && numberOfDeployedPods != wildflyServerSpecSize {
Expand Down Expand Up @@ -548,3 +556,37 @@ func hasServiceMonitor() bool {
Kind: monitoringv1.ServiceMonitorsKind,
})
}

// cleanUpPodStatus clean up the pod status for pods that have been scaled down to be consistent with the current list of pods read from the cluster
func (r *WildFlyServerReconciler) cleanUpPodStatus(w *wildflyv1alpha1.WildFlyServer, podList *corev1.PodList) (bool, error) {
var result []wildflyv1alpha1.PodStatus
Outer:
for _, v := range w.Status.Pods {
if v.State == wildflyv1alpha1.PodStateScalingDownClean {
for _, pv := range podList.Items {
if pv.Name == v.Name {
result = append(result, v)
continue Outer
}
}
} else {
result = append(result, v)
}
}
if len(result) != len(w.Status.Pods) {
patch := client.MergeFrom(w.DeepCopy())
w.Status.Pods = result
if w.Status.Replicas > 0 {
w.Status.Replicas--
}
if w.Status.ScalingdownPods > 0 {
w.Status.ScalingdownPods--
}

if err := r.Client.Status().Patch(context.Background(), w, patch); err != nil {
return false, err
}
return true, nil
}
return false, nil
}

0 comments on commit db216f8

Please sign in to comment.