daemon split (7/n): narrow capability interfaces, wire deps at construction

Stop passing *Daemon into capability hooks. Each capability
implementation is now a struct with explicit service-pointer fields
populated at wireServices time; the six dynamic-dispatch interfaces
(AddStartPreflight, PrepareHost, PostStart, Cleanup, ApplyConfigChange,
AddDoctorChecks) no longer have a *Daemon parameter. Capability
methods reach their dependencies through struct fields, not through
d.vm / d.ws / d.net.

- workDiskCapability carries {vm, ws, store, defaultImageName}
- dnsCapability carries {net}
- natCapability carries {vm, net, logger}

Daemon.defaultCapabilities() builds the production list from the
already-constructed services and is called from wireServices so
d.vmCaps is populated eagerly. Tests that preinstall d.vmCaps with
stubs still work — wireServices only overwrites an empty slice.

registeredCapabilities() is gone (every dispatch loop now reads
d.vmCaps directly). capabilities_test.go's testCapability fake drops
*Daemon from its method set to match the new interfaces.

This finishes the daemon service split: capability implementations
no longer reach through the composition root, there's no path back
to *Daemon from any service or capability, and test construction
goes through one explicit wireServices call instead of lazy getters.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Thales Maciel 2026-04-21 15:59:09 -03:00
parent 16702bd5e1
commit 9c73155e17
No known key found for this signature in database
GPG key ID: 33112E6833C34679
4 changed files with 143 additions and 90 deletions

View file

@ -14,27 +14,27 @@ import (
type testCapability struct {
name string
prepare func(context.Context, *Daemon, *model.VMRecord, model.Image) error
cleanup func(context.Context, *Daemon, model.VMRecord) error
prepare func(context.Context, *model.VMRecord, model.Image) error
cleanup func(context.Context, model.VMRecord) error
contribute func(*guestconfig.Builder, model.VMRecord, model.Image)
contributeFC func(*firecracker.MachineConfig, model.VMRecord, model.Image)
configChange func(context.Context, *Daemon, model.VMRecord, model.VMRecord) error
doctor func(context.Context, *Daemon, *system.Report)
startPreflight func(context.Context, *Daemon, *system.Preflight, model.VMRecord, model.Image)
configChange func(context.Context, model.VMRecord, model.VMRecord) error
doctor func(context.Context, *system.Report)
startPreflight func(context.Context, *system.Preflight, model.VMRecord, model.Image)
}
func (c testCapability) Name() string { return c.name }
func (c testCapability) PrepareHost(ctx context.Context, d *Daemon, vm *model.VMRecord, image model.Image) error {
func (c testCapability) PrepareHost(ctx context.Context, vm *model.VMRecord, image model.Image) error {
if c.prepare != nil {
return c.prepare(ctx, d, vm, image)
return c.prepare(ctx, vm, image)
}
return nil
}
func (c testCapability) Cleanup(ctx context.Context, d *Daemon, vm model.VMRecord) error {
func (c testCapability) Cleanup(ctx context.Context, vm model.VMRecord) error {
if c.cleanup != nil {
return c.cleanup(ctx, d, vm)
return c.cleanup(ctx, vm)
}
return nil
}
@ -51,22 +51,22 @@ func (c testCapability) ContributeMachine(cfg *firecracker.MachineConfig, vm mod
}
}
func (c testCapability) ApplyConfigChange(ctx context.Context, d *Daemon, before, after model.VMRecord) error {
func (c testCapability) ApplyConfigChange(ctx context.Context, before, after model.VMRecord) error {
if c.configChange != nil {
return c.configChange(ctx, d, before, after)
return c.configChange(ctx, before, after)
}
return nil
}
func (c testCapability) AddDoctorChecks(ctx context.Context, d *Daemon, report *system.Report) {
func (c testCapability) AddDoctorChecks(ctx context.Context, report *system.Report) {
if c.doctor != nil {
c.doctor(ctx, d, report)
c.doctor(ctx, report)
}
}
func (c testCapability) AddStartPreflight(ctx context.Context, d *Daemon, checks *system.Preflight, vm model.VMRecord, image model.Image) {
func (c testCapability) AddStartPreflight(ctx context.Context, checks *system.Preflight, vm model.VMRecord, image model.Image) {
if c.startPreflight != nil {
c.startPreflight(ctx, d, checks, vm, image)
c.startPreflight(ctx, checks, vm, image)
}
}
@ -78,27 +78,27 @@ func TestPrepareCapabilityHostsRollsBackPreparedCapabilitiesInReverseOrder(t *te
vmCaps: []vmCapability{
testCapability{
name: "first",
prepare: func(context.Context, *Daemon, *model.VMRecord, model.Image) error {
prepare: func(context.Context, *model.VMRecord, model.Image) error {
return nil
},
cleanup: func(context.Context, *Daemon, model.VMRecord) error {
cleanup: func(context.Context, model.VMRecord) error {
cleanupOrder = append(cleanupOrder, "first")
return nil
},
},
testCapability{
name: "second",
prepare: func(context.Context, *Daemon, *model.VMRecord, model.Image) error {
prepare: func(context.Context, *model.VMRecord, model.Image) error {
return nil
},
cleanup: func(context.Context, *Daemon, model.VMRecord) error {
cleanup: func(context.Context, model.VMRecord) error {
cleanupOrder = append(cleanupOrder, "second")
return nil
},
},
testCapability{
name: "broken",
prepare: func(context.Context, *Daemon, *model.VMRecord, model.Image) error {
prepare: func(context.Context, *model.VMRecord, model.Image) error {
return errors.New("boom")
},
},
@ -146,11 +146,11 @@ func TestContributeHooksPopulateGuestAndMachineConfig(t *testing.T) {
}
}
func TestRegisteredCapabilitiesInOrder(t *testing.T) {
func TestDefaultCapabilitiesInOrder(t *testing.T) {
d := &Daemon{}
wireServices(d)
var names []string
for _, capability := range d.registeredCapabilities() {
for _, capability := range d.vmCaps {
names = append(names, capability.Name())
}
want := []string{"work-disk", "dns", "nat"}