Kro Factories
Factory functions for Kubernetes Resource Orchestrator (Kro) CRDs.
What is Kro?
Kubernetes Resource Orchestrator enables:
- Runtime Dependencies - Resources reference each other's runtime state
- CEL Expressions - Dynamic values evaluated at runtime
- Automatic Reconciliation - Self-healing infrastructure
Quick Example
import { kubernetesComposition } from 'typekro';
import { Deployment } from 'typekro/simple';
const WebApp = kubernetesComposition(definition, (spec) => {
const deploy = Deployment({ id: 'app', name: spec.name, image: spec.image });
return { ready: deploy.status.readyReplicas > 0 };
});
// Deploy using Kro (creates ResourceGraphDefinition)
const factory = WebApp.factory('kro', { namespace: 'default' });
await factory.deploy({ name: 'my-app', image: 'nginx' });
// Or generate YAML for GitOps
const yaml = WebApp.toYaml();Available Factories
Core
| Factory | Description |
|---|---|
resourceGraphDefinition | Kro RGD resource (from typekro/kro) |
Compositions
| Composition | Description |
|---|---|
typeKroRuntimeBootstrap | Install Kro controller |
TypeKro + Kro Integration
TypeKro generates Kro ResourceGraphDefinitions from TypeScript compositions:
import { type } from 'arktype';
import { kubernetesComposition } from 'typekro';
import { Deployment, Service } from 'typekro/simple';
// TypeScript composition
const app = kubernetesComposition({
name: 'webapp',
apiVersion: 'example.com/v1alpha1',
kind: 'WebApp',
spec: type({ name: 'string', replicas: 'number' }),
status: type({ ready: 'boolean', url: 'string' })
}, (spec) => {
const deploy = Deployment({
id: 'deploy',
name: spec.name,
replicas: spec.replicas
});
const svc = Service({
id: 'svc',
name: `${spec.name}-svc`,
selector: { app: spec.name },
ports: [{ port: 80 }]
});
return {
ready: deploy.status.readyReplicas >= spec.replicas,
url: `http://${svc.status.clusterIP}`
};
});
// Generates Kro YAML with CEL expressions
const yaml = app.toYaml();Generated Schema
TypeKro generates nested SimpleSchema objects that map directly to your TypeScript types:
// TypeScript spec
const spec = type({
name: 'string',
database: {
instances: 'number',
storageSize: 'string',
},
cache: {
shards: 'number',
replicas: 'number',
},
});# Generated KRO SimpleSchema (nested, not flattened)
spec:
name: string
database:
instances: integer
storageSize: string
cache:
shards: integer
replicas: integerResources reference these nested paths in CEL expressions: ${schema.spec.database.storageSize}, ${schema.spec.cache.shards}.
forEach Collections
Deploy multiple instances from an array in the spec — TypeKro converts JavaScript for...of loops to KRO's forEach directive:
for (const worker of spec.workers) {
Deployment({
id: `worker-${worker.name}`,
name: `${spec.name}-${worker.name}`,
image: worker.image,
replicas: worker.replicas,
});
}KRO creates one Deployment per array element, with automatic cleanup when elements are removed. See Collections & forEach for complete examples.
Nested Compositions
Compositions can nest other compositions. TypeKro inlines the inner composition's status CEL into the outer RGD — no virtual resource IDs leak into the YAML:
const inngest = inngestBootstrap({ name: `${spec.name}-inngest`, ... });
return {
ready: database.status.ready && inngest.status.ready,
// ↑ Direct CEL ref ↑ Inlined from inner composition
};Direct vs Kro Deployment
| Mode | Description | Use When |
|---|---|---|
direct | TypeKro deploys resources directly | Simple deployments, no Kro controller |
kro | Creates ResourceGraphDefinition | Runtime CEL, self-healing, GitOps |
// Direct: TypeKro handles deployment
const directFactory = app.factory('direct', { namespace: 'prod' });
// Kro: Kro controller handles deployment
const kroFactory = app.factory('kro', { namespace: 'prod' });Installing Kro
See Runtime Bootstrap for complete setup instructions.
import { typeKroRuntimeBootstrap } from 'typekro';
const runtime = typeKroRuntimeBootstrap();
const factory = runtime.factory('direct', {
namespace: 'flux-system',
timeout: 300000
});
await factory.deploy({ namespace: 'flux-system' });Next Steps
- Runtime Bootstrap - Install Kro controller
- Deployment Modes - Direct vs Kro deployment
- Kubernetes Factories - Core Kubernetes resources