/
client.go
452 lines (385 loc) · 12.3 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
package orchestration
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/common/pagination"
"golang.org/x/oauth2"
)
const defaultPageSize = 100
// Client is the interface to interact with the KEB /orchestrations and /upgrade API
// as an HTTP client using OIDC ID token in JWT format.
type Client interface {
ListOrchestrations(params ListParameters) (StatusResponseList, error)
GetOrchestration(orchestrationID string) (StatusResponse, error)
ListOperations(orchestrationID string, params ListParameters) (OperationResponseList, error)
GetOperation(orchestrationID, operationID string) (OperationDetailResponse, error)
UpgradeKyma(params Parameters) (UpgradeResponse, error)
UpgradeCluster(params Parameters) (UpgradeResponse, error)
CancelOrchestration(orchestrationID string) error
RetryOrchestration(orchestrationID string, operationIDs []string, now bool) (RetryResponse, error)
}
type client struct {
url string
httpClient *http.Client
}
// NewClient constructs and returns new Client for KEB /runtimes API
// It takes the following arguments:
// - ctx : context in which the http request will be executed
// - url : base url of all KEB APIs, e.g. https://kyma-env-broker.kyma.local
// - auth : TokenSource object which provides the ID token for the HTTP request
func NewClient(ctx context.Context, url string, auth oauth2.TokenSource) Client {
return &client{
url: url,
httpClient: oauth2.NewClient(ctx, auth),
}
}
// ListOrchestrations fetches the orchestrations from KEB according to the given params.
// If params.Page or params.PageSize is not set (zero), the client will fetch and return all orchestrations.
func (c client) ListOrchestrations(params ListParameters) (StatusResponseList, error) {
orchestrations := StatusResponseList{}
getAll := false
fetchedAll := false
if params.Page == 0 || params.PageSize == 0 {
getAll = true
params.Page = 1
if params.PageSize == 0 {
params.PageSize = defaultPageSize
}
}
for !fetchedAll {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/orchestrations", c.url), nil)
if err != nil {
return orchestrations, fmt.Errorf("while creating request: %w", err)
}
setQuery(req.URL, params)
resp, err := c.httpClient.Do(req)
if err != nil {
return orchestrations, fmt.Errorf("while calling %s: %w", req.URL.String(), err)
}
// Drain response body and close, return error to context if there isn't any.
defer func() {
derr := drainResponseBody(resp.Body)
if err == nil {
err = derr
}
cerr := resp.Body.Close()
if err == nil {
err = cerr
}
}()
if resp.StatusCode != http.StatusOK {
return orchestrations, fmt.Errorf("calling %s returned %s status", req.URL.String(), resp.Status)
}
var srl StatusResponseList
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&srl)
if err != nil {
return orchestrations, fmt.Errorf("while decoding response body: %w", err)
}
orchestrations.TotalCount = srl.TotalCount
orchestrations.Count += srl.Count
orchestrations.Data = append(orchestrations.Data, srl.Data...)
if getAll {
params.Page++
fetchedAll = orchestrations.Count >= orchestrations.TotalCount
} else {
fetchedAll = true
}
}
return orchestrations, nil
}
// GetOrchestration fetches one orchestration by the given ID.
func (c client) GetOrchestration(orchestrationID string) (StatusResponse, error) {
orchestration := StatusResponse{}
url := fmt.Sprintf("%s/orchestrations/%s", c.url, orchestrationID)
resp, err := c.httpClient.Get(url)
if err != nil {
return orchestration, fmt.Errorf("while calling %s: %w", url, err)
}
// Drain response body and close, return error to context if there isn't any.
defer func() {
derr := drainResponseBody(resp.Body)
if err == nil {
err = derr
}
cerr := resp.Body.Close()
if err == nil {
err = cerr
}
}()
if resp.StatusCode != http.StatusOK {
return orchestration, fmt.Errorf("calling %s returned %s status", url, resp.Status)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&orchestration)
if err != nil {
return orchestration, fmt.Errorf("while decoding response body: %w", err)
}
return orchestration, nil
}
// ListOperations fetches the Runtime operations of a given orchestration from KEB according to the given params.
// If params.Page or params.PageSize is not set (zero), the client will fetch and return all operations.
func (c client) ListOperations(orchestrationID string, params ListParameters) (OperationResponseList, error) {
operations := OperationResponseList{}
url := fmt.Sprintf("%s/orchestrations/%s/operations", c.url, orchestrationID)
getAll := false
fetchedAll := false
if params.Page == 0 || params.PageSize == 0 {
getAll = true
params.Page = 1
if params.PageSize == 0 {
params.PageSize = defaultPageSize
}
}
for !fetchedAll {
if params.Page > 1 {
failedFound, failedIndex := c.searchFilter(params.States, "failed")
if failedFound {
params.States = c.removeIndex(params.States, failedIndex)
}
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return operations, fmt.Errorf("while creating request: %w", err)
}
setQuery(req.URL, params)
resp, err := c.httpClient.Do(req)
if err != nil {
return operations, fmt.Errorf("while calling %s: %w", url, err)
}
// Drain response body and close, return error to context if there isn't any.
defer func() {
derr := drainResponseBody(resp.Body)
if err == nil {
err = derr
}
cerr := resp.Body.Close()
if err == nil {
err = cerr
}
}()
if resp.StatusCode != http.StatusOK {
return operations, fmt.Errorf("calling %s returned %s status", url, resp.Status)
}
var orl OperationResponseList
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&orl)
if err != nil {
return operations, fmt.Errorf("while decoding response body: %w", err)
}
operations.TotalCount = orl.TotalCount
operations.Count += orl.Count
operations.Data = append(operations.Data, orl.Data...)
if getAll {
params.Page++
fetchedAll = operations.Count >= operations.TotalCount
} else {
fetchedAll = true
}
}
return operations, nil
}
func (c client) searchFilter(states []string, inputState string) (bool, int) {
var failedFound bool
var failedIndex int
for index, state := range states {
if strings.Contains(state, inputState) {
failedFound = true
failedIndex = index
break
}
}
return failedFound, failedIndex
}
func (c client) removeIndex(arr []string, index int) []string {
var temp = make([]string, len(arr)-1)
j := 0
for i := range arr {
if i != index {
temp[j] = arr[i]
j = j + 1
}
}
return temp
}
// GetOperation fetches detailed Runtime operation corresponding to the given orchestration and operation ID.
func (c client) GetOperation(orchestrationID, operationID string) (OperationDetailResponse, error) {
operation := OperationDetailResponse{}
url := fmt.Sprintf("%s/orchestrations/%s/operations/%s", c.url, orchestrationID, operationID)
resp, err := c.httpClient.Get(url)
if err != nil {
return operation, fmt.Errorf("while calling %s: %w", url, err)
}
// Drain response body and close, return error to context if there isn't any.
defer func() {
derr := drainResponseBody(resp.Body)
if err == nil {
err = derr
}
cerr := resp.Body.Close()
if err == nil {
err = cerr
}
}()
if resp.StatusCode != http.StatusOK {
return operation, fmt.Errorf("calling %s returned %s status", url, resp.Status)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&operation)
if err != nil {
return operation, fmt.Errorf("while decoding response body: %w", err)
}
return operation, nil
}
// UpgradeKyma creates a new Kyma upgrade orchestration according to the given orchestration parameters.
// If successful, the UpgradeResponse returned contains the ID of the newly created orchestration.
func (c client) UpgradeKyma(params Parameters) (UpgradeResponse, error) {
uri := "/upgrade/kyma"
ur, err := c.upgradeOperation(uri, params)
if err != nil {
return ur, fmt.Errorf("while calling kyma upgrade operation: %w", err)
}
return ur, nil
}
// UpgradeCluster creates a new Cluster upgrade orchestration according to the given orchestration parameters.
// If successful, the UpgradeResponse returned contains the ID of the newly created orchestration.
func (c client) UpgradeCluster(params Parameters) (UpgradeResponse, error) {
uri := "/upgrade/cluster"
ur, err := c.upgradeOperation(uri, params)
if err != nil {
return ur, fmt.Errorf("while calling cluster upgrade operation: %w", err)
}
return ur, nil
}
// common func trigger kyma or cluster upgrade
func (c client) upgradeOperation(uri string, params Parameters) (UpgradeResponse, error) {
ur := UpgradeResponse{}
blob, err := json.Marshal(params)
if err != nil {
return ur, fmt.Errorf("while converting upgrade parameters to JSON: %w", err)
}
u, err := url.Parse(c.url)
if err != nil {
return ur, fmt.Errorf("while parsing %s: %w", c.url, err)
}
u.Path = path.Join(u.Path, uri)
resp, err := c.httpClient.Post(u.String(), "application/json", bytes.NewBuffer(blob))
if err != nil {
return ur, fmt.Errorf("while calling %s: %w", u, err)
}
// Drain response body and close, return error to context if there isn't any.
defer func() {
derr := drainResponseBody(resp.Body)
if err == nil {
err = derr
}
cerr := resp.Body.Close()
if err == nil {
err = cerr
}
}()
if resp.StatusCode != http.StatusAccepted {
return ur, fmt.Errorf("calling %s returned %s status", u, resp.Status)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&ur)
if err != nil {
return ur, fmt.Errorf("while decoding response body: %w", err)
}
return ur, nil
}
func (c client) RetryOrchestration(orchestrationID string, operationIDs []string, now bool) (RetryResponse, error) {
rr := RetryResponse{}
uri := fmt.Sprintf("%s/orchestrations/%s/retry", c.url, orchestrationID)
for i, id := range operationIDs {
operationIDs[i] = "operation-id=" + id
}
str := strings.Join(operationIDs, "&")
if now {
str = str + "&immediate=true"
}
body := strings.NewReader(str)
req, err := http.NewRequest(http.MethodPost, uri, body)
if err != nil {
return rr, fmt.Errorf("while creating retry request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.httpClient.Do(req)
if err != nil {
return rr, fmt.Errorf("while calling %s: %w", uri, err)
}
// Drain response body and close, return error to context if there isn't any.
defer func() {
derr := drainResponseBody(resp.Body)
if err == nil {
err = derr
}
cerr := resp.Body.Close()
if err == nil {
err = cerr
}
}()
if resp.StatusCode != http.StatusAccepted {
return rr, fmt.Errorf("calling %s returned %s status", uri, resp.Status)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&rr)
if err != nil {
return rr, fmt.Errorf("while decoding response body: %w", err)
}
return rr, nil
}
func (c client) CancelOrchestration(orchestrationID string) error {
url := fmt.Sprintf("%s/orchestrations/%s/cancel", c.url, orchestrationID)
req, err := http.NewRequest(http.MethodPut, url, nil)
if err != nil {
return fmt.Errorf("while creating cancel request: %w", err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("while calling %s: %w", url, err)
}
// Drain response body and close, return error to context if there isn't any.
defer func() {
derr := drainResponseBody(resp.Body)
if err == nil {
err = derr
}
cerr := resp.Body.Close()
if err == nil {
err = cerr
}
}()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("calling %s returned %s status", url, resp.Status)
}
return nil
}
func setQuery(url *url.URL, params ListParameters) {
query := url.Query()
query.Add(pagination.PageParam, strconv.Itoa(params.Page))
query.Add(pagination.PageSizeParam, strconv.Itoa(params.PageSize))
setParamList(query, StateParam, params.States)
url.RawQuery = query.Encode()
}
func setParamList(query url.Values, key string, values []string) {
for _, value := range values {
query.Add(key, value)
}
}
func drainResponseBody(body io.Reader) error {
if body == nil {
return nil
}
_, err := io.Copy(ioutil.Discard, io.LimitReader(body, 4096))
return err
}