Operator SDK basics in Kubernetes - Time & Space Complexity
When working with Operator SDK, it is important to understand how the time to reconcile resources grows as the number of resources increases.
We want to know how the operator's work changes when it manages more Kubernetes objects.
Analyze the time complexity of the reconcile loop in this Operator SDK controller snippet.
func (r *MyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var myResource myv1.MyResource
if err := r.Get(ctx, req.NamespacedName, &myResource); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// List all related child resources
var childList myv1.ChildResourceList
if err := r.List(ctx, &childList, client.InNamespace(req.Namespace)); err != nil {
return ctrl.Result{}, err
}
// Process each child resource
for _, child := range childList.Items {
// update or reconcile child
}
return ctrl.Result{}, nil
}
This code fetches a resource, lists its child resources, and processes each child in the reconcile loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over all child resources in
childList.Items. - How many times: Once per child resource, which depends on the number of child resources in the namespace.
As the number of child resources grows, the operator spends more time processing each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Processes 10 child resources |
| 100 | Processes 100 child resources |
| 1000 | Processes 1000 child resources |
Pattern observation: The work grows linearly as the number of child resources increases.
Time Complexity: O(n)
This means the time to reconcile grows directly in proportion to the number of child resources.
[X] Wrong: "The reconcile loop always takes the same time regardless of resource count."
[OK] Correct: The loop processes each child resource, so more children mean more work and longer reconcile time.
Understanding how operator reconcile time grows helps you design efficient controllers and shows you can reason about resource management at scale.
"What if the operator cached child resources instead of listing them every time? How would the time complexity change?"