Skip to content

Commit d93b040

Browse files
authored
Merge pull request #253 from kzys/add-id
Use Config.VMID as Firecracker's instance ID
2 parents 2b8dc8a + 01ba3cd commit d93b040

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

jailer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ func TestJail(t *testing.T) {
320320
},
321321
}
322322
cfg := &Config{
323+
VMID: "vmid",
323324
JailerCfg: &c.jailerCfg,
324325
NetNS: c.netns,
325326
SocketPath: c.socketPath,

machine.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ type Config struct {
137137
JailerCfg *JailerConfig
138138

139139
// (Optional) VMID is a unique identifier for this VM. It's set to a
140-
// random uuid if not provided by the user. It's currently used to
141-
// set the CNI ContainerID and create a network namespace path if
142-
// CNI configuration is provided as part of NetworkInterfaces
140+
// random uuid if not provided by the user. It's used to set Firecracker's instance ID.
141+
// If CNI configuration is provided as part of NetworkInterfaces,
142+
// the VMID is used to set CNI ContainerID and create a network namespace path.
143143
VMID string
144144

145145
// NetNS represents the path to a network namespace handle. If present, the
@@ -303,13 +303,27 @@ func (m *Machine) LogLevel() string {
303303
return m.Cfg.LogLevel
304304
}
305305

306+
func configureBuilder(builder VMCommandBuilder, cfg Config) VMCommandBuilder {
307+
return builder.
308+
WithSocketPath(cfg.SocketPath).
309+
AddArgs("--seccomp-level", cfg.SeccompLevel.String(), "--id", cfg.VMID)
310+
}
311+
306312
// NewMachine initializes a new Machine instance and performs validation of the
307313
// provided Config.
308314
func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error) {
309315
m := &Machine{
310316
exitCh: make(chan struct{}),
311317
}
312318

319+
if cfg.VMID == "" {
320+
randomID, err := uuid.NewV4()
321+
if err != nil {
322+
return nil, errors.Wrap(err, "failed to create random ID for VMID")
323+
}
324+
cfg.VMID = randomID.String()
325+
}
326+
313327
m.Handlers = defaultHandlers
314328

315329
if cfg.JailerCfg != nil {
@@ -319,10 +333,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
319333
}
320334
} else {
321335
m.Handlers.Validation = m.Handlers.Validation.Append(ConfigValidationHandler)
322-
m.cmd = defaultFirecrackerVMMCommandBuilder.
323-
WithSocketPath(cfg.SocketPath).
324-
AddArgs("--seccomp-level", cfg.SeccompLevel.String()).
325-
Build(ctx)
336+
m.cmd = configureBuilder(defaultFirecrackerVMMCommandBuilder, cfg).Build(ctx)
326337
}
327338

328339
for _, opt := range opts {
@@ -339,14 +350,6 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
339350
m.client = NewClient(cfg.SocketPath, m.logger, false)
340351
}
341352

342-
if cfg.VMID == "" {
343-
randomID, err := uuid.NewV4()
344-
if err != nil {
345-
return nil, errors.Wrap(err, "failed to create random ID for VMID")
346-
}
347-
cfg.VMID = randomID.String()
348-
}
349-
350353
if cfg.ForwardSignals == nil {
351354
cfg.ForwardSignals = []os.Signal{
352355
os.Interrupt,

machine_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,17 @@ func TestStartVMM(t *testing.T) {
420420
}
421421

422422
func TestLogAndMetrics(t *testing.T) {
423+
const logLevel = "DEBUG"
424+
const vmID = "UserSuppliedVMID"
425+
423426
dir, err := ioutil.TempDir("", t.Name())
424427
require.NoError(t, err)
425428
defer os.RemoveAll(dir)
426429

427430
socketPath := filepath.Join(dir, "fc.sock")
428431

429432
cfg := Config{
433+
VMID: vmID,
430434
SocketPath: socketPath,
431435
DisableValidation: true,
432436
KernelImagePath: getVmlinuxPath(t),
@@ -438,13 +442,10 @@ func TestLogAndMetrics(t *testing.T) {
438442
},
439443
MetricsPath: filepath.Join(dir, "fc-metrics.out"),
440444
LogPath: filepath.Join(dir, "fc.log"),
441-
LogLevel: "Debug",
445+
LogLevel: logLevel,
442446
}
443447
ctx := context.Background()
444-
cmd := VMCommandBuilder{}.
445-
WithSocketPath(cfg.SocketPath).
446-
WithBin(getFirecrackerBinaryPath()).
447-
Build(ctx)
448+
cmd := configureBuilder(VMCommandBuilder{}.WithBin(getFirecrackerBinaryPath()), cfg).Build(ctx)
448449
m, err := NewMachine(ctx, cfg, WithProcessRunner(cmd), WithLogger(fctesting.NewLogEntry(t)))
449450
require.NoError(t, err)
450451

@@ -472,6 +473,10 @@ func TestLogAndMetrics(t *testing.T) {
472473
log, err := os.Stat(cfg.LogPath)
473474
require.NoError(t, err)
474475
assert.NotEqual(t, 0, log.Size())
476+
477+
content, err := ioutil.ReadFile(cfg.LogPath)
478+
require.NoError(t, err)
479+
assert.Contains(t, string(content), "["+vmID+":"+logLevel+"]")
475480
}
476481

477482
func TestStartVMMOnce(t *testing.T) {

0 commit comments

Comments
 (0)