How it works

LabCluster is one Go program that is the website, the session controller and the host for each learner’s control plane, plus a Postgres database. This page explains how those parts fit together, what happens between “Start session” and “Finish”, and why it is built this way.

The big picture

Architecture: the browser reaches the web app through Cloudflare; the web app stores users and scores in Postgres and creates LabSession objects; the controller provisions a namespace per session with a private control plane and a terminal pod, and the web app proxies the terminal WebSocket.
Layer Technology Where in the code
Website and console Go net/http, server-rendered html/template, one stylesheet, a small script for the workspace internal/web
Docs pages Markdown in docs/, rendered at start-up with blackfriday docs/, internal/web/docs.go
Sign-in GitHub OAuth (golang.org/x/oauth2), server-side sessions in Postgres internal/web/auth.go
Data Postgres via lib/pq; the schema is created idempotently at start-up internal/store
Session API LabSession, a cluster-scoped custom resource api/v1alpha1, deploy/base/labsession-crd.yaml
Controller client-go informer and work queue internal/controller
Session clusters Kubernetes v1.34.2 control-plane binaries and KWOK v0.7.0, started by kwokctl internal/session
Node and container behaviour KWOK stages, plus LabCluster’s own internal/simruntime
Grading Declarative checks in each lab’s lab.yaml internal/grader, internal/labspec
Terminal ttyd in a pod, reached only through the website’s authenticated proxy internal/session/terminal.go, images/terminal
Delivery GitHub Actions, GitHub Container Registry, Kustomize, Cloudflare Tunnel .github/workflows, deploy/

The session lifecycle

Session lifecycle: start with a quota check, provision in six steps (namespace and guardrails, session cluster, nodes ready, lab setup, fault injected, terminal), work and grade, then end with a final grade and full deletion; only the score is kept in Postgres.

1. Start. When a learner presses Start session, the website checks their quotas in a single Postgres transaction (one running session, three per 24 hours, and a platform-wide cap, serialised with an advisory lock so concurrent starts cannot overshoot) and records the session. It then creates a LabSession object on the host cluster.

2. Provision. The controller watches LabSession objects and provisions each one in six steps, which the workspace shows live:

  1. Namespace and guardrails: a namespace lab-<session> with Pod Security “restricted”, a ResourceQuota (2 CPU, 3 GiB, no LoadBalancers or NodePorts), a LimitRange and default-deny network policies.
  2. Session cluster: a control-plane pod running labcluster session-host (see below). The controller fetches the cluster’s admin kubeconfig from it and keeps a copy in a Secret in the session namespace.
  3. Nodes and container runtime: the controller installs the node and pod behaviour, creates the lab’s nodes and waits until every node is Ready, so the first events a learner sees are the lab’s own.
  4. Lab setup: the manifests in the lab’s setup/.
  5. Fault injection: the manifests in the lab’s faults/.
  6. Terminal: a ttyd pod whose kubeconfig points at the session’s API server.

3. Work and grade. The learner works in the terminal. Grade increments a counter on the LabSession; the controller runs every check against the session cluster and writes the result to the object’s status. The website mirrors status into Postgres.

4. End. Finish, or the time limit plus a 10-minute grace period, triggers a final grade. The controller then deletes the session namespace, which removes the control plane, the terminal and everything else. The score and the learner’s best result per lab are kept in Postgres; nothing else outlives the session.

5. Recovery. A background loop keeps Postgres in step with LabSession objects even when nobody has the page open. If the controller restarts while a session is provisioning, that session is marked Interrupted and cleaned up, and it does not count against the learner’s daily limit.

Session clusters

A lab needs a cluster that starts in seconds, behaves exactly like Kubernetes at the API, scheduler and controller level, and costs almost nothing to run. LabCluster builds one per session from the official Kubernetes binaries and KWOK (Kubernetes WithOut Kubelet), a Kubernetes SIG Scheduling project that manages nodes and pods without running containers.

What runs for real. kube-apiserver, etcd, kube-scheduler and kube-controller-manager are the upstream binaries. Every API call, admission decision, scheduling decision, quota check, controller reconciliation and event is the real thing. That is why scheduling labs are faithful: when a GPU pod is Pending, the reason in kubectl describe pod comes from the real scheduler comparing the pod’s request with the node’s allocatable nvidia.com/gpu.

What KWOK provides. Nodes are Node objects that KWOK keeps Ready and heartbeating. Pods that are scheduled move through their lifecycle (created, running, ready, completed for Jobs, deleted) driven by KWOK stages, rules that update status. No container processes run.

GPU nodes. A GPU node is a Node whose capacity and allocatable include nvidia.com/gpu, with the labels NVIDIA GPU Feature Discovery would set (nvidia.com/gpu.present, nvidia.com/gpu.count, nvidia.com/gpu.product). This is the same information the scheduler sees on real hardware after the NVIDIA device plugin registers.

Container behaviour labs need. Two additions cover what most troubleshooting labs need:

  • Logs: a lab ships log files; they are converted to the format kubelets serve and returned by kubectl logs for the containers the lab names.
  • Start-up failures: a pod annotated labcluster.dev/requires-env: NAME whose first container lacks the environment variable NAME exits with status 1 and loops in CrashLoopBackOff with growing restart counts and back-off events, like a process that validates its configuration at start-up. Once the variable is present, the pod becomes ready.

