docker_streaming_others.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // +build !windows
  2. /*
  3. Copyright 2019 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package dockershim
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "os/exec"
  20. "strings"
  21. "k8s.io/klog"
  22. )
  23. func (r *streamingRuntime) portForward(podSandboxID string, port int32, stream io.ReadWriteCloser) error {
  24. container, err := r.client.InspectContainer(podSandboxID)
  25. if err != nil {
  26. return err
  27. }
  28. if !container.State.Running {
  29. return fmt.Errorf("container not running (%s)", container.ID)
  30. }
  31. containerPid := container.State.Pid
  32. socatPath, lookupErr := exec.LookPath("socat")
  33. if lookupErr != nil {
  34. return fmt.Errorf("unable to do port forwarding: socat not found")
  35. }
  36. args := []string{"-t", fmt.Sprintf("%d", containerPid), "-n", socatPath, "-", fmt.Sprintf("TCP4:localhost:%d", port)}
  37. nsenterPath, lookupErr := exec.LookPath("nsenter")
  38. if lookupErr != nil {
  39. return fmt.Errorf("unable to do port forwarding: nsenter not found")
  40. }
  41. commandString := fmt.Sprintf("%s %s", nsenterPath, strings.Join(args, " "))
  42. klog.V(4).Infof("executing port forwarding command: %s", commandString)
  43. command := exec.Command(nsenterPath, args...)
  44. command.Stdout = stream
  45. stderr := new(bytes.Buffer)
  46. command.Stderr = stderr
  47. // If we use Stdin, command.Run() won't return until the goroutine that's copying
  48. // from stream finishes. Unfortunately, if you have a client like telnet connected
  49. // via port forwarding, as long as the user's telnet client is connected to the user's
  50. // local listener that port forwarding sets up, the telnet session never exits. This
  51. // means that even if socat has finished running, command.Run() won't ever return
  52. // (because the client still has the connection and stream open).
  53. //
  54. // The work around is to use StdinPipe(), as Wait() (called by Run()) closes the pipe
  55. // when the command (socat) exits.
  56. inPipe, err := command.StdinPipe()
  57. if err != nil {
  58. return fmt.Errorf("unable to do port forwarding: error creating stdin pipe: %v", err)
  59. }
  60. go func() {
  61. io.Copy(inPipe, stream)
  62. inPipe.Close()
  63. }()
  64. if err := command.Run(); err != nil {
  65. return fmt.Errorf("%v: %s", err, stderr.String())
  66. }
  67. return nil
  68. }