Skip to content

Commit 2adb2ea

Browse files
authored
Merge pull request #4973 from lorenz/move-netns-into-statedir
Allow moving netns directory into StateDir
2 parents e288fea + 36d0bc1 commit 2adb2ea

6 files changed

Lines changed: 22 additions & 10 deletions

File tree

docs/cri/config.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ version = 2
5959
# ReadOnlyRootFilesystem since containers won't silently mount a temporary volume.
6060
ignore_image_defined_volumes = false
6161

62+
# netns_mounts_under_state_dir places all mounts for network namespaces under StateDir/netns
63+
# instead of being placed under the hardcoded directory /var/run/netns. Changing this setting
64+
# requires that all containers are deleted.
65+
netns_mounts_under_state_dir = false
66+
6267
# 'plugins."io.containerd.grpc.v1.cri".x509_key_pair_streaming' contains a x509 valid key pair to stream with tls.
6368
[plugins."io.containerd.grpc.v1.cri".x509_key_pair_streaming]
6469
# tls_cert_file is the filepath to the certificate paired with the "tls_key_file"

pkg/cri/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ type PluginConfig struct {
253253
// isolation, security and early detection of issues in the mount configuration when using
254254
// ReadOnlyRootFilesystem since containers won't silently mount a temporary volume.
255255
IgnoreImageDefinedVolumes bool `toml:"ignore_image_defined_volumes" json:"ignoreImageDefinedVolumes"`
256+
// NetNSMountsUnderStateDir places all mounts for network namespaces under StateDir/netns instead
257+
// of being placed under the hardcoded directory /var/run/netns. Changing this setting requires
258+
// that all containers are deleted.
259+
NetNSMountsUnderStateDir bool `toml:"netns_mounts_under_state_dir" json:"netnsMountsUnderStateDir"`
256260
}
257261

258262
// X509KeyPairStreaming contains the x509 configuration for streaming

pkg/cri/server/sandbox_run.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package server
1919
import (
2020
"encoding/json"
2121
"math"
22+
"path/filepath"
2223
goruntime "runtime"
2324
"strings"
2425

@@ -120,7 +121,11 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
120121
// handle. NetNSPath in sandbox metadata and NetNS is non empty only for non host network
121122
// namespaces. If the pod is in host network namespace then both are empty and should not
122123
// be used.
123-
sandbox.NetNS, err = netns.NewNetNS()
124+
var netnsMountDir string = "/var/run/netns"
125+
if c.config.NetNSMountsUnderStateDir {
126+
netnsMountDir = filepath.Join(c.config.StateDir, "netns")
127+
}
128+
sandbox.NetNS, err = netns.NewNetNS(netnsMountDir)
124129
if err != nil {
125130
return nil, errors.Wrapf(err, "failed to create network namespace for sandbox %q", id)
126131
}

pkg/netns/netns_linux.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@ import (
4646
"golang.org/x/sys/unix"
4747
)
4848

49-
const nsRunDir = "/var/run/netns"
50-
5149
// Some of the following functions are migrated from
5250
// https://github.com/containernetworking/plugins/blob/master/pkg/testutils/netns_linux.go
5351

5452
// newNS creates a new persistent (bind-mounted) network namespace and returns the
5553
// path to the network namespace.
56-
func newNS() (nsPath string, err error) {
54+
func newNS(baseDir string) (nsPath string, err error) {
5755
b := make([]byte, 16)
5856
if _, err := rand.Reader.Read(b); err != nil {
5957
return "", errors.Wrap(err, "failed to generate random netns name")
@@ -62,13 +60,13 @@ func newNS() (nsPath string, err error) {
6260
// Create the directory for mounting network namespaces
6361
// This needs to be a shared mountpoint in case it is mounted in to
6462
// other namespaces (containers)
65-
if err := os.MkdirAll(nsRunDir, 0755); err != nil {
63+
if err := os.MkdirAll(baseDir, 0755); err != nil {
6664
return "", err
6765
}
6866

6967
// create an empty file at the mount point
7068
nsName := fmt.Sprintf("cni-%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
71-
nsPath = path.Join(nsRunDir, nsName)
69+
nsPath = path.Join(baseDir, nsName)
7270
mountPointFd, err := os.Create(nsPath)
7371
if err != nil {
7472
return "", err
@@ -162,8 +160,8 @@ type NetNS struct {
162160
}
163161

164162
// NewNetNS creates a network namespace.
165-
func NewNetNS() (*NetNS, error) {
166-
path, err := newNS()
163+
func NewNetNS(baseDir string) (*NetNS, error) {
164+
path, err := newNS(baseDir)
167165
if err != nil {
168166
return nil, errors.Wrap(err, "failed to setup netns")
169167
}

pkg/netns/netns_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type NetNS struct {
3030
}
3131

3232
// NewNetNS creates a network namespace.
33-
func NewNetNS() (*NetNS, error) {
33+
func NewNetNS(baseDir string) (*NetNS, error) {
3434
return nil, errNotImplementedOnUnix
3535
}
3636

pkg/netns/netns_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type NetNS struct {
2626
}
2727

2828
// NewNetNS creates a network namespace for the sandbox
29-
func NewNetNS() (*NetNS, error) {
29+
func NewNetNS(baseDir string) (*NetNS, error) {
3030
temp := hcn.HostComputeNamespace{}
3131
hcnNamespace, err := temp.Create()
3232
if err != nil {

0 commit comments

Comments
 (0)