Merge pull request #739 from Random-Liu/fix-hostnet-port-forward
Fix portforward for host network.
This commit is contained in:
commit
290eec8e34
@ -17,7 +17,7 @@
|
||||
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
|
||||
|
||||
# Not from vendor.conf.
|
||||
CRITOOL_VERSION=f6ed14e642ed2d514501afea7b5ac3d07f3a4150
|
||||
CRITOOL_VERSION=db53d78569a8116fff1f60366a8de3130e767eeb
|
||||
CRITOOL_PKG=github.com/kubernetes-incubator/cri-tools
|
||||
CRITOOL_REPO=github.com/kubernetes-incubator/cri-tools
|
||||
|
||||
|
@ -53,9 +53,24 @@ func (c *criService) portForward(id string, port int32, stream io.ReadWriteClose
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to find sandbox %q in store", id)
|
||||
}
|
||||
var netNSDo func(func(ns.NetNS) error) error
|
||||
// netNSPath is the network namespace path for logging.
|
||||
var netNSPath string
|
||||
securityContext := s.Config.GetLinux().GetSecurityContext()
|
||||
hostNet := securityContext.GetNamespaceOptions().GetNetwork() == runtime.NamespaceMode_NODE
|
||||
if !hostNet {
|
||||
if s.NetNS == nil || s.NetNS.Closed() {
|
||||
return errors.Errorf("network namespace for sandbox %q is closed", id)
|
||||
}
|
||||
netNSDo = s.NetNS.GetNs().Do
|
||||
netNSPath = s.NetNS.GetPath()
|
||||
} else {
|
||||
// Run the function directly for host network.
|
||||
netNSDo = func(do func(_ ns.NetNS) error) error {
|
||||
return do(nil)
|
||||
}
|
||||
netNSPath = "host"
|
||||
}
|
||||
|
||||
socat, err := exec.LookPath("socat")
|
||||
if err != nil {
|
||||
@ -65,8 +80,8 @@ func (c *criService) portForward(id string, port int32, stream io.ReadWriteClose
|
||||
// Check https://linux.die.net/man/1/socat for meaning of the options.
|
||||
args := []string{socat, "-", fmt.Sprintf("TCP4:localhost:%d", port)}
|
||||
|
||||
logrus.Infof("Executing port forwarding command %q in network namespace %q", strings.Join(args, " "), s.NetNS.GetPath())
|
||||
err = s.NetNS.GetNs().Do(func(_ ns.NetNS) error {
|
||||
logrus.Infof("Executing port forwarding command %q in network namespace %q", strings.Join(args, " "), netNSPath)
|
||||
err = netNSDo(func(_ ns.NetNS) error {
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Stdout = stream
|
||||
|
||||
@ -95,12 +110,12 @@ func (c *criService) portForward(id string, port int32, stream io.ReadWriteClose
|
||||
}()
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return errors.Errorf("nsenter command returns error: %v, stderr: %q", err, stderr.String())
|
||||
return errors.Errorf("socat command returns error: %v, stderr: %q", err, stderr.String())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to execute portforward in network namespace %s", s.NetNS.GetPath())
|
||||
return errors.Wrapf(err, "failed to execute portforward in network namespace %q", netNSPath)
|
||||
}
|
||||
logrus.Infof("Finish port forwarding for %q port %d", id, port)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user