Collections
By default, each resource definition (in spec.resources) creates exactly one
Kubernetes resource. This means the number of resources is fixed at design
time - if you need 5 worker Pods, you write 5 resource definitions.
Collections let you declaratively manage multiple similar resources from a single definition. This is useful when the number of resources depends on runtime data like availability zones, tenants, or worker counts.
kro provides the forEach field to turn any resource into a collection. The
field takes one or more iterators, and kro creates one resource for each element
(or combination of elements), keeping them in sync as the collection changes.
Basic Example
This RGD uses forEach to create multiple Pods from a single resource entry:
apiVersion: kro.run/v1alpha1
kind: ResourceGraphDefinition
metadata:
name: worker-pool
spec:
schema:
apiVersion: v1alpha1
kind: WorkerPool
spec:
workers: "[]string"
image: string
resources:
- id: workerPods
forEach:
- worker: ${schema.spec.workers}
template:
apiVersion: v1
kind: Pod
metadata:
name: ${schema.metadata.name + '-' + worker}
spec:
containers:
- name: app
image: ${schema.spec.image}When a user creates an instance with workers: ["alice", "bob"], kro creates two
Pods - one for each worker. If the user later updates the list to
["alice", "bob", "charlie"], kro creates a third Pod for "charlie".