Skip to content

Commit 6c079fa

Browse files
committed
Moves NetNS to Config from JailerConfig
This change moves the NetNS field from the JailerConfig to the Config. This makes the most sense since net namespaces are not subject to only jailers, but can be used generally as well. Signed-off-by: xibz <[email protected]>
1 parent 1ee9961 commit 6c079fa

File tree

3 files changed

+26
-39
lines changed

3 files changed

+26
-39
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: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ type Config struct {
107107
// set the CNI ContainerID and create a network namespace path if
108108
// CNI configuration is provided as part of NetworkInterfaces
109109
VMID string
110+
111+
// NetNS represents the path to a network namespace handle. If present, the
112+
// application will use this to join the associated network namespace
113+
NetNS string
110114
}
111115

112116
// Validate will ensure that the required fields are set and that
@@ -151,6 +155,7 @@ func (cfg *Config) Validate() error {
151155
return nil
152156
}
153157

158+
// ValidateNetwork .
154159
func (cfg *Config) ValidateNetwork() error {
155160
if cfg.DisableValidation {
156161
return nil
@@ -296,6 +301,10 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
296301
m.machineConfig = cfg.MachineCfg
297302
m.Cfg = cfg
298303

304+
if cfg.NetNS == "" && cfg.NetworkInterfaces.cniInterface() != nil {
305+
m.Cfg.NetNS = m.defaultNetNSPath()
306+
}
307+
299308
m.logger.Debug("Called NewMachine()")
300309
return m, nil
301310
}
@@ -353,24 +362,8 @@ func (m *Machine) Wait(ctx context.Context) error {
353362
}
354363
}
355364

356-
func (m *Machine) netNSPath() string {
357-
// If the jailer specifies a netns, use that
358-
if jailerNetNS := m.Cfg.JailerCfg.netNSPath(); jailerNetNS != "" {
359-
return jailerNetNS
360-
}
361-
362-
// If there isn't a jailer netns but there is a network
363-
// interface with CNI configuration, use a default netns path
364-
if m.Cfg.NetworkInterfaces.cniInterface() != nil {
365-
return filepath.Join(defaultNetNSDir, m.Cfg.VMID)
366-
}
367-
368-
// else, just don't use a netns for the VM
369-
return ""
370-
}
371-
372365
func (m *Machine) setupNetwork(ctx context.Context) error {
373-
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)
374367
m.cleanupFuncs = append(m.cleanupFuncs, cleanupFuncs...)
375368
return err
376369
}
@@ -420,19 +413,22 @@ func (m *Machine) attachDrives(ctx context.Context, drives ...models.Drive) erro
420413
return nil
421414
}
422415

416+
func (m *Machine) defaultNetNSPath() string {
417+
return filepath.Join(defaultNetNSDir, m.Cfg.VMID)
418+
}
419+
423420
// startVMM starts the firecracker vmm process and configures logging.
424421
func (m *Machine) startVMM(ctx context.Context) error {
425422
m.logger.Printf("Called startVMM(), setting up a VMM on %s", m.Cfg.SocketPath)
426423

427-
hasNetNS := m.netNSPath() != ""
428-
jailerProvidedNetNS := m.Cfg.JailerCfg.netNSPath() != ""
424+
isDefaultNetNSPath := m.Cfg.NetNS == m.defaultNetNSPath()
429425
startCmd := m.cmd.Start
430426

431427
var err error
432-
if hasNetNS && !jailerProvidedNetNS {
428+
if isDefaultNetNSPath {
433429
// If the VM needs to be started in a netns but no jailer netns was configured,
434430
// start the vmm child process in the netns directly here.
435-
err = ns.WithNetNSPath(m.netNSPath(), func(_ ns.NetNS) error {
431+
err = ns.WithNetNSPath(m.Cfg.NetNS, func(_ ns.NetNS) error {
436432
return startCmd()
437433
})
438434
} else {

0 commit comments

Comments
 (0)