Repository navigation
Conversation
… 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
left a comment
There was a problem hiding this comment.
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:
orphansthen reports a false all-clear.cmd_orphans.go:100printsNo 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.Projectsis empty, nothing is queried, and that is what you read. That is the hostorphansexists for: it is the discovery step beforeimport(the description itself saysadopt/importrun 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.- "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 newscanned the one project; now it scans nothing and says everything is fine.listshows 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 beforeconfig.Load. IfLoadfails for an unrelated reason, no secret is written (good, as described), but an emptyhosts/<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.GenerateKeyabove) and overwritesremote.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 probablyincus config trust edit/--projectson the host.
Source-read only.
F4 [nit] Tests and docs
- The
remote bootstrapordering and the NOTE, anddoctor --deep, are not covered.project_scope_test.gocoversprojectsToQueryonly for the explicit-project path, and the default path only with a hand-builtconfig.Fleet. - An existing host that gets its first instance in a new project now needs a new cert scope, and nothing in
validate,diffordoctorsays so: the failure is an authorization error from Incus at apply time. A sentence indocs/schema.mdunder "Host-scoped projects" would help whoever hits it. stringInSlicecan beslices.Contains(go.mod is on 1.26), andcheckHostProjectsKeysdrops aListHostserror 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/syncneeded no code change" — confirmed.plan.go:150iteratesfleet.Projects; with narrowing, a project used only on another host is not queried. - "Every instance's
project:is still validated against the fleet-wideprojects:list before narrowing" — confirmed.loadInstanceDirruns 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/orphanson a host with no instance files (F1). It holds fordiff/sync/validate. - "
remote bootstraploads the fleet before writingremote.sops.yaml" — confirmed, with the leftover directory in F3. - "Verified this test fails without the fix" — not re-run; the reasoning is sound (
fleet.Projectswould be the fleet-wide list, soGetInstances("db")would hit the Fatalf). - "
go build,go vet,go test ./..." — confirmed onee3e644.
…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.
|
Thanks, all four were fair. Fixed in f56d259 and 3c09910. F1. 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 F2. Checking only F3. Bootstrap removes F4.
The help text for Two things I'd leave for a separate PR, since they're also on master:
|
… unchecked instances as NOT-CHECKED in list, and test the bootstrap NOTE and doctor project check.
|
A second pass, in 0d8060b, and a correction to my description. Correction. I said the global list broke other hosts' That also means Other changes:
Against the lab Incus with a cert scoped to one project:
I called those code paths directly over HTTPS, not through the CLI with a |
Summary
fleet.yaml'sprojects:list was global:ComputePlanqueried/managedinstances 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
dbproject used only onhost-b) meant every other host was treated as needing it too:remote bootstrapprinted atrust add-certificate --projectslistnaming it, which Incus refuses ("Project not found") on a host that
doesn't have it, and
doctor --deepreported it as MISSING. The onlyway round it was to create every project on every host.
(Correction: an earlier version of this description said
diff/syncbroke. 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.Projectsper host instead of leaving it fleet-wide.Design
Fleet.Projects(already a per-host object, sinceconfig.Load(fleetPath, host)takes a host) now holds the set this host needs: the projects itsown instances declare, unioned with an optional
host_projects:map infleet.yamlfor pre-declaring a project before any instance on that hostuses it yet (e.g. ahead of
remote bootstrap/doctor --deepon abrand-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:
(
hosts/<host>/instances/*/instance.yaml'sproject:field), soexisting fleets need no config changes.
host_projectsis the minimal escape hatch for the one case derivationcan't cover: a project that needs to exist on a host before its first
instance file is committed.
project:is still validated against the fleet-wideprojects:list before narrowing, so the existing "every project isdeclared somewhere, explicitly" guarantee is unchanged.
list/orphansare read-only and deliberately not narrowed: with no--projectthey check every fleet-wide project the host has (oneGetProjectNames()call) and print which projects they checked and whichthey skipped, so containers with no instance file yet still show,
as long as the host's cert covers their project.
orphansdoes not printan all-clear when it checked nothing, and
listshows managed instancesin an unchecked project as
NOT-CHECKED, notMISSING.--project Xchecks exactly
X, and fails if the host doesn't list it (Incus wouldotherwise return an empty list, and
orphanswould report anall-clear). A typo'd
--projectfails before connecting.Since a host's
diff/synconly look in its own projects, theisolation-policy check (e.g. a privileged container blocking
sync --apply) only covers those too;host_projectskeeps anotherproject 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-levelprojects:list.internal/config/loader.go—Fleet.Projectsnarrowed to the host'seffective set after instances load (
narrowProjectsToHost); a newFleet.CertProjects()(host-scoped, replaces the old fleet-wideFleetMeta.CertProjects);CheckHostProjectsKeys, whichvalidateruns once to warn when
host_projectsnames a host with nohosts/<host>/directory (a typo there would otherwise be a silentno-op). It is not run inside
Load, so it never shows up as a riskflag on other hosts'
diff/sync.internal/model/instance.go— corrected a staleproject:doc comment(claimed a default-project fallback the loader has never had).
cmd/incus-sync/cmd_doctor.go—doctor --deepchecks projectexistence 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 bootstrapscopes the printedcert
--projectsto the host's effective set, plus any projects passedwith the new
--projectsflag (validated against the fleet list), so anew host's cert can cover the projects
orphans/importneed. It printsthe 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, whichmakes 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 anunrelated fleet validation error fails before any secret file is
written, and removes
hosts/<host>/again if it created it and a laterstep 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 Xqueries exactlyX,validated against the fleet-wide
projects:list.cmd/incus-sync/cmd_validate.go— runs thehost_projectskey checkonce per run.
diff/syncneeded no code change — they already iteratefleet.Projects, so narrowing it in the loader fixes them for free.docs/schema.md,README.md— documenthost_projects:, theper-host scoping rule, the wider
list/orphansdefault, and what todo when a host gets its first instance in a project its cert was not
scoped for.
internal/config/host_projects_test.go(narrowing, thehost_projectsescape hatch, fleet-wide allowlist still enforced,typo warning, host-scoped
CertProjects),internal/incus/scope_test.go(end-to-end:
config.Loada two-host fixture, then assertComputePlannever callsGetInstancesfor the other host's project —verified this test fails without the fix), and
cmd/incus-sync/project_scope_test.go(list/orphansprojectselection with a fake server, the
orphansno-all-clear case, doctor'sproject check, bootstrap's cert scope, and bootstrap's directory cleanup
on a failed load).
Considered but skipped: having
resolveProject(used byshell/adopt/import, single-project commands) auto-pick a host's sole narrowedproject instead of requiring
--project. Skipped becauseadopt/importexist 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)internal/incus/scope_test.go's regression test failswithout
narrowProjectsToHost(temporarily reverted it, re-ran,restored)
fleet repo (offline, in a separate git worktree — nothing committed
there):
validate --host <host-with-project-A>— OK (instances all inproject A)
validate --host <host-with-project-B>— OK (instances all inproject B)
render --host <host-with-project-B> --kind all— no mention ofproject A
Load(dir, host-A).Projects == [A],Load(dir, host-B).Projects == [B], eachCertProjects()correctlyexcludes the other host's project
go test -race ./...projectsToQuerydefault path return the old narrowed(empty) set and confirmed the new test fails
remote bootstrapend to end with a stand-in forsopsagainstone- and two-project copies of
examples/minimal-fleet: the NOTEshows on a new host when the cert leaves out a fleet project (not
when
defaultis the only project),--projectswidens the scope,a typo'd
--projectsfails before anything is generatedscoped to one project (removed afterwards):
an empty list, no error; fetching one instance there returns 403
trust add-certificate --projectswith a nonexistent project failswith "Project not found"
projectsToQuerychecks the in-scope project and reports the otheras skipped;
--project <out-of-scope>errorslistshows the instance in the unchecked project asNOT-CHECKED;orphanslists the real unmanaged containersremote.sops.yaml; thecode paths above were called directly with an HTTPS connection
Left for a follow-up (also on master):
orphans/listmatch livecontainers by bare name across projects, and the
importcommandsorphansprints have no--project.