diff --git a/hack/utils.sh b/hack/utils.sh index b8bff9600..7b1b4b5ef 100755 --- a/hack/utils.sh +++ b/hack/utils.sh @@ -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 diff --git a/pkg/server/sandbox_portforward.go b/pkg/server/sandbox_portforward.go index eb6bed1a7..7106cb673 100644 --- a/pkg/server/sandbox_portforward.go +++ b/pkg/server/sandbox_portforward.go @@ -53,8 +53,23 @@ 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) } - if s.NetNS == nil || s.NetNS.Closed() { - return errors.Errorf("network namespace for sandbox %q is closed", 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") @@ -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)