Skip to content

Commit 6022d6d

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 701dc99 commit 6022d6d

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-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: 16 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
@@ -152,6 +156,7 @@ func (cfg *Config) Validate() error {
152156
return nil
153157
}
154158

159+
// ValidateNetwork .
155160
func (cfg *Config) ValidateNetwork() error {
156161
if cfg.DisableValidation {
157162
return nil
@@ -297,6 +302,10 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
297302
m.machineConfig = cfg.MachineCfg
298303
m.Cfg = cfg
299304

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

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-
373366
func (m *Machine) setupNetwork(ctx context.Context) error {
374-
err, cleanupFuncs := m.Cfg.NetworkInterfaces.setupNetwork(ctx, m.Cfg.VMID, m.netNSPath(), m.logger)
367+
err, cleanupFuncs := m.Cfg.NetworkInterfaces.setupNetwork(ctx, m.Cfg.VMID, m.Cfg.NetNS, m.logger)
375368
m.cleanupFuncs = append(m.cleanupFuncs, cleanupFuncs...)
376369
return err
377370
}
@@ -421,19 +414,20 @@ func (m *Machine) attachDrives(ctx context.Context, drives ...models.Drive) erro
421414
return nil
422415
}
423416

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

432426
var err error
433-
if hasNetNS && !jailerProvidedNetNS {
427+
if m.Cfg.NetNS != "" && m.Cfg.JailerCfg == nil {
434428
// If the VM needs to be started in a netns but no jailer netns was configured,
435429
// start the vmm child process in the netns directly here.
436-
err = ns.WithNetNSPath(m.netNSPath(), func(_ ns.NetNS) error {
430+
err = ns.WithNetNSPath(m.Cfg.NetNS, func(_ ns.NetNS) error {
437431
return startCmd()
438432
})
439433
} else {

0 commit comments

Comments
 (0)