/
async.go
199 lines (170 loc) · 5.53 KB
/
async.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Copyright 2018 The KubeVirt Authors.
Copyright 2024 Flant JSC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Initially copied from https://github.com/kubevirt/kubevirt/blob/main/staging/src/kubevirt.io/client-go/kubecli/async.go
*/
package kubeclient
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/gorilla/websocket"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
)
type AsyncSubresourceError struct {
err string
StatusCode int
}
type asyncWSRoundTripper struct {
Done chan struct{}
Connection chan *websocket.Conn
}
func (aws *asyncWSRoundTripper) WebsocketCallback(ws *websocket.Conn, resp *http.Response, err error) error {
if err != nil {
if resp != nil && resp.StatusCode != http.StatusOK {
return enrichError(err, resp)
}
return fmt.Errorf("Can't connect to websocket: %s\n", err.Error())
}
aws.Connection <- ws
// Keep the roundtripper open until we are done with the stream
<-aws.Done
return nil
}
func (a *AsyncSubresourceError) Error() string {
return a.err
}
func (a *AsyncSubresourceError) GetStatusCode() int {
return a.StatusCode
}
// params are strings with "key=value" format
func asyncSubresourceHelper(config *rest.Config, resource, namespace, name, subresource string, queryParams url.Values) (StreamInterface, error) {
done := make(chan struct{})
aws := &asyncWSRoundTripper{
Connection: make(chan *websocket.Conn),
Done: done,
}
// Create a round tripper with all necessary kubernetes security details
wrappedRoundTripper, err := roundTripperFromConfig(config, aws.WebsocketCallback)
if err != nil {
return nil, fmt.Errorf("unable to create round tripper for remote execution: %w", err)
}
// Create a request out of config and the query parameters
req, err := RequestFromConfig(config, resource, name, namespace, subresource, queryParams)
if err != nil {
return nil, fmt.Errorf("unable to create request for remote execution: %w", err)
}
errChan := make(chan error, 1)
go func() {
// Send the request and let the callback do its work
response, err := wrappedRoundTripper.RoundTrip(req)
if err != nil {
statusCode := 0
if response != nil {
statusCode = response.StatusCode
}
errChan <- &AsyncSubresourceError{err: err.Error(), StatusCode: statusCode}
return
}
if response != nil {
defer response.Body.Close()
switch response.StatusCode {
case http.StatusOK:
case http.StatusNotFound:
err = &AsyncSubresourceError{err: "Virtual Machine not found.", StatusCode: response.StatusCode}
case http.StatusInternalServerError:
err = &AsyncSubresourceError{err: "Websocket failed due to internal server error.", StatusCode: response.StatusCode}
default:
err = &AsyncSubresourceError{err: fmt.Sprintf("Websocket failed with http status: %s", response.Status), StatusCode: response.StatusCode}
}
} else {
err = &AsyncSubresourceError{err: "no response received"}
}
errChan <- err
}()
select {
case err = <-errChan:
return nil, err
case ws := <-aws.Connection:
return &wsStreamer{
conn: ws,
done: done,
}, nil
}
}
func roundTripperFromConfig(config *rest.Config, callback RoundTripCallback) (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,
WriteBufferSize: WebsocketMessageBufferSize,
ReadBufferSize: WebsocketMessageBufferSize,
Subprotocols: []string{PlainStreamProtocolName},
}
// Create a roundtripper which will pass in the final underlying websocket connection to a callback
rt := &WebsocketRoundTripper{
Do: callback,
Dialer: dialer,
}
// Make sure we inherit all relevant security headers
return rest.HTTPWrappersForConfig(config, rt)
}
// enrichError checks the response body for a k8s Status object and extracts the error from it.
func enrichError(httpErr error, resp *http.Response) error {
if resp == nil {
return httpErr
}
httpErr = fmt.Errorf("Can't connect to websocket (%d): %s\n", resp.StatusCode, httpErr.Error())
status := &metav1.Status{}
if resp.Header.Get("Content-Type") != "application/json" {
return httpErr
}
// decode, but if the result is Status return that as an error instead.
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if len(body) == 0 {
return httpErr
}
err = json.Unmarshal(body, status)
if err != nil {
return err
}
if status.Kind == "Status" && status.APIVersion == "v1" {
if status.Status != metav1.StatusSuccess {
return errors.FromObject(status)
}
}
return httpErr
}
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)
}