Rolling Update Pod Availability Bounds
Problem Statement
A Deployment rolling update is constrained by maxUnavailable and maxSurge. Compute the minimum available pods and the maximum total pods allowed during the rollout.
Background
For replicas desired pods:
- maxUnavailable and maxSurge may be integers or percentage strings like "25%".
- A percentage of replicas is floored for maxUnavailable and ceiled for maxSurge (Kubernetes rounding rules).
- Minimum available = replicas - maxUnavailable; maximum total = replicas + maxSurge.
Your Task
def rollout_bounds(replicas, max_unavailable, max_surge):
Return a dict {"min_available": int, "max_total": int}.
Input Format
- replicas (int), max_unavailable, max_surge (int or "NN%" string).
Output Format
- A dict of two ints.
Sample
print(rollout_bounds(10, "25%", "25%"))
Output:
{'min_available': 8, 'max_total': 13}
Example:
print(rollout_bounds(10, "25%", "25%"))
{'min_available': 8, 'max_total': 13}- Parse the percentage inputs by converting them to decimal fractions: 25% becomes 0.25 for both
max_unavailableandmax_surge. - Calculate the integer value for
max_unavailableby multiplying the replicas by the fraction and flooring the result, as per Kubernetes rules for unavailability: ⌊10×0.25⌋=⌊2.5⌋=2. - Calculate the integer value for
max_surgeby multiplying the replicas by the fraction and ceiling the result, as per Kubernetes rules for surge: ⌈10×0.25⌉=⌈2.5⌉=3. - Determine the minimum available pods by subtracting the computed unavailability from the desired replicas: 10−2=8.
- Determine the maximum total pods by adding the computed surge to the desired replicas: 10+3=13.
- The final output is
{'min_available': 8, 'max_total': 13}
Constraints:
- Percentages: floor for maxUnavailable, ceil for maxSurge (of replicas).
- min_available = replicas - maxUnavailable; max_total = replicas + maxSurge.
- Integer inputs are used as-is.
1. Background Knowledge
In Kubernetes, a Deployment manages a set of identical pods. During a rolling update, the system must balance two competing goals: keeping enough pods available to serve traffic and bringing new-version pods online as quickly as possible. Two parameters govern this trade-off:
- maxUnavailable: The maximum number (or percentage) of pods that can be unavailable at any point during the update. This limits how aggressively the controller can terminate old pods.
- maxSurge: The maximum number (or percentage) of pods that can exceed the desired replica count at any point. This limits how many extra new pods can be created simultaneously.
Kubernetes applies specific rounding rules when these values are expressed as percentages: maxUnavailable is floored (rounded down) and maxSurge is ceiled (rounded up). This asymmetry ensures that the system never drops below the intended availability floor and never under-provisions the surge capacity. For example, with 10 replicas and "25%", maxUnavailable becomes ⌊10×0.25⌋=2 and maxSurge becomes ⌈10×0.25⌉=3.
The minimum available pods during the rollout is replicas - maxUnavailable, because at worst, that many pods are being terminated. The maximum total pods is replicas + maxSurge, because at best, that many extra pods are created before old ones are removed.
2. Algorithm Approach
This is a direct computation problem with no iteration or search. The core pattern is:
- Parse each parameter (max_unavailable, max_surge) into an integer, handling both int and "NN%" string inputs.
- Apply Kubernetes rounding: floor for maxUnavailable, ceil for maxSurge.
- Compute the two bounds using simple arithmetic.
The key sub-problem is the percentage-to-integer conversion with the correct rounding direction.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.