Resource quotas per namespace in Kubernetes - Time & Space Complexity
When Kubernetes applies resource quotas per namespace, it checks resource usage limits for all pods in that namespace.
We want to understand how the time to enforce quotas grows as the number of pods increases.
Analyze the time complexity of this resource quota enforcement snippet.
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-resources
namespace: example-namespace
spec:
hard:
pods: "10"
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "10"
limits.memory: 16Gi
This quota limits the number of pods and their CPU and memory usage in a namespace.
When enforcing this quota, Kubernetes must:
- Primary operation: Check each pod's resource requests and limits.
- How many times: Once for every pod in the namespace.
As the number of pods grows, the system checks each pod one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of pods.
Time Complexity: O(n)
This means the time to enforce quotas grows linearly as the number of pods increases.
[X] Wrong: "Checking resource quotas happens instantly no matter how many pods there are."
[OK] Correct: Each pod must be checked, so more pods mean more work and more time.
Understanding how resource quota checks scale helps you design efficient Kubernetes clusters and troubleshoot performance issues calmly.
"What if the quota also limited the number of containers per pod? How would that affect the time complexity?"