forked from kubernetes/test-infra
/
client.go
676 lines (605 loc) · 19.5 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
Copyright 2016 The Kubernetes Authors.
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.
*/
package kube
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/yaml"
)
const (
// TestContainerName specifies the primary container name.
TestContainerName = "test"
inClusterBaseURL = "https://kubernetes.default"
maxRetries = 8
retryDelay = 2 * time.Second
requestTimeout = time.Minute
// EmptySelector selects everything
EmptySelector = ""
// DefaultClusterAlias specifies the default cluster key to schedule jobs.
DefaultClusterAlias = "default"
)
// newClient is used to allow mocking out the behavior of 'NewClient' while testing.
var newClient = NewClient
// Logger can print debug messages
type Logger interface {
Debugf(s string, v ...interface{})
}
// Client interacts with the Kubernetes api-server.
type Client struct {
// If logger is non-nil, log all method calls with it.
logger Logger
baseURL string
deckURL string
client *http.Client
token string
namespace string
fake bool
hiddenReposProvider func() []string
hiddenOnly bool
}
// SetHiddenReposProvider takes a continuation that fetches a list of orgs and repos for
// which PJs should not be returned.
// NOTE: This function is not thread safe and should be called before the client is in use.
func (c *Client) SetHiddenReposProvider(p func() []string, hiddenOnly bool) {
c.hiddenReposProvider = p
c.hiddenOnly = hiddenOnly
}
// Namespace returns a copy of the client pointing at the specified namespace.
func (c *Client) Namespace(ns string) *Client {
nc := *c
nc.namespace = ns
return &nc
}
func (c *Client) log(methodName string, args ...interface{}) {
if c.logger == nil {
return
}
var as []string
for _, arg := range args {
as = append(as, fmt.Sprintf("%v", arg))
}
c.logger.Debugf("%s(%s)", methodName, strings.Join(as, ", "))
}
// ConflictError is http 409.
type ConflictError struct {
e error
}
func (e ConflictError) Error() string {
return e.e.Error()
}
// NewConflictError returns an error with the embedded inner error
func NewConflictError(e error) ConflictError {
return ConflictError{e: e}
}
// UnprocessableEntityError happens when the apiserver returns http 422.
type UnprocessableEntityError struct {
e error
}
func (e UnprocessableEntityError) Error() string {
return e.e.Error()
}
// NewUnprocessableEntityError returns an error with the embedded inner error
func NewUnprocessableEntityError(e error) UnprocessableEntityError {
return UnprocessableEntityError{e: e}
}
// NotFoundError happens when the apiserver returns http 404
type NotFoundError struct {
e error
}
func (e NotFoundError) Error() string {
return e.e.Error()
}
// NewNotFoundError returns an error with the embedded inner error
func NewNotFoundError(e error) NotFoundError {
return NotFoundError{e: e}
}
type request struct {
method string
path string
deckPath string
query map[string]string
requestBody interface{}
}
func (c *Client) request(r *request, ret interface{}) error {
out, err := c.requestRetry(r)
if err != nil {
return err
}
if ret != nil {
if err := json.Unmarshal(out, ret); err != nil {
return err
}
}
return nil
}
func (c *Client) retry(r *request) (*http.Response, error) {
var resp *http.Response
var err error
backoff := retryDelay
for retries := 0; retries < maxRetries; retries++ {
resp, err = c.doRequest(r.method, r.deckPath, r.path, r.query, r.requestBody)
if err == nil {
if resp.StatusCode < 500 {
break
}
resp.Body.Close()
}
time.Sleep(backoff)
backoff *= 2
}
return resp, err
}
// Retry on transport failures. Does not retry on 500s.
func (c *Client) requestRetryStream(r *request) (io.ReadCloser, error) {
if c.fake && r.deckPath == "" {
return nil, nil
}
resp, err := c.retry(r)
if err != nil {
return nil, err
}
if resp.StatusCode == 409 {
return nil, NewConflictError(fmt.Errorf("body cannot be streamed"))
} else if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("response has status \"%s\"", resp.Status)
}
return resp.Body, nil
}
// Retry on transport failures. Does not retry on 500s.
func (c *Client) requestRetry(r *request) ([]byte, error) {
if c.fake && r.deckPath == "" {
return []byte("{}"), nil
}
resp, err := c.retry(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
rb, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode == 409 {
return nil, NewConflictError(fmt.Errorf("body: %s", string(rb)))
} else if resp.StatusCode == 422 {
return nil, NewUnprocessableEntityError(fmt.Errorf("body: %s", string(rb)))
} else if resp.StatusCode == 404 {
return nil, NewNotFoundError(fmt.Errorf("body: %s", string(rb)))
} else if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("response has status \"%s\" and body \"%s\"", resp.Status, string(rb))
}
return rb, nil
}
func (c *Client) doRequest(method, deckPath, urlPath string, query map[string]string, body interface{}) (*http.Response, error) {
url := c.baseURL + urlPath
if c.deckURL != "" && deckPath != "" {
url = c.deckURL + deckPath
}
var buf io.Reader
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
buf = bytes.NewBuffer(b)
}
req, err := http.NewRequest(method, url, buf)
if err != nil {
return nil, err
}
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
if method == http.MethodPatch {
req.Header.Set("Content-Type", "application/strategic-merge-patch+json")
} else {
req.Header.Set("Content-Type", "application/json")
}
q := req.URL.Query()
for k, v := range query {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
return c.client.Do(req)
}
// NewFakeClient creates a client that doesn't do anything. If you provide a
// deck URL then the client will hit that for the supported calls.
func NewFakeClient(deckURL string) *Client {
return &Client{
namespace: "default",
deckURL: deckURL,
client: &http.Client{},
fake: true,
}
}
// NewClientInCluster creates a Client that works from within a pod.
func NewClientInCluster(namespace string) (*Client, error) {
tokenFile := "/var/run/secrets/kubernetes.io/serviceaccount/token"
token, err := ioutil.ReadFile(tokenFile)
if err != nil {
return nil, err
}
rootCAFile := "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
certData, err := ioutil.ReadFile(rootCAFile)
if err != nil {
return nil, err
}
cp := x509.NewCertPool()
cp.AppendCertsFromPEM(certData)
tr := &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: cp,
},
}
return &Client{
logger: logrus.WithField("client", "kube"),
baseURL: inClusterBaseURL,
client: &http.Client{Transport: tr, Timeout: requestTimeout},
token: string(token),
namespace: namespace,
}, nil
}
// Cluster represents the information necessary to talk to a Kubernetes
// master endpoint.
// NOTE: if your cluster runs on GKE you can use the following command to get these credentials:
// gcloud --project <gcp_project> container clusters describe --zone <zone> <cluster_name>
type Cluster struct {
// The IP address of the cluster's master endpoint.
Endpoint string `json:"endpoint"`
// Base64-encoded public cert used by clients to authenticate to the
// cluster endpoint.
ClientCertificate string `json:"clientCertificate"`
// Base64-encoded private key used by clients..
ClientKey string `json:"clientKey"`
// Base64-encoded public certificate that is the root of trust for the
// cluster.
ClusterCACertificate string `json:"clusterCaCertificate"`
}
// NewClientFromFile reads a Cluster object at clusterPath and returns an
// authenticated client using the keys within.
func NewClientFromFile(clusterPath, namespace string) (*Client, error) {
data, err := ioutil.ReadFile(clusterPath)
if err != nil {
return nil, err
}
var c Cluster
if err := yaml.Unmarshal(data, &c); err != nil {
return nil, err
}
return NewClient(&c, namespace)
}
// UnmarshalClusterMap reads a map[string]Cluster in yaml bytes.
func UnmarshalClusterMap(data []byte) (map[string]Cluster, error) {
var raw map[string]Cluster
if err := yaml.Unmarshal(data, &raw); err != nil {
// If we failed to unmarshal the multicluster format try the single Cluster format.
var singleConfig Cluster
if err := yaml.Unmarshal(data, &singleConfig); err != nil {
return nil, err
}
raw = map[string]Cluster{DefaultClusterAlias: singleConfig}
}
return raw, nil
}
// MarshalClusterMap writes c as yaml bytes.
func MarshalClusterMap(c map[string]Cluster) ([]byte, error) {
return yaml.Marshal(c)
}
// ClientMapFromFile reads the file at clustersPath and attempts to load a map of cluster aliases
// to authenticated clients to the respective clusters.
// The file at clustersPath is expected to be a yaml map from strings to Cluster structs OR it may
// simply be a single Cluster struct which will be assigned the alias $DefaultClusterAlias.
// If the file is an alias map, it must include the alias $DefaultClusterAlias.
func ClientMapFromFile(clustersPath, namespace string) (map[string]*Client, error) {
data, err := ioutil.ReadFile(clustersPath)
if err != nil {
return nil, fmt.Errorf("read error: %v", err)
}
raw, err := UnmarshalClusterMap(data)
if err != nil {
return nil, fmt.Errorf("unmarshal error: %v", err)
}
foundDefault := false
result := map[string]*Client{}
for alias, config := range raw {
client, err := newClient(&config, namespace)
if err != nil {
return nil, fmt.Errorf("failed to load config for build cluster alias %q in file %q: %v", alias, clustersPath, err)
}
result[alias] = client
if alias == DefaultClusterAlias {
foundDefault = true
}
}
if !foundDefault {
return nil, fmt.Errorf("failed to find the required %q alias in build cluster config %q", DefaultClusterAlias, clustersPath)
}
return result, nil
}
// NewClient returns an authenticated Client using the keys in the Cluster.
func NewClient(c *Cluster, namespace string) (*Client, error) {
cc, err := base64.StdEncoding.DecodeString(c.ClientCertificate)
if err != nil {
return nil, err
}
ck, err := base64.StdEncoding.DecodeString(c.ClientKey)
if err != nil {
return nil, err
}
ca, err := base64.StdEncoding.DecodeString(c.ClusterCACertificate)
if err != nil {
return nil, err
}
cert, err := tls.X509KeyPair(cc, ck)
if err != nil {
return nil, err
}
cp := x509.NewCertPool()
cp.AppendCertsFromPEM(ca)
tr := &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{cert},
RootCAs: cp,
},
}
return &Client{
logger: logrus.WithField("client", "kube"),
baseURL: c.Endpoint,
client: &http.Client{Transport: tr, Timeout: requestTimeout},
namespace: namespace,
}, nil
}
// GetPod is analogous to kubectl get pods/NAME namespace=client.namespace
func (c *Client) GetPod(name string) (Pod, error) {
c.log("GetPod", name)
var retPod Pod
err := c.request(&request{
path: fmt.Sprintf("/api/v1/namespaces/%s/pods/%s", c.namespace, name),
}, &retPod)
return retPod, err
}
// ListPods is analogous to kubectl get pods --selector=SELECTOR --namespace=client.namespace
func (c *Client) ListPods(selector string) ([]Pod, error) {
c.log("ListPods", selector)
var pl struct {
Items []Pod `json:"items"`
}
err := c.request(&request{
path: fmt.Sprintf("/api/v1/namespaces/%s/pods", c.namespace),
query: map[string]string{"labelSelector": selector},
}, &pl)
return pl.Items, err
}
// DeletePod deletes the pod at name in the client's specified namespace.
//
// Analogous to kubectl delete pod --namespace=client.namespace
func (c *Client) DeletePod(name string) error {
c.log("DeletePod", name)
return c.request(&request{
method: http.MethodDelete,
path: fmt.Sprintf("/api/v1/namespaces/%s/pods/%s", c.namespace, name),
}, nil)
}
// CreateProwJob creates a prowjob in the client's specified namespace.
//
// Analogous to kubectl create prowjob --namespace=client.namespace
func (c *Client) CreateProwJob(j ProwJob) (ProwJob, error) {
var representation string
if out, err := json.Marshal(j); err == nil {
representation = string(out[:])
} else {
representation = fmt.Sprintf("%v", j)
}
c.log("CreateProwJob", representation)
var retJob ProwJob
err := c.request(&request{
method: http.MethodPost,
path: fmt.Sprintf("/apis/prow.k8s.io/v1/namespaces/%s/prowjobs", c.namespace),
requestBody: &j,
}, &retJob)
return retJob, err
}
func (c *Client) getHiddenRepos() sets.String {
if c.hiddenReposProvider == nil {
return nil
}
return sets.NewString(c.hiddenReposProvider()...)
}
func shouldHide(pj *ProwJob, hiddenRepos sets.String, showHiddenOnly bool) bool {
if pj.Spec.Refs == nil {
// periodic jobs do not have refs and therefore cannot be
// hidden by the org/repo mechanism
return false
}
shouldHide := hiddenRepos.HasAny(fmt.Sprintf("%s/%s", pj.Spec.Refs.Org, pj.Spec.Refs.Repo), pj.Spec.Refs.Org)
if showHiddenOnly {
return !shouldHide
}
return shouldHide
}
// GetProwJob returns the prowjob at name in the client's specified namespace.
//
// Analogous to kubectl get prowjob/NAME --namespace=client.namespace
func (c *Client) GetProwJob(name string) (ProwJob, error) {
c.log("GetProwJob", name)
var pj ProwJob
err := c.request(&request{
path: fmt.Sprintf("/apis/prow.k8s.io/v1/namespaces/%s/prowjobs/%s", c.namespace, name),
}, &pj)
if err == nil && shouldHide(&pj, c.getHiddenRepos(), c.hiddenOnly) {
pj = ProwJob{}
// Revealing the existence of this prow job is ok because the pj name cannot be used to
// retrieve the pj itself. Furthermore, a timing attack could differentiate true 404s from
// 404s returned when a hidden pj is queried so returning a 404 wouldn't hide the pj's existence.
err = errors.New("403 ProwJob is hidden")
}
return pj, err
}
// ListProwJobs lists prowjobs using the specified labelSelector in the client's specified namespace.
//
// Analogous to kubectl get prowjobs --selector=SELECTOR --namespace=client.namespace
func (c *Client) ListProwJobs(selector string) ([]ProwJob, error) {
c.log("ListProwJobs", selector)
var jl struct {
Items []ProwJob `json:"items"`
}
err := c.request(&request{
path: fmt.Sprintf("/apis/prow.k8s.io/v1/namespaces/%s/prowjobs", c.namespace),
deckPath: "/prowjobs.js",
query: map[string]string{"labelSelector": selector},
}, &jl)
if err == nil {
hidden := c.getHiddenRepos()
var pjs []ProwJob
for _, pj := range jl.Items {
if !shouldHide(&pj, hidden, c.hiddenOnly) {
pjs = append(pjs, pj)
}
}
jl.Items = pjs
}
return jl.Items, err
}
// DeleteProwJob deletes the prowjob at name in the client's specified namespace.
//
// Analogous to kubectl delete prowjob/NAME --namespace=client.namespace
func (c *Client) DeleteProwJob(name string) error {
c.log("DeleteProwJob", name)
return c.request(&request{
method: http.MethodDelete,
path: fmt.Sprintf("/apis/prow.k8s.io/v1/namespaces/%s/prowjobs/%s", c.namespace, name),
}, nil)
}
// ReplaceProwJob will replace name with job in the client's specified namespace.
//
// Analogous to kubectl replace prowjobs/NAME --namespace=client.namespace
func (c *Client) ReplaceProwJob(name string, job ProwJob) (ProwJob, error) {
c.log("ReplaceProwJob", name, job)
var retJob ProwJob
err := c.request(&request{
method: http.MethodPut,
path: fmt.Sprintf("/apis/prow.k8s.io/v1/namespaces/%s/prowjobs/%s", c.namespace, name),
requestBody: &job,
}, &retJob)
return retJob, err
}
// CreatePod creates a pod in the client's specified namespace.
//
// Analogous to kubectl create pod --namespace=client.namespace
func (c *Client) CreatePod(p v1.Pod) (Pod, error) {
c.log("CreatePod", p)
var retPod Pod
err := c.request(&request{
method: http.MethodPost,
path: fmt.Sprintf("/api/v1/namespaces/%s/pods", c.namespace),
requestBody: &p,
}, &retPod)
return retPod, err
}
// GetLog returns the log of the default container in the specified pod, in the client's specified namespace.
//
// Analogous to kubectl logs pod --namespace=client.namespace
func (c *Client) GetLog(pod string) ([]byte, error) {
c.log("GetLog", pod)
return c.requestRetry(&request{
path: fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/log", c.namespace, pod),
})
}
// GetLogTail returns the last n bytes of the log of the specified container in the specified pod,
// in the client's specified namespace.
//
// Analogous to kubectl logs pod --tail -1 --limit-bytes n -c container --namespace=client.namespace
func (c *Client) GetLogTail(pod, container string, n int64) ([]byte, error) {
c.log("GetLogTail", pod, n)
return c.requestRetry(&request{
path: fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/log", c.namespace, pod),
query: map[string]string{ // Because we want last n bytes, we fetch all lines and then limit to n bytes
"tailLines": "-1",
"container": container,
"limitBytes": strconv.FormatInt(n, 10),
},
})
}
// GetContainerLog returns the log of a container in the specified pod, in the client's specified namespace.
//
// Analogous to kubectl logs pod -c container --namespace=client.namespace
func (c *Client) GetContainerLog(pod, container string) ([]byte, error) {
c.log("GetContainerLog", pod)
return c.requestRetry(&request{
path: fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/log", c.namespace, pod),
query: map[string]string{"container": container},
})
}
// CreateConfigMap creates a configmap, in the client's specified namespace.
//
// Analogous to kubectl create configmap --namespace=client.namespace
func (c *Client) CreateConfigMap(content ConfigMap) (ConfigMap, error) {
c.log("CreateConfigMap")
var retConfigMap ConfigMap
err := c.request(&request{
method: http.MethodPost,
path: fmt.Sprintf("/api/v1/namespaces/%s/configmaps", c.namespace),
requestBody: &content,
}, &retConfigMap)
return retConfigMap, err
}
// GetConfigMap gets the configmap identified, in the client's specified namespace.
//
// Analogous to kubectl get configmap --namespace=client.namespace
func (c *Client) GetConfigMap(name, namespace string) (ConfigMap, error) {
c.log("GetConfigMap", name)
if namespace == "" {
namespace = c.namespace
}
var retConfigMap ConfigMap
err := c.request(&request{
path: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/%s", namespace, name),
}, &retConfigMap)
return retConfigMap, err
}
// ReplaceConfigMap puts the configmap into name.
//
// Analogous to kubectl replace configmap
//
// If config.Namespace is empty, the client's specified namespace is used.
// Returns the content returned by the apiserver
func (c *Client) ReplaceConfigMap(name string, config ConfigMap) (ConfigMap, error) {
c.log("ReplaceConfigMap", name)
namespace := c.namespace
if config.Namespace != "" {
namespace = config.Namespace
}
var retConfigMap ConfigMap
err := c.request(&request{
method: http.MethodPut,
path: fmt.Sprintf("/api/v1/namespaces/%s/configmaps/%s", namespace, name),
requestBody: &config,
}, &retConfigMap)
return retConfigMap, err
}