|
| 1 | +package exec |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/go-logr/logr" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + "k8s.io/client-go/kubernetes/scheme" |
| 12 | + "k8s.io/client-go/rest" |
| 13 | + "k8s.io/client-go/tools/remotecommand" |
| 14 | +) |
| 15 | + |
| 16 | +// IExec is an injectable interface for running remote exec commands. |
| 17 | +type IExec interface { |
| 18 | + // ExecCommandInPodSet exec cmd in pod set. |
| 19 | + ExecCommandInPodSet(podSet []*corev1.Pod, cmd ...string) error |
| 20 | +} |
| 21 | + |
| 22 | +type remoteExec struct { |
| 23 | + restGVKClient rest.Interface |
| 24 | + logger logr.Logger |
| 25 | + config *rest.Config |
| 26 | +} |
| 27 | + |
| 28 | +// NewRemoteExec returns a new IExec which will exec remote cmd. |
| 29 | +func NewRemoteExec(restGVKClient rest.Interface, config *rest.Config, logger logr.Logger) IExec { |
| 30 | + return &remoteExec{ |
| 31 | + restGVKClient: restGVKClient, |
| 32 | + logger: logger, |
| 33 | + config: config, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// ExecOptions passed to ExecWithOptions. |
| 38 | +type ExecOptions struct { |
| 39 | + Command []string |
| 40 | + |
| 41 | + Namespace string |
| 42 | + PodName string |
| 43 | + ContainerName string |
| 44 | + |
| 45 | + Stdin io.Reader |
| 46 | + CaptureStdout bool |
| 47 | + CaptureStderr bool |
| 48 | + // If false, whitespace in std{err,out} will be removed. |
| 49 | + PreserveWhitespace bool |
| 50 | +} |
| 51 | + |
| 52 | +// ExecCommandInPodSet implements IExec interface. |
| 53 | +func (e *remoteExec) ExecCommandInPodSet(podSet []*corev1.Pod, cmd ...string) error { |
| 54 | + for _, pod := range podSet { |
| 55 | + if _, err := e.ExecCommandInContainer(pod.Namespace, pod.Name, pod.Spec.Containers[0].Name, cmd...); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// ExecCommandInContainer executes a command in the specified container. |
| 63 | +func (e *remoteExec) ExecCommandInContainer(namespace, podName, containerName string, cmd ...string) (string, error) { |
| 64 | + stdout, stderr, err := e.ExecCommandInContainerWithFullOutput(namespace, podName, containerName, cmd...) |
| 65 | + if stderr != "" { |
| 66 | + e.logger.Info("ExecCommand", "command", cmd, "stderr", stderr) |
| 67 | + } |
| 68 | + return stdout, err |
| 69 | +} |
| 70 | + |
| 71 | +// ExecCommandInContainerWithFullOutput executes a command in the |
| 72 | +// specified container and return stdout, stderr and error |
| 73 | +func (e *remoteExec) ExecCommandInContainerWithFullOutput(namespace, podName, containerName string, cmd ...string) (string, string, error) { |
| 74 | + return e.ExecWithOptions(ExecOptions{ |
| 75 | + Command: cmd, |
| 76 | + Namespace: namespace, |
| 77 | + PodName: podName, |
| 78 | + ContainerName: containerName, |
| 79 | + |
| 80 | + Stdin: nil, |
| 81 | + CaptureStdout: true, |
| 82 | + CaptureStderr: true, |
| 83 | + PreserveWhitespace: false, |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +// ExecWithOptions executes a command in the specified container, |
| 88 | +// returning stdout, stderr and error. `options` allowed for |
| 89 | +// additional parameters to be passed. |
| 90 | +func (e *remoteExec) ExecWithOptions(options ExecOptions) (string, string, error) { |
| 91 | + const tty = false |
| 92 | + |
| 93 | + req := e.restGVKClient.Post(). |
| 94 | + Resource("pods"). |
| 95 | + Name(options.PodName). |
| 96 | + Namespace(options.Namespace). |
| 97 | + SubResource("exec"). |
| 98 | + Param("container", options.ContainerName) |
| 99 | + |
| 100 | + req.VersionedParams(&corev1.PodExecOptions{ |
| 101 | + Container: options.ContainerName, |
| 102 | + Command: options.Command, |
| 103 | + Stdin: options.Stdin != nil, |
| 104 | + Stdout: options.CaptureStdout, |
| 105 | + Stderr: options.CaptureStderr, |
| 106 | + TTY: tty, |
| 107 | + }, scheme.ParameterCodec) |
| 108 | + |
| 109 | + var stdout, stderr bytes.Buffer |
| 110 | + err := execute("POST", req.URL(), e.config, options.Stdin, &stdout, &stderr, tty) |
| 111 | + |
| 112 | + if options.PreserveWhitespace { |
| 113 | + return stdout.String(), stderr.String(), err |
| 114 | + } |
| 115 | + return strings.TrimSpace(stdout.String()), strings.TrimSpace(stderr.String()), err |
| 116 | +} |
| 117 | + |
| 118 | +func execute(method string, url *url.URL, config *rest.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool) error { |
| 119 | + exec, err := remotecommand.NewSPDYExecutor(config, method, url) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + return exec.Stream(remotecommand.StreamOptions{ |
| 124 | + Stdin: stdin, |
| 125 | + Stdout: stdout, |
| 126 | + Stderr: stderr, |
| 127 | + Tty: tty, |
| 128 | + }) |
| 129 | +} |
0 commit comments