-
Notifications
You must be signed in to change notification settings - Fork 697
/
base.go
168 lines (147 loc) · 4.32 KB
/
base.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.
package client
import (
"bytes"
"context"
"crypto/x509"
"encoding/json"
"io"
"net/http"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/version"
"github.com/elastic/cloud-on-k8s/pkg/utils/stringsutil"
"github.com/hashicorp/go-multierror"
)
type baseClient struct {
User BasicAuth
HTTP *http.Client
Endpoint string
caCerts []*x509.Certificate
version version.Version
}
// Close idle connections in the underlying http client.
// Should be called once this client is not used anymore.
func (c *baseClient) Close() {
if c.HTTP != nil {
// When the http transport goes out of scope, the underlying goroutines responsible
// for handling keep-alive connections are not closed automatically.
// Since this client gets recreated frequently we would effectively be leaking goroutines.
// Let's make sure this does not happen by closing idle connections.
c.HTTP.CloseIdleConnections()
}
}
func (c *baseClient) equal(c2 *baseClient) bool {
// handle nil case
if c2 == nil && c != nil {
return false
}
// compare ca certs
if len(c.caCerts) != len(c2.caCerts) {
return false
}
for i := range c.caCerts {
if !c.caCerts[i].Equal(c2.caCerts[i]) {
return false
}
}
// compare endpoint and user creds
return c.Endpoint == c2.Endpoint &&
c.User == c2.User
}
func (c *baseClient) doRequest(context context.Context, request *http.Request) (*http.Response, error) {
withContext := request.WithContext(context)
withContext.Header.Set("Content-Type", "application/json; charset=utf-8")
if c.User != (BasicAuth{}) {
withContext.SetBasicAuth(c.User.Name, c.User.Password)
}
response, err := c.HTTP.Do(withContext)
if err != nil {
return response, err
}
err = checkError(response)
return response, err
}
func (c *baseClient) get(ctx context.Context, pathWithQuery string, out interface{}) error {
return c.request(ctx, http.MethodGet, pathWithQuery, nil, out, nil)
}
func (c *baseClient) put(ctx context.Context, pathWithQuery string, in, out interface{}) error { //nolint:unparam
return c.request(ctx, http.MethodPut, pathWithQuery, in, out, nil)
}
func (c *baseClient) post(ctx context.Context, pathWithQuery string, in, out interface{}) error {
return c.request(ctx, http.MethodPost, pathWithQuery, in, out, nil)
}
func (c *baseClient) delete(ctx context.Context, pathWithQuery string, in, out interface{}) error {
return c.request(ctx, http.MethodDelete, pathWithQuery, in, out, nil)
}
// request performs a new http request
//
// if requestObj is not nil, it's marshalled as JSON and used as the request body
// if responseObj is not nil, it should be a pointer to an struct. The response body will be unmarshalled from JSON
// into this struct if the status code of the response is 2xx or if the (optional) given skipErrFunc function returns true.
func (c *baseClient) request(
ctx context.Context,
method string,
pathWithQuery string,
requestObj,
responseObj interface{},
skipErrFunc func(error) bool,
) error {
var body io.Reader = http.NoBody
if requestObj != nil {
outData, err := json.Marshal(requestObj)
if err != nil {
return err
}
body = bytes.NewBuffer(outData)
}
request, err := http.NewRequest(method, stringsutil.Concat(c.Endpoint, pathWithQuery), body) //nolint:noctx
if err != nil {
return err
}
var skippedErr error
resp, err := c.doRequest(ctx, request)
if skipErrFunc != nil && skipErrFunc(err) {
skippedErr = err
err = nil
}
if err != nil {
return err
}
defer resp.Body.Close()
if responseObj != nil {
if err := json.NewDecoder(resp.Body).Decode(responseObj); err != nil {
if skippedErr != nil {
err = multierror.Append(err, skippedErr)
}
return err
}
}
return nil
}
func versioned(b *baseClient, v version.Version) Client {
b.version = v
v6 := clientV6{
baseClient: *b,
}
switch v.Major {
case 7:
return &clientV7{
clientV6: v6,
}
case 8:
return &clientV8{
clientV7: clientV7{clientV6: v6},
}
default:
return &v6
}
}
func checkError(response *http.Response) error {
if response.StatusCode < 200 || response.StatusCode >= 300 {
return &APIError{
response: response,
}
}
return nil
}