8.4 KiB
Lab provisioner (labd)
Русский | English
A separate Go service that creates practical labs (MikroTik CHR and small Ubuntu VMs) as LXD virtual machines on the app host and deletes them. labd is the only component that talks to the LXD API: the lxd group is equivalent to root on the host, so the backend gets neither the LXD socket nor node addresses. The backend calls labd through the internal api/labd/ contract over TLS with a bearer token; the backend has the LabProvisioner port, the generated client, and a disabled adapter (backend/internal/adapter/secondary/lab_provisioner/), but neither bootstrap nor a use case wires them yet.
cmd/labd/is the entry point.internal/lab/holds the lab record and the names of its LXD objects (lcxl-<id>-<node>for instances,lxl<id><nn>for bridges) with theuser.lincodex.labmarker.internal/template/is the template model: nodes (mikrotikorubuntu, profile, source snapshot, memory), L2 links, the variant plan from the seed (management addresses, NICs, and MACs), and the hooks that render cloud-init user-data and network-config or a RouterOS script and check a variant. Real templates and images are not part of this build yet: the registry is empty and CreateLab answersvalidation_failed.internal/lxd/is a small client of the LXD REST API over its unix socket. The officialgithub.com/canonical/lxd/clientis not used: since LXD 5.20 it is AGPL-3.0 and pulls in most of the LXD tree.internal/lxd/lxdfake/is an in-memory LXD for unit tests.internal/pool/holds admission by the host memory budget, cloning nodes from snapshots, readiness by the management TCP port, console tokens, checks, expiry, and guaranteed deletion.internal/reaper/runs the periodic sweep (timers, deletion retries) and the reconciliation with LXD (orphans).internal/store/keeps the state in one JSON file.internal/server/is the HTTP API on the generated ogen server (internal/api/, fromcodegen/ogen.yml).
How a lab works
- CreateLab (
PUT /v1/labs/{lab_id}) checks the template and the lifetime, reserves memory and management addresses, writes the lab to the state file, and only then starts working with LXD. It answers 202provisioning; a repeat with the same ID and body returns the same lab. - Every link gets its own bridge without an address on the host (
ipv4.address=none,ipv6.address=none): lab traffic is not routed and reaches neither the host, the application, another lab, nor the internet. Every node is copied from a prepared snapshot (a subvolume snapshot on the btrfs poollincodex, seconds) and gets a management NIC onlxdbr-labmgmtwith a fixed MAC,security.mac_filtering, andsecurity.port_isolation, NICs on the bridges of its own lab, and the cloud-init of its variant;security.devlxd=false. - labd starts the nodes and waits until the management port of each (22 for Ubuntu, 8728 for RouterOS) accepts a TCP connection from the host side of the bridge. The lab becomes
ready; on an LXD error or a timeout it becomesfailedand is deleted. - A lab is deleted at
expires_at(creation time plusttl_seconds) or earlier by DeleteLab. Deletion is retried until LXD confirms that the instances and bridges are gone; only then are the memory and addresses released, and the record becomesdeletedand stays forLABD_DELETED_RETENTION.
Deletion after a crash: a record exists in the file before the first LXD object and disappears after the last one. At startup a lab in provisioning becomes failed (interrupted) and is deleted; the timers of the others live in the file and fire at the first sweep. Reconciliation deletes instances and bridges with the labd prefix and marker that no record owns; an object with the prefix but without the marker is only logged. labd is not ready (/readyz 503) until the first reconciliation succeeds.
Capacity: a lab is admitted only when the memory of all its nodes fits LABD_MEMORY_BUDGET_MIB together with the current labs (and, when set, LABD_MAX_LABS). Otherwise the answer is 503 lab_pool_busy with Retry-After and nothing is stored. A template that does not fit the budget even alone stops labd from starting. There are no pre-started nodes: a snapshot clone is cheap, and every node must boot with its own variant.
Storage: a JSON file that every change rewrites completely through a temporary file, fsync, rename, and a directory fsync. labd holds a few dozen labs, so this needs no SQLite, cgo, or new dependency, and the file can be read during an incident. The file holds variant seeds, so it is 0600 in the directory of the labd user. A corrupt file stops the start: labd must not forget labs whose objects may still exist.
A console token (POST /v1/labs/{lab_id}/console-tokens) is single-use and lives at most 120 seconds; labd keeps only its SHA-256 in memory. The WebSocket attach itself is not implemented yet and answers 501. A check (POST /v1/labs/{lab_id}/check) calls the template checker over the management addresses; when the checker cannot reach a node, the answer is 503 lab_unavailable, never a failed check.
Settings
internal/config/ reads the LABD_* variables and reports the errors of all of them together. TLS (LABD_TLS_CERT_FILE, LABD_TLS_KEY_FILE) and a token (LABD_API_TOKEN_FILE, at least 32 characters) are required; LABD_DEV_ALLOW_PLAINTEXT_HTTP=true is for development only. Main defaults:
| Variable | Default | Meaning |
|---|---|---|
LABD_LISTEN_ADDRESS |
127.0.0.1:8445 |
API address; in production the gateway address of the application Docker network |
LABD_ALLOWED_CLIENTS |
127.0.0.0/8,::1/128 |
networks requests are accepted from; others get 401 |
LABD_LXD_SOCKET, LABD_LXD_PROJECT |
/var/snap/lxd/common/lxd/unix.socket, default |
LXD |
LABD_STATE_FILE |
/var/lib/lincodex-labd/labs.json |
state file |
LABD_MEMORY_BUDGET_MIB, LABD_MAX_LABS |
3072, 0 (no separate cap) |
capacity |
LABD_MAX_LIFETIME, LABD_PROVISION_TIMEOUT |
2h, 5m |
lifetime and provisioning |
LABD_SWEEP_INTERVAL, LABD_RECONCILE_INTERVAL, LABD_DELETED_RETENTION |
15s, 5m, 1h |
deletion |
LABD_CONSOLE_TOKEN_TTL, LABD_CHECK_TIMEOUT, LABD_RETRY_AFTER |
1m, 1m, 30s |
tokens, checks, Retry-After |
LABD_MGMT_NETWORK, LABD_MGMT_SUBNET, LABD_MGMT_HOST_ADDRESS, LABD_MGMT_FIRST_ADDRESS |
lxdbr-labmgmt, 10.81.0.0/24, 10.81.0.1, 10.81.0.16 |
management plane from deploy/labs/lxd-preseed.yaml |
Running on the host
labd runs as a systemd service on the app host, not in Docker: deploy/labs/lincodex-labd.service and the example settings deploy/labs/labd.env.example (no secrets). The service runs as the dedicated user lincodex-labd with the supplementary group lxd, without capabilities, with ProtectSystem=strict, StateDirectory=lincodex-labd, and write access only to the LXD socket. The certificate, key, and token live in /etc/lincodex-labd/, readable only by that user. The host firewall opens the API port only to the application network. A compromise of labd is still a compromise of the host: the lxd group gives root through the LXD API, so labd accepts nothing but the described contract and never executes request data.
Before the rollout, check on the host what this build did not verify: that a bridge without an address gets no IPv6 link-local address (LXD disables IPv6 on such a bridge), that L2 traffic between nodes is not dropped by the Docker FORWARD chain when br_netfilter is loaded, that the LXD version supports security.port_isolation and cloud-init:config, and how the CHR image applies the script from cloud-init user-data. A template checker must authenticate the node (a pinned SSH host key or credentials of this lab), because a node of another lab can answer ARP for a foreign management address.
Checks
From the repository root, make -C labd check runs formatting, the Linux build, unit tests with the race detector, the generated code check, and lint. The tests use the in-memory LXD: creating and deleting a lab (nodes on isolated bridges that disappear completely), restarting labd in the middle of a lab's life and the timer deletion after it, a restart during provisioning, orphans, a failing and retried deletion, and the capacity limit with 503 for the extra lab. make -C labd test-live creates and deletes one bridge and one instance on a real LXD; it does not run without LABD_LIVE_SOCKET and LABD_LIVE_SOURCE and must not target the production host.