@@ -36,6 +36,10 @@ import (
36
36
"github.com/firecracker-microvm/firecracker-containerd/internal/vm"
37
37
)
38
38
39
+ const (
40
+ networkNamespaceRuncName = "network"
41
+ )
42
+
39
43
// runcJailer uses runc to set up a jailed environment for the Firecracker VM.
40
44
type runcJailer struct {
41
45
ctx context.Context
@@ -97,7 +101,7 @@ func (j *runcJailer) JailPath() vm.Dir {
97
101
// instance. In addition, some configuration values will be overwritten to the
98
102
// jailed values, like SocketPath in the machineConfig.
99
103
func (j * runcJailer ) BuildJailedMachine (cfg * Config , machineConfig * firecracker.Config , vmID string ) ([]firecracker.Opt , error ) {
100
- handler := j .BuildJailedRootHandler (cfg , & machineConfig . SocketPath , vmID )
104
+ handler := j .BuildJailedRootHandler (cfg , machineConfig , vmID )
101
105
fifoHandler := j .BuildLinkFifoHandler ()
102
106
// Build a new client since BuildJailedRootHandler modifies the socket path value.
103
107
client := firecracker .NewClient (machineConfig .SocketPath , j .logger , machineConfig .Debug )
@@ -128,10 +132,10 @@ func (j *runcJailer) BuildJailedMachine(cfg *Config, machineConfig *firecracker.
128
132
129
133
// BuildJailedRootHandler will populate the jail with the necessary files, which may be
130
134
// device nodes, hard links, and/or bind-mount targets
131
- func (j * runcJailer ) BuildJailedRootHandler (cfg * Config , socketPath * string , vmID string ) firecracker.Handler {
135
+ func (j * runcJailer ) BuildJailedRootHandler (cfg * Config , machineConfig * firecracker. Config , vmID string ) firecracker.Handler {
132
136
ociBundlePath := j .OCIBundlePath ()
133
137
rootPath := j .RootPath ()
134
- * socketPath = filepath .Join (rootPath , "api.socket" )
138
+ machineConfig . SocketPath = filepath .Join (rootPath , "api.socket" )
135
139
136
140
return firecracker.Handler {
137
141
Name : jailerHandlerName ,
@@ -144,7 +148,7 @@ func (j *runcJailer) BuildJailedRootHandler(cfg *Config, socketPath *string, vmI
144
148
}
145
149
146
150
j .logger .Debug ("Overwritting process args of config" )
147
- if err := j .overwriteConfig (cfg , filepath .Base (m .Cfg .SocketPath ), rootPathToConfig ); err != nil {
151
+ if err := j .overwriteConfig (cfg , machineConfig , filepath .Base (m .Cfg .SocketPath ), rootPathToConfig ); err != nil {
148
152
return errors .Wrap (err , "failed to overwrite config.json" )
149
153
}
150
154
@@ -363,7 +367,7 @@ func (j *runcJailer) jailerCommand(containerName string, isDebug bool) *exec.Cmd
363
367
}
364
368
365
369
// overwriteConfig will set the proper default values if a field had not been set.
366
- func (j * runcJailer ) overwriteConfig (cfg * Config , socketPath , configPath string ) error {
370
+ func (j * runcJailer ) overwriteConfig (cfg * Config , machineConfig * firecracker. Config , socketPath , configPath string ) error {
367
371
var err error
368
372
j .once .Do (func () {
369
373
if configSpec == nil {
@@ -404,6 +408,26 @@ func (j *runcJailer) overwriteConfig(cfg *Config, socketPath, configPath string)
404
408
spec .Process .User .UID = j .uid
405
409
spec .Process .User .GID = j .gid
406
410
411
+ // remove the network namespace if there exists a CNI
412
+ if hasCNI (machineConfig .NetworkInterfaces ) {
413
+ namespaces := []specs.LinuxNamespace {}
414
+ for _ , ns := range spec .Linux .Namespaces {
415
+ if ns .Type != networkNamespaceRuncName {
416
+ namespaces = append (namespaces , ns )
417
+ }
418
+ }
419
+
420
+ spec .Linux .Namespaces = namespaces
421
+ } else if machineConfig .NetNS != "" {
422
+ for i , ns := range spec .Linux .Namespaces {
423
+ if ns .Type == networkNamespaceRuncName {
424
+ ns .Path = machineConfig .NetNS
425
+ spec .Linux .Namespaces [i ] = ns
426
+ break
427
+ }
428
+ }
429
+ }
430
+
407
431
configBytes , err := json .Marshal (& spec )
408
432
if err != nil {
409
433
return err
@@ -477,10 +501,20 @@ func getNetNS(spec *specs.Spec) string {
477
501
}
478
502
479
503
for _ , ns := range spec .Linux .Namespaces {
480
- if ns .Type == "network" {
504
+ if ns .Type == networkNamespaceRuncName {
481
505
return ns .Path
482
506
}
483
507
}
484
508
485
509
return ""
486
510
}
511
+
512
+ func hasCNI (interfaces firecracker.NetworkInterfaces ) bool {
513
+ for _ , iface := range interfaces {
514
+ if iface .CNIConfiguration != nil {
515
+ return true
516
+ }
517
+ }
518
+
519
+ return false
520
+ }
0 commit comments