@@ -36,6 +36,10 @@ import (
3636 "github.com/firecracker-microvm/firecracker-containerd/internal/vm"
3737)
3838
39+ const (
40+ networkNamespaceRuncName = "network"
41+ )
42+
3943// runcJailer uses runc to set up a jailed environment for the Firecracker VM.
4044type runcJailer struct {
4145 ctx context.Context
@@ -97,7 +101,7 @@ func (j *runcJailer) JailPath() vm.Dir {
97101// instance. In addition, some configuration values will be overwritten to the
98102// jailed values, like SocketPath in the machineConfig.
99103func (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 )
101105 fifoHandler := j .BuildLinkFifoHandler ()
102106 // Build a new client since BuildJailedRootHandler modifies the socket path value.
103107 client := firecracker .NewClient (machineConfig .SocketPath , j .logger , machineConfig .Debug )
@@ -128,10 +132,10 @@ func (j *runcJailer) BuildJailedMachine(cfg *Config, machineConfig *firecracker.
128132
129133// BuildJailedRootHandler will populate the jail with the necessary files, which may be
130134// 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 {
132136 ociBundlePath := j .OCIBundlePath ()
133137 rootPath := j .RootPath ()
134- * socketPath = filepath .Join (rootPath , "api.socket" )
138+ machineConfig . SocketPath = filepath .Join (rootPath , "api.socket" )
135139
136140 return firecracker.Handler {
137141 Name : jailerHandlerName ,
@@ -144,7 +148,7 @@ func (j *runcJailer) BuildJailedRootHandler(cfg *Config, socketPath *string, vmI
144148 }
145149
146150 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 {
148152 return errors .Wrap (err , "failed to overwrite config.json" )
149153 }
150154
@@ -363,7 +367,7 @@ func (j *runcJailer) jailerCommand(containerName string, isDebug bool) *exec.Cmd
363367}
364368
365369// 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 {
367371 var err error
368372 j .once .Do (func () {
369373 if configSpec == nil {
@@ -404,6 +408,26 @@ func (j *runcJailer) overwriteConfig(cfg *Config, socketPath, configPath string)
404408 spec .Process .User .UID = j .uid
405409 spec .Process .User .GID = j .gid
406410
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+
407431 configBytes , err := json .Marshal (& spec )
408432 if err != nil {
409433 return err
@@ -477,10 +501,20 @@ func getNetNS(spec *specs.Spec) string {
477501 }
478502
479503 for _ , ns := range spec .Linux .Namespaces {
480- if ns .Type == "network" {
504+ if ns .Type == networkNamespaceRuncName {
481505 return ns .Path
482506 }
483507 }
484508
485509 return ""
486510}
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