From caeb9dfaa79e5c635ac3070edc749dcc62e9bd9e Mon Sep 17 00:00:00 2001 From: Harivansh Rathi Date: Tue, 7 Apr 2026 15:49:26 -0400 Subject: [PATCH] chore: clean types and align --- internal/firecracker/network.go | 35 ++++++++++++++++----------------- internal/firecracker/spec.go | 32 +++++++++++++++--------------- internal/firecracker/state.go | 24 +++++++++++----------- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/internal/firecracker/network.go b/internal/firecracker/network.go index d6f3f32..5af94ea 100644 --- a/internal/firecracker/network.go +++ b/internal/firecracker/network.go @@ -26,18 +26,28 @@ type NetworkAllocation struct { GuestMAC string } -// GuestIP returns the guest IP address. -func (n NetworkAllocation) GuestIP() netip.Addr { - return n.GuestCIDR.Addr() -} - // NetworkAllocator allocates /30 tap networks to machines. type NetworkAllocator struct { basePrefix netip.Prefix } -// NewNetworkAllocator returns a new /30 allocator rooted at the provided IPv4 -// prefix. +// NetworkProvisioner prepares the host-side tap device for a machine. +type NetworkProvisioner interface { + Ensure(context.Context, NetworkAllocation) error + Remove(context.Context, NetworkAllocation) error +} + +// IPTapProvisioner provisions tap devices through the `ip` CLI. +type IPTapProvisioner struct { + runCommand func(context.Context, string, ...string) error +} + +// GuestIP returns the guest IP address. +func (n NetworkAllocation) GuestIP() netip.Addr { + return n.GuestCIDR.Addr() +} + +// NewNetworkAllocator returns a new /30 allocator rooted at the provided IPv4 prefix. func NewNetworkAllocator(cidr string) (*NetworkAllocator, error) { cidr = strings.TrimSpace(cidr) if cidr == "" { @@ -107,17 +117,6 @@ func (a *NetworkAllocator) networkForIndex(index int) (NetworkAllocation, error) }, nil } -// NetworkProvisioner prepares the host-side tap device for a machine. -type NetworkProvisioner interface { - Ensure(context.Context, NetworkAllocation) error - Remove(context.Context, NetworkAllocation) error -} - -// IPTapProvisioner provisions tap devices through the `ip` CLI. -type IPTapProvisioner struct { - runCommand func(context.Context, string, ...string) error -} - // NewIPTapProvisioner returns a provisioner backed by `ip`. func NewIPTapProvisioner() *IPTapProvisioner { return &IPTapProvisioner{ diff --git a/internal/firecracker/spec.go b/internal/firecracker/spec.go index 1e4237c..ba57552 100644 --- a/internal/firecracker/spec.go +++ b/internal/firecracker/spec.go @@ -6,7 +6,7 @@ import ( "strings" ) -// MachineID uniquely identifies a single microVM on a host. +// MachineID uniquely identifies a single firecracler microVM type MachineID string // MachineSpec describes the minimum machine inputs required to boot a guest. @@ -21,6 +21,20 @@ type MachineSpec struct { Vsock *VsockSpec } +// DriveSpec describes an additional guest block device. +type DriveSpec struct { + ID string + Path string + ReadOnly bool +} + +// VsockSpec describes a single host-guest vsock device. +type VsockSpec struct { + ID string + CID uint32 + Path string +} + // Validate reports whether the machine specification is usable for boot. func (s MachineSpec) Validate() error { if strings.TrimSpace(string(s.ID)) == "" { @@ -54,13 +68,6 @@ func (s MachineSpec) Validate() error { return nil } -// DriveSpec describes an additional guest block device. -type DriveSpec struct { - ID string - Path string - ReadOnly bool -} - // Validate reports whether the drive specification is usable. func (d DriveSpec) Validate() error { if strings.TrimSpace(d.ID) == "" { @@ -72,20 +79,13 @@ func (d DriveSpec) Validate() error { return nil } -// VsockSpec describes a single host-guest vsock device. -type VsockSpec struct { - ID string - CID uint32 - Path string -} - // Validate reports whether the vsock specification is usable. func (v VsockSpec) Validate() error { if strings.TrimSpace(v.ID) == "" { return fmt.Errorf("vsock id is required") } if v.CID == 0 { - return fmt.Errorf("vsock cid must be non-zero") + return fmt.Errorf("vsock cid must be non zero") } if strings.TrimSpace(v.Path) == "" { return fmt.Errorf("vsock path is required") diff --git a/internal/firecracker/state.go b/internal/firecracker/state.go index d9280b1..33dbf55 100644 --- a/internal/firecracker/state.go +++ b/internal/firecracker/state.go @@ -5,6 +5,18 @@ import "time" // Phase represents the lifecycle phase of a local microVM. type Phase string +// MachineState describes the current host local state for a machine. +type MachineState struct { + ID MachineID + Phase Phase + PID int + RuntimeHost string + SocketPath string + TapName string + StartedAt *time.Time + Error string +} + const ( // PhaseProvisioning means host-local resources are still being prepared. PhaseProvisioning Phase = "provisioning" @@ -17,15 +29,3 @@ const ( // PhaseError means the runtime observed a terminal failure. PhaseError Phase = "error" ) - -// MachineState describes the current host-local state for a machine. -type MachineState struct { - ID MachineID - Phase Phase - PID int - RuntimeHost string - SocketPath string - TapName string - StartedAt *time.Time - Error string -}