@@ -108,6 +108,10 @@ type Config struct {
108108 // set the CNI ContainerID and create a network namespace path if
109109 // CNI configuration is provided as part of NetworkInterfaces
110110 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
111115}
112116
113117// 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)
297301 m .machineConfig = cfg .MachineCfg
298302 m .Cfg = cfg
299303
304+ if cfg .NetNS == "" && cfg .NetworkInterfaces .cniInterface () != nil {
305+ m .Cfg .NetNS = m .defaultNetNSPath ()
306+ }
307+
300308 m .logger .Debug ("Called NewMachine()" )
301309 return m , nil
302310}
@@ -354,24 +362,8 @@ func (m *Machine) Wait(ctx context.Context) error {
354362 }
355363}
356364
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-
373365func (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 )
375367 m .cleanupFuncs = append (m .cleanupFuncs , cleanupFuncs ... )
376368 return err
377369}
@@ -421,19 +413,20 @@ func (m *Machine) attachDrives(ctx context.Context, drives ...models.Drive) erro
421413 return nil
422414}
423415
416+ func (m * Machine ) defaultNetNSPath () string {
417+ return filepath .Join (defaultNetNSDir , m .Cfg .VMID )
418+ }
419+
424420// startVMM starts the firecracker vmm process and configures logging.
425421func (m * Machine ) startVMM (ctx context.Context ) error {
426422 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 () != ""
430423 startCmd := m .cmd .Start
431424
432425 var err error
433- if hasNetNS && ! jailerProvidedNetNS {
426+ if m . Cfg . NetNS != "" && m . Cfg . JailerCfg == nil {
434427 // If the VM needs to be started in a netns but no jailer netns was configured,
435428 // 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 {
437430 return startCmd ()
438431 })
439432 } else {
0 commit comments