Skip to content

Commit b0d5264

Browse files
committed
use spdy
1 parent 5226831 commit b0d5264

File tree

2 files changed

+34
-50
lines changed

2 files changed

+34
-50
lines changed

config/samples/troubleshoot_v1beta1_collector.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ spec:
2323
# command: ["ping"]
2424
# args: ["www.google.com"]
2525
# # timeout: 5s
26-
# - copy:
27-
# selector:
28-
# - app=illmannered-cricket-mysql
29-
# namespace: default
30-
# containerPath: /etc/hosts
26+
- copy:
27+
selector:
28+
- app=illmannered-cricket-mysql
29+
namespace: default
30+
containerPath: /etc/hosts

pkg/collect/copy.go

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package collect
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
6-
"net/http"
77

8-
"github.com/gorilla/websocket"
98
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
109
corev1 "k8s.io/api/core/v1"
10+
"k8s.io/apimachinery/pkg/runtime"
1111
"k8s.io/client-go/kubernetes"
12-
"k8s.io/client-go/rest"
12+
"k8s.io/client-go/tools/remotecommand"
1313
"sigs.k8s.io/controller-runtime/pkg/client/config"
1414
)
1515

@@ -69,63 +69,47 @@ func copyFiles(client *kubernetes.Clientset, pod corev1.Pod, copyCollector *trou
6969
}
7070

7171
container := pod.Spec.Containers[0].Name
72-
7372
if copyCollector.ContainerName != "" {
7473
container = copyCollector.ContainerName
7574
}
7675

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}
9077

91-
req := &http.Request{
92-
Method: http.MethodGet,
93-
URL: u,
94-
}
78+
output := new(bytes.Buffer)
9579

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 {
9883
return nil, err
9984
}
10085

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())
10797
if err != nil {
10898
return nil, err
10999
}
110-
defer c.Close()
111100

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
126110
}
127111

128112
return map[string][]byte{
129-
copyCollector.ContainerPath: res,
113+
fmt.Sprintf("%s/%s/%s", pod.Namespace, pod.Name, copyCollector.ContainerPath): output.Bytes(),
130114
}, nil
131115
}

0 commit comments

Comments
 (0)