Skip to content

Commit bce5434

Browse files
committed
Refactored NewMachine and Start functions
Signed-off-by: David Son <davbson@amazon.com>
1 parent b6809bb commit bce5434

File tree

7 files changed

+20
-23
lines changed

7 files changed

+20
-23
lines changed

docs/snapshotting.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ m.PauseVM(ctx)
4040
m.CreateSnapshot(ctx, memPath, snapPath)
4141
```
4242

43-
The snapshot can be loaded at any later time at startup of a machine via the machine's `Start()` function, using `WithSnapshot()` as an option. The VM must then be resumed before attempting to use it.
43+
The snapshot can be loaded at any later time at creation of a machine via the machine's `NewMachine()` function, using `WithSnapshot()` as an option. Upon starting, the VM loads the snapshot and must then be resumed before attempting to use it.
4444

4545
```
4646
ctx := context.Background()
@@ -49,9 +49,9 @@ cfg := sdk.Config{
4949
...
5050
5151
}
52-
m, _ := sdk.NewMachine(ctx, cfg)
52+
m, _ := sdk.NewMachine(ctx, cfg, sdk.WithSnapshot(memPath, snapPath))
5353
54-
m.Start(ctx, sdk.WithSnapshot(memPath, snapPath))
54+
m.Start(ctx)
5555
m.ResumeVM(ctx)
5656
```
5757

examples/cmd/snapshotting/example_demo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,13 @@ func loadSnapshotSSH(ctx context.Context, socketPath, memPath, snapPath, ipToRes
305305
// Use the firecracker binary
306306
cmd := sdk.VMCommandBuilder{}.WithSocketPath(socketFile).WithBin(filepath.Join(dir, "firecracker")).Build(ctx)
307307

308-
m, err := sdk.NewMachine(ctx, cfg, sdk.WithProcessRunner(cmd))
308+
m, err := sdk.NewMachine(ctx, cfg, sdk.WithProcessRunner(cmd), sdk.WithSnapshot(memPath, snapPath))
309309
if err != nil {
310310
log.Fatal(err)
311311
}
312312
defer os.Remove(socketFile)
313313

314-
err = m.Start(ctx, sdk.WithSnapshot(memPath, snapPath))
314+
err = m.Start(ctx)
315315
if err != nil {
316316
log.Fatal(err)
317317
}

examples/cmd/snapshotting/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ require (
3939
golang.org/x/net v0.0.0-20220728211354-c7608f3a8462 // indirect
4040
gopkg.in/yaml.v2 v2.4.0 // indirect
4141
)
42+
43+
replace github.com/firecracker-microvm/firecracker-go-sdk => ../../..

machine.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,6 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
339339
m.cmd = configureBuilder(defaultFirecrackerVMMCommandBuilder, cfg).Build(ctx)
340340
}
341341

342-
for _, opt := range opts {
343-
opt(m)
344-
}
345-
346342
if m.logger == nil {
347343
logger := log.New()
348344

@@ -370,6 +366,10 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
370366
m.Cfg.NetNS = m.defaultNetNSPath()
371367
}
372368

369+
for _, opt := range opts {
370+
opt(m)
371+
}
372+
373373
m.logger.Debug("Called NewMachine()")
374374
return m, nil
375375
}
@@ -382,7 +382,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
382382
// handlers succeed, then this will start the VMM instance.
383383
// Start may only be called once per Machine. Subsequent calls will return
384384
// ErrAlreadyStarted.
385-
func (m *Machine) Start(ctx context.Context, opts ...StartOpt) error {
385+
func (m *Machine) Start(ctx context.Context) error {
386386
m.logger.Debug("Called Machine.Start()")
387387
alreadyStarted := true
388388
m.startOnce.Do(func() {
@@ -403,10 +403,6 @@ func (m *Machine) Start(ctx context.Context, opts ...StartOpt) error {
403403
}
404404
}()
405405

406-
for _, opt := range opts {
407-
opt(m)
408-
}
409-
410406
err = m.Handlers.Run(ctx, m)
411407
if err != nil {
412408
return err

machine_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,10 +1945,10 @@ func TestLoadSnapshot(t *testing.T) {
19451945
// some unexported members
19461946
args := m.cmd.Args[1:]
19471947
m.cmd = exec.Command(getFirecrackerBinaryPath(), args...)
1948-
}, WithLogger(logrus.NewEntry(machineLogger)))
1948+
}, WithLogger(logrus.NewEntry(machineLogger)), WithSnapshot(memPath, snapPath))
19491949
require.NoError(t, err)
19501950

1951-
err = m.Start(ctx, WithSnapshot(memPath, snapPath))
1951+
err = m.Start(ctx)
19521952
require.NoError(t, err)
19531953

19541954
err = m.ResumeVM(ctx)
@@ -1971,10 +1971,10 @@ func TestLoadSnapshot(t *testing.T) {
19711971
// some unexported members
19721972
args := m.cmd.Args[1:]
19731973
m.cmd = exec.Command(getFirecrackerBinaryPath(), args...)
1974-
}, WithLogger(logrus.NewEntry(machineLogger)))
1974+
}, WithLogger(logrus.NewEntry(machineLogger)), WithSnapshot(memPath, snapPath))
19751975
require.NoError(t, err)
19761976

1977-
err = m.Start(ctx, WithSnapshot(memPath, snapPath))
1977+
err = m.Start(ctx)
19781978
require.Error(t, err)
19791979
},
19801980
},
@@ -2085,10 +2085,10 @@ func TestLoadSnapshot(t *testing.T) {
20852085

20862086
cmd := VMCommandBuilder{}.WithSocketPath(fmt.Sprintf("%s.load", socketPath)).WithBin(getFirecrackerBinaryPath()).Build(ctx)
20872087

2088-
m, err := NewMachine(ctx, cfg, WithProcessRunner(cmd))
2088+
m, err := NewMachine(ctx, cfg, WithProcessRunner(cmd), WithSnapshot(memPath, snapPath))
20892089
require.NoError(t, err)
20902090

2091-
err = m.Start(ctx, WithSnapshot(memPath, snapPath))
2091+
err = m.Start(ctx)
20922092
require.NoError(t, err)
20932093
defer m.StopVMM()
20942094

machineiface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var _ MachineIface = (*Machine)(nil)
2323
// MachineIface can be used for mocking and testing of the Machine. The Machine
2424
// is subject to change, meaning this interface would change.
2525
type MachineIface interface {
26-
Start(context.Context, ...StartOpt) error
26+
Start(context.Context) error
2727
StopVMM() error
2828
Shutdown(context.Context) error
2929
Wait(context.Context) error

opts.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
// Opt represents a functional option to help modify functionality of a Machine.
2323
type Opt func(*Machine)
24-
type StartOpt func(*Machine)
2524

2625
// WithClient will use the client in place rather than the client constructed
2726
// during bootstrapping of the machine. This option is useful for mocking out
@@ -54,7 +53,7 @@ func WithProcessRunner(cmd *exec.Cmd) Opt {
5453
type WithSnapshotOpt func(*SnapshotConfig)
5554

5655
// WithSnapshot will allow for the machine to start using a given snapshot.
57-
func WithSnapshot(memFilePath, snapshotPath string, opts ...WithSnapshotOpt) StartOpt {
56+
func WithSnapshot(memFilePath, snapshotPath string, opts ...WithSnapshotOpt) Opt {
5857
return func(m *Machine) {
5958
m.Cfg.Snapshot.MemFilePath = memFilePath
6059
m.Cfg.Snapshot.SnapshotPath = snapshotPath

0 commit comments

Comments
 (0)