forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netns.go
106 lines (90 loc) · 3 KB
/
netns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2019 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package netns
import (
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/vishvananda/netlink"
)
// RemoveIfFromNetNSIfExists removes the given interface from the given
// network namespace.
//
// If the interface does not exist, no error will be returned.
func RemoveIfFromNetNSIfExists(netNS ns.NetNS, ifName string) error {
return netNS.Do(func(_ ns.NetNS) error {
l, err := netlink.LinkByName(ifName)
if err != nil {
if strings.Contains(err.Error(), "Link not found") {
return nil
}
return err
}
return netlink.LinkDel(l)
})
}
// ReplaceNetNSWithName creates a network namespace with the given name.
// If such netns already exists from a previous run, it will be removed
// and then re-created to avoid any previous state of the netns.
//
// FIXME: replace "ip-netns" invocations with native Go code
func ReplaceNetNSWithName(netNSName string) (ns.NetNS, error) {
path := netNSPath(netNSName)
if err := ns.IsNSorErr(path); err == nil {
if err := exec.Command("ip", "netns", "del", netNSName).Run(); err != nil {
return nil, fmt.Errorf("Unable to delete named netns %s: %s", netNSName, err)
}
}
if err := exec.Command("ip", "netns", "add", netNSName).Run(); err != nil {
return nil, fmt.Errorf("Unable to create named netns %s: %s", netNSName, err)
}
ns, err := ns.GetNS(path)
if err != nil {
return nil, fmt.Errorf("Unable to open netns %s: %s", path, err)
}
return ns, nil
}
// RemoveNetNSWithName removes the given named network namespace.
//
// FIXME: replace "ip-netns" invocations with native Go code
func RemoveNetNSWithName(netNSName string) error {
if out, err := exec.Command("ip", "netns", "del", netNSName).CombinedOutput(); err != nil {
return fmt.Errorf("Unable to delete named netns %s: %s %s", netNSName, out, err)
}
return nil
}
// ListNamedNetNSWithPrefix returns list of named network namespaces which name
// starts with the given prefix.
func ListNamedNetNSWithPrefix(prefix string) ([]string, error) {
entries, err := ioutil.ReadDir(netNSRootDir())
if err != nil {
return nil, err
}
ns := make([]string, 0)
for _, e := range entries {
if !e.IsDir() && strings.HasPrefix(e.Name(), prefix) {
ns = append(ns, e.Name())
}
}
return ns, nil
}
func netNSPath(name string) string {
return filepath.Join(netNSRootDir(), name)
}
func netNSRootDir() string {
return filepath.Join("/", "var", "run", "netns")
}