-
Notifications
You must be signed in to change notification settings - Fork 82
/
lke_clusters.go
280 lines (225 loc) · 8.76 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
package linodego
import (
"context"
"encoding/json"
"time"
"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 *LKEClusterControlPlaneOptions `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 *LKEClusterControlPlaneOptions `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"`
}
// 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
isHA := i.ControlPlane.HighAvailability
o.ControlPlane = &LKEClusterControlPlaneOptions{
HighAvailability: &isHA,
// ACL will not be populated in the control plane response
}
// @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
isHA := i.ControlPlane.HighAvailability
o.ControlPlane = &LKEClusterControlPlaneOptions{
HighAvailability: &isHA,
// ACL will not be populated in the control plane response
}
return
}
// 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) {
e := "lke/versions"
endpoint, err := generateListCacheURL(e, opts)
if err != nil {
return nil, err
}
if result := c.getCachedResponse(endpoint); result != nil {
return result.([]LKEVersion), nil
}
response, err := getPaginatedResults[LKEVersion](ctx, c, e, opts)
if err != nil {
return nil, err
}
c.addCachedResponse(endpoint, response, &cacheExpiryTime)
return response, 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) {
e := formatAPIPath("lke/versions/%s", version)
if result := c.getCachedResponse(e); result != nil {
result := result.(LKEVersion)
return &result, nil
}
response, err := doGETRequest[LKEVersion](ctx, c, e)
if err != nil {
return nil, err
}
c.addCachedResponse(e, response, &cacheExpiryTime)
return response, 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, err := getPaginatedResults[LKEClusterAPIEndpoint](ctx, c, formatAPIPath("lke/clusters/%d/api-endpoints", clusterID), opts)
if err != nil {
return nil, err
}
return response, nil
}
// ListLKEClusters lists LKEClusters
func (c *Client) ListLKEClusters(ctx context.Context, opts *ListOptions) ([]LKECluster, error) {
response, err := getPaginatedResults[LKECluster](ctx, c, "lke/clusters", opts)
return response, err
}
// GetLKECluster gets the lkeCluster with the provided ID
func (c *Client) GetLKECluster(ctx context.Context, clusterID int) (*LKECluster, error) {
e := formatAPIPath("lke/clusters/%d", clusterID)
response, err := doGETRequest[LKECluster](ctx, c, e)
if err != nil {
return nil, err
}
return response, nil
}
// CreateLKECluster creates a LKECluster
func (c *Client) CreateLKECluster(ctx context.Context, opts LKEClusterCreateOptions) (*LKECluster, error) {
e := "lke/clusters"
response, err := doPOSTRequest[LKECluster](ctx, c, e, opts)
if err != nil {
return nil, err
}
return response, nil
}
// UpdateLKECluster updates the LKECluster with the specified id
func (c *Client) UpdateLKECluster(ctx context.Context, clusterID int, opts LKEClusterUpdateOptions) (*LKECluster, error) {
e := formatAPIPath("lke/clusters/%d", clusterID)
response, err := doPUTRequest[LKECluster](ctx, c, e, opts)
if err != nil {
return nil, err
}
return response, nil
}
// DeleteLKECluster deletes the LKECluster with the specified id
func (c *Client) DeleteLKECluster(ctx context.Context, clusterID int) error {
e := formatAPIPath("lke/clusters/%d", clusterID)
return doDELETERequest(ctx, c, e)
}
// GetLKEClusterKubeconfig gets the Kubeconfig for the LKE Cluster specified
func (c *Client) GetLKEClusterKubeconfig(ctx context.Context, clusterID int) (*LKEClusterKubeconfig, error) {
e := formatAPIPath("lke/clusters/%d/kubeconfig", clusterID)
response, err := doGETRequest[LKEClusterKubeconfig](ctx, c, e)
if err != nil {
return nil, err
}
return response, nil
}
// DeleteLKEClusterKubeconfig deletes the Kubeconfig for the LKE Cluster specified
func (c *Client) DeleteLKEClusterKubeconfig(ctx context.Context, clusterID int) error {
e := formatAPIPath("lke/clusters/%d/kubeconfig", clusterID)
return doDELETERequest(ctx, c, e)
}
// GetLKEClusterDashboard gets information about the dashboard for an LKE cluster
func (c *Client) GetLKEClusterDashboard(ctx context.Context, clusterID int) (*LKEClusterDashboard, error) {
e := formatAPIPath("lke/clusters/%d/dashboard", clusterID)
response, err := doGETRequest[LKEClusterDashboard](ctx, c, e)
if err != nil {
return nil, err
}
return response, nil
}
// RecycleLKEClusterNodes recycles all nodes in all pools of the specified LKE Cluster.
func (c *Client) RecycleLKEClusterNodes(ctx context.Context, clusterID int) error {
e := formatAPIPath("lke/clusters/%d/recycle", clusterID)
_, err := doPOSTRequest[LKECluster, any](ctx, c, 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) {
e := formatAPIPath("lke/clusters/%d/regenerate", clusterID)
response, err := doPOSTRequest[LKECluster](ctx, c, e, opts)
if err != nil {
return nil, err
}
return response, nil
}
// DeleteLKEClusterServiceToken deletes and regenerate the service account token for a Cluster.
func (c *Client) DeleteLKEClusterServiceToken(ctx context.Context, clusterID int) error {
e := formatAPIPath("lke/clusters/%d/servicetoken", clusterID)
return doDELETERequest(ctx, c, e)
}