# Kubernetes Agent Sandbox

Install Agent Sandbox and prepare the namespace, RuntimeClass, and service account token that let Sandcovery run sandboxes on your own Kubernetes cluster.

Sandcovery creates each sandbox as an [Agent Sandbox](https://agent-sandbox.sigs.k8s.io/) `Sandbox` in one namespace of your Kubernetes cluster, through your API server and a service account token you add as the credential. The Agent Sandbox controller runs the sandbox's Pod in the RuntimeClass you name, such as gVisor or Kata Containers, with the official Node.js 26 slim image, pinned by digest and pulled from Google's Docker Hub mirror `mirror.gcr.io`, as user 1000 with every Linux capability dropped, the runtime's default seccomp profile, and no Kubernetes service account token. Commands, file reads, file writes, and listings run through the Kubernetes exec API over WebSocket, with each command's script and environment sent on its stdin, never in the request URL.

## What runs

| Option | Support |
| --- | --- |
| Size | `1x2`, `2x4`, `4x8`, and `8x16` |
| Disk | The node's ephemeral storage |
| Egress | `allow_all`, within your cluster's own network policies |
| Environment variables | Yes |
| Lifetime | Until the sandbox's TTL |
| Pause and resume | No |
| Region | Your cluster's location |

Snapshots, port previews, custom images, idle timeouts, disk sizes, egress limits, region choice, and GPUs are not available on this provider. A create that asks for one routes to another provider.

Sandcovery waits up to 50 seconds for a new Sandbox to report Ready, and deletes it and tries the next provider when it does not, or at once when the Sandbox reports that its Pod failed, broke a resource quota, or is invalid. A Pod that waits for a new node or a first image pull can take longer, so keep a node with room ready for the sizes you route.

## Prepare the cluster

Use Kubernetes 1.31 or later, whose API server takes the `v5.channel.k8s.io` WebSocket exec protocol by default, as the Kubernetes [WebSocket transition post](https://kubernetes.io/blog/2024/08/20/websockets-transition/) describes.

Install a sandbox runtime such as [gVisor](https://gvisor.dev/docs/user_guide/containerd/quick_start/) or [Kata Containers](https://katacontainers.io/) on your nodes, and a [RuntimeClass](https://kubernetes.io/docs/concepts/containers/runtime-class/) that selects it. Then install Agent Sandbox from its [v1.0.11 release](https://github.com/kubernetes-sigs/agent-sandbox/releases/tag/v1.0.11) and create a namespace that holds only Sandcovery's sandboxes:

```sh
kubectl apply -f https://github.com/kubernetes-sigs/agent-sandbox/releases/download/v1.0.11/sandbox.yaml
kubectl create namespace sandcovery
```

Sandcovery's Pods meet the restricted [Pod Security Standard](https://kubernetes.io/docs/concepts/security/pod-security-standards/), so you can enforce it on that namespace:

```sh
kubectl label namespace sandcovery pod-security.kubernetes.io/enforce=restricted
```

## Create the service account and its access

Save this Role as `role.yaml`. It lets its holder create, read, change, and delete Sandboxes and run commands in their Pods in that namespace alone:

```yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: sandcovery
  namespace: sandcovery
rules:
  - apiGroups: [agents.x-k8s.io]
    resources: [sandboxes]
    verbs: [create, get, patch, delete]
  - apiGroups: ['']
    resources: [pods/exec]
    verbs: [create, get]
```

Then create the Role, a service account for Sandcovery, and a RoleBinding that grants the Role to that account:

```sh
kubectl apply -f role.yaml
kubectl -n sandcovery create serviceaccount sandcovery
kubectl -n sandcovery create rolebinding sandcovery --role=sandcovery --serviceaccount=sandcovery:sandcovery
```

Then save this Secret as `token.yaml` to give that account a long-lived token, as the Kubernetes [service account guide](https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#manually-create-a-long-lived-api-token-for-a-serviceaccount) shows:

```yaml
apiVersion: v1
kind: Secret
metadata:
  name: sandcovery-token
  namespace: sandcovery
  annotations:
    kubernetes.io/service-account.name: sandcovery
type: kubernetes.io/service-account-token
```

Create it and read the token:

```sh
kubectl apply -f token.yaml
kubectl -n sandcovery get secret sandcovery-token -o jsonpath='{.data.token}' | base64 -d
```

Deleting the service account removes its token, as the same guide notes.

## Reach the API server

Sandcovery calls your API server from Cloudflare Workers, which accept only a certificate from a public certificate authority. Most clusters' API servers present a certificate from the cluster's own authority, so put the API server behind a proxy or load balancer that presents a publicly trusted certificate for a host name, and give Sandcovery that URL. The URL must use `https`, and it can hold a path, such as a proxy's path for one cluster.

## Add the credential

In **Provider Credentials** in the [dashboard](/app/credentials), add these four fields for Kubernetes Agent Sandbox:

- API server URL: the `https` URL of your proxy or load balancer
- Service account token: the token you read from the `sandcovery-token` secret
- Namespace: `sandcovery`
- RuntimeClass name: the RuntimeClass that selects your sandbox runtime, such as `gvisor`

## Costs

Agent Sandbox is free software under the Apache License 2.0, so Sandcovery lists it at $0 for every size, and its estimates and spend limits count its sandboxes at $0. Your cluster's nodes bill whatever their provider charges, which Sandcovery does not read.
