@@ -107,6 +107,10 @@ type Config struct {
107107 // set the CNI ContainerID and create a network namespace path if
108108 // CNI configuration is provided as part of NetworkInterfaces
109109 VMID string
110+
111+ // NetNS represents the path to a network namespace handle. If present, the
112+ // application will use this to join the associated network namespace
113+ NetNS string
110114}
111115
112116// Validate will ensure that the required fields are set and that
@@ -151,6 +155,7 @@ func (cfg *Config) Validate() error {
151155 return nil
152156}
153157
158+ // ValidateNetwork .
154159func (cfg * Config ) ValidateNetwork () error {
155160 if cfg .DisableValidation {
156161 return nil
@@ -296,6 +301,10 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
296301 m .machineConfig = cfg .MachineCfg
297302 m .Cfg = cfg
298303
304+ if cfg .NetNS == "" && cfg .NetworkInterfaces .cniInterface () != nil {
305+ m .Cfg .NetNS = m .defaultNetNSPath ()
306+ }
307+
299308 m .logger .Debug ("Called NewMachine()" )
300309 return m , nil
301310}
@@ -353,24 +362,8 @@ func (m *Machine) Wait(ctx context.Context) error {
353362 }
354363}
355364
356- func (m * Machine ) netNSPath () string {
357- // If the jailer specifies a netns, use that
358- if jailerNetNS := m .Cfg .JailerCfg .netNSPath (); jailerNetNS != "" {
359- return jailerNetNS
360- }
361-
362- // If there isn't a jailer netns but there is a network
363- // interface with CNI configuration, use a default netns path
364- if m .Cfg .NetworkInterfaces .cniInterface () != nil {
365- return filepath .Join (defaultNetNSDir , m .Cfg .VMID )
366- }
367-
368- // else, just don't use a netns for the VM
369- return ""
370- }
371-
372365func (m * Machine ) setupNetwork (ctx context.Context ) error {
373- 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 )
374367 m .cleanupFuncs = append (m .cleanupFuncs , cleanupFuncs ... )
375368 return err
376369}
@@ -420,19 +413,22 @@ func (m *Machine) attachDrives(ctx context.Context, drives ...models.Drive) erro
420413 return nil
421414}
422415
416+ func (m * Machine ) defaultNetNSPath () string {
417+ return filepath .Join (defaultNetNSDir , m .Cfg .VMID )
418+ }
419+
423420// startVMM starts the firecracker vmm process and configures logging.
424421func (m * Machine ) startVMM (ctx context.Context ) error {
425422 m .logger .Printf ("Called startVMM(), setting up a VMM on %s" , m .Cfg .SocketPath )
426423
427- hasNetNS := m .netNSPath () != ""
428- jailerProvidedNetNS := m .Cfg .JailerCfg .netNSPath () != ""
424+ isDefaultNetNSPath := m .Cfg .NetNS == m .defaultNetNSPath ()
429425 startCmd := m .cmd .Start
430426
431427 var err error
432- if hasNetNS && ! jailerProvidedNetNS {
428+ if isDefaultNetNSPath {
433429 // If the VM needs to be started in a netns but no jailer netns was configured,
434430 // start the vmm child process in the netns directly here.
435- err = ns .WithNetNSPath (m .netNSPath () , func (_ ns.NetNS ) error {
431+ err = ns .WithNetNSPath (m .Cfg . NetNS , func (_ ns.NetNS ) error {
436432 return startCmd ()
437433 })
438434 } else {
0 commit comments