k8s driver
The driver for sandboxes on Kubernetes. This page is for whoever runs kōjō on a cluster, usually with the Helm chart; the sandboxes reach the network through kojo-proxy.
The k8s driver runs each session in a pod managed by Kubernetes Agent Sandbox, isolated by the cluster’s Kata or gVisor RuntimeClass. It was written against Agent Sandbox v1.0.12.
Requirements
- Agent Sandbox v1.0.12 installed in the cluster (its CRDs and controller).
- A RuntimeClass that isolates. The chart does not install a runtime.
- kojo-proxy, and a NetworkPolicy or CiliumNetworkPolicy that only lets sandbox pods reach it, on a CNI that enforces policies.
The Helm chart sets all of this up except the runtime; see Kubernetes with Helm.
Configuration
The sandbox block the chart writes looks like this:
sandbox:
driver: k8s
ttl: 4h
k8s:
namespace: kojo-sandboxes
runtime_class: kata
warm_pool_size: 2
storage_class: fast-ssd
proxy:
mode: remote
listen: ":3128"
admin_listen: ":3129"
advertise_url: http://kojo-proxy.kojo.svc:3128
# A headless Service: each address it resolves to is one proxy replica.
admin_url: http://kojo-proxy-admin.kojo.svc:3129
admin_discovery: dns
admin_token: "${KOJO_PROXY_ADMIN_TOKEN}"
ca_cert_file: /var/run/kojo/proxy-ca/tls.crt
ca_key_file: /var/run/kojo/proxy-ca/tls.key
| Key | Default | Meaning |
|---|---|---|
sandbox.k8s.namespace |
kōjō’s own namespace in a cluster, else the kubeconfig context’s | Namespace sandboxes run in. The Helm chart sets <release namespace>-sandboxes: kōjō may run commands in every pod there, so it should not be kōjō’s own namespace. |
sandbox.k8s.runtime_class |
kata |
RuntimeClass of every sandbox pod. |
sandbox.k8s.warm_pool_size |
0 |
Pre-warmed pods per skill. Zero keeps none: every sandbox starts cold. |
sandbox.k8s.storage_class |
the cluster’s default | StorageClass of the workspace volumes. |
sandbox.k8s.kubeconfig |
in-cluster config | Kubeconfig file, for kōjō running outside the cluster. |
sandbox.k8s.unschedulable_timeout |
2m |
How long a sandbox pod may stay unschedulable before the sandbox fails. See Scheduling. |
sandbox.k8s.proxy_image |
none | Informational. The Helm chart deploys the proxy and writes its image here so the rendered config says what runs. The binary does not read it. |
How it works
- Per skill and kit, kōjō writes a
SandboxTemplatenamedkojo-<skill>-<hash>(image, resources, RuntimeClass, the workspace volume claim, the proxy’s CA) and aSandboxWarmPoolof the same name. The hash is that of the template, so two kits of one skill, such as two repositories’ overrides of it, or a repository’s override and the built-in kit, get two templates: a session’s pod is always made from its own kit. Skills are runtime configuration, so the templates are created at runtime and the chart ships none. - A template that no claim uses, and that no session has asked for in the last hour, is deleted with its warm pool by the periodic cleanup that also deletes expired sandboxes.
- The kit’s CPU and memory are the sandbox container’s requests and its limits, and its disk is the size of the workspace volume claim (
10Giwhen it names none). Whensandbox.max_resourcesis set, each is lowered to it first, the default size included, and the driver logs what it lowered. - Per sandbox, kōjō creates a
SandboxClaimnamedkojo-<random>. The claim’s name is the sandbox ID. It carries the labelskojo.dev/sessionandkojo.dev/skill. - The driver keeps no state outside the cluster, so any kōjō replica finds any sandbox.
Scheduling
A sandbox pod asks the scheduler for its kit’s CPU and memory, the built-in kits up to 8 CPUs and 16Gi. When no node has that much free, the pod stays Pending: its PodScheduled condition is False with the reason Unschedulable, and a message such as 0/1 nodes are available: 1 Insufficient cpu.
While it waits for a sandbox, the driver reads that condition. When the pod has been unschedulable for sandbox.k8s.unschedulable_timeout (default 2 minutes), the driver deletes the claim, and creating the sandbox fails with an error that names the sandbox’s request and quotes the scheduler:
k8s: sandbox kojo-5f0c… cannot be scheduled: no node took its pod in 2m0s (the sandbox of skill fix-bug requests cpu 4, memory 8Gi). The scheduler says: 0/1 nodes are available: 1 Insufficient cpu. …
The timeout is the time a cluster autoscaler gets to add a node; raise it when yours takes longer. Creating a sandbox has 5 minutes in all, so a value above that changes nothing. A pod that becomes schedulable within the timeout starts as usual.
A sandbox that did not start is a transient failure for the session, which is retried twice, 30 and 60 seconds later, in case a node frees up. When it still does not fit, the session fails with this error, and the error is what kōjō answers where the request came from. A dry run (a skill test, an eval) fails at the first error.
To fit the sandboxes to your nodes, lower the kits’ resources (a repository’s kit.yaml) or set sandbox.max_resources to what a node has free after everything else it runs. The kind stack sets it to 1 CPU and 2Gi, so that a sandbox fits its one 4-CPU node next to kōjō and its dependencies.
Isolation
- The isolation level comes from the RuntimeClass handler: a handler that starts with
kataismicrovm;runscor a handler containinggvisorisgvisor; anything else iscontainer. - A
containerRuntimeClass is refused, unlessdev: trueis set and the RuntimeClass carries the labelkojo.dev/insecure-dev=true. That is how runc is used on kind. - Every sandbox container runs with
allowPrivilegeEscalation: false,capabilities.drop: [ALL]and theRuntimeDefaultseccomp profile: code in it gains nothing from a setuid binary, and an image that runs as root still holds no capability. So a kit’s setup cannot install system packages; they belong in its image. The workspace’s init container runs as root, because a fresh volume belongs to root and only its owner may open it to the image’s user, with no capability (an owner needs none tochmod) and a read-only root filesystem. - A skill with
needs_dockergets a privileged container, and only on amicrovmRuntimeClass, where the privilege is that of the VM. Kata then passes every device of the node into the VM, unless the container runtime setsprivileged_without_host_devices = truefor the Kata runtime handler (containerd and CRI-O both have the option; Kata’s own installer, kata-deploy, sets it). kōjō cannot read the node’s runtime configuration, so check it before you run such skills: a repository can declareneeds_dockerin its own skills’ kits. That container gets onlyprivileged: true: the Docker daemon needs every capability (SYS_ADMINfor its mounts and cgroups,NET_ADMINfor its bridge and iptables rules), the devices and no seccomp filter, and Kubernetes refusesallowPrivilegeEscalation: falseon a privileged container.
The sandbox namespace
kōjō may run commands in every pod of the sandbox namespace and write ConfigMaps there, so the Helm chart gives sandboxes a namespace of their own, <release namespace>-sandboxes, and grants kōjō nothing in the release namespace. There:
- Pod Security enforces
baselinewith gVisor andprivilegedon Kata, where a kit that needs Docker runs a privileged container in the pod’s VM.restrictedneedsrunAsNonRoot, which the workspace’s init container and most kit images, running as root, do not meet. - A LimitRange gives a container that names no resources default requests and limits: the init container, and a kit without
resources. - A ValidatingAdmissionPolicy (Kubernetes 1.30 and later) refuses pods, SandboxTemplates and Sandboxes that use the node’s network, process or IPC namespace, mount a hostPath, run a privileged container (except on Kata), use another RuntimeClass or ServiceAccount, or mount a ServiceAccount token. Pod Security does not look at SandboxTemplates; the policy does, so the Role that lets kōjō write templates is no way onto the node. The conformance suite (
make conformance DRIVER=k8s) installs the chart’s policy for its namespace, so every case shows the driver’s templates and pods pass it, and checks what it refuses.
Egress and credentials
- No real credential is written to the cluster. The pod spec, the claim and the ConfigMap
kojo-proxy-cahold the proxy’s CA certificate and placeholders. - Sandbox pods carry the label
kojo.dev/sandbox=true, which the chart’s network policies select. Their service account token is not mounted. - A sandbox is given the proxy by address: kōjō resolves the host of
advertise_url(again after 30 seconds) and puts the address it got intoHTTP_PROXYandHTTPS_PROXY. So a sandbox needs no DNS, and the chart allows it none, because a resolver that forwards any name lets code in the sandbox send data out as the names it looks up. When kōjō cannot resolve the name, which is when it runs outside the cluster, the sandbox gets the name and needs DNS (networkPolicy.sandboxEgress.dns.enabled); kōjō logs a warning.
Limits
- The proxy variables direct traffic; they do not force it. The network policy does.
Healthreports egress as enforced only when a NetworkPolicy or CiliumNetworkPolicy labelledapp.kubernetes.io/component=sandbox-egressin the sandbox namespace selects the sandbox pods. It does not judge the policy’s rules. - Proxy settings are set per command, not in the pod spec, so that warm-pool pods work unchanged. A daemon the image starts by itself does not get them.
- The CA bundle a sandbox trusts is the system roots of the machine kōjō runs on plus the proxy CA, not the roots of the sandbox image.
- With
warm_pool_sizeabove zero, every kit in use keeps that many warm pods, until its template has been unused for an hour. - With
sandbox.proxy.mode: embeddedthe k8s driver needssandbox.proxy.advertise_url, because pods cannot use the address kōjō listens on.