@@ -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
@@ -152,6 +156,7 @@ func (cfg *Config) Validate() error {
152
156
return nil
153
157
}
154
158
159
+ // ValidateNetwork .
155
160
func (cfg * Config ) ValidateNetwork () error {
156
161
if cfg .DisableValidation {
157
162
return nil
@@ -297,6 +302,10 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
297
302
m .machineConfig = cfg .MachineCfg
298
303
m .Cfg = cfg
299
304
305
+ if cfg .NetNS == "" && cfg .NetworkInterfaces .cniInterface () != nil {
306
+ m .Cfg .NetNS = m .defaultNetNSPath ()
307
+ }
308
+
300
309
m .logger .Debug ("Called NewMachine()" )
301
310
return m , nil
302
311
}
@@ -354,24 +363,8 @@ func (m *Machine) Wait(ctx context.Context) error {
354
363
}
355
364
}
356
365
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
366
func (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 )
375
368
m .cleanupFuncs = append (m .cleanupFuncs , cleanupFuncs ... )
376
369
return err
377
370
}
@@ -421,19 +414,20 @@ func (m *Machine) attachDrives(ctx context.Context, drives ...models.Drive) erro
421
414
return nil
422
415
}
423
416
417
+ func (m * Machine ) defaultNetNSPath () string {
418
+ return filepath .Join (defaultNetNSDir , m .Cfg .VMID )
419
+ }
420
+
424
421
// startVMM starts the firecracker vmm process and configures logging.
425
422
func (m * Machine ) startVMM (ctx context.Context ) error {
426
423
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
424
startCmd := m .cmd .Start
431
425
432
426
var err error
433
- if hasNetNS && ! jailerProvidedNetNS {
427
+ if m . Cfg . NetNS != "" && m . Cfg . JailerCfg == nil {
434
428
// If the VM needs to be started in a netns but no jailer netns was configured,
435
429
// 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 {
437
431
return startCmd ()
438
432
})
439
433
} else {
0 commit comments