Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,18 @@ hosts:
# (two hosts without `network:` both have the fleet's, so they overlap too). Either field may be set;
# the other falls back to the fleet-level `network:`.
node-02:
provider: digitalocean
address: auto
provider: static
address: 192.0.2.11 # DNS records that follow a service on this host point here (needs an IP)
incus_remote: node-02 # optional: the Incus remote that reaches this host; a migration's
# target_host: node-02 goes to it (docs/daemon.md, "Moving an instance")
network:
ipv4_cidr: 10.0.101.0/24
# This host's part of the mesh (below): its WireGuard public key, as its daemon's plan reports it.
# Optional: address (its tunnel address; otherwise derived from mesh.subnet and the sorted host
# names) and endpoint (host[:port]; otherwise this host's address and mesh.listen_port).
mesh:
public_key: <node-02's public key>
# reconcile brings each host up on its own, in name order; one failing does not stop the others.
# The host a service without `host:` runs on: required with two or more hosts (with one, that host is
# the default). Each host's daemon runs with `serve --host-name <host>` and applies only its own
# services: see docs/daemon.md, "Several hosts".
Expand All @@ -318,13 +321,19 @@ daemon:
flags: "--addr 10.0.100.1:8686 --enable-apply"

# Records the plan diffs against the provider and a deploy tag applies (create or update, never delete),
# beyond the computed apex + wildcard A. Each names its own zone.
# beyond the computed apex + wildcard A (which point at the default host). Each names its own zone.
# With several hosts, DNS follows the host: a record with `service:` belongs to that service's host, and
# only that host's daemon writes it (an A/AAAA one leaves `value:` out and gets the host's address); a
# service off the default host routed under `domain:` gets an A record at its host; every other record is
# the default host's. Wildcards tied to a service, and records two hosts would claim, are refused.
# See docs/daemon.md, "DNS with several hosts".
dns_records:
- zone: example.com
type: MX
name: inbound # inbound.example.com
value: inbound.example.com. # the mail host
priority: 10
# - {type: A, name: git, service: gitea} # git.example.com -> gitea's host (its address: must be an IP)

# Local snapshots, kept by Incus itself (no host timer): the default for every volume a service declares.
# A volume's own `snapshots:` replaces it; `snapshots: {}` on a volume turns them off for it. Absent here
Expand Down
49 changes: 43 additions & 6 deletions cmd/native-ops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,34 @@ func handleValidateCommand(ctx context.Context, args []string) {
fmt.Printf(" • Hosts: %d (%v)\n", len(summary.Hosts), summary.Hosts)
fmt.Printf(" • Services: %d (%v)\n", len(summary.Services), summary.Services)
fmt.Printf(" • Templates: %d (%v)\n", len(summary.Templates), summary.Templates)
// With several hosts, what each host's daemon plans and applies.
if len(summary.PerHost) >= 2 {
for _, h := range summary.PerHost {
def := ""
if h.Default {
def = ", default host"
}
fmt.Printf(" • Host %s (%s%s)\n", h.Name, orDash(h.Address), def)
fmt.Printf(" services: %s\n", orDash(strings.Join(h.Services, ", ")))
if len(h.DNS) == 0 {
fmt.Printf(" dns: -\n")
}
for i, r := range h.DNS {
label := " "
if i == 0 {
label = "dns: "
}
fmt.Printf(" %s%s\n", label, r)
}
}
}
}

func orDash(s string) string {
if s == "" {
return "-"
}
return s
}

func handleReconcileCommand(ctx context.Context, args []string) {
Expand Down Expand Up @@ -553,19 +581,28 @@ func handleDNSCommand(ctx context.Context, args []string) {
}

logf := func(format string, a ...any) { fmt.Printf(format+"\n", a...) }
if err := syncDeclaredRecords(ctx, fleet, dnsProv, logf); err != nil {
// Every host's records: this is the operator's tool, not a daemon, so it is not limited to one host's share.
services, err := config.LoadServices(*configDir, "")
if err != nil && len(fleet.Hosts) >= 2 {
log.Fatalf("Load services (the DNS records of a fleet of several hosts follow their services): %v", err)
}
if err := syncDeclaredRecords(ctx, fleet, services, dnsProv, logf); err != nil {
log.Fatal(err)
}
}

// syncDeclaredRecords creates or updates the records declared in fleet.yml (`dns_records:`), zone by
// zone. A sync never deletes a record it was not told about. `dns sync` and the daemon's
// POST /v1/dns/sync both use it.
func syncDeclaredRecords(ctx context.Context, fleet *config.FleetConfig, dnsProv provider.DNSProvider, logf func(string, ...any)) error {
byZone, err := fleet.DNSRecordsByZone()
// syncDeclaredRecords creates or updates the fleet's DNS records (fleet.yml's `dns_records:`, with each
// record tied to a service pointing at that service's host, and the records of the routes of services off
// the default host), zone by zone. A sync never deletes a record it was not told about.
func syncDeclaredRecords(ctx context.Context, fleet *config.FleetConfig, services []*config.ServiceConfig, dnsProv provider.DNSProvider, logf func(string, ...any)) error {
all, err := fleet.DNSRecordsByHost(services)
if err != nil {
return fmt.Errorf("dns_records: %w", err)
}
byZone := map[string][]config.DNSRecordConfig{}
for _, r := range all {
byZone[r.Zone] = append(byZone[r.Zone], r.DNSRecordConfig)
}
zones := make([]string, 0, len(byZone))
for z := range byZone {
zones = append(zones, z)
Expand Down
2 changes: 2 additions & 0 deletions cmd/native-ops/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ func newDaemon(cfg daemonConfig) (*server.Server, func(), error) {
opts.ResumeFile = filepath.Join(cfg.StateDir, "deploy-resume.json")
opts.Flags = cfg.Flags
}
// A deploy tag records fleet.yml's hosts and their Incus remotes, for a migration by target_host.
opts.HostRemotesFile, opts.HostName = filepath.Join(cfg.StateDir, "host-remotes.json"), cfg.HostName
if cfg.EnableBackup {
opts.BackupScheduleDir = filepath.Join(cfg.StateDir, "backup-schedule")
opts.Backup, opts.Restore = backupSource(cfg.Exec, cfg.Pool, lookup, cfg.HostName), restoreSource(cfg.Exec, cfg.Pool, lookup, cfg.HostName)
Expand Down
69 changes: 61 additions & 8 deletions docs/daemon.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ the host, or by your IaC. It is not something a person sets up by hand.
| `PATCH /v1/instances/{name}/labels` | deployer | change a tenant instance's labels live (`{"set": {...}, "remove": [...]}`), as a job; no restart |
| `POST /v1/instances/{name}/resize` | deployer | a live `limits.cpu`/`limits.memory` change, as a job; no restart |
| `POST /v1/instances/{name}/suspend` | deployer | replace the published route with a static 503 naming a reason, as a job, without touching the instance; undone by asking for the instance again (`PUT`), which always republishes the normal route |
| `POST /v1/instances/{name}/migrate` | admin, or a token scoped to the instance | on the source host: move a tenant instance and its volumes to another host (`{"target_remote": "..."}`), as a job; on success its route here is dropped and the copy here is marked migrated ([Moving an instance to another host](#moving-an-instance-to-another-host)) |
| `POST /v1/instances/{name}/migrate` | admin, or a token scoped to the instance | on the source host: move a tenant instance and its volumes to another host (`{"target_remote": "..."}`, or `{"target_host": "..."}`, a `fleet.yml` host whose `incus_remote` is used), as a job; on success its route here is dropped and the copy here is marked migrated ([Moving an instance to another host](#moving-an-instance-to-another-host)) |
| `GET /v1/instances/{name}/export-spec` | viewer | the instance's spec (image, limits, volumes, health, domain, route directives, labels), never its environment, for the target host to adopt |
| `POST /v1/instances/{name}/adopt` | admin, or a token scoped to the instance | on the target host: take over an instance a migration put there (check it, record the spec, publish the route), as a job; refused if it is not there |
| `POST /v1/instances/{name}/finalize-migration` | admin, or a token scoped to the instance | on the source host: delete the stopped copy of an instance it migrated away, as a job, once the target is verified |
Expand Down Expand Up @@ -312,7 +312,7 @@ planned. Once applied, the same commit plans to nothing. That includes the netwo

| What | Declared in | Planned as | Applied |
|---|---|---|---|
| DNS records | `fleet.yml` `dns_records` (each names its zone) | `dns-create` / `dns-update`, diffed against the provider (read only) | through the built-in provider; the zone is read again first, and if it moved since the plan the apply refuses ("plan again") |
| DNS records | `fleet.yml` `dns_records` (each names its zone); with several hosts, only those that point at this daemon's host ([DNS with several hosts](#dns-with-several-hosts)) | `dns-create` / `dns-update`, diffed against the provider (read only) | through the built-in provider; the zone is read again first, and if it moved since the plan the apply refuses ("plan again") |
| The edge's main Caddyfile | `edge/Caddyfile` | `edge-caddyfile`, with the lines added and removed; its content is in the hash | validated, reloaded, and rolled back if Caddy rejects it |
| Host ports | a service's `forwards:` | `publish-port` / `remove-port` on that service | an Incus proxy device per forward |
| The instance bridge | `fleet.yml` `network:`, or the host's own `hosts.<name>.network:` | checked only | never: the daemon does not re-address a live bridge, so a difference **blocks** the plan. With more than one host in `fleet.yml`, each daemon checks the bridge of the host it is (`--host-name`, see [Several hosts](#several-hosts)) |
Expand Down Expand Up @@ -410,7 +410,7 @@ manager, or an admin) runs the sequence:

```bash
# 1. on the SOURCE daemon: copy the instance and its volumes to the target, stop the source, start and check the target
curl -X POST "$SOURCE/v1/instances/cust-one/migrate" -d '{"target_remote": "host-b"}' # 202, a job: wait for it
curl -X POST "$SOURCE/v1/instances/cust-one/migrate" -d '{"target_host": "host-b"}' # 202, a job: wait for it (or "target_remote")
# 2. on the SOURCE daemon: the spec the target needs (never the environment)
curl "$SOURCE/v1/instances/cust-one/export-spec" # {"name", "spec", "migrated_to"}
# 3. on the TARGET daemon: take the instance over, with that spec
Expand All @@ -432,6 +432,11 @@ curl -X POST "$SOURCE/v1/instances/cust-one/finalize-migration" -d '{}'
source with its copy of the volumes. `resume: true` continues an interrupted run onto the stopped copy it
left on the target. Refused (`409 not_migratable`) for an instance that mounts a host path, has no
recorded spec, or was already migrated.
- **`target_host`** names the target by its `fleet.yml` host: the daemon resolves it to that host's
`hosts.<name>.incus_remote`, as the `fleet.yml` of the last deploy tag it applied gives it (a deploy
records the hosts and their remotes in the state directory). It is refused when no deploy recorded the
hosts yet, when the host is not in them or has no `incus_remote`, when it is this daemon's own host, and
with `target_remote` beside it. `target_remote` still works without `fleet.yml`.
- **`target_remote`** is an Incus remote configured **on the source host** for the daemon's user
(`incus remote add host-b https://<target>:8443 ...`, trusted on the target), the same remotes the CLI's
`--target` names. The copy is pushed directly from the source to the target, so **the two hosts must reach
Expand Down Expand Up @@ -807,8 +812,10 @@ host: host-b # must name an entry of hosts:; left out, the service i
- **A plan is one host's share.** It plans the services of its host (their `host:`, else `default_host`)
and lists every other service as one line, `- db (skipped: on host host-b)`, without reading anything
about it from the host. A host that runs the edge instance applies `edge/Caddyfile` to it; a host without
one, when the edge service is pinned to another host, notes that instead of being blocked. `dns_records` are the fleet's, so only the default host's daemon plans and applies them;
the others note it.
one, when the edge service is pinned to another host, notes that instead of being blocked.
- **DNS follows the host.** Each daemon plans and applies only the DNS records that point at its own host,
so two daemons never write the same record (below). Its plan names, per other host, the records that
host's daemon does (`note: dns: host host-b's daemon plans and applies A db.example.com, ...`).
- **Each host checks its own bridge.** The plan checks the live bridge against that host's network (its
`hosts.<name>.network`, else the fleet's `network:`), and apply's launches make sure of that bridge, never
the fleet's or another host's.
Expand All @@ -820,9 +827,52 @@ host: host-b # must name an entry of hosts:; left out, the service i
one. A `--host-name` that is not in `hosts:` is refused the same way, and apply checks again that every
service it changes is its host's. The CLI's `plan` and `apply` take `--host-name` and follow the same
rule; `reconcile --deploy-services` (one host over SSH) is refused for several hosts.
- **`validate` shows each host's share**: with two or more hosts it lists, per host, its address, its
services and the DNS records that point at it.
- **A single-host fleet is unchanged.** With one entry in `hosts:` (or none) that host is the default,
`--host-name` may be left out, and plans, their text and their hashes are exactly as before.

### DNS with several hosts

Which host's daemon writes a record:

| Record | Points at | Written by |
|---|---|---|
| `domain:`'s apex and wildcard (`A @`, `A *`) | the default host | `reconcile`, as before |
| a `dns_records` entry with `service: <name>` | that service's host (`host:`, else `default_host`); an `A`/`AAAA` record leaves `value:` out and gets the host's `address:` | that host's daemon |
| a route (`routing.domain`) of a service **not** on the default host, under `domain:` | an `A` record of that name at its host's `address:` (the wildcard points at the default host, so without it the name would reach the wrong host) | that host's daemon |
| any other `dns_records` entry (no `service:`) | its `value:`, as written | the default host's daemon |

```yaml
# fleet.yml
domain: example.com
dns_records:
- {type: A, name: mail, service: mailer} # mailer's host's address
- {type: MX, name: "@", value: mail.example.com., priority: 10, service: mailer} # follows mailer
- {type: TXT, name: "@", value: "v=spf1 mx -all"} # the default host's
```

Refused by `validate` and blocking the plan, with a message that says why:

- a record tied to a service whose name is a **wildcard** (it would claim the names of other hosts'
services), and a service off the default host **routed at a wildcard** or at the domain's apex;
- a record **two hosts would claim** (the same zone, name and type, or a `CNAME` beside any record of
its name, with different hosts), including a record that collides with `domain:`'s apex or wildcard;
- a record tied to a service that does not exist, an `A`/`AAAA` record tied to a service that also gives
`value:`, and one whose host has no IP address of that family in `hosts.<name>.address`.

A route outside `domain:`'s zone gets no record of its own: declare it in `dns_records` with `service:`.
`native-ops dns sync` (an operator's tool, not a daemon) writes every host's records. A fleet of one
host has nothing to split: its records, plans and hashes are as before.

### Reconcile with several hosts

`native-ops reconcile` brings each host up on its own, in the order of their names: each with its own
address, SSH user and port, and bridge. A host that fails (it cannot be provisioned, or SSH never
answers) is logged, the others carry on, and the run fails at the end naming every host that failed.
The fleet's `domain:` points at the default host, whatever order the hosts are in. With one host the
output and errors are exactly as before.

**One tag, every daemon.** A deploy tag is sent to every host's daemon by a CI matrix, one job per host
with that host's URL and token ([ci-examples/deploy-hosts.yml](ci-examples/deploy-hosts.yml)):

Expand Down Expand Up @@ -1063,6 +1113,7 @@ Everything the daemon keeps is in `--state-dir` (default `/var/lib/native-ops`,
| `audit.log` | one JSON line per request and per finished job (never a value or credential) |
| `bin/` | the daemon's binary, the previous one, and the upgrade markers (see above) |
| `deploy-resume.json` | a deploy that upgraded the daemon to `fleet.yml`'s pin, until the new binary resumes it |
| `host-remotes.json` | `fleet.yml`'s hosts and their `incus_remote`, from the last deploy tag: where a migration's `target_host` goes |

All files are 0600. Back the directory up if losing approvals, job history or users matters to you;
tokens and secrets can be re-created from the git server.
Expand Down Expand Up @@ -1127,7 +1178,9 @@ record. Normally services reach a host through an approved apply.

Done: services pinned to hosts, with one daemon per host applying its own share and checking its own
bridge subnet ([Several hosts](#several-hosts)), driven by a CI matrix, and a WireGuard mesh between them
([Mesh between hosts](#mesh-between-hosts)). Next: `native-ops remote` fanning a plan or apply out to each
host's daemon and gathering the results; DNS and reconcile per host, backups keyed by host; instance migrate and previews as jobs; a live view of
a running job; and per-user API tokens. The API is versioned (`/v1`) so a separate fleet manager can
([Mesh between hosts](#mesh-between-hosts)); DNS that follows each service's host and reconcile per host
([DNS with several hosts](#dns-with-several-hosts)); backups keyed by host; instance migrate as a job, with
`target_host` resolved through `incus_remote`. Next: `native-ops remote` fanning a plan or apply out to each
host's daemon and gathering the results; previews as jobs; a live view of a running job; and per-user API
tokens. The API is versioned (`/v1`) so a separate fleet manager can
depend on it.
16 changes: 11 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ type DNSRecordConfig struct {
Value string `yaml:"value"`
TTL int `yaml:"ttl,omitempty"`
Priority int `yaml:"priority,omitempty"`
// Service ties the record to a service: it follows that service's host (its `host:`, else the default
// host), and only that host's daemon plans and applies it. An A or AAAA record tied to a service leaves
// Value out: it is the host's `address:`. A record tied to no service belongs to the default host.
Service string `yaml:"service,omitempty"`
}

// DNSRecordsByZone groups the declared records by zone (fleet.domain when a record omits it). It errors
Expand Down Expand Up @@ -300,11 +304,13 @@ type ProxmoxConfig struct {
}

type HostConfig struct {
Provider string `yaml:"provider"` // "digitalocean", "proxmox", "static"
Address string `yaml:"address"`
SSHUser string `yaml:"ssh_user"`
SSHPort int `yaml:"ssh_port"`
IncusRemote string `yaml:"incus_remote"` // optional Incus remote name
Provider string `yaml:"provider"` // "digitalocean", "proxmox", "static"
Address string `yaml:"address"`
SSHUser string `yaml:"ssh_user"`
SSHPort int `yaml:"ssh_port"`
// IncusRemote is the Incus remote, as the other hosts name it, that reaches this host: a migration to
// it (POST /v1/instances/{name}/migrate with target_host) goes to this remote. Optional.
IncusRemote string `yaml:"incus_remote,omitempty"`
// Network overrides the fleet-level `network:` for this host; a field left empty falls back to it.
// Each host needs its own subnet (LoadFleetConfig refuses two that overlap).
Network *NetworkConfig `yaml:"network,omitempty"`
Expand Down
Loading
Loading