Merge pull request 'feature-10' (#16) from feature-10 into main
Reviewed-on: #16
This commit is contained in:
commit
487972e698
12 changed files with 209 additions and 1 deletions
|
|
@ -32,7 +32,12 @@ jobs:
|
|||
matrix:
|
||||
goos: [linux]
|
||||
goarch: [amd64]
|
||||
binaries: [db, metadata, metacli, agent]
|
||||
binaries:
|
||||
- db
|
||||
- metadata
|
||||
- metacli
|
||||
- agent
|
||||
- netns
|
||||
uses: ./.forgejo/workflows/build.yml
|
||||
with:
|
||||
tag: ${{ needs.set-release-target.outputs.release_cible }}
|
||||
|
|
|
|||
40
cmd/netns/main.go
Normal file
40
cmd/netns/main.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
)
|
||||
|
||||
var (
|
||||
netns_name = flag.String("netns", "", "Network namespace à faire")
|
||||
action = flag.String("action", "", "Action a faire")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
switch *action {
|
||||
case "create":
|
||||
err := netns.Create(*netns_name)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
case "delete":
|
||||
err := netns.Delete(*netns_name)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
case "check":
|
||||
if netns.Exist(*netns_name) {
|
||||
fmt.Printf("netns %s exist\n", *netns_name)
|
||||
} else {
|
||||
fmt.Printf("netns %s do not exist\n", *netns_name)
|
||||
}
|
||||
default:
|
||||
fmt.Printf("Available commande:\n - create\n - delete\n - check\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
5
internal/netns/call.go
Normal file
5
internal/netns/call.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package netns
|
||||
|
||||
func Call(name string, fn func() error) error {
|
||||
return call(name, fn)
|
||||
}
|
||||
44
internal/netns/call_linux.go
Normal file
44
internal/netns/call_linux.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
//go:build linux
|
||||
|
||||
package netns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func call(name string, fn func() error) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
// sauvegarde du netns courant
|
||||
orig, err := os.Open("/proc/self/ns/net")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer orig.Close()
|
||||
|
||||
// entrer dans le netns cible
|
||||
f, err := os.Open(fmt.Sprintf("/var/run/netns/%s", name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if err := unix.Setns(int(f.Fd()), unix.CLONE_NEWNET); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// exécuter la fonction dans le netns
|
||||
err = fn()
|
||||
|
||||
// toujours revenir au netns d'origine
|
||||
if restoreErr := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); restoreErr != nil {
|
||||
return restoreErr
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
7
internal/netns/call_other.go
Normal file
7
internal/netns/call_other.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
//go:build !linux
|
||||
|
||||
package netns
|
||||
|
||||
func call(name string, fn func() error) error {
|
||||
return fn()
|
||||
}
|
||||
5
internal/netns/create.go
Normal file
5
internal/netns/create.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package netns
|
||||
|
||||
func Create(name string) error {
|
||||
return create(name)
|
||||
}
|
||||
55
internal/netns/create_linux.go
Normal file
55
internal/netns/create_linux.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
//go:build linux
|
||||
|
||||
package netns
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func create(name string) error {
|
||||
base := "/var/run/netns"
|
||||
path := base + "/" + name
|
||||
|
||||
if err := os.MkdirAll(base, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// fichier cible
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.Close()
|
||||
|
||||
// sauvegarde du netns courant
|
||||
orig, err := os.Open("/proc/self/ns/net")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer orig.Close()
|
||||
|
||||
// nouveau netns
|
||||
if err := unix.Unshare(unix.CLONE_NEWNET); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// bind mount du netns courant vers /var/run/netns/<name>
|
||||
if err := unix.Mount(
|
||||
"/proc/self/ns/net",
|
||||
path,
|
||||
"",
|
||||
unix.MS_BIND,
|
||||
"",
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// revenir au netns original
|
||||
if err := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
5
internal/netns/create_other.go
Normal file
5
internal/netns/create_other.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
//go:build !linux
|
||||
|
||||
package netns
|
||||
|
||||
func create(string) error { return nil }
|
||||
5
internal/netns/delete.go
Normal file
5
internal/netns/delete.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package netns
|
||||
|
||||
func Delete(name string) error {
|
||||
return delete(name)
|
||||
}
|
||||
18
internal/netns/delete_linux.go
Normal file
18
internal/netns/delete_linux.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
//go:build linux
|
||||
|
||||
package netns
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func delete(name string) error {
|
||||
path := "/var/run/netns/" + name
|
||||
|
||||
if err := unix.Unmount(path, unix.MNT_DETACH); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Remove(path)
|
||||
}
|
||||
5
internal/netns/delete_other.go
Normal file
5
internal/netns/delete_other.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
//go:build !linux
|
||||
|
||||
package netns
|
||||
|
||||
func delete(string) error { return nil }
|
||||
14
internal/netns/exist.go
Normal file
14
internal/netns/exist.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package netns
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func exist(name string) bool {
|
||||
_, err := os.Stat("/var/run/netns/" + name)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func Exist(name string) bool {
|
||||
return exist(name)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue