From d4cb4a57b964e28c4fc1466083c03fc09dce5498 Mon Sep 17 00:00:00 2001 From: Yeray Borges Date: Mon, 12 Feb 2024 17:39:18 +0000 Subject: [PATCH] Fix replicas and ScalingdownPods could be wrongly calculated when there are more than one pod scaled down and recycled --- controllers/wildflyserver_controller.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/controllers/wildflyserver_controller.go b/controllers/wildflyserver_controller.go index 34cddfda..1dfa09ce 100644 --- a/controllers/wildflyserver_controller.go +++ b/controllers/wildflyserver_controller.go @@ -573,16 +573,14 @@ Outer: result = append(result, v) } } - if len(result) != len(w.Status.Pods) { + totalOld := len(w.Status.Pods) + totalNew := len(result) + if totalOld != totalNew { 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-- - } - + delta := totalOld - totalNew + w.Status.Replicas = Max(w.Status.Replicas-int32(delta), 0) + w.Status.ScalingdownPods = Max(w.Status.ScalingdownPods-int32(delta), 0) if err := r.Client.Status().Patch(context.Background(), w, patch); err != nil { return false, err } @@ -590,3 +588,10 @@ Outer: } return false, nil } + +func Max(x, y int32) int32 { + if x < y { + return y + } + return x +}