Tutorial - Write Your First Workload Definition
In this tutorial, you write a Karta definition from scratch for a Kubernetes batch/v1 Job. By the end, you have a complete, valid definition, and you understand the building blocks well enough to describe any other workload type.
A Job is the simplest useful example. It has one component, one pod template, and a status that it reports through conditions. The catalog has definitions for more complex workloads, such as JobSet, PyTorchJob, RayCluster, and KServe. When you write your own definition, start from the catalog definition that is closest to your workload. For every field that a definition supports, refer to the Workload Definition Spec.
Prerequisites
kliinstalled. For more information, refer to Installation.- A Kubernetes cluster with the Karta Operator installed, to see the definition work in Step 4. For more information, refer to Installation. Steps 1 to 3 and the validation in Step 4 need no cluster.
Related Concepts
Before you start, make sure that you are familiar with the following:
- Kubernetes custom resources and YAML.
- jq path syntax. Every path in a definition is a jq expression.
- The workload that you describe. You need to know its group, version, and kind, the conditions or phases that it reports in
.status, and where it stores its pod template. - The components of a definition. For more information, refer to Core Concepts.
A definition describes a workload as a tree of components:
- The root component is the top-level resource itself. Every definition has exactly one. It must declare a full group, version, and kind, and a
statusDefinition. - Child components are resources that the root component owns. Each child must declare an
ownerRefthat points to its parent. A Job has no children, so this tutorial uses only the root component.
Every path that you write is a jq expression that is evaluated against a resource. Paths in specDefinition, scaleDefinition, and statusDefinition are evaluated against the workload object. Paths in podSelector and in optimizationInstructions are evaluated against pod manifests. Mixing these up is the most common authoring mistake.
Tutorial Steps
Step 1: Start From the Boilerplate
Create a file named job.yaml. Every definition starts with the same header and a structureDefinition that holds a rootComponent.
The name field is a free-form identifier for the component. It must be unique within the definition. The kind field is the full group, version, and kind of the workload. Always provide all three. The only exception is the core Pod kind, which has an empty group.
For a definition that needs an explicit opt-in, set spec.experimental: true. The definition stays available in the SDK and the catalog. The CLI and the Headlamp plugin exclude it until their experimental setting is on, even if it is installed in the cluster. If you omit the field or set it to false, the definition behaves normally. Before you install an experimental definition, update the Karta CRD to a version that includes spec.experimental. The Helm chart updates the CRD during upgrades by default. If you manage the CRD separately, apply the updated run.ai_kartas.yaml first.
Step 2: Map the Status
The statusDefinition field translates the conditions or phases of the workload into the common statuses of Workload Map, such as running, completed, and failed. It is required on the root component.
- A
byConditionsrule lists one or more conditions that must all hold. This is AND logic. - A
byExpressionrule evaluates a jq expression and compares the result to an expected result. This is useful when a workload reports state through status fields instead of conditions.
A Job uses both. Rules under the same status are ORed, so the status matches when any one of its rules matches. A workload can match several statuses at once, and each match is returned in MatchedStatuses. When no rule matches, the status resolves to Undefined.
Add the following under the rootComponent, at the same level as kind:
Note the // 0 defaults. A path that may be absent must supply a default, so that the expression stays null-safe. The field .status.active does not exist before the Job starts, so (.status.active // 0) reads as 0 instead of returning an error.
Map only the statuses that the workload reports, and base each rule on the real condition types and fields in the API of that workload. If you invent condition types that the controller never sets, the definition validates but never reports the right status.
Step 3: Point to the Pods and Declare the Scale
The specDefinition field tells Workload Map where the workload stores its pod template. There are three patterns, and they are mutually exclusive. Pick exactly one for each component:
podTemplateSpecPathwhen the resource embeds a full pod template.podSpecPath, with an optionalmetadataPath, when the resource embeds a bare pod spec.fragmentedPodSpecDefinitionwhen the pod fields are scattered across the spec.
A Job embeds a full pod template at .spec.template, so use the first pattern. The scaleDefinition field tells Workload Map how to read the replica count. The parallelism of a Job is its replica count.
Add the following under the rootComponent, below statusDefinition:
The // 1 default keeps the path null-safe. The parallelism field is optional and defaults to 1 when it is not set.
Your complete job.yaml now looks like the following. It is a valid definition as it is. The scheduling instructions in the optional step at the end are not required.
This minimal file does not map suspended Jobs. A suspended Job resolves to Undefined only when no other rule matches. While its pods are still active, it can still match running or initializing. The shipped definition in docs/catalog/batch-job-v1.yaml adds a suspended mapping and suspend and resume actions.
Step 4: Validate the Definition and See It Work
-
Validate the file:
The command needs no cluster. It checks the shape of the definition, the required fields, and the jq expressions. It does not check that the definition maps the workload correctly. It also does not reject unknown field names. A misspelled field passes validation and is ignored, so check the spelling of each field. A valid definition prints the following:
Without
kli, run thekarta-verifytool from thekarta/sdkdirectory of a clone:go run ./hack/karta-verify --karta /path/to/job.yaml. For more information, refer to Define a Workload Type.If a check fails, the error message names the component and the problem. For each message and its fix, refer to the Troubleshooting page. Before you use a definition, confirm the following:
- All kinds use a full group, version, and kind.
- The root component has a
statusDefinition. - All jq paths are absolute and null-safe, and reference the correct resource, either the workload object or the pod.
- There are no duplicated child kinds.
- Pod selectors are mutually exclusive.
- Status conditions match the real API of the workload.
- Every child component has an
ownerRefto an existing component, with no ownership cycles.
-
In a cluster with the Karta Operator installed, run a Job:
-
Describe the Job with the definition that is built into
kli:The first line of the output shows the definition that
kliused. It is the catalog definition,batch-job-v1: -
Apply your definition to the cluster:
-
Check that the Karta Operator accepted the definition:
The output is
Validated=True CRDExists=True Ready=True. -
Describe the Job again:
The first line now shows your definition. A definition in the cluster overrides the catalog definition for the same workload type:
Optional Step: Add Scheduling Instructions
This part of the API is experimental and may change.
The optimizationInstructions field is optional, and schedulers use it. For gang scheduling, you declare a pod group and map the components of the workload onto it. The current format is podGroup. The group gets a name, each listed component becomes a subgroup, and you can set topology constraints per subgroup or for the whole group.
Add the following under spec, at the same level as structureDefinition:
Every componentName must match a component that you defined. The older podGroups list format still appears in some samples, including docs/catalog/batch-job-v1.yaml. It is deprecated in favor of podGroup.
What You Learned
- A definition describes a workload as a tree of components, and every path in it is a jq expression.
- How to map the status, point to the pods, and declare the scale for a workload.
- How to validate a definition with
kli validate, and how to see the cluster use it withkli describe.
Next Steps
- Add child components for workloads that own other resources. Each child needs an
ownerRef. For examples, refer to the KServe InferenceService and PyTorchJob definitions. - Handle workloads with repeated roles or instances, such as the replicated jobs of a JobSet, with
instanceIdPathand a matching component instance selector. For an example, refer to the JobSet definition. - Read a definition from a controller with the Go component API. Follow the Go Library quickstart or the controller-runtime example.
- To write a definition for your own workload type, refer to Define a Workload Type.
- Read the full field reference in the Workload Definition Spec.