Desired Replicas from CPU Utilization
Problem Statement
The Horizontal Pod Autoscaler scales replicas so average CPU utilization returns to a target. Compute the desired replica count.
Background
The HPA formula is
desired = ceil(current_replicas * current_util / target_util)
where utilizations are percentages. The result is clamped to [min_replicas, max_replicas].
Your Task
def desired_replicas(current_replicas, current_util, target_util, min_r, max_r):
Return the clamped desired replica count (int).
Input Format
- current_replicas (int), current_util, target_util (float, percent), min_r, max_r (int).
Output Format
- A single int.
Sample
print(desired_replicas(3, 90.0, 60.0, 1, 10))
Output:
5
Example
print(desired_replicas(3, 90.0, 60.0, 1, 10))
5
- Calculate the raw desired replica count by scaling the current replicas according to the ratio of current to target CPU utilization:
- Apply the ceiling function to the result to ensure the replica count is an integer and sufficient to meet the target, since we cannot have a fraction of a pod:
- Clamp the calculated value to the allowed range by taking the maximum of the minimum replicas and the minimum of the maximum replicas and the calculated value:
- The final output is 5
Constraints
desired = ceil(current_replicas * current_util / target_util).- Clamp to
[min_r, max_r]. - Return an int.