Skip to content

Commit 4d64bf8

Browse files
committed
Merge remote-tracking branch 'origin/pr/36'
2 parents b912042 + d2a99cd commit 4d64bf8

File tree

5 files changed

+133
-51
lines changed

5 files changed

+133
-51
lines changed

command_builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ var defaultFirecrackerVMMCommandBuilder = VMCommandBuilder{}.
2626
WithStdout(os.Stdout).
2727
WithStderr(os.Stderr)
2828

29-
// VMCommandBuilder is a command builder that builds a command specific
30-
// to the firecracker command using exec.Command.
29+
// VMCommandBuilder is a utility for building an exec.Cmd that represents how to
30+
// start the Firecracker VMM.
3131
type VMCommandBuilder struct {
3232
bin string
3333
args []string

doc.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
/*
15+
Package firecracker provides a library to interact with the Firecracker API.
16+
17+
Firecracker is an open-source virtualization technology that is purpose-built
18+
for creating and managing secure, multi-tenant containers and functions-based
19+
services. See https://firecracker-microvm.github.io/ for more details.
20+
21+
This library requires Go 1.11 and can be used with Go modules.
22+
23+
BUG(aws): There are some Firecracker features that are not yet supported by the
24+
SDK. These are tracked as GitHub issues with the firecracker-feature label:
25+
https://github.com/firecracker-microvm/firecracker-go-sdk/issues?q=is%3Aissue+is%3Aopen+label%3Afirecracker-feature
26+
27+
This library is licensed under the Apache 2.0 License.
28+
*/
29+
package firecracker

firecracker.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"net/http"
2020
"time"
2121

22-
"github.com/sirupsen/logrus"
2322
"github.com/go-openapi/strfmt"
23+
"github.com/sirupsen/logrus"
2424

2525
"github.com/firecracker-microvm/firecracker-go-sdk/client"
2626
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
@@ -30,10 +30,12 @@ import (
3030

3131
const firecrackerRequestTimeout = 500 * time.Millisecond
3232

33+
// FirecrackerClient is a client for interacting with the Firecracker API
3334
type FirecrackerClient struct {
3435
client *client.Firecracker
3536
}
3637

38+
// NewFirecrackerClient creates a FirecrackerClient
3739
func NewFirecrackerClient(socketPath string, logger *logrus.Entry, debug bool) *FirecrackerClient {
3840
httpClient := client.NewHTTPClient(strfmt.NewFormats())
3941

machine.go

Lines changed: 91 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ const (
4242
CPUTemplateC3 = models.CPUTemplateC3
4343
)
4444

45-
// Firecracker is an interface that can be used to mock
46-
// out an Firecracker agent for testing purposes.
45+
// Firecracker is an interface that can be used to mock out a Firecracker agent
46+
// for testing purposes.
4747
type Firecracker interface {
4848
PutLogger(ctx context.Context, logger *models.Logger) (*ops.PutLoggerNoContent, error)
4949
PutMachineConfiguration(ctx context.Context, cfg *models.MachineConfiguration) (*ops.PutMachineConfigurationNoContent, error)
@@ -58,26 +58,70 @@ type Firecracker interface {
5858

5959
// Config is a collection of user-configurable VMM settings
6060
type Config struct {
61+
// SocketPath defines the file path where the Firecracker control socket
62+
// should be created.
6163
SocketPath string
6264

63-
LogFifo string
64-
LogLevel string
65-
MetricsFifo string
66-
KernelImagePath string
67-
KernelArgs string
68-
CPUCount int64
69-
HtEnabled bool
70-
CPUTemplate CPUTemplate
71-
MemInMiB int64
72-
RootDrive BlockDevice
65+
// LogFifo defines the file path where the Firecracker log named-pipe should
66+
// be located.
67+
LogFifo string
68+
69+
// LogLevel defines the verbosity of Firecracker logging. Valid values are
70+
// "Error", "Warning", "Info", and "Debug", and are case-sensitive.
71+
LogLevel string
72+
73+
// MetricsFifo defines the file path where the Firecracker metrics
74+
// named-pipe should be located.
75+
MetricsFifo string
76+
77+
// KernelImagePath defines the file path where the kernel image is located.
78+
// The kernel image must be an uncompressed ELF image.
79+
KernelImagePath string
80+
81+
// KernelArgs defines the command-line arguments that should be passed to
82+
// the kernel.
83+
KernelArgs string
84+
85+
// CPUCount defines the number of CPU threads that should be available to
86+
// the micro-VM.
87+
CPUCount int64
88+
89+
// HtEnabled defines whether hyper-threading should be enabled for the
90+
// microVM.
91+
HtEnabled bool
92+
93+
// CPUTemplate defines the Firecracker CPU template to use. Valid values
94+
// are CPUTemplateT2 and CPUTemplateC3,
95+
CPUTemplate CPUTemplate
96+
97+
// MemInMiB defines the amount of memory that should be made available to
98+
// the microVM.
99+
MemInMiB int64
100+
101+
// RootDrive specifies the BlockDevice that contains the root filesystem.
102+
RootDrive BlockDevice
103+
104+
// RootPartitionUUID defines the UUID that specifies the root partition.
73105
RootPartitionUUID string
74-
AdditionalDrives []BlockDevice
106+
107+
// AdditionalDrives specifies additional BlockDevices that should be made
108+
// available to the microVM.
109+
AdditionalDrives []BlockDevice
110+
111+
// NetworkInterfaces specifies the tap devices that should be made available
112+
// to the microVM.
75113
NetworkInterfaces []NetworkInterface
76-
VsockDevices []VsockDevice
77-
Debug bool
78-
machineCfg models.MachineConfiguration
79114

80-
// Allows for easier mock testing
115+
// VsockDevices specifies the vsock devices that should be made available to
116+
// the microVM.
117+
VsockDevices []VsockDevice
118+
119+
// Debug enables debug-level logging for the SDK.
120+
Debug bool
121+
machineCfg models.MachineConfiguration
122+
123+
// DisableValidation allows for easier mock testing by disabling the
124+
// validation of configuration performed by the SDK.
81125
DisableValidation bool
82126
}
83127

@@ -89,21 +133,21 @@ func (cfg *Config) Validate() error {
89133
}
90134

91135
if _, err := os.Stat(cfg.KernelImagePath); err != nil {
92-
return fmt.Errorf("Failed to stat kernal image path, %q: %v", cfg.KernelImagePath, err)
136+
return fmt.Errorf("failed to stat kernal image path, %q: %v", cfg.KernelImagePath, err)
93137
}
94138
if _, err := os.Stat(cfg.RootDrive.HostPath); err != nil {
95-
return fmt.Errorf("Failed to stat host path, %q: %v", cfg.RootDrive.HostPath, err)
139+
return fmt.Errorf("failed to stat host path, %q: %v", cfg.RootDrive.HostPath, err)
96140
}
97141

98142
// Check the non-existence of some files:
99143
if _, err := os.Stat(cfg.SocketPath); err == nil {
100-
return fmt.Errorf("Socket %s already exists", cfg.SocketPath)
144+
return fmt.Errorf("socket %s already exists", cfg.SocketPath)
101145
}
102146

103147
return nil
104148
}
105149

106-
// Machine is the main object for manipulating firecracker VMs
150+
// Machine is the main object for manipulating Firecracker microVMs
107151
type Machine struct {
108152
cfg Config
109153
client Firecracker
@@ -114,32 +158,41 @@ type Machine struct {
114158

115159
// Logger returns a logrus logger appropriate for logging hypervisor messages
116160
func (m *Machine) Logger() *log.Entry {
117-
// return m.logger.WithField("subsystem", "firecracker-go-sdk")
118-
return m.logger
161+
return m.logger.WithField("subsystem", userAgent)
119162
}
120163

121-
// NetworkInterface represents a Firecracker VMs network interface.
164+
// NetworkInterface represents a Firecracker microVM's network interface.
122165
type NetworkInterface struct {
123-
MacAddress string
166+
// MacAddress defines the MAC address that should be assigned to the network
167+
// interface inside the microVM.
168+
MacAddress string
169+
// HostDevName defines the file path of the tap device on the host.
124170
HostDevName string
125-
AllowMDDS bool
171+
// AllowMMDS makes the Firecracker MMDS available on this network interface.
172+
AllowMDDS bool
126173
}
127174

128-
// BlockDevice represents a host block device mapped to the firecracker VM.
129-
// mode is either "ro" or "rw"
175+
// BlockDevice represents a host block device mapped to the Firecracker microVM.
130176
type BlockDevice struct {
177+
// HostPath defines the filesystem path of the block device on the host.
131178
HostPath string
132-
Mode string
179+
// Mode defines whether the device is writable. Valid values are "ro" and
180+
// "rw".
181+
Mode string
133182
}
134183

135-
// VsockDevice represents a vsock connection between the
136-
// host and the guest VM.
184+
// VsockDevice represents a vsock connection between the host and the guest
185+
// microVM.
137186
type VsockDevice struct {
187+
// Path defines the filesystem path of the vsock device on the host.
138188
Path string
139-
CID uint32
189+
// CID defines the 32-bit Context Identifier for the vsock device. See
190+
// the vsock(7) manual page for more information.
191+
CID uint32
140192
}
141193

142-
// SocketPath returns the filesystem path to the socket used for VMM communication
194+
// SocketPath returns the filesystem path to the socket used for VMM
195+
// communication
143196
func (m Machine) socketPath() string {
144197
return m.cfg.SocketPath
145198
}
@@ -154,7 +207,8 @@ func (m Machine) LogLevel() string {
154207
return m.cfg.LogLevel
155208
}
156209

157-
// NewMachine initializes a new Machine instance
210+
// NewMachine initializes a new Machine instance and performs validation of the
211+
// provided Config.
158212
func NewMachine(cfg Config, opts ...Opt) (*Machine, error) {
159213
if err := cfg.Validate(); err != nil {
160214
return nil, err
@@ -287,7 +341,7 @@ func (m *Machine) startVMM(ctx context.Context) (<-chan error, error) {
287341
exitCh <- err
288342
}()
289343

290-
// Set up a signal hander and pass INT, QUIT, and TERM through to firecracker
344+
// Set up a signal handler and pass INT, QUIT, and TERM through to firecracker
291345
vmchan := make(chan error)
292346
sigchan := make(chan os.Signal)
293347
signal.Notify(sigchan, os.Interrupt,
@@ -334,7 +388,7 @@ func (m *Machine) stopVMM() error {
334388
return nil
335389
}
336390

337-
// createFifos sets up the firecracker logging and metrics fifos
391+
// createFifos sets up the firecracker logging and metrics FIFOs
338392
func createFifos(logFifo, metricsFifo string) error {
339393
log.Debugf("Creating FIFO %s", logFifo)
340394
err := syscall.Mkfifo(logFifo, 0700)
@@ -488,7 +542,7 @@ func (m *Machine) addVsock(ctx context.Context, dev VsockDevice) error {
488542
return nil
489543
}
490544

491-
// StartInstance starts the firecracker VM
545+
// StartInstance starts the Firecracker microVM
492546
func (m *Machine) StartInstance(ctx context.Context) error {
493547
return m.startInstance(ctx)
494548
}

opts.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,29 @@ import (
1919
"github.com/sirupsen/logrus"
2020
)
2121

22-
// Opt represents a functional option to help
23-
// modify functionality of a Machine.
22+
// Opt represents a functional option to help modify functionality of a Machine.
2423
type Opt func(*Machine)
2524

26-
// WithClient will use the client in place rather
27-
// than the client constructed during bootstrapping of
28-
// the machine. This option is useful for mocking out
25+
// WithClient will use the client in place rather than the client constructed
26+
// during bootstrapping of the machine. This option is useful for mocking out
2927
// tests.
3028
func WithClient(client Firecracker) Opt {
3129
return func(machine *Machine) {
3230
machine.client = client
3331
}
3432
}
3533

36-
// WithLogger will allow for the Machine to use
37-
// the provided logger.
34+
// WithLogger will allow for the Machine to use the provided logger.
3835
func WithLogger(logger *logrus.Entry) Opt {
3936
return func(machine *Machine) {
4037
machine.logger = logger
4138
}
4239
}
4340

44-
// WithProcessRunner will allow for a specific command
45-
// to be run instead of the default firecracker command.
46-
// For example, this could be used to instead call the
47-
// jailer instead of firecracker directly.
41+
// WithProcessRunner will allow for a specific command to be run instead of the
42+
// default firecracker command.
43+
// For example, this could be used to instead call the jailer instead of
44+
// firecracker directly.
4845
func WithProcessRunner(cmd *exec.Cmd) Opt {
4946
return func(machine *Machine) {
5047
machine.cmd = cmd

0 commit comments

Comments
 (0)