From 94e8c6ffbf3bb03cc1b37d71992703328f90e0fa Mon Sep 17 00:00:00 2001 From: Johannes Nicolai Date: Fri, 22 Jan 2021 02:11:21 +0100 Subject: [PATCH] minReplicas <= desiredReplicas <= maxReplicas (#267) * ensure that minReplicas <= desiredReplicas <= maxReplicas no matter what * before this change, if the number of runners was much larger than the max number, the applied scale down factor might still result in a desired value > maxReplicas * if for resource constraints in the cluster, runners would be permanently restarted, the number of runners could go up more than the reverse scale down factor until the next reconciliation round, resulting in a situation where the number of runners climbs up even though it should actually go down * by checking whether the desiredReplicas is always <= maxReplicas, infinite scaling up loops can be prevented --- controllers/autoscaling.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/controllers/autoscaling.go b/controllers/autoscaling.go index ef82aab0..f6c4e92d 100644 --- a/controllers/autoscaling.go +++ b/controllers/autoscaling.go @@ -219,22 +219,19 @@ func (r *HorizontalRunnerAutoscalerReconciler) calculateReplicasByPercentageRunn var desiredReplicas int fractionBusy := float64(numRunnersBusy) / float64(numRunners) if fractionBusy >= scaleUpThreshold { - scaleUpReplicas := int(math.Ceil(float64(numRunners) * scaleUpFactor)) - if scaleUpReplicas > maxReplicas { - desiredReplicas = maxReplicas - } else { - desiredReplicas = scaleUpReplicas - } + desiredReplicas = int(math.Ceil(float64(numRunners) * scaleUpFactor)) } else if fractionBusy < scaleDownThreshold { - scaleDownReplicas := int(float64(numRunners) * scaleDownFactor) - if scaleDownReplicas < minReplicas { - desiredReplicas = minReplicas - } else { - desiredReplicas = scaleDownReplicas - } + desiredReplicas = int(float64(numRunners) * scaleDownFactor) } else { desiredReplicas = *rd.Spec.Replicas } + + if desiredReplicas < minReplicas { + desiredReplicas = minReplicas + } else if desiredReplicas > maxReplicas { + desiredReplicas = maxReplicas + } + r.Log.V(1).Info( "Calculated desired replicas",