Skip to content

Commit e996a5e

Browse files
author
Noah Meyerhans
committed
update SDK to support firecracker 0.17.0
Signed-off-by: Noah Meyerhans <[email protected]>
1 parent 9e2903b commit e996a5e

File tree

6 files changed

+64
-41
lines changed

6 files changed

+64
-41
lines changed

example_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212

1313
func ExampleWithProcessRunner_logging() {
1414
const socketPath = "/tmp/firecracker.sock"
15+
1516
cfg := firecracker.Config{
1617
SocketPath: socketPath,
1718
KernelImagePath: "/path/to/kernel",
1819
Drives: firecracker.NewDrivesBuilder("/path/to/rootfs").Build(),
1920
MachineCfg: models.MachineConfiguration{
20-
VcpuCount: 1,
21+
VcpuCount: firecracker.Int64(1),
2122
},
2223
JailerCfg: firecracker.JailerConfig{
2324
GID: firecracker.Int(100),
@@ -98,7 +99,7 @@ func ExampleDrivesBuilder() {
9899
// build our drives into the machine's configuration
99100
Drives: b.Build(),
100101
MachineCfg: models.MachineConfiguration{
101-
VcpuCount: 1,
102+
VcpuCount: firecracker.Int64(1),
102103
},
103104
}
104105

@@ -140,7 +141,7 @@ func ExampleDrivesBuilder_driveOpt() {
140141
// build our drives into the machine's configuration
141142
Drives: drives,
142143
MachineCfg: models.MachineConfiguration{
143-
VcpuCount: 1,
144+
VcpuCount: firecracker.Int64(1),
144145
},
145146
}
146147

@@ -197,7 +198,7 @@ func ExampleNetworkInterface_rateLimiting() {
197198
KernelImagePath: "/path/to/kernel",
198199
Drives: firecracker.NewDrivesBuilder("/path/to/rootfs").Build(),
199200
MachineCfg: models.MachineConfiguration{
200-
VcpuCount: 1,
201+
VcpuCount: firecracker.Int64(1),
201202
},
202203
NetworkInterfaces: networkIfaces,
203204
}
@@ -234,6 +235,7 @@ func ExampleJailerCommandBuilder() {
234235
WithStderr(os.Stderr)
235236

236237
const socketPath = "/tmp/firecracker/my-test-id/api.socket"
238+
237239
cfg := firecracker.Config{
238240
SocketPath: socketPath,
239241
KernelImagePath: "./vmlinux",
@@ -246,7 +248,7 @@ func ExampleJailerCommandBuilder() {
246248
},
247249
},
248250
MachineCfg: models.MachineConfiguration{
249-
VcpuCount: 1,
251+
VcpuCount: firecracker.Int64(1),
250252
},
251253
DisableValidation: true,
252254
}

firecracker.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,20 @@ func (f *Client) PutMmds(ctx context.Context, metadata interface{}, opts ...PutM
213213
return f.client.Operations.PutMmds(params)
214214
}
215215

216-
// GetMachineConfigOpt is a functional option to be used for the
217-
// GetMachineConfig API in setting any additional optional fields.
218-
type GetMachineConfigOpt func(*ops.GetMachineConfigParams)
216+
// GetMachineConfigurationOpt is a functional option to be used for the
217+
// GetMachineConfiguration API in setting any additional optional fields.
218+
type GetMachineConfigurationOpt func(*ops.GetMachineConfigurationParams)
219219

220-
// GetMachineConfig is a wrapper for the swagger generated client to make
220+
// GetMachineConfiguration is a wrapper for the swagger generated client to make
221221
// calling of the API easier.
222-
func (f *Client) GetMachineConfig(opts ...GetMachineConfigOpt) (*ops.GetMachineConfigOK, error) {
223-
p := ops.NewGetMachineConfigParams()
222+
func (f *Client) GetMachineConfiguration(opts ...GetMachineConfigurationOpt) (*ops.GetMachineConfigurationOK, error) {
223+
p := ops.NewGetMachineConfigurationParams()
224224
p.SetTimeout(firecrackerRequestTimeout)
225225
for _, opt := range opts {
226226
opt(p)
227227
}
228228

229-
return f.client.Operations.GetMachineConfig(p)
229+
return f.client.Operations.GetMachineConfiguration(p)
230230
}
231231

232232
// PatchGuestDriveByIDOpt is a functional option to be used for the PutMmds API in setting

handlers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ func TestHandlers(t *testing.T) {
519519
called = CreateMachineHandler.Name
520520
return &ops.PutMachineConfigurationNoContent{}, nil
521521
},
522-
GetMachineConfigFn: func(params *ops.GetMachineConfigParams) (*ops.GetMachineConfigOK, error) {
523-
return &ops.GetMachineConfigOK{
522+
GetMachineConfigurationFn: func(params *ops.GetMachineConfigurationParams) (*ops.GetMachineConfigurationOK, error) {
523+
return &ops.GetMachineConfigurationOK{
524524
Payload: &models.MachineConfiguration{},
525525
}, nil
526526
},

machine.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (cfg *Config) Validate() error {
106106
}
107107

108108
if _, err := os.Stat(cfg.KernelImagePath); err != nil {
109-
return fmt.Errorf("failed to stat kernal image path, %q: %v", cfg.KernelImagePath, err)
109+
return fmt.Errorf("failed to stat kernel image path, %q: %v", cfg.KernelImagePath, err)
110110
}
111111

112112
rootPath := ""
@@ -126,6 +126,17 @@ func (cfg *Config) Validate() error {
126126
return fmt.Errorf("socket %s already exists", cfg.SocketPath)
127127
}
128128

129+
if cfg.MachineCfg.VcpuCount == nil ||
130+
Int64Value(cfg.MachineCfg.VcpuCount) < 1 {
131+
return fmt.Errorf("machine needs a nonzero VcpuCount")
132+
}
133+
if cfg.MachineCfg.MemSizeMib == nil ||
134+
Int64Value(cfg.MachineCfg.MemSizeMib) < 1 {
135+
return fmt.Errorf("machine needs a nonzero amount of memory")
136+
}
137+
if cfg.MachineCfg.HtEnabled == nil {
138+
return fmt.Errorf("machine needs a setting for ht_enabled")
139+
}
129140
return nil
130141
}
131142

@@ -232,6 +243,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
232243
m.client = NewClient(cfg.SocketPath, m.logger, cfg.Debug)
233244
}
234245

246+
m.machineConfig = cfg.MachineCfg
235247
m.cfg = cfg
236248

237249
m.logger.Debug("Called NewMachine()")
@@ -482,9 +494,9 @@ func (m *Machine) createMachine(ctx context.Context) error {
482494
}
483495

484496
m.logger.Debug("PutMachineConfiguration returned")
485-
err = m.refreshMachineConfig()
497+
err = m.refreshMachineConfiguration()
486498
if err != nil {
487-
log.Errorf("Unable to inspect Firecracker MachineConfig. Continuing anyway. %s", err)
499+
log.Errorf("Unable to inspect Firecracker MachineConfiguration. Continuing anyway. %s", err)
488500
}
489501
m.logger.Debug("createMachine returning")
490502
return err
@@ -568,8 +580,9 @@ func (m *Machine) addVsock(ctx context.Context, dev VsockDevice) error {
568580
}
569581

570582
func (m *Machine) startInstance(ctx context.Context) error {
583+
action := models.InstanceActionInfoActionTypeInstanceStart
571584
info := models.InstanceActionInfo{
572-
ActionType: models.InstanceActionInfoActionTypeInstanceStart,
585+
ActionType: &action,
573586
}
574587

575588
resp, err := m.client.CreateSyncAction(ctx, &info)
@@ -582,8 +595,9 @@ func (m *Machine) startInstance(ctx context.Context) error {
582595
}
583596

584597
func (m *Machine) sendCtrlAltDel(ctx context.Context) error {
598+
action := models.InstanceActionInfoActionTypeSendCtrlAltDel
585599
info := models.InstanceActionInfo{
586-
ActionType: models.InstanceActionInfoActionTypeSendCtrlAltDel,
600+
ActionType: &action,
587601
}
588602

589603
resp, err := m.client.CreateSyncAction(ctx, &info)
@@ -618,15 +632,15 @@ func (m *Machine) UpdateGuestDrive(ctx context.Context, driveID, pathOnHost stri
618632
return nil
619633
}
620634

621-
// refreshMachineConfig synchronizes our cached representation of the machine configuration
635+
// refreshMachineConfiguration synchronizes our cached representation of the machine configuration
622636
// with that reported by the Firecracker API
623-
func (m *Machine) refreshMachineConfig() error {
624-
resp, err := m.client.GetMachineConfig()
637+
func (m *Machine) refreshMachineConfiguration() error {
638+
resp, err := m.client.GetMachineConfiguration()
625639
if err != nil {
626640
return err
627641
}
628642

629-
m.logger.Infof("refreshMachineConfig: %s", resp.Error())
643+
m.logger.Infof("refreshMachineConfiguration: %s", resp.Error())
630644
m.machineConfig = *resp.Payload
631645
return nil
632646
}
@@ -650,7 +664,7 @@ func (m *Machine) waitForSocket(timeout time.Duration, exitchan chan error) erro
650664
}
651665

652666
// Send test HTTP request to make sure socket is available
653-
if _, err := m.client.GetMachineConfig(); err != nil {
667+
if _, err := m.client.GetMachineConfiguration(); err != nil {
654668
continue
655669
}
656670

machine_test.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ func TestNewMachine(t *testing.T) {
7272
Config{
7373
Debug: true,
7474
DisableValidation: true,
75+
MachineCfg: models.MachineConfiguration{
76+
VcpuCount: Int64(1),
77+
MemSizeMib: Int64(100),
78+
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
79+
HtEnabled: Bool(false),
80+
},
7581
JailerCfg: JailerConfig{
7682
GID: Int(100),
7783
UID: Int(100),
@@ -158,9 +164,10 @@ func TestJailerMicroVMExecution(t *testing.T) {
158164
LogLevel: "Debug",
159165
KernelImagePath: vmlinuxPath,
160166
MachineCfg: models.MachineConfiguration{
161-
VcpuCount: nCpus,
167+
VcpuCount: Int64(nCpus),
162168
CPUTemplate: cpuTemplate,
163-
MemSizeMib: memSz,
169+
MemSizeMib: Int64(memSz),
170+
HtEnabled: Bool(false),
164171
},
165172
Drives: []models.Drive{
166173
models.Drive{
@@ -274,9 +281,10 @@ func TestMicroVMExecution(t *testing.T) {
274281
MetricsFifo: metricsFifo,
275282
LogLevel: "Debug",
276283
MachineCfg: models.MachineConfiguration{
277-
VcpuCount: nCpus,
284+
VcpuCount: Int64(nCpus),
278285
CPUTemplate: cpuTemplate,
279-
MemSizeMib: memSz,
286+
MemSizeMib: Int64(memSz),
287+
HtEnabled: Bool(false),
280288
},
281289
Debug: true,
282290
DisableValidation: true,
@@ -388,7 +396,10 @@ func TestStartVMMOnce(t *testing.T) {
388396
DisableValidation: true,
389397
KernelImagePath: getVmlinuxPath(t),
390398
MachineCfg: models.MachineConfiguration{
391-
VcpuCount: 1,
399+
VcpuCount: Int64(1),
400+
MemSizeMib: Int64(64),
401+
CPUTemplate: models.CPUTemplate(models.CPUTemplateT2),
402+
HtEnabled: Bool(false),
392403
},
393404
}
394405
ctx := context.Background()
@@ -453,14 +464,10 @@ func testCreateMachine(ctx context.Context, t *testing.T, m *Machine) {
453464
}
454465

455466
func testMachineConfigApplication(ctx context.Context, t *testing.T, m *Machine, expectedValues Config) {
456-
if m.machineConfig.VcpuCount != expectedValues.MachineCfg.VcpuCount {
457-
t.Errorf("Got unexpected number of VCPUs: Expected: %d, Actual: %d",
458-
expectedValues.MachineCfg.VcpuCount, m.cfg.MachineCfg.VcpuCount)
459-
}
460-
if m.machineConfig.MemSizeMib != expectedValues.MachineCfg.MemSizeMib {
461-
t.Errorf("Got unexpected value for machine memory: expected: %d, Got %d",
462-
expectedValues.MachineCfg.MemSizeMib, m.cfg.MachineCfg.MemSizeMib)
463-
}
467+
assert.Equal(t, expectedValues.MachineCfg.VcpuCount,
468+
m.machineConfig.VcpuCount, "CPU count should be equal")
469+
470+
assert.Equal(t, expectedValues.MachineCfg.MemSizeMib, m.machineConfig.MemSizeMib, "memory...")
464471
}
465472

466473
func testCreateBootSource(ctx context.Context, t *testing.T, m *Machine, vmlinuxPath string) {
@@ -582,7 +589,7 @@ func testShutdown(ctx context.Context, t *testing.T, m *Machine) {
582589
func TestWaitForSocket(t *testing.T) {
583590
okClient := fctesting.MockClient{}
584591
errClient := fctesting.MockClient{
585-
GetMachineConfigFn: func(params *ops.GetMachineConfigParams) (*ops.GetMachineConfigOK, error) {
592+
GetMachineConfigurationFn: func(params *ops.GetMachineConfigurationParams) (*ops.GetMachineConfigurationOK, error) {
586593
return nil, errors.New("http error")
587594
},
588595
}
@@ -663,8 +670,8 @@ func TestLogFiles(t *testing.T) {
663670
}
664671

665672
opClient := fctesting.MockClient{
666-
GetMachineConfigFn: func(params *ops.GetMachineConfigParams) (*ops.GetMachineConfigOK, error) {
667-
return &ops.GetMachineConfigOK{
673+
GetMachineConfigurationFn: func(params *ops.GetMachineConfigurationParams) (*ops.GetMachineConfigurationOK, error) {
674+
return &ops.GetMachineConfigurationOK{
668675
Payload: &models.MachineConfiguration{},
669676
}, nil
670677
},

utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func waitForAliveVMM(ctx context.Context, client *Client) error {
2121
case <-ctx.Done():
2222
return ctx.Err()
2323
case <-t.C:
24-
if _, err := client.GetMachineConfig(); err == nil {
24+
if _, err := client.GetMachineConfiguration(); err == nil {
2525
return nil
2626
}
2727
}

0 commit comments

Comments
 (0)