Skip to content

Commit 43d336c

Browse files
authored
Merge pull request #155 from xibz/netns-handler
Moves NetNS to Config from JailerConfig
2 parents 107ab9a + f24bcda commit 43d336c

File tree

3 files changed

+24
-40
lines changed

3 files changed

+24
-40
lines changed

jailer.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ type JailerConfig struct {
8686
// default is /srv/jailer
8787
ChrootBaseDir string
8888

89-
// NetNS represents the path to a network namespace handle. If present, the
90-
// jailer will use this to join the associated network namespace
91-
NetNS string
92-
9389
// Daemonize is set to true, call setsid() and redirect STDIN, STDOUT, and
9490
// STDERR to /dev/null
9591
Daemonize bool
@@ -114,13 +110,6 @@ type JailerConfig struct {
114110
Stdin io.Reader
115111
}
116112

117-
func (jailerCfg *JailerConfig) netNSPath() string {
118-
if jailerCfg == nil {
119-
return ""
120-
}
121-
return jailerCfg.NetNS
122-
}
123-
124113
// JailerCommandBuilder will build a jailer command. This can be used to
125114
// specify that a jailed firecracker executable wants to be run on the Machine.
126115
type JailerCommandBuilder struct {
@@ -348,8 +337,8 @@ func jail(ctx context.Context, m *Machine, cfg *Config) error {
348337
builder = builder.WithBin(jailerBinary)
349338
}
350339

351-
if netNS := cfg.JailerCfg.NetNS; netNS != "" {
352-
builder = builder.WithNetNS(netNS)
340+
if cfg.NetNS != "" {
341+
builder = builder.WithNetNS(cfg.NetNS)
353342
}
354343

355344
if stdin := cfg.JailerCfg.Stdin; stdin != nil {

jailer_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var testCases = []struct {
1111
name string
1212
jailerCfg JailerConfig
1313
expectedArgs []string
14+
netns string
1415
expectedSockPath string
1516
}{
1617
{
@@ -69,15 +70,15 @@ var testCases = []struct {
6970
expectedSockPath: filepath.Join(defaultJailerPath, "my-test-id", rootfsFolderName, "api.socket"),
7071
},
7172
{
72-
name: "optional fields",
73+
name: "optional fields",
74+
netns: "/path/to/netns",
7375
jailerCfg: JailerConfig{
7476
ID: "my-test-id",
7577
UID: Int(123),
7678
GID: Int(100),
7779
NumaNode: Int(1),
7880
ChrootStrategy: NewNaiveChrootStrategy("path", "kernel-image-path"),
7981
ExecFile: "/path/to/firecracker",
80-
NetNS: "/net/namespace",
8182
ChrootBaseDir: "/tmp",
8283
SeccompLevel: SeccompLevelAdvanced,
8384
JailerBinary: "/path/to/the/jailer",
@@ -97,7 +98,7 @@ var testCases = []struct {
9798
"--chroot-base-dir",
9899
"/tmp",
99100
"--netns",
100-
"/net/namespace",
101+
"/path/to/netns",
101102
"--seccomp-level",
102103
"2",
103104
},
@@ -124,8 +125,8 @@ func TestJailerBuilder(t *testing.T) {
124125
b = b.WithChrootBaseDir(c.jailerCfg.ChrootBaseDir)
125126
}
126127

127-
if len(c.jailerCfg.NetNS) > 0 {
128-
b = b.WithNetNS(c.jailerCfg.NetNS)
128+
if c.netns != "" {
129+
b = b.WithNetNS(c.netns)
129130
}
130131

131132
if c.jailerCfg.Daemonize {
@@ -150,6 +151,7 @@ func TestJail(t *testing.T) {
150151
}
151152
cfg := &Config{
152153
JailerCfg: &c.jailerCfg,
154+
NetNS: c.netns,
153155
}
154156
jail(context.Background(), m, cfg)
155157

machine.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -297,6 +301,10 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
297301
m.machineConfig = cfg.MachineCfg
298302
m.Cfg = cfg
299303

304+
if cfg.NetNS == "" && cfg.NetworkInterfaces.cniInterface() != nil {
305+
m.Cfg.NetNS = m.defaultNetNSPath()
306+
}
307+
300308
m.logger.Debug("Called NewMachine()")
301309
return m, nil
302310
}
@@ -354,24 +362,8 @@ func (m *Machine) Wait(ctx context.Context) error {
354362
}
355363
}
356364

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-
373365
func (m *Machine) setupNetwork(ctx context.Context) error {
374-
err, cleanupFuncs := m.Cfg.NetworkInterfaces.setupNetwork(ctx, m.Cfg.VMID, m.netNSPath(), m.logger)
366+
err, cleanupFuncs := m.Cfg.NetworkInterfaces.setupNetwork(ctx, m.Cfg.VMID, m.Cfg.NetNS, m.logger)
375367
m.cleanupFuncs = append(m.cleanupFuncs, cleanupFuncs...)
376368
return err
377369
}
@@ -421,19 +413,20 @@ func (m *Machine) attachDrives(ctx context.Context, drives ...models.Drive) erro
421413
return nil
422414
}
423415

416+
func (m *Machine) defaultNetNSPath() string {
417+
return filepath.Join(defaultNetNSDir, m.Cfg.VMID)
418+
}
419+
424420
// startVMM starts the firecracker vmm process and configures logging.
425421
func (m *Machine) startVMM(ctx context.Context) error {
426422
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() != ""
430423
startCmd := m.cmd.Start
431424

432425
var err error
433-
if hasNetNS && !jailerProvidedNetNS {
426+
if m.Cfg.NetNS != "" && m.Cfg.JailerCfg == nil {
434427
// If the VM needs to be started in a netns but no jailer netns was configured,
435428
// start the vmm child process in the netns directly here.
436-
err = ns.WithNetNSPath(m.netNSPath(), func(_ ns.NetNS) error {
429+
err = ns.WithNetNSPath(m.Cfg.NetNS, func(_ ns.NetNS) error {
437430
return startCmd()
438431
})
439432
} else {

0 commit comments

Comments
 (0)