@@ -108,6 +108,10 @@ type Config struct {
108
108
// set the CNI ContainerID and create a network namespace path if
109
109
// CNI configuration is provided as part of NetworkInterfaces
110
110
VMID string
111
+
112
+ // NetNS represents the path to a network namespace handle. If present, the
113
+ // application will use this to join the associated network namespace
114
+ NetNS string
111
115
}
112
116
113
117
// Validate will ensure that the required fields are set and that
@@ -297,6 +301,10 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
297
301
m .machineConfig = cfg .MachineCfg
298
302
m .Cfg = cfg
299
303
304
+ if cfg .NetNS == "" && cfg .NetworkInterfaces .cniInterface () != nil {
305
+ m .Cfg .NetNS = m .defaultNetNSPath ()
306
+ }
307
+
300
308
m .logger .Debug ("Called NewMachine()" )
301
309
return m , nil
302
310
}
@@ -354,24 +362,8 @@ func (m *Machine) Wait(ctx context.Context) error {
354
362
}
355
363
}
356
364
357
- func (m * Machine ) netNSPath () string {
358
- // If the jailer specifies a netns, use that
359
- if jailerNetNS := m .Cfg .JailerCfg .netNSPath (); jailerNetNS != "" {
360
- return jailerNetNS
361
- }
362
-
363
- // If there isn't a jailer netns but there is a network
364
- // interface with CNI configuration, use a default netns path
365
- if m .Cfg .NetworkInterfaces .cniInterface () != nil {
366
- return filepath .Join (defaultNetNSDir , m .Cfg .VMID )
367
- }
368
-
369
- // else, just don't use a netns for the VM
370
- return ""
371
- }
372
-
373
365
func (m * Machine ) setupNetwork (ctx context.Context ) error {
374
- err , cleanupFuncs := m .Cfg .NetworkInterfaces .setupNetwork (ctx , m .Cfg .VMID , m .netNSPath () , m .logger )
366
+ err , cleanupFuncs := m .Cfg .NetworkInterfaces .setupNetwork (ctx , m .Cfg .VMID , m .Cfg . NetNS , m .logger )
375
367
m .cleanupFuncs = append (m .cleanupFuncs , cleanupFuncs ... )
376
368
return err
377
369
}
@@ -421,19 +413,20 @@ func (m *Machine) attachDrives(ctx context.Context, drives ...models.Drive) erro
421
413
return nil
422
414
}
423
415
416
+ func (m * Machine ) defaultNetNSPath () string {
417
+ return filepath .Join (defaultNetNSDir , m .Cfg .VMID )
418
+ }
419
+
424
420
// startVMM starts the firecracker vmm process and configures logging.
425
421
func (m * Machine ) startVMM (ctx context.Context ) error {
426
422
m .logger .Printf ("Called startVMM(), setting up a VMM on %s" , m .Cfg .SocketPath )
427
-
428
- hasNetNS := m .netNSPath () != ""
429
- jailerProvidedNetNS := m .Cfg .JailerCfg .netNSPath () != ""
430
423
startCmd := m .cmd .Start
431
424
432
425
var err error
433
- if hasNetNS && ! jailerProvidedNetNS {
426
+ if m . Cfg . NetNS != "" && m . Cfg . JailerCfg == nil {
434
427
// If the VM needs to be started in a netns but no jailer netns was configured,
435
428
// start the vmm child process in the netns directly here.
436
- err = ns .WithNetNSPath (m .netNSPath () , func (_ ns.NetNS ) error {
429
+ err = ns .WithNetNSPath (m .Cfg . NetNS , func (_ ns.NetNS ) error {
437
430
return startCmd ()
438
431
})
439
432
} else {
0 commit comments