|
@@ -394,13 +394,10 @@ func (n *networkNamespace) InvokeFunc(f func()) error {
|
|
|
// InitOSContext initializes OS context while configuring network resources
|
|
|
func InitOSContext() func() {
|
|
|
runtime.LockOSThread()
|
|
|
- if err := ns.SetNamespace(); err != nil {
|
|
|
- logrus.Error(err)
|
|
|
- }
|
|
|
return runtime.UnlockOSThread
|
|
|
}
|
|
|
|
|
|
-func nsInvoke(path string, prefunc func(nsFD int) error, postfunc func(callerFD int) error) error {
|
|
|
+func nsInvoke(path string, prefunc, postfunc func(int) error) error {
|
|
|
defer InitOSContext()()
|
|
|
|
|
|
newNs, err := netns.GetFromPath(path)
|
|
@@ -415,10 +412,17 @@ func nsInvoke(path string, prefunc func(nsFD int) error, postfunc func(callerFD
|
|
|
return fmt.Errorf("failed in prefunc: %v", err)
|
|
|
}
|
|
|
|
|
|
+ // save the current namespace (host namespace)
|
|
|
+ curNs, err := netns.Get()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ defer curNs.Close()
|
|
|
if err = netns.Set(newNs); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- defer ns.SetNamespace()
|
|
|
+ // will restore the previous namespace before unlocking the thread
|
|
|
+ defer netns.Set(curNs)
|
|
|
|
|
|
// Invoked after the namespace switch.
|
|
|
return postfunc(ns.ParseHandlerInt())
|