|
1 | 1 | package collect |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
6 | | - "net/http" |
7 | 7 |
|
8 | | - "github.com/gorilla/websocket" |
9 | 8 | troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" |
10 | 9 | corev1 "k8s.io/api/core/v1" |
| 10 | + "k8s.io/apimachinery/pkg/runtime" |
11 | 11 | "k8s.io/client-go/kubernetes" |
12 | | - "k8s.io/client-go/rest" |
| 12 | + "k8s.io/client-go/tools/remotecommand" |
13 | 13 | "sigs.k8s.io/controller-runtime/pkg/client/config" |
14 | 14 | ) |
15 | 15 |
|
@@ -69,63 +69,47 @@ func copyFiles(client *kubernetes.Clientset, pod corev1.Pod, copyCollector *trou |
69 | 69 | } |
70 | 70 |
|
71 | 71 | container := pod.Spec.Containers[0].Name |
72 | | - |
73 | 72 | if copyCollector.ContainerName != "" { |
74 | 73 | container = copyCollector.ContainerName |
75 | 74 | } |
76 | 75 |
|
77 | | - u := client.CoreV1().RESTClient().Get().Namespace(pod.Namespace).Name(pod.Name). |
78 | | - Resource("pods").SubResource("exec"). |
79 | | - Param("command", "/bin/cat").Param("command", copyCollector.ContainerPath). |
80 | | - Param("container", container).Param("stderr", "true").Param("stdout", "true").URL() |
81 | | - |
82 | | - switch u.Scheme { |
83 | | - case "https": |
84 | | - u.Scheme = "wss" |
85 | | - case "http": |
86 | | - u.Scheme = "ws" |
87 | | - default: |
88 | | - return nil, err |
89 | | - } |
| 76 | + command := []string{"cat", copyCollector.ContainerPath} |
90 | 77 |
|
91 | | - req := &http.Request{ |
92 | | - Method: http.MethodGet, |
93 | | - URL: u, |
94 | | - } |
| 78 | + output := new(bytes.Buffer) |
95 | 79 |
|
96 | | - tlsConfig, err := rest.TLSConfigFor(cfg) |
97 | | - if err != nil { |
| 80 | + req := client.CoreV1().RESTClient().Post().Resource("pods").Name(pod.Name).Namespace(pod.Namespace).SubResource("exec") |
| 81 | + scheme := runtime.NewScheme() |
| 82 | + if err := corev1.AddToScheme(scheme); err != nil { |
98 | 83 | return nil, err |
99 | 84 | } |
100 | 85 |
|
101 | | - dialer := &websocket.Dialer{ |
102 | | - Proxy: http.ProxyFromEnvironment, |
103 | | - TLSClientConfig: tlsConfig, |
104 | | - } |
105 | | - |
106 | | - c, _, err := dialer.Dial(u.String(), req.Header) |
| 86 | + parameterCodec := runtime.NewParameterCodec(scheme) |
| 87 | + req.VersionedParams(&corev1.PodExecOptions{ |
| 88 | + Command: command, |
| 89 | + Container: container, |
| 90 | + Stdin: true, |
| 91 | + Stdout: false, |
| 92 | + Stderr: true, |
| 93 | + TTY: false, |
| 94 | + }, parameterCodec) |
| 95 | + |
| 96 | + exec, err := remotecommand.NewSPDYExecutor(cfg, "POST", req.URL()) |
107 | 97 | if err != nil { |
108 | 98 | return nil, err |
109 | 99 | } |
110 | | - defer c.Close() |
111 | 100 |
|
112 | | - var res []byte |
113 | | - for { |
114 | | - msgT, p, err := c.ReadMessage() |
115 | | - if err != nil { |
116 | | - if _, ok := err.(*websocket.CloseError); ok { |
117 | | - break |
118 | | - } |
119 | | - fmt.Printf("err %T %v\n", err, err) |
120 | | - break |
121 | | - } |
122 | | - if msgT != 2 { |
123 | | - return nil, fmt.Errorf("unknown message type %d", msgT) |
124 | | - } |
125 | | - res = append(res, p...) |
| 101 | + var stderr bytes.Buffer |
| 102 | + err = exec.Stream(remotecommand.StreamOptions{ |
| 103 | + Stdin: nil, |
| 104 | + Stdout: output, |
| 105 | + Stderr: &stderr, |
| 106 | + Tty: false, |
| 107 | + }) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
126 | 110 | } |
127 | 111 |
|
128 | 112 | return map[string][]byte{ |
129 | | - copyCollector.ContainerPath: res, |
| 113 | + fmt.Sprintf("%s/%s/%s", pod.Namespace, pod.Name, copyCollector.ContainerPath): output.Bytes(), |
130 | 114 | }, nil |
131 | 115 | } |
0 commit comments