daemon split (1/5): extract *HostNetwork service
First phase of splitting the daemon god-struct into focused services with explicit ownership. HostNetwork now owns everything host-networking: the TAP interface pool (initializeTapPool / ensureTapPool / acquireTap / releaseTap / createTap), bridge + socket dir setup, firecracker process primitives (find/resolve/kill/wait/ensureSocketAccess/sendCtrlAltDel), DM snapshot lifecycle, NAT rule enforcement, guest DNS server lifecycle + routing setup, and the vsock-agent readiness probe. That's 7 files whose receivers flipped from *Daemon to *HostNetwork, plus a new host_network.go that declares the struct, its hostNetworkDeps, and the factored firecracker + DNS helpers that used to live in vm.go. Daemon gives up the tapPool and vmDNS fields entirely; they're now HostNetwork's business. Construction goes through newHostNetwork in Daemon.Open with an explicit dependency bag (runner, logger, config, layout, closing). A lazy-init hostNet() helper on Daemon supports test literals that don't wire net explicitly — production always populates it eagerly. Signature tightenings where the old receiver reached into VM-service state: - ensureNAT(ctx, vm, enable) → ensureNAT(ctx, guestIP, tap, enable). Callers resolve tap from the handle cache themselves. - initializeTapPool(ctx) → initializeTapPool(usedTaps []string). Daemon.Open enumerates VMs, collects taps from handles, hands the slice in. rebuildDNS stays on *Daemon as the orchestrator — it filters by vm-alive (a VMService concern handles will move to in phase 4) then calls HostNetwork.replaceDNS with the already-filtered map. Capability hooks continue to take *Daemon; they now use it as a facade to reach services (d.net.ensureNAT, d.hostNet().*). Planned CapabilityHost interface extraction is orthogonal, left for later. Tests: dns_routing_test.go + fastpath_test.go + nat_test.go + snapshot_test.go + open_close_test.go were touched to construct HostNetwork literals where they exercise its methods directly, or route through d.hostNet() where they exercise the Daemon entry points. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
eba9a553bf
commit
362009d747
18 changed files with 461 additions and 326 deletions
|
|
@ -15,49 +15,49 @@ var (
|
|||
vmDNSAddrFunc = func(server *vmdns.Server) string { return server.Addr() }
|
||||
)
|
||||
|
||||
func (d *Daemon) syncVMDNSResolverRouting(ctx context.Context) error {
|
||||
if d == nil || d.vmDNS == nil {
|
||||
func (n *HostNetwork) syncVMDNSResolverRouting(ctx context.Context) error {
|
||||
if n == nil || n.vmDNS == nil {
|
||||
return nil
|
||||
}
|
||||
if strings.TrimSpace(d.config.BridgeName) == "" {
|
||||
if strings.TrimSpace(n.config.BridgeName) == "" {
|
||||
return nil
|
||||
}
|
||||
if _, err := lookupExecutableFunc("resolvectl"); err != nil {
|
||||
return nil
|
||||
}
|
||||
if _, err := d.runner.Run(ctx, "ip", "link", "show", d.config.BridgeName); err != nil {
|
||||
if _, err := n.runner.Run(ctx, "ip", "link", "show", n.config.BridgeName); err != nil {
|
||||
return nil
|
||||
}
|
||||
serverAddr := strings.TrimSpace(vmDNSAddrFunc(d.vmDNS))
|
||||
serverAddr := strings.TrimSpace(vmDNSAddrFunc(n.vmDNS))
|
||||
if serverAddr == "" {
|
||||
return nil
|
||||
}
|
||||
if _, err := d.runner.RunSudo(ctx, "resolvectl", "dns", d.config.BridgeName, serverAddr); err != nil {
|
||||
if _, err := n.runner.RunSudo(ctx, "resolvectl", "dns", n.config.BridgeName, serverAddr); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := d.runner.RunSudo(ctx, "resolvectl", "domain", d.config.BridgeName, vmResolverRouteDomain); err != nil {
|
||||
if _, err := n.runner.RunSudo(ctx, "resolvectl", "domain", n.config.BridgeName, vmResolverRouteDomain); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := d.runner.RunSudo(ctx, "resolvectl", "default-route", d.config.BridgeName, "no")
|
||||
_, err := n.runner.RunSudo(ctx, "resolvectl", "default-route", n.config.BridgeName, "no")
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Daemon) clearVMDNSResolverRouting(ctx context.Context) error {
|
||||
if d == nil || strings.TrimSpace(d.config.BridgeName) == "" {
|
||||
func (n *HostNetwork) clearVMDNSResolverRouting(ctx context.Context) error {
|
||||
if n == nil || strings.TrimSpace(n.config.BridgeName) == "" {
|
||||
return nil
|
||||
}
|
||||
if _, err := lookupExecutableFunc("resolvectl"); err != nil {
|
||||
return nil
|
||||
}
|
||||
if _, err := d.runner.Run(ctx, "ip", "link", "show", d.config.BridgeName); err != nil {
|
||||
if _, err := n.runner.Run(ctx, "ip", "link", "show", n.config.BridgeName); err != nil {
|
||||
return nil
|
||||
}
|
||||
_, err := d.runner.RunSudo(ctx, "resolvectl", "revert", d.config.BridgeName)
|
||||
_, err := n.runner.RunSudo(ctx, "resolvectl", "revert", n.config.BridgeName)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Daemon) ensureVMDNSResolverRouting(ctx context.Context) {
|
||||
if err := d.syncVMDNSResolverRouting(ctx); err != nil && d.logger != nil {
|
||||
d.logger.Warn("vm dns resolver route sync failed", "bridge", d.config.BridgeName, "error", err.Error())
|
||||
func (n *HostNetwork) ensureVMDNSResolverRouting(ctx context.Context) {
|
||||
if err := n.syncVMDNSResolverRouting(ctx); err != nil && n.logger != nil {
|
||||
n.logger.Warn("vm dns resolver route sync failed", "bridge", n.config.BridgeName, "error", err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue