# cat operate-netns.go
package main
import (
"fmt"
"log"
"os"
"path"
"runtime"
"github.com/containernetworking/plugins/pkg/ns"
"golang.org/x/sys/unix"
)
const (
bindMountPath = "/run/netns" /* Bind mount path for named netns */
)
// NsHandle is a handle to a network namespace. It can be cast directly
// to an int and used as a file descriptor.
type NsHandle int
// None gets an empty (closed) NsHandle.
func ClosedNs() NsHandle {
return NsHandle(-1)
}
// GetFromPath gets a handle to a network namespace
// identified by the path
func GetNsFromPath(path string) (NsHandle, error) {
fd, err := unix.Open(path, unix.O_RDONLY|unix.O_CLOEXEC, 0)
if err != nil {
return -1, err
}
return NsHandle(fd), nil
}
// GetFromThread gets a handle to the network namespace of a given pid and tid.
func GetNsFromThread(pid, tid int) (NsHandle, error) {
return GetNsFromPath(fmt.Sprintf("/proc/%d/task/%d/ns/net", pid, tid))
}
// Get gets a handle to the current threads network namespace.
func GetNs() (NsHandle, error) {
return GetNsFromThread(os.Getpid(), unix.Gettid())
}
// New creates a new network namespace, sets it as current and returns
// a handle to it.
func newNs() (ns NsHandle, err error) {
if err := unix.Unshare(unix.CLONE_NEWNET); err != nil {
return -1, err
}
return GetNs()
}
// NewNamed creates a new named network namespace, sets it as current,
// and returns a handle to it
func NewNamedNs(name string) (NsHandle, error) {
if _, err := os.Stat(bindMountPath); os.IsNotExist(err) {
err = os.MkdirAll(bindMountPath, 0755)
if err != nil {
return ClosedNs(), err
}
}
newNs, err := newNs()
if err != nil {
return ClosedNs(), err
}
namedPath := path.Join(bindMountPath, name)
f, err := os.OpenFile(namedPath, os.O_CREATE|os.O_EXCL, 0444)
if err != nil {
return ClosedNs(), err
}
f.Close()
nsPath := fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), unix.Gettid())
err = unix.Mount(nsPath, namedPath, "bind", unix.MS_BIND, "")
if err != nil {
return ClosedNs(), err
}
return newNs, nil
}
// DeleteNamed deletes a named network namespace
func DeleteNamedNs(name string) error {
namedPath := path.Join(bindMountPath, name)
err := unix.Unmount(namedPath, unix.MNT_DETACH)
if err != nil {
return err
}
return os.Remove(namedPath)
}
// GetFromName gets a handle to a named network namespace such as one
// created by `ip netns add`.
func GetNsFromName(name string) (NsHandle, error) {
return GetNsFromPath(fmt.Sprintf("/var/run/netns/%s", name))
}
const (
NSName = "ovnext0"
NsPath = "/var/run/netns/ovnext0"
)
func Error(e error) {
if e != nil {
log.Fatalln(e)
}
}
func SetupNetNamespace() *NsHandle {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
log.Println("SetupNetNamespace...running")
_, err := GetNsFromName(NSName)
if err == nil {
log.Printf("%s net ns is exists. Delete netns %s\n", NSName, NSName)
}
newns, err := NewNamedNs(NSName)
Error(err)
log.Println("SetupNetNamespace...done")
podNS, err := ns.GetNS(NsPath)
if err != nil {
log.Println("SetupNetNamespace...done")
Error(err)
}
log.Println("PodNs: ", podNS)
return &newns
}
func main() {
ns := SetupNetNamespace()
log.Println("added ns: ", ns)
}
golang create netns
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- golang web develop always using tree menu in our project....
- 解决办法来源:https://github.com/tealeg/xlsx/issues/365
- 本节简单介绍了创建执行计划中的create_plan->create_plan_recurse->create_p...
- 问题 解决方案 我们可以google找到相应的解决方案Solution 创建docker-machine 新的问题...
- 区别: create table select 会将原表中的数据完整复制一份,但表结构中的索引会丢失。 creat...