How a Controller Uses Workload Map
A controller is a program that watches objects in a Kubernetes cluster and reacts when they change. When a controller manages more than one workload type, it must know where each type stores its pods, its replica count, and its status. Workload Map moves that knowledge out of the controller code and into definitions, so the same code works for every workload type that has a definition.
This page explains the problem, what the Go library gives a controller, and how a reconcile loop that uses it is structured. To build a controller, follow the tutorial.
The Problem
Each workload type stores its pod templates in a different place. A JobSet keeps them in .spec.replicatedJobs. A LeaderWorkerSet keeps a leader template and a worker template under .spec.leaderWorkerTemplate. Without Workload Map, a controller that changes the pods of both types has one branch for each type, and it needs a new branch for every type that it adds:
With Workload Map, the same task is one loop over the components of the workload. The code is identical for every workload type:
What the Library Provides
The karta/sdk Go library reads a definition and a workload object, and exposes the workload as a set of components. For each component, a controller can do the following:
The definition tells the library where each value is. The controller never contains a path such as .spec.leaderWorkerTemplate.workerTemplate. For the calls and their code, refer to Use the Go Library.
Where Definitions Come From
A controller can read definitions from two places:
- The catalog that is built into the library. It contains definitions for common workload types, such as JobSet, PyTorchJob, and LeaderWorkerSet.
Kartaresources in the cluster. A controller that reads them picks up new and changed definitions at runtime, with no rebuild or redeploy.
The example controller reads Karta resources from the cluster, so its structure data is cluster data and not code. It needs the Karta CRD, which is a custom resource definition. It does not need the Karta Operator.
How a Reconcile Loop Is Structured
The example controller in the repository runs one reconcile loop for every workload type that it watches. A reconcile loop is the function that Kubernetes calls each time a watched object changes. For each change, the example does the following:
- Finds the definition in the cluster whose root component matches the group, version, and kind of the workload. It never hard-codes a definition name.
- Fetches the workload as an unstructured object.
- Creates a component factory from the definition and the workload. The factory is the single entry point for every read and write.
- Reads the status, the scale, and the container resource requests of every component, including the root.
- Adds a label to the pod template of every component that defines pods.
- Writes the results back to the workload as annotations, and records Kubernetes events when the status changes.
There is no switch on the workload kind anywhere in the loop. The example controller also watches the Karta resources. When a definition changes, the controller reconciles every workload that the definition describes, with the new structure.
Choose What to Read Next
- To run the example controller on a local cluster, follow the tutorial.
- To write your own code against the library, refer to Use the Go Library.
- To make a running controller manage another workload type, refer to Watch Another Workload Type.