exec.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2016 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 remotecommand
  14. import (
  15. "fmt"
  16. "io"
  17. "net/http"
  18. "time"
  19. "k8s.io/kubernetes/pkg/api"
  20. apierrors "k8s.io/kubernetes/pkg/api/errors"
  21. "k8s.io/kubernetes/pkg/api/unversioned"
  22. "k8s.io/kubernetes/pkg/types"
  23. utilexec "k8s.io/kubernetes/pkg/util/exec"
  24. "k8s.io/kubernetes/pkg/util/runtime"
  25. "k8s.io/kubernetes/pkg/util/term"
  26. )
  27. const (
  28. NonZeroExitCodeReason = unversioned.StatusReason("NonZeroExitCode")
  29. ExitCodeCauseType = unversioned.CauseType("ExitCode")
  30. )
  31. // Executor knows how to execute a command in a container in a pod.
  32. type Executor interface {
  33. // ExecInContainer executes a command in a container in the pod, copying data
  34. // between in/out/err and the container's stdin/stdout/stderr.
  35. ExecInContainer(name string, uid types.UID, container string, cmd []string, in io.Reader, out, err io.WriteCloser, tty bool, resize <-chan term.Size) error
  36. }
  37. // ServeExec handles requests to execute a command in a container. After
  38. // creating/receiving the required streams, it delegates the actual execution
  39. // to the executor.
  40. func ServeExec(w http.ResponseWriter, req *http.Request, executor Executor, podName string, uid types.UID, container string, idleTimeout, streamCreationTimeout time.Duration, supportedProtocols []string) {
  41. ctx, ok := createStreams(req, w, supportedProtocols, idleTimeout, streamCreationTimeout)
  42. if !ok {
  43. // error is handled by createStreams
  44. return
  45. }
  46. defer ctx.conn.Close()
  47. cmd := req.URL.Query()[api.ExecCommandParamm]
  48. err := executor.ExecInContainer(podName, uid, container, cmd, ctx.stdinStream, ctx.stdoutStream, ctx.stderrStream, ctx.tty, ctx.resizeChan)
  49. if err != nil {
  50. if exitErr, ok := err.(utilexec.ExitError); ok && exitErr.Exited() {
  51. rc := exitErr.ExitStatus()
  52. ctx.writeStatus(&apierrors.StatusError{ErrStatus: unversioned.Status{
  53. Status: unversioned.StatusFailure,
  54. Reason: NonZeroExitCodeReason,
  55. Details: &unversioned.StatusDetails{
  56. Causes: []unversioned.StatusCause{
  57. {
  58. Type: ExitCodeCauseType,
  59. Message: fmt.Sprintf("%d", rc),
  60. },
  61. },
  62. },
  63. Message: fmt.Sprintf("command terminated with non-zero exit code: %v", exitErr),
  64. }})
  65. } else {
  66. err = fmt.Errorf("error executing command in container: %v", err)
  67. runtime.HandleError(err)
  68. ctx.writeStatus(apierrors.NewInternalError(err))
  69. }
  70. } else {
  71. ctx.writeStatus(&apierrors.StatusError{ErrStatus: unversioned.Status{
  72. Status: unversioned.StatusSuccess,
  73. }})
  74. }
  75. }