Skip to content

Scope fleet projects per host - #10

Open
gunnsth wants to merge 7 commits into
masterfrom
host-scoped-projects
Open

gunnsth wants to merge 7 commits into
masterfrom
host-scoped-projects

Conversation

@gunnsth

@gunnsth gunnsth commented Oct 24, 2026 •

Copy link
Copy Markdown

Summary

fleet.yaml's projects: list was global: ComputePlan queried/managed
instances in every listed project on every host, and the loader's
instance-project allowlist checked against that same global list. Adding
a project used on only one host (e.g. a db project used only on
host-b) meant every other host was treated as needing it too:
remote bootstrap printed a trust add-certificate --projects list
naming it, which Incus refuses ("Project not found") on a host that
doesn't have it, and doctor --deep reported it as MISSING. The only
way round it was to create every project on every host.

(Correction: an earlier version of this description said diff/sync
broke. They don't: I checked against Incus 7.6, and listing instances
in a project that doesn't exist, or that a restricted cert can't see,
returns an empty list with no error.)

This scopes fleet.Projects per host instead of leaving it fleet-wide.

Design

Fleet.Projects (already a per-host object, since config.Load(fleetPath, host) takes a host) now holds the set this host needs: the projects its
own instances declare, unioned with an optional host_projects: map in
fleet.yaml for pre-declaring a project before any instance on that host
uses it yet (e.g. ahead of remote bootstrap/doctor --deep on a
brand-new host). A project used only by host A never appears in host B's
Projects.

