This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
forked from nrmitchi/k8s-controller-sidecars
-
Notifications
You must be signed in to change notification settings - Fork 2
/
exec.go
112 lines (93 loc) · 2.48 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// The majority of this file was borrowed from https://github.com/rmohr/kubernetes-custom-exec
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"github.com/gorilla/websocket"
"k8s.io/client-go/rest"
)
type RoundTripCallback func(conn *websocket.Conn, resp *http.Response, err error) error
type WebsocketRoundTripper struct {
Dialer *websocket.Dialer
Do RoundTripCallback
}
func (d *WebsocketRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
conn, resp, err := d.Dialer.Dial(r.URL.String(), r.Header)
if err == nil {
defer conn.Close()
}
return resp, d.Do(conn, resp, err)
}
func WebsocketCallback(ws *websocket.Conn, resp *http.Response, err error) error {
if err != nil {
if resp != nil && resp.StatusCode != http.StatusOK {
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return fmt.Errorf("Can't connect to console (%d): %s\n", resp.StatusCode, buf.String())
}
return fmt.Errorf("Can't connect to console: %s\n", err.Error())
}
txt := ""
for {
_, body, err := ws.ReadMessage()
if err != nil {
fmt.Println(txt)
if err == io.EOF {
return nil
}
if websocket.IsCloseError(err, 1000) {
return nil
}
return err
}
txt = txt + string(body)
}
}
func roundTripperFromConfig(config *rest.Config) (http.RoundTripper, error) {
// Configure TLS
tlsConfig, err := rest.TLSConfigFor(config)
if err != nil {
return nil, err
}
// Configure the websocket dialer
dialer := &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
}
// Create a roundtripper which will pass in the final underlying websocket connection to a callback
rt := &WebsocketRoundTripper{
Do: WebsocketCallback,
Dialer: dialer,
}
// Make sure we inherit all relevant security headers
return rest.HTTPWrappersForConfig(config, rt)
}
func requestFromConfig(config *rest.Config, pod string, container string, namespace string, cmd string) (*http.Request, error) {
u, err := url.Parse(config.Host)
if err != nil {
return nil, err
}
switch u.Scheme {
case "https":
u.Scheme = "wss"
case "http":
u.Scheme = "ws"
default:
return nil, fmt.Errorf("Malformed URL %s", u.String())
}
u.Path = fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/exec", namespace, pod)
if container != "" {
u.RawQuery = "command=" + cmd +
"&container=" + container +
"&stderr=true&stdout=true"
}
req := &http.Request{
Method: http.MethodGet,
URL: u,
Header: map[string][]string{},
}
return req, nil
}