Build a Controller on kind
Deploy a controller that reads a LeaderWorkerSet, annotates it with its status and resource requests, and labels its leader and worker pod templates. On each reconcile, the controller reads the workload’s structure from a Karta definition in the cluster. Editing that definition changes what the controller reads.

You can run the Go library quickstart offline first.
Prerequisites
-
kind,
kubectl, anddocker. -
A clone of the repository. The commands run from the repository root:
The example is a standalone Go module. Its local replace directive selects the repository library in karta/sdk/, so the image is built from the repository root.
What the Controller Does
The example controller selects the definition whose root component matches the workload’s group, version, and kind. It reads status, scale, and resource requests from the workload and uses the definition to find the pod templates to label. For the sequence of reads and writes, see How a Reconcile Loop Is Structured.
Create a Cluster
Create a kind cluster:
Install the LeaderWorkerSet Operator
The CRD and the controller of LeaderWorkerSet are not built into Kubernetes. Install them:
Install the Karta CRD and the Definition
Install the Karta CRD, then apply the LeaderWorkerSet definition. The order matters. The CRD must exist before the object that uses it.
The LeaderWorkerSet definition maps the leader and worker templates and the computed worker replica count. The controller therefore reads and writes them with plain GetPodTemplateSpec and UpdatePodTemplateSpec calls.
Build and Deploy the Controller
Build the controller image and side-load it into the cluster:
Deploy the controller:
Apply a LeaderWorkerSet and See the Result
Apply the LeaderWorkerSet and inspect what the controller wrote:
When the group is ready, the annotations are as follows. The replicas value sums the group, the leader component, and the worker component. The CPU and memory annotations add the container requests from one leader template and one worker template. Their values stay the same if you add more groups with those templates.
The controller emits this event when it prepares the label update. The template labels shown above confirm the write succeeded.
The pod templates of a LeaderWorkerSet can be changed while it runs. An update triggers a normal rolling update, and the LeaderWorkerSet has no suspend field. For this reason, the controller injects the managed-by label directly into the leader and worker templates, and the library routes each update to the correct path in the workload.
Trigger a Status Change
The controller emits StatusRecorded when it first saves a known status. Later, if the saved status changes, it emits StatusChanged. Start a rolling update and inspect the events as the group returns to Running.
The event looks like the following:
Change the Definition Live
The controller also watches Karta objects. When you edit the object whose root component is leaderworkerset.x-k8s.io/v1 LeaderWorkerSet, the controller reconciles every governed LeaderWorkerSet again with the new structure, with no redeploy:
For ready-made definitions for PyTorchJob, RayCluster, JobSet, and more, refer to the Catalog of Definitions.
Watch Another Workload Type
To extend the running controller to JobSet, follow these steps before cleaning up the cluster.
How the Flag Works
The --watch-gvk flag takes a comma-separated list of workload types. Write each type as group/version/kind. The core group is empty, so a Pod is /v1/Pod. The default value is leaderworkerset.x-k8s.io/v1/LeaderWorkerSet.
The controller creates one watch and one reconciler for each configured type at startup. A change to the flag therefore takes effect on the next rollout.
Add JobSet
-
Install the JobSet operator. The controller can watch only the types that the API server knows:
-
Apply the definition of JobSet:
-
Add the type to the flag in
docs/tutorials/controller-runtime/manifests/01-deployment.yaml: -
Grant RBAC for the new resource in
docs/tutorials/controller-runtime/manifests/00-rbac.yaml: -
Apply the manifests again:
-
Wait for the controller to roll out, then apply the sample JobSet. The sample sets
spec.suspend: trueso the controller can label its pod template before any pods are created: -
Wait for the template label, resume the JobSet, and list its pods that inherited the label:
Clean Up
Delete the kind cluster to remove the controller, workloads, and installed operators:
What You Learned
-
A controller can read, aggregate, and change any workload that has a definition, with no code for a specific workload type.
-
The controller finds the definition in the cluster, so a change to the definition changes the behavior of the controller without a redeploy.
-
The files of the example and their purpose:
Next Steps
- To add a workload type that has no catalog definition, see Define a Workload Type.
- To write your own code against the library, refer to Use the Go Library.