Summary
A CRD extends the Kubernetes API by defining a new resource type (its schema, versions, validation, etc.). Once a CRD is installed, the API server can accept, validate, and store objects of that new type.
A CR is an instance of that new type—an actual object created by users or tools that carries the desired state for your operator to realize. The API server treats it like any other object once the CRD exists.
The API server validates your Custom Resource (CR) against its Custom Resource Definition (CRD) and, if valid, stores it in the etcd database. The controller is a separate process that reacts to this event.
Relationship between Controller and Reconciler
They are fundamentally linked. The reconciler is the core piece of logic inside the controller.
* Controller: The controller is the entire running program (the operator). Its primary job is to "watch" the Kubernetes API server for changes to specific resources (like the creation of your CR). It handles the boilerplate work of connecting to the cluster, managing caches of objects, and putting events into a work queue.
* Reconciler: The reconciler is the specific function or method that the controller calls to process an event from the work queue. It contains the actual business logic of your operator. Its one and only goal is to make the current state of the world match the desired state defined in a resource.
Think of the controller as the engine and the reconciler as the specific set of instructions the engine executes for a given task.
The Flow: From CR Creation to Reconciliation
Here is a detailed, step-by-step explanation of the flow:
1. CR Creation: You (or another program) send a manifest for a new Custom Resource to the Kubernetes API server (e.g., via kubectl apply).
2. API Server & etcd: The API server authenticates and validates the request. If it's valid, the API server persists the new CR object as a record in the etcd database. At this point, the object exists in the cluster as the "desired state."
3. The Watcher (Informer): Your running controller has established a "watch" on the API server for the type of CR you just created. The API server notifies the controller's watcher that a new object has been created.
4. The Work Queue: The controller's internal machinery (often called an Informer) receives this notification and places a key for the object (typically in the format namespace/name) into a work queue. This queue ensures that every change is processed, even if the operator is busy, and handles retries on failure.
5. The Reconcile Loop: The controller has a loop that constantly pulls keys from the work queue. When it pulls the key for your new CR, it directly calls the Reconciler function, passing it that key.
6. Reconciliation Logic: Inside the Reconciler function, the following happens:
* It fetches the full CR object from the cluster using the provided key.
* It observes the actual state of the world (e.g., checks if a Pod, a ConfigMap, or an external resource it's supposed to manage already exists).
* It compares the desired state (from the CR's spec) with the actual state.
* It takes action to converge the actual state toward the desired state. For a new CR, this usually means creating new Kubernetes objects (like Deployments, Services, etc.) or interacting with an external API.
* Finally, it often updates the status field of your CR to report on the actual state of the resources it now manages.
CRDs can be categorized into two main types
1. Active CRDs (The kind you are thinking of)
This is the most common pattern. The CR represents a desired, active state in the cluster. You create the CR because you want the operator to do something and create other resources.
* Example: agent
* You want: A running agent pod, a Kubernetes Service, a ConfigMap, etc.
* How it works: You kubectl apply an agent manifest. The agent controller sees it, reads its spec, and creates a Deployment, Service, etc., to make your wish a reality. The controller's job is to reconcile the agent object.
2. Passive / Configuration CRDs (The exception)
This is a less common but powerful pattern. The CR does not represent an active resource, but rather a reusable piece of configuration, a template, or a policy. It doesn't do anything on its own. It just sits in the API server, holding data for other controllers to read and use.
* Example: ServingRuntime
* You want: A reusable blueprint for how agent pods should be configured (e.g., what container image to use, what environment variables are needed).
* How it works: An administrator kubectl applies a ServingRuntime manifest. Nothing happens in the cluster. No pods are created. The CR is just stored as data.
Example CRD, CR
click to open
Active CRD - CRD - Agent
Defines the schema your controller watches and reconciles.
CRs - Agent
Minimal:
Full (references a ConfigMap and exposes a Service):
Passive / Configuration CRD - CRD - ServingRuntime
A reusable config object that doesn’t create pods by itself; your controller reads it when reconciling Agent.
CRs - ServingRuntime
REF
https://kubernetes.io/docs/concepts/extend-kubernetes/operator/https://aws-controllers-k8s.github.io/community/docs/community/overview/
No comments:
Post a Comment