0
0
Kubernetesdevops~5 mins

Custom resources concept in Kubernetes - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Custom resources concept
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of Widgets grows, the controller must process more items each cycle.

Input Size (n)Approx. Operations
10Processes 10 Widgets
100Processes 100 Widgets
1000Processes 1000 Widgets

Pattern observation: The work grows directly with the number of Widgets. Double the Widgets, double the work.

Final Time Complexity

Time Complexity: O(n)

This means the time to process all custom resources grows linearly as you add more of them.

Common Mistake

[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.

Interview Connect

Understanding how resource counts affect processing time helps you design scalable Kubernetes controllers and troubleshoot performance.

Self-Check

"What if the controller only processed changed Widgets instead of all Widgets each cycle? How would the time complexity change?"