Components
Process model
On a single host there is one control plane process. It opens its SQLite files in the data dir, starts the reconcile engine against the local Docker daemon, binds the API and the ingress, and serves the dashboard from the embedded files.agent.Local calls the Docker runtime in the same process.
With extra nodes, each node runs levelrail-agent next to its own Docker daemon. The agent connects out to the control plane, so a managed node needs no inbound port for control traffic. The control plane holds the other end of that session as agent.GRPCTransport. Controllers call the same agent.Transport interface either way.
Deploy data flow
The same path serves the dashboard, the CLI and the MCP tools, because they all call the REST API.levelrail-cli apply -f is the declarative entry. The verb apps apply is different: it restarts an app so saved env, secret and config changes reach the running container.
levelrail-cli apply -fvalidates the files, asksPOST /api/v1/apply/planfor a plan, prints it, and sendsPOST /api/v1/applywith the plan hash it saw. The server recomputes the plan and returns a conflict if it changed. Apply re-issues each change through the ordinary routes with the caller’s own credentials, so ability checks and audit rows are the same as for a manual change.- A deploy is
POST /api/v1/apps/{name}/deploys, guarded by the deploy ability on that app.handleTriggerDeployloads the desired service, resolves the image or release, applies environment protection (a protected environment turns the deploy into a pending approval), and records a deploy attempt. - For git or source builds,
deploy.Pipeline.Deploybuilds the image (Dockerfile through BuildKit, Railpack otherwise, static sites separately) and then saves desired state withSaveDesiredService. Image deploys write the new image into desired state directly. Neither step creates a container. - The reconcile engine runs when a Docker event arrives, when
Engine.Nudgeis called, and on a periodic resync. The application controller for that service reads desired state inController.Reconcile. - By strategy (
reconcileBlueGreen,reconcileRolling,reconcileRecreate) it computes target container names from the service, image, restart nonce and replica index, and callsensureReplicaRunningfor each. A missing target is created and started through thedocker.Runtimeinterface (createAndStart). waitReadyruns the readiness probe, and a watch for early exit or OOM kill where the runtime can report it. A replica that fails readiness stops the pass and the failure is recorded.- When every target is ready, stale containers of earlier releases are retired (
retireStale), subject to the drain window and the previous-release hold. The controller returns conditions with a reason string, and the engine stores them for the dashboard.
Reconcile loop invariants
These are properties the code and its tests rely on. Controllers ininternal/reconcile must keep them.
- Idempotent and level-triggered. A call derives what to do from desired and observed state, never from the memory of a half-finished call.
- A settled pass creates no containers. The application controller tests run
Reconciletwice and assert the create count did not change. Recreate has an explicit no-op check, because without it a healthy set would be recreated on every resync. - Every pass returns conditions with a reason. A maintenance hold, a suspended app and an app still waiting for its first build each return their own reason instead of silence.
- A maintenance hold wins over everything else. While
DesiredService.MaintenanceHoldis set, the controller leaves the containers to whoever set the hold. - Suspended means zero containers. The controller removes the service’s containers and returns early.
- A deploy that lands mid-pass owns the next verdict. If desired state changed while a pass ran, that pass’s result is dropped and the nudge from the deploy queues the next one.
- Drain and hold. A retired release is removed only after its drain window, measured from when traffic left it (
DrainTracker), and the previous release can be kept for a configured hold (heldFilter). Both are in memory, so after a restart the timers fall back to creation times. - Rolling waves.
reconcileRollingreplaces replicas in waves bounded by max surge and max unavailable from the rollout policy. - Storm protection. The engine rate limits passes and opens a per-controller circuit breaker after repeated failures. The limits are read from
APP_RECONCILE_*environment variables, andNudgeControllercloses a breaker when a change makes a retry worthwhile. - Shutdown. A pass in progress can finish for a drain timeout after a stop signal before its context is cancelled.
Trust boundaries
Where state lives
The data dir isAPP_DATA_DIR, default ./data for the binary. Installers set their own path.
Separate SQLite files for telemetry and alerting keep their write load away from the main store. Container state is not copied anywhere: Docker is the source of truth for what is running, and desired state is the source of truth for what should run.
Migrations live in
internal/store/migrations/*.sql, are embedded in the binary and applied in version order at start. The migration version check fails on duplicate numbers. An upgrade that fails to open or migrate the store starts safe mode instead of a normal boot.
API and generated files
Routes are registered ininternal/api/routes_*.go. scripts/gen-api-reference reads them and writes docs/api-reference.md, docs/public/openapi.json and internal/api/openapi_gen.go. These are committed. scripts/code-gen-check.sh runs every generator and fails if any tracked file changes. The route tree for the dashboard is generated by scripts/gen-route-tree.mjs and is not committed.
Ingress
Caddy runs inside the control plane process and is driven through its admin API. Certificates and ACME account keys are stored through acertmagic.Storage implementation over the store, so they are shared across nodes. The default issuer is internal and self-signed until HTTPS is enabled for a hostname. Real Let’s Encrypt issuance has been run over a <dashed-ip>.sslip.io name on a public host, see the recorded run. Renewal and DNS-01 wildcards are not verified on a real host.
Extension points
- Templates.
internal/catalogholds the one-click templates as compose bodies. A test parses and validates every one against the supported compose subset. See Adding a service template. - Plugins. The manifest kinds are
notification_channel,dns_providerandpanel_provider. A plugin runs out of process and reaches the control plane only through the abilities it was granted. See Plugins and ADR 034. - Notification channels. Built-in channels live in
internal/alerting. A plugin of kindnotification_channeladds more. - Platform as code. Resource kinds and their JSON schema live in
internal/iac, and the schema is published todocs/schemas/resources.schema.json.