exec_pod.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright 2021 The Rook Authors. All rights reserved.
  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. // Heavily inspired by https://github.com/kubernetes/kubernetes/blob/master/test/e2e/framework/exec_util.go
  14. package exec
  15. import (
  16. "bytes"
  17. "context"
  18. "fmt"
  19. "io"
  20. "net/http"
  21. "net/url"
  22. "strconv"
  23. "strings"
  24. "github.com/pkg/errors"
  25. v1 "k8s.io/api/core/v1"
  26. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  27. "k8s.io/client-go/kubernetes"
  28. "k8s.io/client-go/kubernetes/scheme"
  29. "k8s.io/client-go/rest"
  30. "k8s.io/client-go/tools/remotecommand"
  31. )
  32. // ExecOptions passed to ExecWithOptions
  33. type ExecOptions struct {
  34. Command []string
  35. Namespace string
  36. PodName string
  37. ContainerName string
  38. Stdin io.Reader
  39. CaptureStdout bool
  40. CaptureStderr bool
  41. // If false, whitespace in std{err,out} will be removed.
  42. PreserveWhitespace bool
  43. }
  44. // RemotePodCommandExecutor is an exec.Executor that execs every command in a remote container
  45. // This is especially useful when the CephCluster networking type is Multus and when the Operator pod
  46. // does not have the right network annotations.
  47. type RemotePodCommandExecutor struct {
  48. ClientSet kubernetes.Interface
  49. RestClient *rest.Config
  50. }
  51. // ExecWithOptions executes a command in the specified container,
  52. // returning stdout, stderr and error. `options` allowed for
  53. // additional parameters to be passed.
  54. func (e *RemotePodCommandExecutor) ExecWithOptions(ctx context.Context, options ExecOptions) (string, string, error) {
  55. const tty = false
  56. logger.Debugf("ExecWithOptions %+v", options)
  57. req := e.ClientSet.CoreV1().RESTClient().Post().
  58. Resource("pods").
  59. Name(options.PodName).
  60. Namespace(options.Namespace).
  61. SubResource("exec").
  62. Param("container", options.ContainerName)
  63. req.VersionedParams(&v1.PodExecOptions{
  64. Container: options.ContainerName,
  65. Command: options.Command,
  66. Stdin: options.Stdin != nil,
  67. Stdout: options.CaptureStdout,
  68. Stderr: options.CaptureStderr,
  69. TTY: tty,
  70. }, scheme.ParameterCodec)
  71. var stdout, stderr bytes.Buffer
  72. err := execute(ctx, http.MethodPost, req.URL(), e.RestClient, options.Stdin, &stdout, &stderr, tty)
  73. if options.PreserveWhitespace {
  74. return stdout.String(), stderr.String(), err
  75. }
  76. return strings.TrimSpace(stdout.String()), strings.TrimSpace(stderr.String()), err
  77. }
  78. // ExecCommandInContainerWithFullOutput executes a command in the
  79. // specified container and return stdout, stderr and error
  80. func (e *RemotePodCommandExecutor) ExecCommandInContainerWithFullOutput(ctx context.Context, appLabel, containerName, namespace string, cmd ...string) (string, string, error) {
  81. options := metav1.ListOptions{LabelSelector: fmt.Sprintf("app=%s", appLabel)}
  82. pods, err := e.ClientSet.CoreV1().Pods(namespace).List(ctx, options)
  83. if err != nil {
  84. return "", "", err
  85. }
  86. if len(pods.Items) == 0 {
  87. return "", "", errors.Errorf("no pods found with selector %q", appLabel)
  88. }
  89. return e.ExecWithOptions(ctx, ExecOptions{
  90. Command: cmd,
  91. Namespace: namespace,
  92. // Always pick the first pod, it's always 1 unless stretched cluster is enabled
  93. // TODO: if we have 2 pods we could try each result if the command fails to run due to a network partition-related error.
  94. PodName: pods.Items[0].Name,
  95. ContainerName: containerName,
  96. Stdin: nil,
  97. CaptureStdout: true,
  98. CaptureStderr: true,
  99. PreserveWhitespace: false,
  100. })
  101. }
  102. func execute(ctx context.Context, method string, url *url.URL, config *rest.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool) error {
  103. exec, err := remotecommand.NewSPDYExecutor(config, method, url)
  104. if err != nil {
  105. return err
  106. }
  107. return exec.StreamWithContext(ctx, remotecommand.StreamOptions{
  108. Stdin: stdin,
  109. Stdout: stdout,
  110. Stderr: stderr,
  111. Tty: tty,
  112. })
  113. }
  114. func (e *RemotePodCommandExecutor) ExecCommandInContainerWithFullOutputWithTimeout(ctx context.Context, appLabel, containerName, namespace string, cmd ...string) (string, string, error) {
  115. return e.ExecCommandInContainerWithFullOutput(ctx, appLabel, containerName, namespace, append([]string{"timeout", strconv.Itoa(int(CephCommandsTimeout.Seconds()))}, cmd...)...)
  116. }