Custom Resource Definitions (CRDs) in Kubernetes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with Custom Resource Definitions (CRDs) in Kubernetes, it's important to understand how the system handles multiple custom resources.
We want to know how the time to process these resources grows as we add more of them.
Analyze the time complexity of the following Kubernetes YAML snippet defining a CRD.
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
shortNames:
- wdgt
This snippet defines a new custom resource type called "Widget" that Kubernetes can manage.
When Kubernetes processes CRDs and their instances, it performs operations repeatedly.
- Primary operation: Iterating over all custom resource instances of the CRD.
- How many times: Once for each instance of the custom resource in the cluster.
As the number of custom resource instances grows, the time to process them grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The processing time grows linearly as the number of custom resource instances increases.
Time Complexity: O(n)
This means the time to handle custom resources grows directly with how many instances exist.
[X] Wrong: "Adding more custom resource instances won't affect processing time much because Kubernetes handles them all at once."
[OK] Correct: Kubernetes processes each instance individually, so more instances mean more work and longer processing time.
Understanding how Kubernetes scales with custom resources shows you grasp real-world system behavior and resource management.
"What if we added caching for custom resource instances? How would the time complexity change?"
Practice
Solution
Step 1: Understand what CRDs do
CRDs allow users to create their own resource types beyond the default Kubernetes resources.Step 2: Compare options
Automatic version updates, user permissions (RBAC), and cluster monitoring are separate Kubernetes features unrelated to CRDs.Final Answer:
To add new resource types to Kubernetes that are not built-in -> Option AQuick Check:
CRDs = add custom resource types [OK]
- Confusing CRDs with RBAC for permissions
- Thinking CRDs update Kubernetes versions
- Assuming CRDs monitor cluster health
Solution
Step 1: Recall Kubernetes YAML syntax
Kubernetes uses camelCase keys likeapiVersionto specify API versions.Step 2: Check other options
version,api_version, andapiVeruse incorrect formats not recognized by Kubernetes.Final Answer:
apiVersion -> Option CQuick Check:
Correct YAML key = apiVersion [OK]
- Using underscores instead of camelCase
- Using incomplete or shortened keys
- Confusing version with apiVersion
spec:
scope: Namespaced
group: example.com
names:
kind: Widget
plural: widgets
Solution
Step 1: Read the scope field
Thescopeis set toNamespaced, meaning the resource exists within namespaces.Step 2: Understand scope meanings
Namespacedmeans the resource is limited to a specific namespace, not cluster-wide or node-scoped.Final Answer:
Resource limited to a specific namespace -> Option BQuick Check:
scope Namespaced = namespace-limited resource [OK]
- Confusing Namespaced with Cluster scope
- Assuming default namespace only
- Thinking scope relates to nodes
Solution
Step 1: Analyze the error message
The error says "unknown field 'kindd'", indicating a typo in the YAML key.Step 2: Identify the correct key
The correct key iskind, sokinddis a misspelling causing the error.Final Answer:
Typo in the YAML key 'kindd' instead of 'kind' -> Option AQuick Check:
YAML key typos cause unknown field errors [OK]
- Ignoring spelling errors in YAML keys
- Assuming missing fields cause unknown field errors
- Blaming permissions for syntax errors
Gadget that is cluster-scoped and has a plural name gadgets. Which YAML snippet correctly defines this?Solution
Step 1: Check scope value
The resource must be cluster-scoped, soscope: Clusteris correct.Step 2: Verify kind and plural names
kindshould be capitalized asGadgetandpluralshould begadgets(plural lowercase).Step 3: Compare options
The snippet withscope: Cluster,kind: Gadget,plural: gadgetsmatches all requirements. Snippets withscope: Namespaced, lowercasekind: gadget, or singularplural: gadgetare incorrect.Final Answer:
spec: group: example.com scope: Cluster names: kind: Gadget plural: gadgets -> Option DQuick Check:
Cluster scope + correct kind/plural = spec: group: example.com scope: Cluster names: kind: Gadget plural: gadgets [OK]
- Using Namespaced instead of Cluster scope
- Incorrect casing for kind or plural
- Using singular for plural name
