exec.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package dockertools
  14. import (
  15. "fmt"
  16. "io"
  17. "os"
  18. "os/exec"
  19. "time"
  20. dockertypes "github.com/docker/engine-api/types"
  21. "github.com/golang/glog"
  22. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  23. utilexec "k8s.io/kubernetes/pkg/util/exec"
  24. "k8s.io/kubernetes/pkg/util/term"
  25. )
  26. // ExecHandler knows how to execute a command in a running Docker container.
  27. type ExecHandler interface {
  28. ExecInContainer(client DockerInterface, container *dockertypes.ContainerJSON, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan term.Size) error
  29. }
  30. // NsenterExecHandler executes commands in Docker containers using nsenter.
  31. type NsenterExecHandler struct{}
  32. // TODO should we support nsenter in a container, running with elevated privs and --pid=host?
  33. func (*NsenterExecHandler) ExecInContainer(client DockerInterface, container *dockertypes.ContainerJSON, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan term.Size) error {
  34. nsenter, err := exec.LookPath("nsenter")
  35. if err != nil {
  36. return fmt.Errorf("exec unavailable - unable to locate nsenter")
  37. }
  38. containerPid := container.State.Pid
  39. // TODO what if the container doesn't have `env`???
  40. args := []string{"-t", fmt.Sprintf("%d", containerPid), "-m", "-i", "-u", "-n", "-p", "--", "env", "-i"}
  41. args = append(args, fmt.Sprintf("HOSTNAME=%s", container.Config.Hostname))
  42. args = append(args, container.Config.Env...)
  43. args = append(args, cmd...)
  44. command := exec.Command(nsenter, args...)
  45. if tty {
  46. p, err := kubecontainer.StartPty(command)
  47. if err != nil {
  48. return err
  49. }
  50. defer p.Close()
  51. // make sure to close the stdout stream
  52. defer stdout.Close()
  53. kubecontainer.HandleResizing(resize, func(size term.Size) {
  54. term.SetSize(p.Fd(), size)
  55. })
  56. if stdin != nil {
  57. go io.Copy(p, stdin)
  58. }
  59. if stdout != nil {
  60. go io.Copy(stdout, p)
  61. }
  62. err = command.Wait()
  63. } else {
  64. if stdin != nil {
  65. // Use an os.Pipe here as it returns true *os.File objects.
  66. // This way, if you run 'kubectl exec <pod> -i bash' (no tty) and type 'exit',
  67. // the call below to command.Run() can unblock because its Stdin is the read half
  68. // of the pipe.
  69. r, w, err := os.Pipe()
  70. if err != nil {
  71. return err
  72. }
  73. go io.Copy(w, stdin)
  74. command.Stdin = r
  75. }
  76. if stdout != nil {
  77. command.Stdout = stdout
  78. }
  79. if stderr != nil {
  80. command.Stderr = stderr
  81. }
  82. err = command.Run()
  83. }
  84. if exitErr, ok := err.(*exec.ExitError); ok {
  85. return &utilexec.ExitErrorWrapper{ExitError: exitErr}
  86. }
  87. return err
  88. }
  89. // NativeExecHandler executes commands in Docker containers using Docker's exec API.
  90. type NativeExecHandler struct{}
  91. func (*NativeExecHandler) ExecInContainer(client DockerInterface, container *dockertypes.ContainerJSON, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan term.Size) error {
  92. createOpts := dockertypes.ExecConfig{
  93. Cmd: cmd,
  94. AttachStdin: stdin != nil,
  95. AttachStdout: stdout != nil,
  96. AttachStderr: stderr != nil,
  97. Tty: tty,
  98. }
  99. execObj, err := client.CreateExec(container.ID, createOpts)
  100. if err != nil {
  101. return fmt.Errorf("failed to exec in container - Exec setup failed - %v", err)
  102. }
  103. // Have to start this before the call to client.StartExec because client.StartExec is a blocking
  104. // call :-( Otherwise, resize events don't get processed and the terminal never resizes.
  105. kubecontainer.HandleResizing(resize, func(size term.Size) {
  106. client.ResizeExecTTY(execObj.ID, int(size.Height), int(size.Width))
  107. })
  108. startOpts := dockertypes.ExecStartCheck{Detach: false, Tty: tty}
  109. streamOpts := StreamOptions{
  110. InputStream: stdin,
  111. OutputStream: stdout,
  112. ErrorStream: stderr,
  113. RawTerminal: tty,
  114. }
  115. err = client.StartExec(execObj.ID, startOpts, streamOpts)
  116. if err != nil {
  117. return err
  118. }
  119. ticker := time.NewTicker(2 * time.Second)
  120. defer ticker.Stop()
  121. count := 0
  122. for {
  123. inspect, err2 := client.InspectExec(execObj.ID)
  124. if err2 != nil {
  125. return err2
  126. }
  127. if !inspect.Running {
  128. if inspect.ExitCode != 0 {
  129. err = &dockerExitError{inspect}
  130. }
  131. break
  132. }
  133. count++
  134. if count == 5 {
  135. glog.Errorf("Exec session %s in container %s terminated but process still running!", execObj.ID, container.ID)
  136. break
  137. }
  138. <-ticker.C
  139. }
  140. return err
  141. }