Skip to content

Commit bebea92

Browse files
committed
fixup! Add support for netns
Signed-off-by: xibz <[email protected]>
1 parent b7b87f3 commit bebea92

File tree

5 files changed

+64
-147
lines changed

5 files changed

+64
-147
lines changed

runtime/cni_integ_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,18 @@ import (
3939
)
4040

4141
func TestCNISupport_Isolated(t *testing.T) {
42-
prepareIntegTest(t)
42+
t.Run("With jailer", func(t *testing.T) {
43+
prepareIntegTest(t, withJailer())
44+
testCNISupport(t)
45+
})
46+
47+
t.Run("Without jailer", func(t *testing.T) {
48+
prepareIntegTest(t)
49+
testCNISupport(t)
50+
})
51+
}
4352

53+
func testCNISupport(t *testing.T) {
4454
testTimeout := 120 * time.Second
4555
ctx, cancel := context.WithTimeout(namespaces.WithNamespace(context.Background(), defaultNamespace), testTimeout)
4656
defer cancel()

runtime/runc_jailer.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
4044
type 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.
99103
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)
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+
}

runtime/runc_jailer_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ func TestBuildJailedRootHandler_Isolated(t *testing.T) {
6161
KernelImagePath: kernelImagePath,
6262
RootDrive: rootDrivePath,
6363
}
64-
socketPath := "/path/to/api.socket"
64+
machineConfig := firecracker.Config{
65+
SocketPath: "/path/to/api.socket",
66+
KernelImagePath: kernelImagePath,
67+
Drives: []models.Drive{
68+
{
69+
PathOnHost: firecracker.String(rootDrivePath),
70+
IsRootDevice: firecracker.Bool(true),
71+
},
72+
},
73+
}
6574
vmID := "foo"
66-
handler := jailer.BuildJailedRootHandler(&cfg, &socketPath, vmID)
75+
handler := jailer.BuildJailedRootHandler(&cfg, &machineConfig, vmID)
6776

6877
machine := firecracker.Machine{
69-
Cfg: firecracker.Config{
70-
SocketPath: socketPath,
71-
KernelImagePath: kernelImagePath,
72-
Drives: []models.Drive{
73-
{
74-
PathOnHost: firecracker.String(rootDrivePath),
75-
IsRootDevice: firecracker.Bool(true),
76-
},
77-
},
78-
},
78+
Cfg: machineConfig,
7979
}
8080
err = handler.Fn(context.Background(), &machine)
8181
assert.NoError(t, err, "jailed handler failed to run")

tools/docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ COPY _submodules/firecracker/target/$FIRECRACKER_TARGET/release/firecracker /usr
151151
COPY _submodules/firecracker/target/$FIRECRACKER_TARGET/release/jailer /usr/local/bin/
152152
COPY _submodules/runc/runc /usr/local/bin
153153
COPY tools/image-builder/rootfs.img /var/lib/firecracker-containerd/runtime/default-rootfs.img
154-
COPY tools/docker//firecracker-runc-config.json /etc/containerd/firecracker-runc-config.json
154+
COPY runtime/firecracker-runc-config.json.example /etc/containerd/firecracker-runc-config.json
155155

156156
# pull the images the tests need into the content store so we don't need internet
157157
# access during the tests themselves

tools/docker/firecracker-runc-config.json

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)