forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lke_clusters.go
355 lines (301 loc) · 11.2 KB
/
lke_clusters.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
package linodego
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"
"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)
// LKEClusterStatus represents the status of an LKECluster
type LKEClusterStatus string
// LKEClusterStatus enums start with LKECluster
const (
LKEClusterReady LKEClusterStatus = "ready"
LKEClusterNotReady LKEClusterStatus = "not_ready"
)
// LKECluster represents a LKECluster object
type LKECluster struct {
ID int `json:"id"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
Label string `json:"label"`
Region string `json:"region"`
Status LKEClusterStatus `json:"status"`
K8sVersion string `json:"k8s_version"`
Tags []string `json:"tags"`
ControlPlane LKEClusterControlPlane `json:"control_plane"`
}
// LKEClusterCreateOptions fields are those accepted by CreateLKECluster
type LKEClusterCreateOptions struct {
NodePools []LKENodePoolCreateOptions `json:"node_pools"`
Label string `json:"label"`
Region string `json:"region"`
K8sVersion string `json:"k8s_version"`
Tags []string `json:"tags,omitempty"`
ControlPlane *LKEClusterControlPlane `json:"control_plane,omitempty"`
}
// LKEClusterUpdateOptions fields are those accepted by UpdateLKECluster
type LKEClusterUpdateOptions struct {
K8sVersion string `json:"k8s_version,omitempty"`
Label string `json:"label,omitempty"`
Tags *[]string `json:"tags,omitempty"`
ControlPlane *LKEClusterControlPlane `json:"control_plane,omitempty"`
}
// LKEClusterAPIEndpoint fields are those returned by ListLKEClusterAPIEndpoints
type LKEClusterAPIEndpoint struct {
Endpoint string `json:"endpoint"`
}
// LKEClusterKubeconfig fields are those returned by GetLKEClusterKubeconfig
type LKEClusterKubeconfig struct {
KubeConfig string `json:"kubeconfig"`
}
// LKEClusterDashboard fields are those returned by GetLKEClusterDashboard
type LKEClusterDashboard struct {
URL string `json:"url"`
}
// LKEClusterControlPlane fields contained within the `control_plane` attribute of an LKE cluster.
type LKEClusterControlPlane struct {
HighAvailability bool `json:"high_availability"`
}
// LKEVersion fields are those returned by GetLKEVersion
type LKEVersion struct {
ID string `json:"id"`
}
// LKEClusterRegenerateOptions fields are those accepted by RegenerateLKECluster
type LKEClusterRegenerateOptions struct {
KubeConfig bool `json:"kubeconfig"`
ServiceToken bool `json:"servicetoken"`
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (i *LKECluster) UnmarshalJSON(b []byte) error {
type Mask LKECluster
p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
}{
Mask: (*Mask)(i),
}
if err := json.Unmarshal(b, &p); err != nil {
return err
}
i.Created = (*time.Time)(p.Created)
i.Updated = (*time.Time)(p.Updated)
return nil
}
// GetCreateOptions converts a LKECluster to LKEClusterCreateOptions for use in CreateLKECluster
func (i LKECluster) GetCreateOptions() (o LKEClusterCreateOptions) {
o.Label = i.Label
o.Region = i.Region
o.K8sVersion = i.K8sVersion
o.Tags = i.Tags
o.ControlPlane = &i.ControlPlane
// @TODO copy NodePools?
return
}
// GetUpdateOptions converts a LKECluster to LKEClusterUpdateOptions for use in UpdateLKECluster
func (i LKECluster) GetUpdateOptions() (o LKEClusterUpdateOptions) {
o.K8sVersion = i.K8sVersion
o.Label = i.Label
o.Tags = &i.Tags
o.ControlPlane = &i.ControlPlane
return
}
// LKEVersionsPagedResponse represents a paginated LKEVersion API response
type LKEVersionsPagedResponse struct {
*PageOptions
Data []LKEVersion `json:"data"`
}
// endpoint gets the endpoint URL for LKEVersion
func (LKEVersionsPagedResponse) endpoint(_ ...any) string {
return "lke/versions"
}
func (resp *LKEVersionsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(LKEVersionsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*LKEVersionsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListLKEVersions lists the Kubernetes versions available through LKE. This endpoint is cached by default.
func (c *Client) ListLKEVersions(ctx context.Context, opts *ListOptions) ([]LKEVersion, error) {
response := LKEVersionsPagedResponse{}
endpoint, err := generateListCacheURL(response.endpoint(), opts)
if err != nil {
return nil, err
}
if result := c.getCachedResponse(endpoint); result != nil {
return result.([]LKEVersion), nil
}
err = c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
c.addCachedResponse(endpoint, response.Data, &cacheExpiryTime)
return response.Data, nil
}
// GetLKEVersion gets details about a specific LKE Version. This endpoint is cached by default.
func (c *Client) GetLKEVersion(ctx context.Context, version string) (*LKEVersion, error) {
version = url.PathEscape(version)
e := fmt.Sprintf("lke/versions/%s", version)
if result := c.getCachedResponse(e); result != nil {
result := result.(LKEVersion)
return &result, nil
}
req := c.R(ctx).SetResult(&LKEVersion{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
c.addCachedResponse(e, r.Result(), &cacheExpiryTime)
return r.Result().(*LKEVersion), nil
}
// LKEClusterAPIEndpointsPagedResponse represents a paginated LKEClusterAPIEndpoints API response
type LKEClusterAPIEndpointsPagedResponse struct {
*PageOptions
Data []LKEClusterAPIEndpoint `json:"data"`
}
// endpoint gets the endpoint URL for LKEClusterAPIEndpointsPagedResponse
func (LKEClusterAPIEndpointsPagedResponse) endpoint(ids ...any) string {
id := ids[0].(int)
return fmt.Sprintf("lke/clusters/%d/api-endpoints", id)
}
func (resp *LKEClusterAPIEndpointsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(LKEClusterAPIEndpointsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*LKEClusterAPIEndpointsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListLKEClusterAPIEndpoints gets the API Endpoint for the LKE Cluster specified
func (c *Client) ListLKEClusterAPIEndpoints(ctx context.Context, clusterID int, opts *ListOptions) ([]LKEClusterAPIEndpoint, error) {
response := LKEClusterAPIEndpointsPagedResponse{}
err := c.listHelper(ctx, &response, opts, clusterID)
if err != nil {
return nil, err
}
return response.Data, nil
}
// LKEClustersPagedResponse represents a paginated LKECluster API response
type LKEClustersPagedResponse struct {
*PageOptions
Data []LKECluster `json:"data"`
}
// endpoint gets the endpoint URL for LKECluster
func (LKEClustersPagedResponse) endpoint(_ ...any) string {
return "lke/clusters"
}
func (resp *LKEClustersPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(LKEClustersPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*LKEClustersPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListLKEClusters lists LKEClusters
func (c *Client) ListLKEClusters(ctx context.Context, opts *ListOptions) ([]LKECluster, error) {
response := LKEClustersPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetLKECluster gets the lkeCluster with the provided ID
func (c *Client) GetLKECluster(ctx context.Context, clusterID int) (*LKECluster, error) {
e := fmt.Sprintf("lke/clusters/%d", clusterID)
req := c.R(ctx).SetResult(&LKECluster{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*LKECluster), nil
}
// CreateLKECluster creates a LKECluster
func (c *Client) CreateLKECluster(ctx context.Context, opts LKEClusterCreateOptions) (*LKECluster, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := "lke/clusters"
req := c.R(ctx).SetResult(&LKECluster{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
return r.Result().(*LKECluster), nil
}
// UpdateLKECluster updates the LKECluster with the specified id
func (c *Client) UpdateLKECluster(ctx context.Context, clusterID int, opts LKEClusterUpdateOptions) (*LKECluster, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := fmt.Sprintf("lke/clusters/%d", clusterID)
req := c.R(ctx).SetResult(&LKECluster{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}
return r.Result().(*LKECluster), nil
}
// DeleteLKECluster deletes the LKECluster with the specified id
func (c *Client) DeleteLKECluster(ctx context.Context, clusterID int) error {
e := fmt.Sprintf("lke/clusters/%d", clusterID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
}
// GetLKEClusterKubeconfig gets the Kubeconfig for the LKE Cluster specified
func (c *Client) GetLKEClusterKubeconfig(ctx context.Context, clusterID int) (*LKEClusterKubeconfig, error) {
e := fmt.Sprintf("lke/clusters/%d/kubeconfig", clusterID)
req := c.R(ctx).SetResult(&LKEClusterKubeconfig{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*LKEClusterKubeconfig), nil
}
// GetLKEClusterDashboard gets information about the dashboard for an LKE cluster
func (c *Client) GetLKEClusterDashboard(ctx context.Context, clusterID int) (*LKEClusterDashboard, error) {
e := fmt.Sprintf("lke/clusters/%d/dashboard", clusterID)
req := c.R(ctx).SetResult(&LKEClusterDashboard{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*LKEClusterDashboard), nil
}
// RecycleLKEClusterNodes recycles all nodes in all pools of the specified LKE Cluster.
func (c *Client) RecycleLKEClusterNodes(ctx context.Context, clusterID int) error {
e := fmt.Sprintf("lke/clusters/%d/recycle", clusterID)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
return err
}
// RegenerateLKECluster regenerates the Kubeconfig file and/or the service account token for the specified LKE Cluster.
func (c *Client) RegenerateLKECluster(ctx context.Context, clusterID int, opts LKEClusterRegenerateOptions) (*LKECluster, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := fmt.Sprintf("lke/clusters/%d/regenerate", clusterID)
req := c.R(ctx).SetResult(&LKECluster{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
return r.Result().(*LKECluster), nil
}
// DeleteLKEClusterServiceToken deletes and regenerate the service account token for a Cluster.
func (c *Client) DeleteLKEClusterServiceToken(ctx context.Context, clusterID int) error {
e := fmt.Sprintf("lke/clusters/%d/servicetoken", clusterID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
}