What is not available. kubectl exec, port-forward, attach and logs --previous need a container runtime, so they do not work in lab clusters. Labs are written so they are never needed.

Speed. A session cluster is ready in about 4 seconds and fully prepared in about 8 on a development machine (measured by lab CI). In production, scheduling the control-plane pod and, the first time on a node, pulling the image add to that.

Isolation between learners

Each learner is the administrator of their own cluster, so the design assumes they will try anything that cluster’s API allows. Isolation therefore comes from the host cluster, not from the session cluster:

  • One control plane per pod. Each session’s API server and etcd run in their own pod (labcluster session-host), so anything a learner can reach through their API server, including its own etcd, is theirs. Nothing is shared between sessions except the host cluster.
  • One namespace per session, with Pod Security “restricted”, a ResourceQuota and a LimitRange.
  • Network policies: by default nothing goes in or out. Inside the namespace the terminal can reach the control plane; from outside, only the platform’s own namespace (the website’s terminal proxy and the controller) can connect. Egress is limited to DNS.
  • Hardened pods: both pods run as non-root with a read-only root filesystem, all capabilities dropped and no service account token.
  • Bootstrap secret: the controller fetches the session’s kubeconfig from the control-plane pod with a random bearer token stored in a Secret in the session namespace. The kubeconfig names the in-cluster Service and verifies the API server’s certificate under the name it was issued for (localhost), using the kubeconfig’s tls-server-name field.
  • Terminal access: the terminal is reachable only through the website’s proxy, which checks that the signed-in user owns the session, strips cookies, and refuses cross-site WebSocket upgrades.

Security lists everything that is enforced and how to report a problem.

Grading

Checks are declared in lab.yaml and come in two kinds: kube checks assert on object state with a JSONPath expression, and command checks run a shell command with KUBECONFIG pointed at the session cluster. Each check is worth points, so partial solutions earn partial credit.

Hidden checks are shown to the learner only after grading. They protect the rules in the goal: a lab that says “do not change node capacity” has a hidden check that node capacity is unchanged, so deleting or rewriting the thing under test does not pass.

Grading runs inside the controller with the session’s kubeconfig, so a grade takes about as long as the checks themselves, with no pod to start. Objects that are being deleted are ignored by default, so learners are not marked down for old pods in their termination grace period.

Data and the leaderboard

Postgres holds four tables: users (from GitHub), web_sessions (sign-ins), lab_sessions (every session with its phase, timings and grade) and progress (each learner’s best result per lab). The schema is created and migrated idempotently when the website starts.

The leaderboard is computed in one SQL query from lab_sessions: for each learner and lab, the best score as a share of the maximum, multiplied by the lab’s points (100, 200 or 300 by difficulty), summed per learner. Learners with equal points share a rank. Private profiles and banned accounts are excluded. Results are cached for 30 seconds and refreshed as soon as a session ends with a score.

Why the website is fast

  • Server-rendered pages. Each page is a single HTML response; there is no client-side framework to download and boot.
  • One stylesheet, system fonts. No web font downloads, no layout shift from late fonts.
  • Fingerprinted, precompressed assets. Static files are embedded in the binary, gzip-compressed once at start-up and served from URLs that include a content hash with a one-year immutable cache, so repeat visits load them from the browser or Cloudflare’s edge.
  • Compressed pages. HTML and JSON responses are gzip-compressed on the fly.
  • Prefetching. Pages declare speculation rules so supporting browsers prefetch same-site links as the pointer approaches them.
  • Caching of public queries. Leaderboard and landing-page figures are cached in memory for seconds, not recomputed per request.
  • Cloudflare in front. TLS terminates at the nearest Cloudflare location and static files are served from its cache.

Lab CI

Every pull request and a weekly schedule run labcluster lab test for every lab: create a session cluster, apply setup and faults, check that a scenario starts below a full score, run the lab’s reference solution, and require a full score within 60 seconds. A lab cannot merge if its own solution does not pass, and a platform change cannot merge if it breaks a lab.

Design decisions

  • One binary. The website, controller, session host and lab tooling share types and code paths (the grader that runs in production is the one lab CI runs), and there is one image to build, scan and deploy.
  • Server-rendered HTML. The pages are documents and forms; rendering on the server keeps them fast on slow networks and keeps the code readable for contributors.
  • Labs as YAML and shell in Git. Authors need no Go, reviews happen in pull requests, and CI proves every lab works.
  • A custom resource for sessions. The controller is level-triggered: it can restart at any time and converge from the objects on the host cluster.
  • Grading in the controller. Checks only read the session cluster, so a separate grading pod would add start-up time to every click without adding isolation.

What is next

  • Labs with running containers. Labs on model serving and observability need real processes. The plan is a second session type built on vCluster, which is open source under Apache-2.0, with each virtual cluster’s pods running as real containers inside the session namespace’s guardrails.
  • More labs. The catalogue shows which tracks need authors. See Writing labs.
  • Operations. Leader election for the controller, Prometheus metrics, and abuse detection.

Edit this page on GitHub