incus-templated is designed around strict process isolation, single-goroutine state confinement, and direct storage volume manipulation over the Incus API.
incus-templated separates control-plane responsibilities from the data plane across two isolated processes:
flowchart TD
subgraph ControlPlane["Control Plane (Private)"]
CC["incus-templated Controller"]
Credentials[("Incus Client Certificate<br/>(/var/lib/incus-templated)")]
CC --- Credentials
end
subgraph DataPlane["Data Plane (Public)"]
Caddy["Caddy Reverse Proxy"]
Ports["Public Ports 80 / 443"]
Ports --- Caddy
end
IncusDaemon["Incus Daemon API<br/>(HTTPS / Unix Socket)"]
CC -->|"1. Subscribes to events<br/>2. Discovers instances"| IncusDaemon
CC -->|"3. SFTP directly to storage volume"| StorageVolume[("Incus Storage Volume<br/>(config)")]
CC -->|"4. Exec 'caddy fmt' & 'caddy reload'"| Caddy
StorageVolume -.->|mounted at /config| Caddy
| Property | incus-templated |
caddy |
|---|---|---|
| Role | Control-plane event listener & config reconciler | Data-plane HTTP/HTTPS reverse proxy |
| Network Exposure | None (internal :9153 healthcheck only) |
Public ports 80 and 443 |
| Incus Credentials | Holds client TLS certificate or trust token | None (zero access to Incus API) |
| Caddy Admin API | Accesses via in-container exec (caddy reload) |
Bound to localhost:2019 inside container |
| Disk Access | Writes to storage volume via Incus SFTP | Mounts /config and /data storage volumes |
Because Caddy holds no Incus credentials and its Admin API is bound strictly to localhost:2019, a compromised backend service or proxy vulnerability cannot access the Incus API or manipulate container definitions.
A target instance keeps its file in an Incus custom storage volume, e.g. Caddy's caddy-config volume mounted at /config. The diagram follows the Caddy preset; any target with its own commands goes through the same steps.
Rather than requiring incus-templated to mount the storage volume locally on the host or in its own container, incus-templated communicates entirely through the Incus API:
sequenceDiagram
participant P as incus-templated Plugin
participant API as Incus API
participant Vol as Incus Storage Volume
participant C as Caddy Container
P->>API: GetInstance(project, instance)
API-->>P: Instance device configuration
Note over P: resolveVolume inspects disk devices<br/>attached to /config
alt Custom Storage Volume Attached
P->>API: GetStoragePoolVolumeFileSFTP(pool, "custom", name)
API-->>P: sftp.Client (volume root)
else No Volume Device
P->>API: GetInstanceFileSFTP(project, instance)
API-->>P: sftp.Client (container root)
end
P->>Vol: Create & write /Caddyfile.tmp
alt Container is Running
P->>API: ExecInstance("caddy fmt --overwrite /Caddyfile.tmp")
API->>C: caddy fmt --overwrite
C-->>API: exit code 0
API-->>P: Formatting and validation succeeded
P->>Vol: sftp.PosixRename("/Caddyfile.tmp", "/Caddyfile")
P->>API: ExecInstance("caddy reload --config /config/Caddyfile")
API->>C: caddy reload
else Container is Stopped (Cold Staging)
P->>Vol: sftp.PosixRename("/Caddyfile.tmp", "/Caddyfile")
Note over P,Vol: Staged directly onto volume.<br/>Caddy boots with valid config on start.
end
resolveVolume):
deploy() inspects the instance's expanded devices to locate a disk device whose mount path matches the parent directory of the target path (default /config with the Caddy preset). It extracts the pool and volume source name.incus-templated still updates the file on the storage volume. When the instance starts up or reboots, it reads the updated file immediately. If the instance started while the file was being written, its reload runs, since the service may have read the previous file.incus-templated never writes directly over the live file. It writes to <path>.tmp and only renames via sftp.PosixRename after the target's format and validate commands succeed in-container.--target <label>,path=<path>)When incus-templated runs on the same host, container, or VM as the service reading the file:
<path>.tmp in the target directory and atomically swaps it in via os.Rename. The live file keeps its mode, and a new one is 0644.format, validate and reload on the local machine. A failed reload is retried; the file is already in place for the next start.All state in templated.Plugin (instances, lastDeployed, chain) is strictly confined to p.Run(ctx)'s single goroutine:
flowchart LR
subgraph ieventChain["ievent Chain"]
Next["next(ev)"]
end
Event[Incoming Event] --> Handle["p.Handle(ev)"]
Handle --> Next
Handle -->|non-blocking send| Inbox[("p.inbox (chan *Event)")]
subgraph RunLoop["p.Run(ctx) Single Goroutine"]
Inbox --> RunSelect{select}
CommandIn[p.commandIn] --> RunSelect
Ctx[ctx.Done] --> RunSelect
RunSelect -->|event| Process["processEvent()"]
Process --> Reconcile["reconcile()"]
end
Plugin contains zero mutexes (sync.Mutex or sync.RWMutex) and zero atomic flags.Handle() are placed into a buffered inbox channel (p.inbox) and forwarded along the ievent chain immediately.processEvent() and reconcile() execute exclusively on the Run() goroutine. Race conditions between concurrent state updates and reloads are impossible by design.iutil.CommandDrain), p.Run drains remaining events in p.inbox via drainInbox(), forwards the drain command to p.commandOut, and exits cleanly.incus-templated coordinates with the ievent enricher to avoid flapping routes during startup or daemon reconnection:
iutil.ChainCold):
When the process starts, the enricher performs an initial fleet sweep of all monitored projects. During this sweep, instance events are recorded into p.instances, but all deployments are gated and paused.iutil.ChainWarm):
Once the sweep finishes, the enricher emits ActionSweepEnd. incus-templated transitions to ChainWarm and performs a single initial reconcile() deploying every target.ChainCold. Every target keeps its last good file while incus-templated reconnects and re-synchronizes the fleet.