Skip to content

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

typescript
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

FactoryDescription
resourceGraphDefinitionKro RGD resource (from typekro/kro)

Compositions

CompositionDescription
typeKroRuntimeBootstrapInstall Kro controller

TypeKro + Kro Integration

TypeKro generates Kro ResourceGraphDefinitions from TypeScript compositions:

typescript
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
// TypeScript spec
const spec = type({
  name: 'string',
  database: {
    instances: 'number',
    storageSize: 'string',
  },
  cache: {
    shards: 'number',
    replicas: 'number',
  },
});
yaml
# Generated KRO SimpleSchema (nested, not flattened)
spec:
  name: string
  database:
    instances: integer
    storageSize: string
  cache:
    shards: integer
    replicas: integer

Resources 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:

typescript
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:

typescript
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

ModeDescriptionUse When
directTypeKro deploys resources directlySimple deployments, no Kro controller
kroCreates ResourceGraphDefinitionRuntime CEL, self-healing, GitOps
typescript
// 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.

typescript
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

Released under the Apache 2.0 License.