This repository has been archived by the owner on Oct 29, 2022. It is now read-only.
forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
303 lines (278 loc) · 9 KB
/
client.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package kubelet
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"syscall"
"time"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"github.com/argoproj/argo/errors"
"github.com/argoproj/argo/workflow/common"
execcommon "github.com/argoproj/argo/workflow/executor/common"
)
const (
readWSResponseTimeout = time.Minute * 1
)
type kubeletClient struct {
httpClient *http.Client
httpHeader http.Header
websocketDialer *websocket.Dialer
// kubeletEndpoint is host:port without any scheme like:
// - 127.0.0.1:10250
// - my-host.com:10250
kubeletEndpoint string
}
var _ execcommon.KubernetesClientInterface = &kubeletClient{}
func newKubeletClient() (*kubeletClient, error) {
kubeletHost := os.Getenv(common.EnvVarDownwardAPINodeIP)
if kubeletHost == "" {
return nil, fmt.Errorf("empty envvar %s", common.EnvVarDownwardAPINodeIP)
}
kubeletPort, _ := strconv.Atoi(os.Getenv(common.EnvVarKubeletPort))
if kubeletPort == 0 {
kubeletPort = 10250
log.Infof("Non configured envvar %s, defaulting the kubelet port to %d", common.EnvVarKubeletPort, kubeletPort)
}
b, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
if err != nil {
return nil, errors.InternalWrapError(err)
}
bearerToken := string(b)
tlsConfig := &tls.Config{}
if os.Getenv(common.EnvVarKubeletInsecure) == "true" {
log.Warningf("Using a kubelet client with insecure options")
tlsConfig.InsecureSkipVerify = true
} else {
log.Warningf("Loading service account ca.crt as certificate authority to reach the kubelet api")
caCert, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")
if err != nil {
return nil, errors.InternalWrapError(err)
}
caCertPool := x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(caCert) {
return nil, errors.InternalWrapError(fmt.Errorf("fail to load certificate authority: %s", string(caCert)))
}
tlsConfig.RootCAs = caCertPool
tlsConfig.BuildNameToCertificate()
}
return &kubeletClient{
httpClient: &http.Client{
Transport: &http.Transport{TLSClientConfig: tlsConfig},
Timeout: time.Second * 60,
},
httpHeader: http.Header{
"Authorization": {"bearer " + bearerToken},
},
websocketDialer: &websocket.Dialer{
TLSClientConfig: tlsConfig,
HandshakeTimeout: time.Second * 5,
},
kubeletEndpoint: fmt.Sprintf("%s:%d", kubeletHost, kubeletPort),
}, nil
}
func checkHTTPErr(resp *http.Response) error {
if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
return errors.InternalWrapError(fmt.Errorf("unexpected non 200 status code: %d, body: %s", resp.StatusCode, string(b)))
}
return nil
}
func (k *kubeletClient) getPodList() (*corev1.PodList, error) {
u, err := url.ParseRequestURI(fmt.Sprintf("https://%s/pods", k.kubeletEndpoint))
if err != nil {
return nil, errors.InternalWrapError(err)
}
resp, err := k.httpClient.Do(&http.Request{
Method: http.MethodGet,
Header: k.httpHeader,
URL: u,
})
if err != nil {
return nil, errors.InternalWrapError(err)
}
err = checkHTTPErr(resp)
if err != nil {
return nil, err
}
podListDecoder := json.NewDecoder(resp.Body)
podList := &corev1.PodList{}
err = podListDecoder.Decode(podList)
if err != nil {
_ = resp.Body.Close()
return nil, errors.InternalWrapError(err)
}
return podList, resp.Body.Close()
}
func (k *kubeletClient) GetLogStream(containerID string) (io.ReadCloser, error) {
podList, err := k.getPodList()
if err != nil {
return nil, err
}
for _, pod := range podList.Items {
for _, container := range pod.Status.ContainerStatuses {
if execcommon.GetContainerID(&container) != containerID {
continue
}
resp, err := k.doRequestLogs(pod.Namespace, pod.Name, container.Name)
if err != nil {
return nil, err
}
return resp.Body, nil
}
}
return nil, errors.New(errors.CodeNotFound, fmt.Sprintf("containerID %q is not found in the pod list", containerID))
}
func (k *kubeletClient) doRequestLogs(namespace, podName, containerName string) (*http.Response, error) {
u, err := url.ParseRequestURI(fmt.Sprintf("https://%s/containerLogs/%s/%s/%s", k.kubeletEndpoint, namespace, podName, containerName))
if err != nil {
return nil, errors.InternalWrapError(err)
}
resp, err := k.httpClient.Do(&http.Request{
Method: http.MethodGet,
Header: k.httpHeader,
URL: u,
})
if err != nil {
return nil, err
}
err = checkHTTPErr(resp)
if err != nil {
return nil, err
}
return resp, nil
}
func (k *kubeletClient) GetContainerStatus(containerID string) (*corev1.Pod, *corev1.ContainerStatus, error) {
podList, err := k.getPodList()
if err != nil {
return nil, nil, errors.InternalWrapError(err)
}
for _, pod := range podList.Items {
for _, container := range pod.Status.ContainerStatuses {
if execcommon.GetContainerID(&container) != containerID {
continue
}
return &pod, &container, nil
}
}
return nil, nil, errors.New(errors.CodeNotFound, fmt.Sprintf("containerID %q is not found in the pod list", containerID))
}
func (k *kubeletClient) exec(u *url.URL) (*url.URL, error) {
_, resp, err := k.websocketDialer.Dial(u.String(), k.httpHeader)
if resp == nil {
return nil, err
}
if resp.StatusCode != http.StatusFound {
return nil, err
}
log.Infof("Exec for %s with status code: %d", u.String(), resp.StatusCode)
redirect, err := url.Parse(resp.Header.Get("Location"))
if err != nil {
return nil, err
}
redirect.Scheme = "ws"
log.Infof("Exec for %s returns URL: %s", u.String(), redirect.String())
return redirect, nil
}
func (k *kubeletClient) readFileContents(u *url.URL) (*bytes.Buffer, error) {
conn, _, err := k.websocketDialer.Dial(u.String(), nil)
if err != nil {
return nil, err
}
timeout := time.NewTimer(readWSResponseTimeout)
defer timeout.Stop()
buf := &bytes.Buffer{}
for {
select {
case <-timeout.C:
return nil, fmt.Errorf("timeout of %s reached while reading file contents", readWSResponseTimeout)
default:
_, b, err := conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
return buf, conn.Close()
}
log.Errorf("Unexpected error while reading messages on WS: %v", err)
_ = conn.Close()
return buf, err
}
if len(b) < 1 {
continue
}
i := 0
// skip SOH (start of heading)
if int(b[0]) == 1 {
i = 1
}
_, err = buf.Write(b[i:])
if err != nil {
log.Errorf("Unexpected error while reading messages on WS: %v", err)
_ = conn.Close()
return nil, err
}
}
}
}
// createArchive exec in the given containerID and create a tarball of the given sourcePath. Works with directory
func (k *kubeletClient) CreateArchive(containerID, sourcePath string) (*bytes.Buffer, error) {
return k.getCommandOutput(containerID, fmt.Sprintf("command=tar&command=-cf&command=-&command=%s&output=1", sourcePath))
}
// GetFileContents exec in the given containerID and cat the given sourcePath.
func (k *kubeletClient) GetFileContents(containerID, sourcePath string) (*bytes.Buffer, error) {
return k.getCommandOutput(containerID, fmt.Sprintf("command=cat&command=%s&output=1", sourcePath))
}
func (k *kubeletClient) getCommandOutput(containerID, command string) (*bytes.Buffer, error) {
podList, err := k.getPodList()
if err != nil {
return nil, errors.InternalWrapError(err)
}
for _, pod := range podList.Items {
for _, container := range pod.Status.ContainerStatuses {
if execcommon.GetContainerID(&container) != containerID {
continue
}
if container.State.Terminated != nil {
err = fmt.Errorf("container %s is terminated: %v", container.ContainerID, container.State.Terminated.String())
return nil, err
}
u, err := url.ParseRequestURI(fmt.Sprintf("wss://%s/exec/%s/%s/%s?%s", k.kubeletEndpoint, pod.Namespace, pod.Name, container.Name, command))
if err != nil {
return nil, err
}
u, err = k.exec(u)
if err != nil {
return nil, err
}
return k.readFileContents(u)
}
}
return nil, errors.New(errors.CodeNotFound, fmt.Sprintf("containerID %q is not found in the pod list", containerID))
}
// WaitForTermination of the given containerID, set the timeout to 0 to discard it
func (k *kubeletClient) WaitForTermination(containerID string, timeout time.Duration) error {
return execcommon.WaitForTermination(k, containerID, timeout)
}
func (k *kubeletClient) KillContainer(pod *corev1.Pod, container *corev1.ContainerStatus, sig syscall.Signal) error {
u, err := url.ParseRequestURI(fmt.Sprintf("wss://%s/exec/%s/%s/%s?command=/bin/sh&&command=-c&command=kill+-%d+1&output=1&error=1", k.kubeletEndpoint, pod.Namespace, pod.Name, container.Name, sig))
if err != nil {
return errors.InternalWrapError(err)
}
_, err = k.exec(u)
return err
}
func (k *kubeletClient) KillGracefully(containerID string) error {
return execcommon.KillGracefully(k, containerID)
}
func (k *kubeletClient) CopyArchive(containerID, sourcePath, destPath string) error {
return execcommon.CopyArchive(k, containerID, sourcePath, destPath)
}