Merge branch 'feature-91'
All checks were successful
Documentation / publish (push) Successful in 1m41s
All checks were successful
Documentation / publish (push) Successful in 1m41s
This commit is contained in:
commit
29a96409ed
5 changed files with 257 additions and 59 deletions
|
|
@ -5,22 +5,11 @@ 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 {
|
||||
|
|
@ -28,17 +17,7 @@ func call(name string, fn func() error) error {
|
|||
}
|
||||
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
|
||||
return onThread(func() error {
|
||||
return unix.Setns(int(f.Fd()), unix.CLONE_NEWNET)
|
||||
}, fn)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,15 +5,11 @@ package netns
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func create(name string) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
base := "/var/run/netns"
|
||||
path := base + "/" + name
|
||||
|
||||
|
|
@ -34,36 +30,20 @@ func create(name string) error {
|
|||
}
|
||||
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 du thread courant vers /var/run/netns/<name>
|
||||
// /proc/self/ns/net pointe vers le ns du processus (thread principal),
|
||||
// pas du thread courant — il faut utiliser le tid explicitement
|
||||
threadNsPath := fmt.Sprintf("/proc/self/task/%d/ns/net", unix.Gettid())
|
||||
if err := unix.Mount(
|
||||
threadNsPath,
|
||||
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
|
||||
return onThread(func() error {
|
||||
return unix.Unshare(unix.CLONE_NEWNET)
|
||||
}, func() error {
|
||||
// bind mount du netns du thread courant vers /var/run/netns/<name>
|
||||
// /proc/self/ns/net pointe vers le ns du processus (thread principal),
|
||||
// pas du thread courant — il faut utiliser le tid explicitement
|
||||
threadNsPath := fmt.Sprintf("/proc/self/task/%d/ns/net", unix.Gettid())
|
||||
return unix.Mount(
|
||||
threadNsPath,
|
||||
path,
|
||||
"",
|
||||
unix.MS_BIND,
|
||||
"",
|
||||
)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
33
internal/netns/thread_linux.go
Normal file
33
internal/netns/thread_linux.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
//go:build linux
|
||||
|
||||
package netns
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func onThread(enter func() error, fn func() error) error {
|
||||
runtime.LockOSThread()
|
||||
|
||||
orig, err := os.Open("/proc/thread-self/ns/net")
|
||||
if err != nil {
|
||||
runtime.UnlockOSThread()
|
||||
return err
|
||||
}
|
||||
defer orig.Close()
|
||||
|
||||
err = enter()
|
||||
if err == nil {
|
||||
err = fn()
|
||||
}
|
||||
|
||||
if restoreErr := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); restoreErr != nil {
|
||||
return restoreErr
|
||||
}
|
||||
|
||||
runtime.UnlockOSThread()
|
||||
return err
|
||||
}
|
||||
199
internal/netns/thread_linux_test.go
Normal file
199
internal/netns/thread_linux_test.go
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
//go:build linux
|
||||
|
||||
package netns
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func init() {
|
||||
runtime.LockOSThread()
|
||||
}
|
||||
|
||||
var onMain = make(chan func())
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
code := make(chan int)
|
||||
go func() { code <- m.Run() }()
|
||||
for {
|
||||
select {
|
||||
case f := <-onMain:
|
||||
f()
|
||||
case c := <-code:
|
||||
os.Exit(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runOnMain(f func()) {
|
||||
done := make(chan struct{})
|
||||
onMain <- func() {
|
||||
f()
|
||||
close(done)
|
||||
}
|
||||
<-done
|
||||
}
|
||||
|
||||
type nsID struct{ dev, ino uint64 }
|
||||
|
||||
func statNS(t *testing.T, path string) nsID {
|
||||
t.Helper()
|
||||
var st unix.Stat_t
|
||||
if err := unix.Stat(path, &st); err != nil {
|
||||
t.Fatalf("stat %s : %v", path, err)
|
||||
}
|
||||
return nsID{uint64(st.Dev), st.Ino}
|
||||
}
|
||||
|
||||
func threadNS(t *testing.T) nsID {
|
||||
t.Helper()
|
||||
return statNS(t, "/proc/thread-self/ns/net")
|
||||
}
|
||||
|
||||
func setupMainInOtherNS(t *testing.T) (nsID, string) {
|
||||
t.Helper()
|
||||
if os.Geteuid() != 0 {
|
||||
t.Skip("netns : root requis")
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
root := threadNS(t)
|
||||
rootFD, err := os.Open("/proc/thread-self/ns/net")
|
||||
if err != nil {
|
||||
t.Fatalf("netns racine : %v", err)
|
||||
}
|
||||
runtime.UnlockOSThread()
|
||||
|
||||
other := fmt.Sprintf("t91-main-%d", os.Getpid())
|
||||
if err := Create(other); err != nil {
|
||||
t.Fatalf("création de %s : %v", other, err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := Delete(other); err != nil {
|
||||
t.Errorf("suppression de %s : %v", other, err)
|
||||
}
|
||||
})
|
||||
|
||||
var setErr error
|
||||
runOnMain(func() {
|
||||
f, err := os.Open("/var/run/netns/" + other)
|
||||
if err != nil {
|
||||
setErr = err
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
setErr = unix.Setns(int(f.Fd()), unix.CLONE_NEWNET)
|
||||
})
|
||||
if setErr != nil {
|
||||
t.Fatalf("thread principal dans %s : %v", other, setErr)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
var err error
|
||||
runOnMain(func() { err = unix.Setns(int(rootFD.Fd()), unix.CLONE_NEWNET) })
|
||||
rootFD.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("thread principal ramené au netns racine : %v", err)
|
||||
}
|
||||
})
|
||||
return root, other
|
||||
}
|
||||
|
||||
func assertBackToRoot(t *testing.T, root nsID) {
|
||||
t.Helper()
|
||||
if got := threadNS(t); got != root {
|
||||
t.Fatalf("thread laissé dans le netns %v, netns racine %v attendu", got, root)
|
||||
}
|
||||
runtime.UnlockOSThread()
|
||||
}
|
||||
|
||||
func TestCall_ThreadPrincipalDansUnAutreNetns(t *testing.T) {
|
||||
root, other := setupMainInOtherNS(t)
|
||||
target := fmt.Sprintf("t91-cible-%d", os.Getpid())
|
||||
if err := Create(target); err != nil {
|
||||
t.Fatalf("création de %s : %v", target, err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := Delete(target); err != nil {
|
||||
t.Errorf("suppression de %s : %v", target, err)
|
||||
}
|
||||
})
|
||||
|
||||
runtime.LockOSThread()
|
||||
var inside nsID
|
||||
if err := Call(target, func() error {
|
||||
inside = threadNS(t)
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("Call : %v", err)
|
||||
}
|
||||
if want := statNS(t, "/var/run/netns/"+target); inside != want {
|
||||
t.Errorf("fn exécutée dans %v, netns %s %v attendu", inside, target, want)
|
||||
}
|
||||
if inside == statNS(t, "/var/run/netns/"+other) {
|
||||
t.Errorf("fn exécutée dans le netns du thread principal")
|
||||
}
|
||||
assertBackToRoot(t, root)
|
||||
}
|
||||
|
||||
func TestCall_ErreurDeFnRenvoyeeEtThreadRestaure(t *testing.T) {
|
||||
root, _ := setupMainInOtherNS(t)
|
||||
target := fmt.Sprintf("t91-err-%d", os.Getpid())
|
||||
if err := Create(target); err != nil {
|
||||
t.Fatalf("création de %s : %v", target, err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := Delete(target); err != nil {
|
||||
t.Errorf("suppression de %s : %v", target, err)
|
||||
}
|
||||
})
|
||||
|
||||
runtime.LockOSThread()
|
||||
want := errors.New("échec de fn")
|
||||
if err := Call(target, func() error { return want }); !errors.Is(err, want) {
|
||||
t.Errorf("Call : %v, %v attendu", err, want)
|
||||
}
|
||||
assertBackToRoot(t, root)
|
||||
}
|
||||
|
||||
func TestCall_NetnsAbsent(t *testing.T) {
|
||||
root, _ := setupMainInOtherNS(t)
|
||||
|
||||
runtime.LockOSThread()
|
||||
called := false
|
||||
err := Call(fmt.Sprintf("t91-absent-%d", os.Getpid()), func() error {
|
||||
called = true
|
||||
return nil
|
||||
})
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
t.Errorf("Call : %v, netns absent attendu", err)
|
||||
}
|
||||
if called {
|
||||
t.Errorf("fn appelée sans netns")
|
||||
}
|
||||
assertBackToRoot(t, root)
|
||||
}
|
||||
|
||||
func TestCreate_ThreadPrincipalDansUnAutreNetns(t *testing.T) {
|
||||
root, other := setupMainInOtherNS(t)
|
||||
name := fmt.Sprintf("t91-cree-%d", os.Getpid())
|
||||
|
||||
runtime.LockOSThread()
|
||||
if err := Create(name); err != nil {
|
||||
t.Fatalf("Create : %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := Delete(name); err != nil {
|
||||
t.Errorf("suppression de %s : %v", name, err)
|
||||
}
|
||||
})
|
||||
created := statNS(t, "/var/run/netns/"+name)
|
||||
if created == root || created == statNS(t, "/var/run/netns/"+other) {
|
||||
t.Errorf("netns %s %v : nouveau netns attendu", name, created)
|
||||
}
|
||||
assertBackToRoot(t, root)
|
||||
}
|
||||
|
|
@ -80,6 +80,13 @@
|
|||
- **La suppression d'un VPC échoue si l'un de ses subnets échoue (#79).** Elle attendait qu'aucun
|
||||
subnet ne soit plus en `deleting`, puis supprimait le VPC même si un subnet était passé en
|
||||
`error`. Il en va de même pour un subnet dont une VM attendue passe en `error`
|
||||
- **Un thread de l'agent ne reste plus dans le netns d'un VPC (#91).** Après une opération dans
|
||||
un netns, l'agent revenait au netns du thread principal du processus et non à celui du thread
|
||||
courant. Si le thread principal était lui-même dans un VPC à cet instant, le thread restait dans
|
||||
ce VPC et la goroutine suivante y travaillait : en lab, la suppression d'un subnet a échoué sur
|
||||
`delete vxlan: Link not found` et le watchdog a signalé `br-000000` et des VRF absents. L'agent
|
||||
revient désormais au netns du thread courant, et un thread qui ne peut pas y revenir est
|
||||
détruit au lieu d'être réutilisé. La création d'un netns est corrigée de la même façon
|
||||
|
||||
## Mise à jour depuis la 0.2.0
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue