Define a Workload Type
A Karta definition describes the structure of a workload type: its root resource, the child resources and pods that it owns, where each pod template is stored, how to read the replica count, and how its status maps to a normalized phase. The CLI, the Karta Operator, and schedulers read this description to handle any workload type through the same API.
Write a definition when the catalog has none for your workload type, or when you need to change how the catalog reads one. In this guide, you copy a catalog definition, adapt it to your workload type, validate it, and apply it to a cluster. If you have never written a definition, start with the tutorial, which builds one from scratch for a Kubernetes Job. For every field that a definition supports, refer to the Workload Definition Spec.
Prerequisites
kliinstalled. For more information, refer to Installation.- A cluster with the CRD of your workload type installed, and a running workload of that type to use as a reference.
- The Karta Operator installed in that cluster, to apply the definition and verify it. For more information, refer to Installation. Writing and validating a definition need no operator.
Gather the Workload Details
Before you write the definition, collect the following from the API of your workload type:
- The group, version, and kind of the workload.
- The field that reports its state: a list of conditions, a single phase field, or other status fields.
- The location of each pod template in the spec.
- The child resources that the controller creates, and the pod labels that identify the role of each pod.
To read these values, inspect a running workload and its pods:
Check for an Existing Definition
List the definitions that cover your group and kind:
If the output lists a definition, kli already reads this workload type. A definition that you apply to the cluster overrides the catalog definition for the same type, so you can still write your own to change how kli reads the type.
Copy a Catalog Definition
Choose the catalog definition whose structure is closest to your workload type:
Export the definition to a file. The following command exports the PyTorchJob definition:
In the file, set metadata.name to a name that is unique in the cluster. The next steps change the rest of the file.
Set the Root Component and Its Status
The root component is the top-level resource of the workload. Set rootComponent.kind to the full group, version, and kind of your workload type. The root component requires a statusDefinition.
Select the rule that matches the field in which the workload reports its state:
Map a status only if the workload reports it, and use the condition types and fields that appear in the API of the workload. A condition type that the controller never sets passes validation but never matches. For the matching rules, refer to Step 2 of the tutorial.
Set the Pod Template and the Scale
Set specDefinition to the location of the pod template. Use exactly one of the following patterns for each component:
Set scaleDefinition.replicasPath to the location of the replica count.
Every path is a jq expression. Paths in specDefinition and scaleDefinition are evaluated against the workload object. Give an optional field a default so that the expression stays null-safe, for example .spec.parallelism // 1.
Declare Child Components
Declare a child component for each resource or group of pods that the workload owns and that you want to show as a separate component. A child component requires the following fields:
A child component usually also has a specDefinition, a scaleDefinition, and a podSelector.
When the controller creates the pods directly, declare the child with the Pod kind, which has an empty group, and identify its pods with a podSelector. The PyTorchJob definition declares its worker role as follows:
Paths in a podSelector are evaluated against the manifest of a pod, not against the workload object. If you omit value, the selector matches any pod that has the key. The selectors of each kind must be mutually exclusive, so that no pod matches two components.
Distinguish Repeated Instances
A component can read several pod definitions from one array or map in the spec. A JobSet, for example, lists its replicated jobs in .spec.replicatedJobs. Declare one component for the whole list, and set two fields to tell its instances apart:
The JobSet definition sets them as follows:
When the spec holds a map, use its keys as the instance names, for example .spec.jobs | to_entries[] | .key. To separate identical groups of the same structure, such as the groups of a LeaderWorkerSet, add a replicaSelector. For more information, refer to Core Concepts.
Set Optional Fields
Set the following fields only if they apply to your workload type:
Validate the Definition
Validate the file. The command needs no cluster:
A valid definition prints a line like the following. The group, version, and kind are those of your workload type:
The command checks the schema, the required fields, and the syntax of the jq expressions. It does not check that the definition maps your workload type correctly. It also does not reject unknown field names. A misspelled field passes validation and is ignored, so check the spelling of each field. To read the definition from standard input, use - in place of the file name.
The exit code reports the result:
Check the Definition Against a Real Workload
Validation does not show whether a jq path resolves against a real object, so a definition can pass validation and still extract nothing. The karta-verify tool runs the definition against a real custom resource, and it reports what it extracted. It needs no cluster. It is part of the karta/sdk module, so it needs a clone of the repository and Go. For more information, refer to Prerequisites.
From the karta/sdk directory of the clone, run the tool with the definition and the manifest of a workload:
The output has a validation result and the values that the definition extracted from the manifest:
Without --workload, the tool runs the validation only. The tool has the following options:
Every warning describes a valid but empty extraction, which the validation cannot detect:
- The status is unresolved, so no rule in
statusDefinitionmatched the object. - A component declares a
specDefinitionbut extracted no pod spec, so the spec path missed. - A component extracted a pod spec with no containers.
- A component produced no instances, so
instanceIdPathmatched nothing.
The exit code is 0 for success, 1 for a load or validation failure, 2 for a prediction mismatch, and 3 for warnings under --strict. The go run command reports a non-zero exit of the tool as exit status N and exits with 1 itself. To branch on the exact code, build the tool first with go build -o verify ., and run ./verify with the same options.
For the predictions file format and more examples, refer to the karta-verify README.
Apply the Definition and Verify It
-
Apply the definition to the cluster:
-
Check that the Karta Operator accepted the definition:
The output is
Validated=True CRDExists=True Ready=True. -
Describe a running workload of your type:
The first line of the output names the definition that
kliused. For your definition, it shows the definition name and(cluster). Check that the tree contains the components that you declared, and that each pod appears under the correct component.
To apply a definition with spec.experimental, the Karta CRD must include that field. The Helm chart updates the CRD during upgrades by default. If you manage the CRD separately, apply the updated run.ai_kartas.yaml first.
The Karta Operator rejects a second definition for the same group, version, and kind, and the kind of the root component cannot change. To change a definition, edit the file and apply it again.
Next Steps
- To fix a validation error, refer to Troubleshoot Definitions.
- To look up every field of a definition, refer to the Workload Definition Spec.
- To find a definition to start from, refer to the Catalog of Definitions.
- To read a definition from your own code, refer to How a Controller Uses Workload Map.