Skip to content

Commit 4e20b59

Browse files
authored
Merge pull request #3 from mxpv/master
Configure machine dependencies via options
2 parents 136d079 + 854e731 commit 4e20b59

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

cmd/firectl/main.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@ import (
2121
"strconv"
2222
"strings"
2323

24-
firecracker "github.com/firecracker-microvm/go-firecracker"
25-
flags "github.com/jessevdk/go-flags"
24+
"github.com/firecracker-microvm/go-firecracker"
25+
"github.com/jessevdk/go-flags"
2626
log "github.com/sirupsen/logrus"
2727
)
2828

29-
func validateDriveEntry(entry string) error {
30-
31-
return nil
32-
}
33-
3429
func checkConfig(cfg firecracker.Config) error {
3530
var err error
3631

@@ -216,8 +211,7 @@ func main() {
216211
log.Fatalf("Configuration error: %s", err)
217212
}
218213

219-
fireracker := firecracker.NewFirecrackerClient(fcCfg.SocketPath)
220-
m := firecracker.NewMachine(fcCfg, fireracker, logger)
214+
m := firecracker.NewMachine(fcCfg, firecracker.WithLogger(logger))
221215

222216
ctx := context.Background()
223217
vmmCtx, vmmCancel := context.WithCancel(ctx)

machine.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,21 @@ func (m Machine) LogLevel() string {
135135
}
136136

137137
// NewMachine initializes a new Machine instance
138-
func NewMachine(cfg Config, firecracker Firecracker, l *log.Logger) *Machine {
138+
func NewMachine(cfg Config, opts ...Opt) *Machine {
139139
m := &Machine{}
140-
if l != nil {
141-
m.logger = l
142-
} else {
140+
141+
for _, opt := range opts {
142+
opt(m)
143+
}
144+
145+
if m.logger == nil {
143146
m.logger = log.New()
144147
}
148+
149+
if m.client == nil {
150+
m.client = NewFirecrackerClient(cfg.SocketPath)
151+
}
152+
145153
m.logger.Debug("Called NewMachine()")
146154

147155
m.cfg = cfg
@@ -151,14 +159,13 @@ func NewMachine(cfg Config, firecracker Firecracker, l *log.Logger) *Machine {
151159
HtEnabled: cfg.HtEnabled,
152160
CPUTemplate: models.CPUTemplate(cfg.CPUTemplate),
153161
}
162+
154163
if len(cfg.BinPath) > 0 {
155164
m.binPath = cfg.BinPath
156165
} else {
157166
m.binPath = fcExecutable
158167
}
159168

160-
m.client = firecracker
161-
162169
return m
163170
}
164171

machine_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func init() {
3939

4040
// Ensure that we can create a new machine
4141
func TestNewMachine(t *testing.T) {
42-
m := NewMachine(Config{}, NewFirecrackerClient(""), nil)
42+
m := NewMachine(Config{})
4343
if m == nil {
4444
t.Errorf("NewMachine did not create a Machine")
4545
}
@@ -63,7 +63,7 @@ func TestMicroVMExecution(t *testing.T) {
6363
CPUTemplate: cpuTemplate,
6464
MemInMiB: memSz,
6565
}
66-
m := NewMachine(cfg, NewFirecrackerClient(cfg.SocketPath), nil)
66+
m := NewMachine(cfg)
6767
ctx := context.Background()
6868
vmmCtx, vmmCancel := context.WithTimeout(ctx, 30*time.Second)
6969
defer vmmCancel()
@@ -97,7 +97,7 @@ func TestStartVMM(t *testing.T) {
9797
SocketPath: filepath.Join("fc-start-vmm-test.sock"),
9898
BinPath: getFirecrackerBinaryPath(),
9999
}
100-
m := NewMachine(cfg, NewFirecrackerClient(cfg.SocketPath), nil)
100+
m := NewMachine(cfg)
101101
timeout, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
102102
defer cancel()
103103
errchan, err := m.startVMM(timeout)

opts.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package firecracker
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
)
6+
7+
type Opt func(*Machine)
8+
9+
func WithClient(client Firecracker) Opt {
10+
return func(machine *Machine) {
11+
machine.client = client
12+
}
13+
}
14+
15+
func WithLogger(logger *logrus.Logger) Opt {
16+
return func(machine *Machine) {
17+
machine.logger = logger
18+
}
19+
}

0 commit comments

Comments
 (0)