mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-30 12:44:39 +00:00
runtime: remove same function
Add EnterNetNS in virtcontainers to remove same function. FIXes #5394 Signed-off-by: Rouzip <1226015390@qq.com>
This commit is contained in:
parent
5d17cbeef7
commit
39363ffbfb
@ -11,7 +11,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
goruntime "runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containernetworking/plugins/pkg/ns"
|
"github.com/containernetworking/plugins/pkg/ns"
|
||||||
@ -27,30 +26,7 @@ const procMountInfoFile = "/proc/self/mountinfo"
|
|||||||
// into runtime.LockOSThread(), meaning it won't be executed in a
|
// into runtime.LockOSThread(), meaning it won't be executed in a
|
||||||
// different thread than the one expected by the caller.
|
// different thread than the one expected by the caller.
|
||||||
func EnterNetNS(networkID string, cb func() error) error {
|
func EnterNetNS(networkID string, cb func() error) error {
|
||||||
if networkID == "" {
|
return vc.EnterNetNS(networkID, cb)
|
||||||
return cb()
|
|
||||||
}
|
|
||||||
|
|
||||||
goruntime.LockOSThread()
|
|
||||||
defer goruntime.UnlockOSThread()
|
|
||||||
|
|
||||||
currentNS, err := ns.GetCurrentNS()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer currentNS.Close()
|
|
||||||
|
|
||||||
targetNS, err := ns.GetNS(networkID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := targetNS.Set(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer currentNS.Set()
|
|
||||||
|
|
||||||
return cb()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupNetworkNamespace create a network namespace
|
// SetupNetworkNamespace create a network namespace
|
||||||
|
@ -1045,6 +1045,15 @@ func doNetNS(netNSPath string, cb func(ns.NetNS) error) error {
|
|||||||
return cb(targetNS)
|
return cb(targetNS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnterNetNS is free from any call to a go routine, and it calls
|
||||||
|
// into runtime.LockOSThread(), meaning it won't be executed in a
|
||||||
|
// different thread than the one expected by the caller.
|
||||||
|
func EnterNetNS(networkID string, cb func() error) error {
|
||||||
|
return doNetNS(networkID, func(nn ns.NetNS) error {
|
||||||
|
return cb()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func deleteNetNS(netNSPath string) error {
|
func deleteNetNS(netNSPath string) error {
|
||||||
n, err := ns.GetNS(netNSPath)
|
n, err := ns.GetNS(netNSPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user