@@ -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
@@ -152,6 +156,7 @@ func (cfg *Config) Validate() error {
152156 return nil
153157}
154158
159+ // ValidateNetwork .
155160func (cfg * Config ) ValidateNetwork () error {
156161 if cfg .DisableValidation {
157162 return nil
@@ -297,6 +302,10 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
297302 m .machineConfig = cfg .MachineCfg
298303 m .Cfg = cfg
299304
305+ if cfg .NetNS == "" && cfg .NetworkInterfaces .cniInterface () != nil {
306+ m .Cfg .NetNS = m .defaultNetNSPath ()
307+ }
308+
300309 m .logger .Debug ("Called NewMachine()" )
301310 return m , nil
302311}
@@ -354,24 +363,8 @@ func (m *Machine) Wait(ctx context.Context) error {
354363 }
355364}
356365
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-
373366func (m * Machine ) setupNetwork (ctx context.Context ) error {
374- err , cleanupFuncs := m .Cfg .NetworkInterfaces .setupNetwork (ctx , m .Cfg .VMID , m .netNSPath () , m .logger )
367+ err , cleanupFuncs := m .Cfg .NetworkInterfaces .setupNetwork (ctx , m .Cfg .VMID , m .Cfg . NetNS , m .logger )
375368 m .cleanupFuncs = append (m .cleanupFuncs , cleanupFuncs ... )
376369 return err
377370}
@@ -421,19 +414,20 @@ func (m *Machine) attachDrives(ctx context.Context, drives ...models.Drive) erro
421414 return nil
422415}
423416
417+ func (m * Machine ) defaultNetNSPath () string {
418+ return filepath .Join (defaultNetNSDir , m .Cfg .VMID )
419+ }
420+
424421// startVMM starts the firecracker vmm process and configures logging.
425422func (m * Machine ) startVMM (ctx context.Context ) error {
426423 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 () != ""
430424 startCmd := m .cmd .Start
431425
432426 var err error
433- if hasNetNS && ! jailerProvidedNetNS {
427+ if m . Cfg . NetNS != "" && m . Cfg . JailerCfg == nil {
434428 // If the VM needs to be started in a netns but no jailer netns was configured,
435429 // start the vmm child process in the netns directly here.
436- err = ns .WithNetNSPath (m .netNSPath () , func (_ ns.NetNS ) error {
430+ err = ns .WithNetNSPath (m .Cfg . NetNS , func (_ ns.NetNS ) error {
437431 return startCmd ()
438432 })
439433 } else {
0 commit comments