Why this shape over a new per-host YAML file or always-manual map:

  • It's derived automatically from what's already declared
    (hosts/<host>/instances/*/instance.yaml's project: field), so
    existing fleets need no config changes.
  • host_projects is the minimal escape hatch for the one case derivation
    can't cover: a project that needs to exist on a host before its first
    instance file is committed.
  • Every instance's project: is still validated against the fleet-wide
    projects: list before narrowing, so the existing "every project is
    declared somewhere, explicitly" guarantee is unchanged.

list/orphans are read-only and deliberately not narrowed: with no
--project they check every fleet-wide project the host has (one
GetProjectNames() call) and print which projects they checked and which
they skipped, so containers with no instance file yet still show,
as long as the host's cert covers their project. orphans does not print
an all-clear when it checked nothing, and list shows managed instances
in an unchecked project as NOT-CHECKED, not MISSING. --project X
checks exactly X, and fails if the host doesn't list it (Incus would
otherwise return an empty list, and orphans would report an
all-clear). A typo'd --project fails before connecting.

Since a host's diff/sync only look in its own projects, the
isolation-policy check (e.g. a privileged container blocking
sync --apply) only covers those too; host_projects keeps another
project covered. Documented in docs/schema.md.

Changes

  • internal/config/fleet_meta.go — host_projects: {host: [project...]}
    field on fleet.yaml; validated as a subset of the top-level
    projects: list.
  • internal/config/loader.go — Fleet.Projects narrowed to the host's
    effective set after instances load (narrowProjectsToHost); a new
    Fleet.CertProjects() (host-scoped, replaces the old fleet-wide
    FleetMeta.CertProjects); CheckHostProjectsKeys, which validate
    runs once to warn when host_projects names a host with no
    hosts/<host>/ directory (a typo there would otherwise be a silent
    no-op). It is not run inside Load, so it never shows up as a risk
    flag on other hosts' diff/sync.
  • internal/model/instance.go — corrected a stale project: doc comment
    (claimed a default-project fallback the loader has never had).
  • cmd/incus-sync/cmd_doctor.go — doctor --deep checks project
    existence only for the host's effective set, and says "missing or not
    visible to this cert", since a restricted cert cannot tell the two apart.
    If the fleet does not load, it reports that, skips the project check and
    still runs the profile/network checks. Note it now loads the whole
    fleet for the host, not just fleet.yaml, to know the host's projects.
  • cmd/incus-sync/cmd_remote.go — remote bootstrap scopes the printed
    cert --projects to the host's effective set, plus any projects passed
    with the new --projects flag (validated against the fleet list), so a
    new host's cert can cover the projects orphans/import need. It prints
    the client cert fingerprint, and a NOTE for a new host whose cert leaves
    out fleet projects, explaining what is blind until it is widened and how:
    incus config trust edit <fingerprint>, or re-running bootstrap, which
    makes a new key and cert to register (and the old trust entry to
    remove). It
    also loads the fleet before encrypting/writing remote.sops.yaml, so an
    unrelated fleet validation error fails before any secret file is
    written, and removes hosts/<host>/ again if it created it and a later
    step fails while it is still empty.
  • cmd/incus-sync/cmd_list.go, cmd_orphans.go, main.go — see above:
    default checks every fleet-wide project the host has and reports
    skipped ones (projectsToQuery); --project X queries exactly X,
    validated against the fleet-wide projects: list.
  • cmd/incus-sync/cmd_validate.go — runs the host_projects key check
    once per run.
  • diff/sync needed no code change — they already iterate
    fleet.Projects, so narrowing it in the loader fixes them for free.
  • docs/schema.md, README.md — document host_projects:, the
    per-host scoping rule, the wider list/orphans default, and what to
    do when a host gets its first instance in a project its cert was not
    scoped for.
  • Tests: internal/config/host_projects_test.go (narrowing, the
    host_projects escape hatch, fleet-wide allowlist still enforced,
    typo warning, host-scoped CertProjects), internal/incus/scope_test.go
    (end-to-end: config.Load a two-host fixture, then assert
    ComputePlan never calls GetInstances for the other host's project —
    verified this test fails without the fix), and
    cmd/incus-sync/project_scope_test.go (list/orphans project
    selection with a fake server, the orphans no-all-clear case, doctor's
    project check, bootstrap's cert scope, and bootstrap's directory cleanup
    on a failed load).

Considered but skipped: having resolveProject (used by shell/adopt/
import, single-project commands) auto-pick a host's sole narrowed
project instead of requiring --project. Skipped because adopt/import
exist specifically to pull live state before a fleet instance file
exists, so the narrowed set is usually empty for exactly the case this
would help — not worth the complexity for a net-zero (or worse) ergonomic
win.

Test plan

  • go build ./...
  • go vet ./...
  • go test ./...
  • go fmt ./... (no diff)
  • Confirmed internal/incus/scope_test.go's regression test fails
    without narrowProjectsToHost (temporarily reverted it, re-ran,
    restored)
  • Built a dev binary and ran it against a real two-host, two-project
    fleet repo (offline, in a separate git worktree — nothing committed
    there):
    • validate --host <host-with-project-A> — OK (instances all in
      project A)
    • validate --host <host-with-project-B> — OK (instances all in
      project B)
    • render --host <host-with-project-B> --kind all — no mention of
      project A
    • Direct check: Load(dir, host-A).Projects == [A],
      Load(dir, host-B).Projects == [B], each CertProjects() correctly
      excludes the other host's project
  • go test -race ./...
  • Made the projectsToQuery default path return the old narrowed
    (empty) set and confirmed the new test fails
  • Ran remote bootstrap end to end with a stand-in for sops against
    one- and two-project copies of examples/minimal-fleet: the NOTE
    shows on a new host when the cert leaves out a fleet project (not
    when default is the only project), --projects widens the scope,
    a typo'd --projects fails before anything is generated
  • Against Incus 7.6 in a lab VM with a temporary restricted cert
    scoped to one project (removed afterwards):
    • listing instances in an out-of-scope or nonexistent project returns
      an empty list, no error; fetching one instance there returns 403
    • trust add-certificate --projects with a nonexistent project fails
      with "Project not found"
    • projectsToQuery checks the in-scope project and reports the other
      as skipped; --project <out-of-scope> errors
    • list shows the instance in the unchecked project as NOT-CHECKED;
      orphans lists the real unmanaged containers
    • Not run end to end through the CLI with a remote.sops.yaml; the
      code paths above were called directly with an HTTPS connection

Left for a follow-up (also on master): orphans/list match live
containers by bare name across projects, and the import commands
orphans prints have no --project.

… never query a project a host doesn't actually use.
… instead of silently filtering an empty, host-narrowed loop.
…e so a validation error can't leave a half-written host.

@unidoc-ahall unidoc-ahall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: REQUEST_CHANGES — 0 must-fix, 1 should-fix, 3 nits

The direction is right: one global projects: list made every host's diff/sync query projects it does not have, and deriving the per-host set from the instance files is a good shape. The loader change is small and the order of steps is sound (the fleet-wide allowlist check runs before narrowing, and host_projects is validated in FleetMeta.Validate). I checked out ee3e644: go build, go vet and go test ./... pass. diff/sync really need no code change, since ComputePlan only creates and updates and never deletes, so a narrowed set cannot make it remove anything.

One thing I would change before merging: the narrowing is applied to list and orphans too, and that takes away what those two are for.

Findings are keyed F1, F2, … so GitHub does not link them to issues.


F1 [should-fix] list/orphans go blind on exactly the hosts and projects they are used for

cmd/incus-sync/main.go projectsToQuery returns fleet.Projects (the narrowed set) when --project is not given. For a host with no instance file in a project, that project is never queried. The description accepts this as a documented trade-off, but two things make it worse than documented:

  1. orphans then reports a false all-clear. cmd_orphans.go:100 prints No orphans on <host> — every container is under fleet control. when the live list is empty. On a host whose containers are all hand-made (no instance files yet), fleet.Projects is empty, nothing is queried, and that is what you read. That is the host orphans exists for: it is the discovery step before import (the description itself says adopt/import run before an instance file exists). The same happens for a project whose last instance file was deleted from the fleet: its live containers, the ones you most want listed as unmanaged, are no longer looked at.
  2. "The common single-project fleet … behaves exactly as before" is not true for a new host or a host with no instance files in a single-project fleet. Before, orphans --host new scanned the one project; now it scans nothing and says everything is fine. list shows nothing.

The bug this PR fixes is that diff/sync fail on a project that does not exist on the host. list/orphans are read-only, so they can keep the old reach and still avoid that failure: query every fleet-wide project, but skip a project the host does not have. For example, in projectsToQuery, when explicitProject == "", load the fleet meta, ask the server for GetProjectNames() once, and return meta.Projects filtered by that list. Then --project can keep its current meaning, and host_projects stays what it is for: diff/sync/doctor/the cert scope. The trade-off paragraph in docs/schema.md and the README can go.

If you would rather keep the narrowed default, at least make orphans say what it looked at, for example Checked projects: web (this host's instances use only these; pass --project to look elsewhere), and do not print "every container is under fleet control" when the checked set is empty.

Source-read only; not run against an Incus daemon.


F2 [nit] A host_projects entry for a host that is not bootstrapped yet warns on every other host

checkHostProjectsKeys runs in every Load, for every host, and the warning text is added to fleet.Warnings. sync --apply prints those under ⚠️ RISK FLAGS: and diff prints them in the review text. The description calls pre-declaring a project before remote bootstrap creates hosts/<host>/ a legitimate ordering, but between writing the entry and bootstrapping, every other host's sync --apply and diff shows a risk flag about it. Either only check the key for the host being loaded (f.Host), which is the only entry that has any effect there, or keep the global check in validate only.

Source-read only.


F3 [nit] remote bootstrap: the directory is left behind on a failed load, and "re-run bootstrap" rotates the credential

  • os.MkdirAll(hosts/<host>) now runs before config.Load. If Load fails for an unrelated reason, no secret is written (good, as described), but an empty hosts/<host>/ stays in the working tree. It also silences the new typo warning for that host. Removing the directory if this call created it, or loading with a stat-tolerant path, avoids that.
  • The NOTE says to add host_projects "and re-run bootstrap". Bootstrap generates a new ECDSA key and certificate on every run (ecdsa.GenerateKey above) and overwrites remote.sops.yaml, so re-running means registering a new cert on the host, not widening the existing one. If that is the intended path it is worth saying so in the NOTE; the cheaper path for an already registered cert is probably incus config trust edit/--projects on the host.

Source-read only.


F4 [nit] Tests and docs

  • The remote bootstrap ordering and the NOTE, and doctor --deep, are not covered. project_scope_test.go covers projectsToQuery only for the explicit-project path, and the default path only with a hand-built config.Fleet.
  • An existing host that gets its first instance in a new project now needs a new cert scope, and nothing in validate, diff or doctor says so: the failure is an authorization error from Incus at apply time. A sentence in docs/schema.md under "Host-scoped projects" would help whoever hits it.
  • stringInSlice can be slices.Contains (go.mod is on 1.26), and checkHostProjectsKeys drops a ListHosts error silently.
  • The last Test-plan item (a patch file "produced earlier") is not something a reader can check and can be removed from the description.

PR description verification

  • "diff/sync needed no code change" — confirmed. plan.go:150 iterates fleet.Projects; with narrowing, a project used only on another host is not queried.
  • "Every instance's project: is still validated against the fleet-wide projects: list before narrowing" — confirmed. loadInstanceDir runs before the new steps; the error text still lists the fleet-wide set.
  • "The common single-project fleet needs zero config changes and behaves exactly as before" — not confirmed for list/orphans on a host with no instance files (F1). It holds for diff/sync/validate.
  • "remote bootstrap loads the fleet before writing remote.sops.yaml" — confirmed, with the leftover directory in F3.
  • "Verified this test fails without the fix" — not re-run; the reasoning is sound (fleet.Projects would be the fleet-wide list, so GetInstances("db") would hit the Fatalf).
  • "go build, go vet, go test ./..." — confirmed on ee3e644.

…the host_projects typo check only in validate, and tidy bootstrap's leftovers and NOTE.
…tor running when the fleet fails to load, and check --project before connecting.
@gunnsth

gunnsth commented Nov 18, 2026

Copy link
Copy Markdown
Author

Thanks, all four were fair. Fixed in f56d259 and 3c09910.

F1. list and orphans with no --project now check every project in projects: that the host has, using one GetProjectNames() call. They print which projects they checked and which fleet projects they skipped. orphans no longer prints the all-clear when it checked nothing. --project works as before. It's now checked against the fleet list before connecting, so a typo doesn't ask for a vault unlock first. The trade-off paragraph is gone from the docs and README.

I kept the "checked/skipped" line on purpose. A restricted cert only sees its own projects. On a new host, bootstrap used to scope the cert to the network project only, so orphans would honestly report "checked nothing", but it still couldn't find anything, and import/adopt got a 403. To fix that, remote bootstrap now takes --projects a,b, which is checked against the fleet list and added to the cert scope. The NOTE spells out what is blind until the cert is widened.

F2. Checking only f.Host wouldn't fire, since Load already fails when hosts/<host>/ is missing. So the check moved out of Load into config.CheckHostProjectsKeys, which validate runs once. Nothing shows up on other hosts' diff or sync --apply any more. It now returns the ListHosts error instead of dropping it.

F3. Bootstrap removes hosts/<host>/ again if it created it and a later step fails, as long as it's still empty (os.Remove). A directory that already existed is left alone. Bootstrap now prints the client cert's fingerprint. The NOTE gives incus config trust edit <that fingerprint> for widening the existing cert, and says that re-running makes a new key and cert, so you register it and remove the old trust entry.

F4.

  • stringInSlice is now slices.Contains.
  • docs/schema.md explains how a cert's scope is set and what fails until it covers a project: list/orphans skip it, import/adopt get a 403, sync gets an authorization error. It also says how to widen it.
  • doctor --deep says "MISSING or not visible to this cert".
  • If the fleet doesn't load, doctor --deep now reports it and keeps going with the other checks, instead of stopping.
  • New tests cover:
    • the list/orphans project selection, with a fake server
    • the no-all-clear case
    • doctor's project check
    • bootstrap's cert scope and its cleanup when the load fails
  • To check the new projectsToQuery test, I made the default path behave the old way, and the test failed. The old code itself has a different signature, so the test doesn't compile against it.
  • I removed the patch-file item from the description.

The help text for --project on list, orphans, shell, import and adopt no longer has backticks, which made cobra print --project projects:.

Two things I'd leave for a separate PR, since they're also on master:

  • orphans/list match live containers by bare name, so the same name in two projects can hide an unmanaged one.
  • The import commands orphans prints have no --project, which fails on a multi-project fleet.

go build, go vet, go test -race ./... and gofmt are clean. I ran bootstrap end to end with a stand-in for sops against a one-project and a two-project copy of the example fleet. I haven't run it against an Incus daemon.

… unchecked instances as NOT-CHECKED in list, and test the bootstrap NOTE and doctor project check.
@gunnsth

gunnsth commented Nov 18, 2026

Copy link
Copy Markdown
Author

A second pass, in 0d8060b, and a correction to my description.

Correction. I said the global list broke other hosts' diff/sync. It doesn't. I checked against Incus 7.6 in a lab VM with a restricted cert. Listing instances in a project that doesn't exist, or that the cert can't see, returns an empty list with no error. What did break: trust add-certificate --projects is refused with "Project not found" if any listed project is missing on the host, and master's bootstrap listed every fleet project. doctor --deep also reported those projects as MISSING. I've fixed the description.

That also means orphans --project X could still give a false all-clear when X was out of the cert's scope. It now checks that the host lists X and errors if not.

Other changes:

  • list shows managed instances in an unchecked project as NOT-CHECKED instead of MISSING.
  • docs/schema.md now says:
    • with a narrowed cert, diff shows existing instances as create and sync --apply then fails with a 403;
    • the isolation-policy check only covers the host's own projects, and host_projects keeps another one covered.
  • Bootstrap checks --projects before generating a key, and also removes hosts/ if it created that and failed.
  • The NOTE condition and doctor's project check are separate functions now, with table tests.
  • The remaining help strings (diff, sync, prunecheck) are fixed.

Against the lab Incus with a cert scoped to one project:

  • the default check covered the in-scope project and reported the other as skipped;
  • --project <out-of-scope> errored;
  • list showed NOT-CHECKED;
  • orphans listed the real unmanaged containers.

I called those code paths directly over HTTPS, not through the CLI with a remote.sops.yaml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants