SwarmKit Package Reference
pkg/swarmkit/— Executor, Controller, VMM Manager, and Task Translator.
Overview
Section titled “Overview”The pkg/swarmkit package implements the SwarmKit executor interface, transforming SwarmKit container tasks into Firecracker microVMs. It is the core integration point between SwarmKit’s orchestration and Firecracker’s VM execution.
Package Structure:
pkg/swarmkit/├── executor.go # Executor implementation (SwarmKit interface)├── vmm.go # VMM Manager (Firecracker process management)├── translator.go # Task → Firecracker config translation├── interfaces.go # Interface definitions├── mocks_test.go # Test mocks└── configs/ # SwarmKit configuration helpersExecutor
Section titled “Executor”File: executor.go
The Executor struct implements SwarmKit’s swarmkit_exec.Executor interface, providing the bridge between SwarmKit task scheduling and Firecracker VM execution.
Type Definition
Section titled “Type Definition”type Executor struct { config *Config imagePrep types.ImagePreparer networkMgr types.NetworkManager volumeMgr *storage.VolumeManager secretMgr *storage.SecretManager vmmMgr VMMManagerInterface controllers map[string]*Controller executorMu sync.RWMutex cleanupCancel context.CancelFunc cleanupDone chan struct{} networkKeys []*api.EncryptionKey cleanupMu sync.Mutex}Configuration
Section titled “Configuration”type Config struct { FirecrackerPath string `yaml:"firecracker_path"` KernelPath string `yaml:"kernel_path"` RootfsDir string `yaml:"rootfs_dir"` SocketDir string `yaml:"socket_dir"` DefaultVCPUs int `yaml:"default_vcpus"` DefaultMemoryMB int `yaml:"default_memory_mb"` BridgeName string `yaml:"bridge_name"` Subnet string `yaml:"subnet"` BridgeIP string `yaml:"bridge_ip"` IPMode string `yaml:"ip_mode"` NATEnabled *bool `yaml:"nat_enabled"` VXLANEnabled bool `yaml:"vxlan_enabled"` VXLANPeers []string `yaml:"vxlan_peers"` Debug bool `yaml:"debug"` ReservedCPUs int `yaml:"reserved_cpus"` ReservedMemoryMB int `yaml:"reserved_memory_mb"` MaxImageAgeDays int `yaml:"max_image_age_days"` StateDir string `yaml:"state_dir"` LogDir string `yaml:"log_dir"` GoldenDir string `yaml:"golden_dir"` KernelProfiles map[string]string `yaml:"-"`
// Jailer configuration EnableJailer bool `yaml:"enable_jailer"` JailerPath string `yaml:"jailer_path"` JailerUID int `yaml:"jailer_uid"` JailerGID int `yaml:"jailer_gid"` JailerChrootDir string `yaml:"jailer_chroot_dir"` ParentCgroup string `yaml:"parent_cgroup"` CgroupVersion string `yaml:"cgroup_version"` EnableCgroups bool `yaml:"enable_cgroups"`
// Network identity Hostname string `yaml:"hostname"` JoinAddr string `yaml:"join_addr"` AdvertiseAddr string `yaml:"advertise_addr"`
// Consul service discovery ConsulEnabled bool `yaml:"consul_enabled"` ConsulAddress string `yaml:"consul_address"`}Constructor
Section titled “Constructor”func NewExecutor(config *Config) (*Executor, error)Parameters:
config— Executor configuration (required)
Defaults Applied:
| Field | Default Value |
|---|---|
FirecrackerPath |
"firecracker" |
KernelPath |
"/usr/share/firecracker/vmlinux" |
RootfsDir |
"/var/lib/firecracker/rootfs" |
SocketDir |
"/var/run/firecracker" |
DefaultVCPUs |
1 |
DefaultMemoryMB |
512 |
BridgeName |
"swarm-br0" |
Subnet |
"192.168.127.0/24" |
BridgeIP |
"192.168.127.1/24" |
IPMode |
"static" |
Example:
config := &swarmkit.Config{ KernelPath: "/usr/share/firecracker/vmlinux", RootfsDir: "/var/lib/firecracker/rootfs", DefaultVCPUs: 2, DefaultMemoryMB: 1024, BridgeName: "swarm-br0", ConsulEnabled: true, ConsulAddress: "127.0.0.1:8500",}
exec, err := swarmkit.NewExecutor(config)if err != nil { log.Fatal(err)}SwarmKit Interface Methods
Section titled “SwarmKit Interface Methods”The Executor implements these SwarmKit executor interface methods:
Configure
Section titled “Configure”func (e *Executor) Configure(ctx context.Context, node *api.Node) errorPurpose: Apply SwarmKit node state to the executor.
Parameters:
ctx— Context for cancellationnode— SwarmKit node being configured
Currently a no-op (node state is read directly when needed).
Controller
Section titled “Controller”func (e *Executor) Controller(t *api.Task) (swarmkit_exec.Controller, error)Purpose: Return the controller for a task, creating it on first request.
Parameters:
t— SwarmKit task specification
Returns:
Controller— Task controller for lifecycle management (a cached controller is returned if one already exists)error— Creation error
Describe
Section titled “Describe”func (e *Executor) Describe(ctx context.Context) (*api.NodeDescription, error)Purpose: Report host resources (CPUs, memory, Firecracker/KVM availability) to SwarmKit.
SetNetworkBootstrapKeys
Section titled “SetNetworkBootstrapKeys”func (e *Executor) SetNetworkBootstrapKeys(keys []*api.EncryptionKey) errorPurpose: Store VXLAN overlay encryption keys and apply them to the network manager.
func (e *Executor) Close() errorPurpose: Shutdown executor and cleanup all resources.
Side Effects:
- Stops cleanup goroutine
- Removes all controllers
- Cleanup network infrastructure
Controller
Section titled “Controller”File: executor.go
The Controller manages the lifecycle of a single task/VM.
Type Definition
Section titled “Type Definition”type Controller struct { task *api.Task config *Config imagePrep types.ImagePreparer networkMgr types.NetworkManager volumeMgr *storage.VolumeManager secretMgr *storage.SecretManager vmmMgr VMMManagerInterface trans types.TaskTranslator mu sync.Mutex prepared bool started bool startTime time.Time internalTask *types.Task socketPath string logger zerolog.Logger OnRemove func()}Methods
Section titled “Methods”Prepare
Section titled “Prepare”func (c *Controller) Prepare(ctx context.Context) errorPurpose: Prepare task for execution (image prep, network setup).
Steps:
- Validate task runtime (must be container)
- Prepare rootfs image via
ImagePreparer.Prepare()(or a golden image) - Prepare TAP device and network via
NetworkManager.PrepareNetwork() - Prepare volumes via
VolumeManager - Inject secrets/configs
func (c *Controller) Start(ctx context.Context) errorPurpose: Start the Firecracker VM.
Steps:
- Translate task to Firecracker config
- Start Firecracker process via
VMMManager.Start() - Configure VM (kernel, rootfs, network, machine config)
- Send InstanceStart action
- Wait for VM to reach running state
func (c *Controller) Wait(ctx context.Context) errorPurpose: Wait for task completion.
Returns:
nil— Task completed successfullyerror— Task failed or context canceled
Shutdown
Section titled “Shutdown”func (c *Controller) Shutdown(ctx context.Context) errorPurpose: Gracefully stop the VM.
Steps:
- Send graceful shutdown signal via
VMMManager.Stop() - Clean up the task network
- Mark the task as not started
Terminate
Section titled “Terminate”func (c *Controller) Terminate(ctx context.Context) errorPurpose: Forcefully terminate the VM without a grace period.
Steps:
- Force kill the VM process via
VMMManager.Stop()with the caller’s context - Mark the task as not started
Update
Section titled “Update”func (c *Controller) Update(ctx context.Context, t *api.Task) errorPurpose: Update the task spec before it starts; a no-op for already-started tasks (SwarmKit creates a new task for rolling updates).
Remove
Section titled “Remove”func (c *Controller) Remove(ctx context.Context) errorPurpose: Remove task and cleanup resources.
Cleanup Steps:
- Sync volume data back
- Clean up the task network (
NetworkManager.CleanupNetwork()) - Remove VM (stops if running and removes sockets)
- Remove the rootfs image
- Remove controller from executor
ContainerStatus / PortStatus
Section titled “ContainerStatus / PortStatus”func (c *Controller) ContainerStatus(ctx context.Context) (*api.ContainerStatus, error)func (c *Controller) PortStatus(ctx context.Context) (*api.PortStatus, error)Purpose: Report the VM’s status (task ID, PID, exit code) to SwarmKit.
func (c *Controller) Close() errorPurpose: Close controller without cleanup (for failed tasks).
VMMManager
Section titled “VMMManager”File: vmm.go
The VMMManager manages Firecracker VM processes and API communication.
Type Definition
Section titled “Type Definition”type VMMManager struct { firecrackerPath string jailerPath string socketDir string useJailer bool jailerConfig *jailer.Config jailer *jailer.Jailer cgroupMgr jailer.CgroupOps cgroupErr error processes map[string]*exec.Cmd processWaits map[string]*processWait processMutex sync.Mutex consoles map[string]*console.Server consoleMutex sync.Mutex logFiles map[string]*os.File logMutex sync.Mutex logDir string logger zerolog.Logger}VM States
Section titled “VM States”
pkg/swarmkitdoes not define aVMStatetype; VM state constants live inpkg/lifecycle(see the Lifecycle Reference).
Constructor
Section titled “Constructor”func NewVMMManager(firecrackerPath, socketDir string) (*VMMManager, error)Parameters:
firecrackerPath— path to the Firecracker binarysocketDir— directory for Firecracker API sockets
Returns:
*VMMManager— the managererror— construction error (e.g. binary not found)
NewVMMManagerWithConfig(cfg *VMMManagerConfig) (*VMMManager, error) is the
advanced constructor used when jailer/cgroup options are needed.
Methods
Section titled “Methods”The exported method set is:
Start(ctx context.Context, task *types.Task, config interface{}) errorStop(ctx context.Context, task *types.Task) errorForceStop(ctx context.Context, task *types.Task) errorWait(ctx context.Context, task *types.Task) (*types.TaskStatus, error)GetPID(taskID string) intCheckVMAPIHealth(ctx context.Context, taskID string) boolIsRunning(taskID string) boolRemove(ctx context.Context, task *types.Task) errorDescribe(ctx context.Context, task *types.Task) (*types.TaskStatus, error)GetRunningProcesses() map[string]*exec.CmdRemoveProcess(taskID string)putAPI(ctx, socketPath, path string, data interface{}) error is the internal
helper that performs the PUT calls against the Firecracker API socket.
func (v *VMMManager) Start(ctx context.Context, task *types.Task, config interface{}) errorPurpose: Start a Firecracker VM for the task.
Steps:
- Find Firecracker binary
- Start process with API socket
- Wait for API server ready (10s timeout)
- Configure VM via HTTP API
- Send InstanceStart action
- Track VM process
func (v *VMMManager) Stop(ctx context.Context, task *types.Task) errorPurpose: Stop a running VM gracefully (SIGTERM, then SIGKILL after 10s).
ForceStop
Section titled “ForceStop”func (v *VMMManager) ForceStop(ctx context.Context, task *types.Task) errorPurpose: Kill the VM process immediately without a grace period.
func (v *VMMManager) Wait(ctx context.Context, task *types.Task) (*types.TaskStatus, error)Purpose: Wait for the VM process to exit and return its task status.
GetPID
Section titled “GetPID”func (v *VMMManager) GetPID(taskID string) intPurpose: Return the Firecracker process PID for a task (0 if not running).
CheckVMAPIHealth
Section titled “CheckVMAPIHealth”func (v *VMMManager) CheckVMAPIHealth(ctx context.Context, taskID string) boolPurpose: Query the VM’s API socket to check liveness.
IsRunning
Section titled “IsRunning”func (v *VMMManager) IsRunning(taskID string) boolPurpose: Report whether the VM process is alive (and not a zombie).
Remove
Section titled “Remove”func (v *VMMManager) Remove(ctx context.Context, task *types.Task) errorPurpose: Stop the VM if running and remove its socket and tracking state.
Describe
Section titled “Describe”func (v *VMMManager) Describe(ctx context.Context, task *types.Task) (*types.TaskStatus, error)Purpose: Return the current status of the VM.
GetRunningProcesses
Section titled “GetRunningProcesses”func (v *VMMManager) GetRunningProcesses() map[string]*exec.CmdPurpose: Return a copy of the task-ID → process map.
RemoveProcess
Section titled “RemoveProcess”func (v *VMMManager) RemoveProcess(taskID string)Purpose: Drop a task from process tracking and close its console.
Firecracker API Types
Section titled “Firecracker API Types”type BootSource struct { KernelImagePath string `json:"kernel_image_path"` BootArgs string `json:"boot_args,omitempty"`}
type Drive struct { DriveID string `json:"drive_id"` IsRootDevice bool `json:"is_root_device"` IsReadOnly bool `json:"is_read_only"` PathOnHost string `json:"path_on_host"`}
type MachineConfig struct { VcpuCount int `json:"vcpu_count"` MemSizeMib int `json:"mem_size_mib"` HtEnabled bool `json:"ht_enabled"` TrackDirtyPages bool `json:"track_dirty_pages,omitempty"`}
type NetworkInterface struct { IfaceID string `json:"iface_id"` GuestMac string `json:"guest_mac,omitempty"` HostDevName string `json:"host_dev_name,omitempty"`}
type Action struct { ActionType string `json:"action_type"` TimeoutMS int `json:"timeout_ms,omitempty"`}Translator
Section titled “Translator”File: translator.go
The Translator converts SwarmKit task specifications into Firecracker VM configurations.
Type Definition
Section titled “Type Definition”The implementation type is unexported; callers use the types.TaskTranslator
interface returned by the constructors.
type taskTranslatorImpl struct { // kernelPath, bridgeIP, kernelProfiles (private)}Constructor
Section titled “Constructor”func NewTaskTranslator(kernelPath, bridgeIP string) (types.TaskTranslator, error)func NewTaskTranslatorWithProfiles(kernelPath, bridgeIP string, kernelProfiles map[string]string) (types.TaskTranslator, error)Methods
Section titled “Methods”Translate
Section titled “Translate”func (t *taskTranslatorImpl) Translate(task *types.Task) (interface{}, error)Purpose: Convert a SwarmKit task to a Firecracker VM config (a
map[string]interface{} consumed by the VMM manager).
Translation Steps:
- Extract container spec from task
- Determine vCPUs and memory from task resources or defaults
- Build kernel boot args
- Configure drives (rootfs path)
- Setup network config
- Apply init system settings
Example Output:
map[string]interface{}{ "boot-source": map[string]interface{}{ "kernel_image_path": "/usr/share/firecracker/vmlinux", "boot_args": "console=ttyS0 reboot=k panic=1 pci=off ip=dhcp", }, "drives": []map[string]interface{}{ { "drive_id": "task-abc123", "path_on_host": "/var/lib/firecracker/rootfs/<id>.ext4", "is_root_device": true, "is_read_only": false, }, }, "machine-config": map[string]interface{}{ "vcpu_count": 2, "mem_size_mib": 1024, "smt": false, }, "network-interfaces": []map[string]interface{}{ /* … */ },}Interfaces
Section titled “Interfaces”File: interfaces.go
VMMManagerInterface
Section titled “VMMManagerInterface”type VMMManagerInterface interface { Start(ctx context.Context, task *types.Task, config interface{}) error Stop(ctx context.Context, task *types.Task) error ForceStop(ctx context.Context, task *types.Task) error Wait(ctx context.Context, task *types.Task) (*types.TaskStatus, error) Remove(ctx context.Context, task *types.Task) error GetPID(taskID string) int CheckVMAPIHealth(ctx context.Context, taskID string) bool IsRunning(taskID string) bool GetRunningProcesses() map[string]*exec.Cmd RemoveProcess(taskID string)}Consul Integration
Section titled “Consul Integration”When ConsulEnabled is true, the executor:
- Registers service in Consul catalog
- Starts peer watcher via
WatchPeers() - Updates VXLAN FDB on peer changes
// Consul client creationconsulClient, err := discovery.NewConsulClient(discovery.ConsulConfig{ Address: config.ConsulAddress, ServiceID: config.Hostname, LocalIP: localIP, LocalHostname: config.Hostname, VXLANPort: 4789,})
// Register and watchconsulClient.RegisterService(vxlanID, config.BridgeIP)networkMgr.SetNodeDiscovery(consulClient)
go consulClient.WatchPeers(ctx, func(peers []string) { networkMgr.UpdateVXLANPeers(peers)})Error Handling
Section titled “Error Handling”Common Errors
Section titled “Common Errors”| Error | Cause | Resolution |
|---|---|---|
"config cannot be nil" |
Nil config passed | Provide valid Config struct |
"unsupported runtime type" |
Task not container | Use container runtime tasks |
"VM already exists for task" |
Duplicate task ID | Wait for cleanup or force remove |
"firecracker binary not found" |
Firecracker not installed | Install Firecracker v1.15+ |
"firecracker API server not ready" |
Socket timeout | Check Firecracker process |
Testing
Section titled “Testing”The package includes comprehensive test coverage with mocks:
// Create mock executor for testingmockVMM := &MockVMMManager{}mockNetwork := &MockNetworkManager{}mockPreparer := &MockImagePreparer{}
exec := &Executor{ vmmMgr: mockVMM, networkMgr: mockNetwork, imagePrep: mockPreparer, controllers: make(map[string]*Controller),}Related Documentation
Section titled “Related Documentation”| Topic | Document |
|---|---|
| Network internals | Network Reference |
| VM lifecycle | Lifecycle Reference |
| Image preparation | Image Reference |
| Architecture overview | Architecture Overview |
See Also: SwarmKit Integration Guide | SwarmKit User Guide