|
| 1 | +package collect |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "time" |
| 9 | + |
| 10 | + troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/client-go/kubernetes" |
| 14 | + "k8s.io/client-go/tools/remotecommand" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 16 | +) |
| 17 | + |
| 18 | +type ExecOutput struct { |
| 19 | + Results map[string][]byte `json:"exec/,omitempty"` |
| 20 | +} |
| 21 | + |
| 22 | +type execResult struct { |
| 23 | + Stdout string `json:"stdout,omitempty"` |
| 24 | + Stderr string `json:"stderr,omitempty"` |
| 25 | + Error error `json:"error,omitempty"` |
| 26 | +} |
| 27 | + |
| 28 | +func Exec(execCollector *troubleshootv1beta1.Exec, redact bool) error { |
| 29 | + if execCollector.Timeout == "" { |
| 30 | + return execWithoutTimeout(execCollector, redact) |
| 31 | + } |
| 32 | + |
| 33 | + timeout, err := time.ParseDuration(execCollector.Timeout) |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + execChan := make(chan error, 1) |
| 39 | + go func() { |
| 40 | + execChan <- execWithoutTimeout(execCollector, redact) |
| 41 | + }() |
| 42 | + |
| 43 | + select { |
| 44 | + case <-time.After(timeout): |
| 45 | + return errors.New("timeout") |
| 46 | + case err := <-execChan: |
| 47 | + return err |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func execWithoutTimeout(execCollector *troubleshootv1beta1.Exec, redact bool) error { |
| 52 | + cfg, err := config.GetConfig() |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + client, err := kubernetes.NewForConfig(cfg) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + pods, err := listPodsInSelectors(client, execCollector.Namespace, execCollector.Selector) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + execOutput := &ExecOutput{ |
| 68 | + Results: make(map[string][]byte), |
| 69 | + } |
| 70 | + |
| 71 | + for _, pod := range pods { |
| 72 | + output, err := getExecOutputs(client, pod, execCollector, redact) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + b, err := json.MarshalIndent(output, "", " ") |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + execOutput.Results[fmt.Sprintf("%s/%s/%s", pod.Namespace, pod.Name, execCollector.Name)] = b |
| 83 | + } |
| 84 | + |
| 85 | + if redact { |
| 86 | + execOutput, err = execOutput.Redact() |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + b, err := json.MarshalIndent(execOutput, "", " ") |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + fmt.Printf("%s\n", b) |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func getExecOutputs(client *kubernetes.Clientset, pod corev1.Pod, execCollector *troubleshootv1beta1.Exec, doRedact bool) (*execResult, error) { |
| 103 | + cfg, err := config.GetConfig() |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + container := pod.Spec.Containers[0].Name |
| 109 | + if execCollector.ContainerName != "" { |
| 110 | + container = execCollector.ContainerName |
| 111 | + } |
| 112 | + |
| 113 | + req := client.CoreV1().RESTClient().Post().Resource("pods").Name(pod.Name).Namespace(pod.Namespace).SubResource("exec") |
| 114 | + scheme := runtime.NewScheme() |
| 115 | + if err := corev1.AddToScheme(scheme); err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + parameterCodec := runtime.NewParameterCodec(scheme) |
| 120 | + req.VersionedParams(&corev1.PodExecOptions{ |
| 121 | + Command: append(execCollector.Command, execCollector.Args...), |
| 122 | + Container: container, |
| 123 | + Stdin: true, |
| 124 | + Stdout: false, |
| 125 | + Stderr: true, |
| 126 | + TTY: false, |
| 127 | + }, parameterCodec) |
| 128 | + |
| 129 | + exec, err := remotecommand.NewSPDYExecutor(cfg, "POST", req.URL()) |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + |
| 134 | + stdout := new(bytes.Buffer) |
| 135 | + stderr := new(bytes.Buffer) |
| 136 | + |
| 137 | + err = exec.Stream(remotecommand.StreamOptions{ |
| 138 | + Stdin: nil, |
| 139 | + Stdout: stdout, |
| 140 | + Stderr: stderr, |
| 141 | + Tty: false, |
| 142 | + }) |
| 143 | + |
| 144 | + return &execResult{ |
| 145 | + Stdout: stdout.String(), |
| 146 | + Stderr: stderr.String(), |
| 147 | + Error: err, |
| 148 | + }, nil |
| 149 | +} |
| 150 | + |
| 151 | +func (r *ExecOutput) Redact() (*ExecOutput, error) { |
| 152 | + results, err := redactMap(r.Results) |
| 153 | + if err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + |
| 157 | + return &ExecOutput{ |
| 158 | + Results: results, |
| 159 | + }, nil |
| 160 | +} |
0 commit comments