exec_util.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 framework
  14. import (
  15. "bytes"
  16. "context"
  17. "io"
  18. "net/url"
  19. "strings"
  20. "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/client-go/kubernetes/scheme"
  23. restclient "k8s.io/client-go/rest"
  24. "k8s.io/client-go/tools/remotecommand"
  25. "github.com/onsi/gomega"
  26. )
  27. // ExecOptions passed to ExecWithOptions
  28. type ExecOptions struct {
  29. Command []string
  30. Namespace string
  31. PodName string
  32. ContainerName string
  33. Stdin io.Reader
  34. CaptureStdout bool
  35. CaptureStderr bool
  36. // If false, whitespace in std{err,out} will be removed.
  37. PreserveWhitespace bool
  38. }
  39. // ExecWithOptions executes a command in the specified container,
  40. // returning stdout, stderr and error. `options` allowed for
  41. // additional parameters to be passed.
  42. func (f *Framework) ExecWithOptions(options ExecOptions) (string, string, error) {
  43. Logf("ExecWithOptions %+v", options)
  44. config, err := LoadConfig()
  45. ExpectNoError(err, "failed to load restclient config")
  46. const tty = false
  47. req := f.ClientSet.CoreV1().RESTClient().Post().
  48. Resource("pods").
  49. Name(options.PodName).
  50. Namespace(options.Namespace).
  51. SubResource("exec").
  52. Param("container", options.ContainerName)
  53. req.VersionedParams(&v1.PodExecOptions{
  54. Container: options.ContainerName,
  55. Command: options.Command,
  56. Stdin: options.Stdin != nil,
  57. Stdout: options.CaptureStdout,
  58. Stderr: options.CaptureStderr,
  59. TTY: tty,
  60. }, scheme.ParameterCodec)
  61. var stdout, stderr bytes.Buffer
  62. err = execute("POST", req.URL(), config, options.Stdin, &stdout, &stderr, tty)
  63. if options.PreserveWhitespace {
  64. return stdout.String(), stderr.String(), err
  65. }
  66. return strings.TrimSpace(stdout.String()), strings.TrimSpace(stderr.String()), err
  67. }
  68. // ExecCommandInContainerWithFullOutput executes a command in the
  69. // specified container and return stdout, stderr and error
  70. func (f *Framework) ExecCommandInContainerWithFullOutput(podName, containerName string, cmd ...string) (string, string, error) {
  71. return f.ExecWithOptions(ExecOptions{
  72. Command: cmd,
  73. Namespace: f.Namespace.Name,
  74. PodName: podName,
  75. ContainerName: containerName,
  76. Stdin: nil,
  77. CaptureStdout: true,
  78. CaptureStderr: true,
  79. PreserveWhitespace: false,
  80. })
  81. }
  82. // ExecCommandInContainer executes a command in the specified container.
  83. func (f *Framework) ExecCommandInContainer(podName, containerName string, cmd ...string) string {
  84. stdout, stderr, err := f.ExecCommandInContainerWithFullOutput(podName, containerName, cmd...)
  85. Logf("Exec stderr: %q", stderr)
  86. ExpectNoError(err,
  87. "failed to execute command in pod %v, container %v: %v",
  88. podName, containerName, err)
  89. return stdout
  90. }
  91. // ExecShellInContainer executes the specified command on the pod's container.
  92. func (f *Framework) ExecShellInContainer(podName, containerName string, cmd string) string {
  93. return f.ExecCommandInContainer(podName, containerName, "/bin/sh", "-c", cmd)
  94. }
  95. func (f *Framework) execCommandInPod(podName string, cmd ...string) string {
  96. pod, err := f.PodClient().Get(context.TODO(), podName, metav1.GetOptions{})
  97. ExpectNoError(err, "failed to get pod %v", podName)
  98. gomega.Expect(pod.Spec.Containers).NotTo(gomega.BeEmpty())
  99. return f.ExecCommandInContainer(podName, pod.Spec.Containers[0].Name, cmd...)
  100. }
  101. func (f *Framework) execCommandInPodWithFullOutput(podName string, cmd ...string) (string, string, error) {
  102. pod, err := f.PodClient().Get(context.TODO(), podName, metav1.GetOptions{})
  103. ExpectNoError(err, "failed to get pod %v", podName)
  104. gomega.Expect(pod.Spec.Containers).NotTo(gomega.BeEmpty())
  105. return f.ExecCommandInContainerWithFullOutput(podName, pod.Spec.Containers[0].Name, cmd...)
  106. }
  107. // ExecShellInPod executes the specified command on the pod.
  108. func (f *Framework) ExecShellInPod(podName string, cmd string) string {
  109. return f.execCommandInPod(podName, "/bin/sh", "-c", cmd)
  110. }
  111. // ExecShellInPodWithFullOutput executes the specified command on the Pod and returns stdout, stderr and error.
  112. func (f *Framework) ExecShellInPodWithFullOutput(podName string, cmd string) (string, string, error) {
  113. return f.execCommandInPodWithFullOutput(podName, "/bin/sh", "-c", cmd)
  114. }
  115. func execute(method string, url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool) error {
  116. exec, err := remotecommand.NewSPDYExecutor(config, method, url)
  117. if err != nil {
  118. return err
  119. }
  120. return exec.Stream(remotecommand.StreamOptions{
  121. Stdin: stdin,
  122. Stdout: stdout,
  123. Stderr: stderr,
  124. Tty: tty,
  125. })
  126. }