Custom resources concept in Kubernetes - Time & Space Complexity
We want to understand how the time to process custom resources changes as we add more of them in Kubernetes.
How does the system handle more custom resources and how does that affect performance?
Analyze the time complexity of the following Kubernetes custom resource definition and controller watch loop.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: widgets.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
scope: Namespaced
names:
plural: widgets
singular: widget
kind: Widget
---
# Controller watches all Widget resources
while true {
widgets = listAllWidgets()
for widget in widgets {
reconcile(widget)
}
sleep(10)
}
This code defines a custom resource called Widget and a controller that repeatedly lists and processes all Widget objects.
Look at what repeats in this process.
- Primary operation: Loop over all Widget resources to reconcile each one.
- How many times: Once every 10 seconds, the controller lists and processes all Widgets.
As the number of Widgets grows, the controller must process more items each cycle.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Processes 10 Widgets |
| 100 | Processes 100 Widgets |
| 1000 | Processes 1000 Widgets |
Pattern observation: The work grows directly with the number of Widgets. Double the Widgets, double the work.
Time Complexity: O(n)
This means the time to process all custom resources grows linearly as you add more of them.
[X] Wrong: "Processing custom resources takes the same time no matter how many exist."
[OK] Correct: Each resource must be handled individually, so more resources mean more work and more time.
Understanding how resource counts affect processing time helps you design scalable Kubernetes controllers and troubleshoot performance.
"What if the controller only processed changed Widgets instead of all Widgets each cycle? How would the time complexity change?"