Requirements
- Assume a Kubernetes controller base class is already available.
- Implement a controller that prevents the pod count from drifting above or below the desired level.
- The controller must operate as an independent reconciliation loop: inspect the current cluster state, compare it with the target state, and request the required pod creation, deletion, or redistribution actions.
- Treat API-server errors as possible runtime conditions rather than assuming that every request succeeds.
Use the following method shape for the controller entry point:
class PodController(KubernetesController):
def reconcile(self, desired_replicas: int) -> None:
pass
The inherited Kubernetes interface provides operations for observing pods and requesting pod-management actions. Keep reconciliation safe to repeat when the same target state is processed more than once.
Examples
Example 1
Input: desired_replicas = 4, currently observed pods = 2
Output: request creation of 2 pods
The controller is below the target by two pods, so it should issue two creation requests.
Example 2
Input: desired_replicas = 3, currently observed pods = 5
Output: request deletion of 2 pods
There are two excess pods, so the controller should remove two of them.
Example 3
Input: desired_replicas = 3, currently observed pods = 3
Output: take no pod-count action
The observed state already matches the desired state, so no change is necessary.
Constraints
- The desired replica count is a non-negative integer.
- The controller must not intentionally leave the system with more or fewer pods than the requested target after successful actions.
- A failed API-server request must not be treated as a successful state change.
- Repeated reconciliation of an already-correct state should not create or delete additional pods.
Follow-ups
- What responsibilities does a Kubernetes controller typically have?
- How should the controller behave while the Kubernetes API server is unavailable?