midnight-saci

Run workers as Kubernetes Jobs

With worker.runtime: kubernetes, the backend starts one Job per run in a single namespace, using the Kubernetes API. Supervision is the same as with Podman or Docker (supervision): the backend watches the Job, stops it on cancellation or at the deadline, records the outcome when the worker cannot, and deletes the Job afterwards. The worker image and the reporting protocol do not change. No container runtime socket is needed. The run report records the digest of the image that ran (worker image).

worker:
  runtime: kubernetes
  image: ghcr.io/orpiske/saci-worker:0.1.0
  kubernetes:
    namespace: saci              # default: the backend pod's namespace
    service_account: saci-worker # default: none, see Identities
    image_pull_policy: IfNotPresent
llm:
  api_key: env:SACI_LLM_API_KEY
  api_key_secret: { name: saci-llm, key: api-key }
environments:
  same-namespace:
    secrets:
      SERVICE_TOKEN: { secret: demo-service, key: token }

Connecting to the API

Inside a pod, the backend uses its own service account: the token, CA and namespace mounted at /var/run/secrets/kubernetes.io/serviceaccount. It reads the token again for every request, so rotated tokens keep working.

Outside the cluster, for development, set worker.kubernetes.api_url and worker.kubernetes.namespace. The backend sends requests to that URL without credentials of its own, so point it at kubectl proxy, which adds yours:

kubectl proxy --port=8001 &
worker:
  runtime: kubernetes
  kubernetes:
    api_url: http://127.0.0.1:8001
    namespace: saci-dev

Workers must then reach the backend at backend.worker_url from inside the cluster.

At start-up the backend lists Jobs once. If that fails, it logs why and keeps serving, and every run ends error with the reason, as with a missing container runtime. For example:

the worker could not be launched: the Kubernetes API refused GET /apis/batch/v1/namespaces/saci/jobs?limit=1
(403 Forbidden): jobs.batch is forbidden: ...; grant the backend's service account the Role in
docs/operate/kubernetes-jobs.md#permissions

Permissions

The backend’s service account needs this Role in the workers’ namespace, and nothing cluster-wide:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: saci-launcher
rules:
  - apiGroups: ["batch"]
    resources: ["jobs"]
    verbs: ["create", "get", "list", "patch", "delete"]
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list", "delete"]
  - apiGroups: [""]
    resources: ["pods/log"]
    verbs: ["get"]
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["create"]

The backend never reads Secrets. It creates one per run, and the environment’s Secrets are referenced by name and read only by the worker’s pod.

Identities

The backend and the workers use different service accounts:

What a run gets

For run run-1a2b… the backend creates a Job and a Secret, both named saci-run-1a2b… and labelled io.saci.managed=true and io.saci.run=run-1a2b…:

Lifecycle

Event What happens
The pod is pending or running The run is watched. A pod that cannot start (an image pull failure, for example) is ended by the run deadline, and its waiting reason is added to the recorded reason.
The worker reports the run’s end The backend deletes the Job with background propagation. Kubernetes then removes the pod and the run’s Secret.
Cancellation or the backend’s deadline The backend suspends the Job (patch), so its controller starts no replacement pod, then deletes the pod with gracePeriodSeconds: 30: SIGTERM, then SIGKILL. On SIGTERM the worker cancels and reports. The Job is then removed.
The worker exits without reporting The run ends error with the exit code and the last 40 lines of the pod’s log, redacted.
The Job disappears The run ends error (the worker disappeared).
The backend restarts Jobs of unfinished runs are watched again. Runs never handed to a worker are launched. Other unfinished runs end error and are not repeated. Jobs of runs that had finished are deleted. Jobs of runs this backend does not know are left alone.

Next steps