Minimum Rollout Steps Under Surge and Unavailability
Problem Statement
Simulate a rolling update and count how many batch steps it takes to move from all-old to all-new pods, respecting maxSurge and maxUnavailable.
Background
Start with replicas old pods, target replicas new pods. Total pods may range within [replicas - maxUnavailable, replicas + maxSurge]. Each step, the controller: (1) creates as many new pods as surge allows (up to replicas + maxSurge total), then (2) deletes old pods as long as available (non-terminating) pods stay >= replicas - maxUnavailable. Count steps until all pods are new. Model each step as: can_add = (replicas + maxSurge) - (old + new); add that many new; then can_remove = (new + old) - (replicas - maxUnavailable) old pods removed (bounded by old remaining and by new pods created so far able to serve). Return the number of steps. Assume maxSurge + maxUnavailable >= 1 so progress is always possible.
Your Task
def rollout_steps(replicas, max_surge, max_unavailable):
Return the number of steps (int) to fully roll out.
Input Format
- replicas (int), max_surge (int), max_unavailable (int).
Output Format
- A single int.
Sample
print(rollout_steps(4, 1, 1))
Output:
3
Example
print(rollout_steps(4, 1, 1))
3
-
Initialization: Start with
old = 4(all pods are old) andnew = 0. The target isnew = 4. The minimum allowed total pods is , and the maximum allowed is . -
Step 1:
- Add: We can add up to new pod. So,
newbecomes . - Remove: The current total pods is . We can remove up to old pods, but we are limited by the number of new pods created so far (1) to ensure service availability. Thus, we remove 1 old pod.
oldbecomes . - State:
old = 3,new = 1.
- Add: We can add up to new pod. So,
-
Step 2:
- Add: We can add up to new pod. So,
newbecomes . - Remove: The current total pods is . We can remove up to old pods. We are limited by the number of new pods (2), so we remove 2 old pods.
oldbecomes . - State:
old = 1,new = 2.
- Add: We can add up to new pod. So,
-
Step 3:
- Add: We can add up to new pods. We need more to reach the target. So, we add 2.
newbecomes . - Remove: The current total pods is . We can remove up to old pods. We only have 1 old pod left, so we remove 1.
oldbecomes . - State:
old = 0,new = 4.
- Add: We can add up to new pods. We need more to reach the target. So, we add 2.
-
The loop terminates because
newhas reachedreplicas(4). The total number of steps taken is 3. -
The final output is 3
Constraints
- Pods count stays within
[replicas - maxUnavailable, replicas + maxSurge]. - Each step: add new up to surge cap, then remove old down to the availability floor.
- Return the step count;
maxSurge + maxUnavailable >= 1.