Skip to main content

Overview

The K8s Scheduler operator is a Kubernetes controller that reconciles custom resources (CRDs) for user deployments, ephemeral agent tasks, and multi-step workflows. It uses controller-runtime and follows Kubernetes operator best practices.

Architecture

Operator Entry Point

The operator is initialized in cmd/operator/main.go:
cmd/operator/main.go:33-155
Source: cmd/operator/main.go:33-155

Custom Resource Definitions

The operator manages three CRDs defined in internal/operator/apis/scheduler/v1alpha1/types.go:

UserDeployment CRD

Represents a user’s long-lived deployment (web app, API, database, etc.).
internal/operator/apis/scheduler/v1alpha1/types.go:16-23
Source: internal/operator/apis/scheduler/v1alpha1/types.go:16-23

Spec Fields

internal/operator/apis/scheduler/v1alpha1/types.go:25-74
Source: internal/operator/apis/scheduler/v1alpha1/types.go:25-74

Status Fields

internal/operator/apis/scheduler/v1alpha1/types.go:119-135
Source: internal/operator/apis/scheduler/v1alpha1/types.go:119-135

Phase Constants

internal/operator/apis/scheduler/v1alpha1/types.go:152-159
Source: internal/operator/apis/scheduler/v1alpha1/types.go:152-159

AgentTask CRD

Represents an ephemeral agent execution (short-lived task).
internal/operator/apis/scheduler/v1alpha1/types.go:190-197
Source: internal/operator/apis/scheduler/v1alpha1/types.go:190-197

AgentTask Spec

internal/operator/apis/scheduler/v1alpha1/types.go:199-227
Source: internal/operator/apis/scheduler/v1alpha1/types.go:199-227

Workflow CRD

Orchestrates sequential multi-step agent executions.
internal/operator/apis/scheduler/v1alpha1/types.go:550-556
Source: internal/operator/apis/scheduler/v1alpha1/types.go:550-556

UserDeployment Controller

The UserDeployment controller reconciles user deployments by creating/updating Kubernetes resources.

Controller Structure

internal/operator/controller/userdeployment_controller.go:64-74
Source: internal/operator/controller/userdeployment_controller.go:64-74

Reconciliation Loop

internal/operator/controller/userdeployment_controller.go:281-310
Source: internal/operator/controller/userdeployment_controller.go:281-300

Reconciliation Steps

  1. Fetch UserDeployment CR from Kubernetes API
  2. Check desired state:
    • If deleted: Add finalizer and clean up resources
    • If running: Provision/update resources
  3. Load template from ConfigMap
  4. Create namespace (if needed)
  5. Create ConfigMaps for user configuration
  6. Create ExternalSecrets for secrets injection
  7. Create Deployments for each service
  8. Create Services for networking
  9. Create Ingresses (Traefik IngressRoute)
  10. Create NetworkPolicies for isolation
  11. Update status with phase and ingress URLs

Resource Creation

Deployment Creation

The controller creates Kubernetes Deployments for each service in the template:

Service Creation

Kubernetes Services are created for each service:

Ingress Creation

Traefik IngressRoutes are created for public/internal access:

Secrets Integration

The controller creates ExternalSecret resources for Vault/AWS secrets:

Finalizer Pattern

The controller uses finalizers for cleanup:

AgentTask Controller

The AgentTask controller manages ephemeral agent executions by creating Kubernetes Jobs.

Job Creation

The controller creates a Job for each AgentTask:

Status Syncing

The controller watches Job/Pod status and updates the AgentTask CR:
  • Pending: Job created, waiting for Pod
  • Running: Pod is running
  • Succeeded: Pod completed with exit code 0
  • Failed: Pod completed with non-zero exit code
  • Timeout: Execution exceeded timeout limit

Workflow Controller

The Workflow controller orchestrates sequential AgentTask executions.

Step Execution

The controller:
  1. Creates AgentTask CR for current step
  2. Waits for step completion
  3. Captures step output
  4. Substitutes output into next step’s input (supports ${steps.N.output.KEY} syntax)
  5. Advances to next step
  6. Repeats until all steps complete or one fails

Workflow Status

Source: internal/operator/apis/scheduler/v1alpha1/types.go:592-611

RBAC Permissions

The operator requires these Kubernetes permissions:
Source: internal/operator/controller/userdeployment_controller.go:267-279

Health Checks

The operator exposes health endpoints:
  • GET :8081/healthz - Liveness probe
  • GET :8081/readyz - Readiness probe

Metrics

Prometheus metrics are exposed on the configured metrics port (default: disabled).

Leader Election

The operator supports leader election for high availability:
Only one instance will actively reconcile resources.

Configuration

Command-Line Flags

Source: cmd/operator/main.go:34-51

Environment Variables

  • DEPLOYMENT_DOMAIN - Domain for deployment ingresses
  • CLUSTER_SECRET_STORE - External Secrets ClusterSecretStore name
  • TASK_NAMESPACE - Namespace for ephemeral tasks
  • DISABLE_NETWORK_POLICIES - Skip network policy creation (dev mode)

Deployment

The operator is deployed as a Kubernetes Deployment with:
  • Service account with RBAC permissions
  • Leader election for HA
  • Health probes for liveness/readiness
  • Metrics endpoint (optional)

Frontend Architecture

React 19 frontend with TypeScript and TanStack Query

Server Architecture

Go backend with HTTP handlers and middleware