Lorenz Brun | 74e8e5c | 2021-01-26 14:00:50 +0100 | [diff] [blame^] | 1 | Copyright 2020 The Monogon Project Authors. |
| 2 | |
| 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | you may not use this file except in compliance with the License. |
| 5 | You may obtain a copy of the License at |
| 6 | |
| 7 | http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | |
| 9 | Unless required by applicable law or agreed to in writing, software |
| 10 | distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | See the License for the specific language governing permissions and |
| 13 | limitations under the License. |
| 14 | |
| 15 | |
| 16 | From 3e7a8cebf9d40487adc7d4a22b5c628add5e7eac Mon Sep 17 00:00:00 2001 |
| 17 | From: Lorenz Brun <lorenz@nexantic.com> |
| 18 | Date: Wed, 27 Jan 2021 13:05:30 +0100 |
| 19 | Subject: [PATCH] Move netns directory into StateDir |
| 20 | |
| 21 | --- |
| 22 | pkg/netns/netns_unix.go | 12 +++++------- |
| 23 | pkg/server/sandbox_run.go | 3 ++- |
| 24 | 2 files changed, 7 insertions(+), 8 deletions(-) |
| 25 | |
| 26 | diff --git a/pkg/netns/netns_unix.go b/pkg/netns/netns_unix.go |
| 27 | index 7449e235..b31716cb 100644 |
| 28 | --- a/pkg/netns/netns_unix.go |
| 29 | +++ b/pkg/netns/netns_unix.go |
| 30 | @@ -48,14 +48,12 @@ import ( |
| 31 | osinterface "github.com/containerd/cri/pkg/os" |
| 32 | ) |
| 33 | |
| 34 | -const nsRunDir = "/var/run/netns" |
| 35 | - |
| 36 | // Some of the following functions are migrated from |
| 37 | // https://github.com/containernetworking/plugins/blob/master/pkg/testutils/netns_linux.go |
| 38 | |
| 39 | // newNS creates a new persistent (bind-mounted) network namespace and returns the |
| 40 | // path to the network namespace. |
| 41 | -func newNS() (nsPath string, err error) { |
| 42 | +func newNS(baseDir string) (nsPath string, err error) { |
| 43 | b := make([]byte, 16) |
| 44 | if _, err := rand.Reader.Read(b); err != nil { |
| 45 | return "", errors.Wrap(err, "failed to generate random netns name") |
| 46 | @@ -64,13 +62,13 @@ func newNS() (nsPath string, err error) { |
| 47 | // Create the directory for mounting network namespaces |
| 48 | // This needs to be a shared mountpoint in case it is mounted in to |
| 49 | // other namespaces (containers) |
| 50 | - if err := os.MkdirAll(nsRunDir, 0755); err != nil { |
| 51 | + if err := os.MkdirAll(baseDir, 0755); err != nil { |
| 52 | return "", err |
| 53 | } |
| 54 | |
| 55 | // create an empty file at the mount point |
| 56 | nsName := fmt.Sprintf("cni-%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]) |
| 57 | - nsPath = path.Join(nsRunDir, nsName) |
| 58 | + nsPath = path.Join(baseDir, nsName) |
| 59 | mountPointFd, err := os.Create(nsPath) |
| 60 | if err != nil { |
| 61 | return "", err |
| 62 | @@ -164,8 +162,8 @@ type NetNS struct { |
| 63 | } |
| 64 | |
| 65 | // NewNetNS creates a network namespace. |
| 66 | -func NewNetNS() (*NetNS, error) { |
| 67 | - path, err := newNS() |
| 68 | +func NewNetNS(baseDir string) (*NetNS, error) { |
| 69 | + path, err := newNS(baseDir) |
| 70 | if err != nil { |
| 71 | return nil, errors.Wrap(err, "failed to setup netns") |
| 72 | } |
| 73 | diff --git a/pkg/server/sandbox_run.go b/pkg/server/sandbox_run.go |
| 74 | index dd4c51e3..32a2d6e8 100644 |
| 75 | --- a/pkg/server/sandbox_run.go |
| 76 | +++ b/pkg/server/sandbox_run.go |
| 77 | @@ -19,6 +19,7 @@ package server |
| 78 | import ( |
| 79 | "encoding/json" |
| 80 | "math" |
| 81 | + "path/filepath" |
| 82 | goruntime "runtime" |
| 83 | "strings" |
| 84 | |
| 85 | @@ -117,7 +118,7 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox |
| 86 | // handle. NetNSPath in sandbox metadata and NetNS is non empty only for non host network |
| 87 | // namespaces. If the pod is in host network namespace then both are empty and should not |
| 88 | // be used. |
| 89 | - sandbox.NetNS, err = netns.NewNetNS() |
| 90 | + sandbox.NetNS, err = netns.NewNetNS(filepath.Join(c.config.StateDir, "netns")) |
| 91 | if err != nil { |
| 92 | return nil, errors.Wrapf(err, "failed to create network namespace for sandbox %q", id) |
| 93 | } |
| 94 | -- |
| 95 | 2.25.1 |
| 96 | |