forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes.go
419 lines (349 loc) · 13.6 KB
/
kubernetes.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
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const vkePath = "/v2/kubernetes/clusters"
// KubernetesService is the interface to interact with kubernetes endpoint on the Vultr API
// Link : https://www.vultr.com/api/#tag/kubernetes
type KubernetesService interface {
CreateCluster(ctx context.Context, createReq *ClusterReq) (*Cluster, *http.Response, error)
GetCluster(ctx context.Context, id string) (*Cluster, *http.Response, error)
ListClusters(ctx context.Context, options *ListOptions) ([]Cluster, *Meta, *http.Response, error)
UpdateCluster(ctx context.Context, vkeID string, updateReq *ClusterReqUpdate) error
DeleteCluster(ctx context.Context, id string) error
DeleteClusterWithResources(ctx context.Context, id string) error
CreateNodePool(ctx context.Context, vkeID string, nodePoolReq *NodePoolReq) (*NodePool, *http.Response, error)
ListNodePools(ctx context.Context, vkeID string, options *ListOptions) ([]NodePool, *Meta, *http.Response, error)
GetNodePool(ctx context.Context, vkeID, nodePoolID string) (*NodePool, *http.Response, error)
UpdateNodePool(ctx context.Context, vkeID, nodePoolID string, updateReq *NodePoolReqUpdate) (*NodePool, *http.Response, error)
DeleteNodePool(ctx context.Context, vkeID, nodePoolID string) error
DeleteNodePoolInstance(ctx context.Context, vkeID, nodePoolID, nodeID string) error
RecycleNodePoolInstance(ctx context.Context, vkeID, nodePoolID, nodeID string) error
GetKubeConfig(ctx context.Context, vkeID string) (*KubeConfig, *http.Response, error)
GetVersions(ctx context.Context) (*Versions, *http.Response, error)
GetUpgrades(ctx context.Context, vkeID string) ([]string, *http.Response, error)
Upgrade(ctx context.Context, vkeID string, body *ClusterUpgradeReq) error
}
// KubernetesHandler handles interaction with the kubernetes methods for the Vultr API
type KubernetesHandler struct {
client *Client
}
// Cluster represents a full VKE cluster
type Cluster struct {
ID string `json:"id"`
Label string `json:"label"`
DateCreated string `json:"date_created"`
ClusterSubnet string `json:"cluster_subnet"`
ServiceSubnet string `json:"service_subnet"`
IP string `json:"ip"`
Endpoint string `json:"endpoint"`
Version string `json:"version"`
Region string `json:"region"`
Status string `json:"status"`
HAControlPlanes bool `json:"ha_controlplanes"`
FirewallGroupID string `json:"firewall_group_id"`
NodePools []NodePool `json:"node_pools"`
}
// NodePool represents a pool of nodes that are grouped by their label and plan type
type NodePool struct {
ID string `json:"id"`
DateCreated string `json:"date_created"`
DateUpdated string `json:"date_updated"`
Label string `json:"label"`
Plan string `json:"plan"`
Status string `json:"status"`
NodeQuantity int `json:"node_quantity"`
MinNodes int `json:"min_nodes"`
MaxNodes int `json:"max_nodes"`
AutoScaler bool `json:"auto_scaler"`
Tag string `json:"tag"`
Labels map[string]string `json:"labels"`
Nodes []Node `json:"nodes"`
}
// Node represents a node that will live within a nodepool
type Node struct {
ID string `json:"id"`
DateCreated string `json:"date_created"`
Label string `json:"label"`
Status string `json:"status"`
}
// KubeConfig will contain the kubeconfig b64 encoded
type KubeConfig struct {
KubeConfig string `json:"kube_config"`
}
// ClusterReq struct used to create a cluster
type ClusterReq struct {
Label string `json:"label"`
Region string `json:"region"`
Version string `json:"version"`
HAControlPlanes bool `json:"ha_controlplanes,omitempty"`
EnableFirewall bool `json:"enable_firewall,omitempty"`
NodePools []NodePoolReq `json:"node_pools"`
}
// ClusterReqUpdate struct used to update update a cluster
type ClusterReqUpdate struct {
Label string `json:"label"`
}
// NodePoolReq struct used to create a node pool
type NodePoolReq struct {
NodeQuantity int `json:"node_quantity"`
Label string `json:"label"`
Plan string `json:"plan"`
Tag string `json:"tag"`
MinNodes int `json:"min_nodes,omitempty"`
MaxNodes int `json:"max_nodes,omitempty"`
AutoScaler *bool `json:"auto_scaler"`
Labels map[string]string `json:"labels,omitempty"`
}
// NodePoolReqUpdate struct used to update a node pool
type NodePoolReqUpdate struct {
NodeQuantity int `json:"node_quantity,omitempty"`
Tag *string `json:"tag,omitempty"`
MinNodes int `json:"min_nodes,omitempty"`
MaxNodes int `json:"max_nodes,omitempty"`
AutoScaler *bool `json:"auto_scaler,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
type vkeClustersBase struct {
VKEClusters []Cluster `json:"vke_clusters"`
Meta *Meta `json:"meta"`
}
type vkeClusterBase struct {
VKECluster *Cluster `json:"vke_cluster"`
}
type vkeNodePoolsBase struct {
NodePools []NodePool `json:"node_pools"`
Meta *Meta `json:"meta"`
}
type vkeNodePoolBase struct {
NodePool *NodePool `json:"node_pool"`
}
// Versions that are supported for VKE
type Versions struct {
Versions []string `json:"versions"`
}
// AvailableUpgrades for a given VKE cluster
type availableUpgrades struct {
AvailableUpgrades []string `json:"available_upgrades"`
}
// ClusterUpgradeReq struct for vke upgradse
type ClusterUpgradeReq struct {
UpgradeVersion string `json:"upgrade_version,omitempty"`
}
// CreateCluster will create a Kubernetes cluster.
func (k *KubernetesHandler) CreateCluster(ctx context.Context, createReq *ClusterReq) (*Cluster, *http.Response, error) {
req, err := k.client.NewRequest(ctx, http.MethodPost, vkePath, createReq)
if err != nil {
return nil, nil, err
}
var k8 = new(vkeClusterBase)
resp, err := k.client.DoWithContext(ctx, req, &k8)
if err != nil {
return nil, resp, err
}
return k8.VKECluster, resp, nil
}
// GetCluster will return a Kubernetes cluster.
func (k *KubernetesHandler) GetCluster(ctx context.Context, id string) (*Cluster, *http.Response, error) {
req, err := k.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s", vkePath, id), nil)
if err != nil {
return nil, nil, err
}
k8 := new(vkeClusterBase)
resp, err := k.client.DoWithContext(ctx, req, &k8)
if err != nil {
return nil, resp, err
}
return k8.VKECluster, resp, nil
}
// ListClusters will return all kubernetes clusters.
func (k *KubernetesHandler) ListClusters(ctx context.Context, options *ListOptions) ([]Cluster, *Meta, *http.Response, error) { //nolint:dupl,lll
req, err := k.client.NewRequest(ctx, http.MethodGet, vkePath, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
k8s := new(vkeClustersBase)
resp, err := k.client.DoWithContext(ctx, req, &k8s)
if err != nil {
return nil, nil, resp, err
}
return k8s.VKEClusters, k8s.Meta, resp, nil
}
// UpdateCluster updates label on VKE cluster
func (k *KubernetesHandler) UpdateCluster(ctx context.Context, vkeID string, updateReq *ClusterReqUpdate) error {
req, err := k.client.NewRequest(ctx, http.MethodPut, fmt.Sprintf("%s/%s", vkePath, vkeID), updateReq)
if err != nil {
return err
}
_, err = k.client.DoWithContext(ctx, req, nil)
return err
}
// DeleteCluster will delete a Kubernetes cluster.
func (k *KubernetesHandler) DeleteCluster(ctx context.Context, id string) error {
req, err := k.client.NewRequest(ctx, http.MethodDelete, fmt.Sprintf("%s/%s", vkePath, id), nil)
if err != nil {
return err
}
_, err = k.client.DoWithContext(ctx, req, nil)
return err
}
// DeleteClusterWithResources will delete a Kubernetes cluster and all related resources.
func (k *KubernetesHandler) DeleteClusterWithResources(ctx context.Context, id string) error {
req, err := k.client.NewRequest(ctx, http.MethodDelete, fmt.Sprintf("%s/%s/delete-with-linked-resources", vkePath, id), nil)
if err != nil {
return err
}
_, err = k.client.DoWithContext(ctx, req, nil)
return err
}
// CreateNodePool creates a nodepool on a VKE cluster
func (k *KubernetesHandler) CreateNodePool(ctx context.Context, vkeID string, nodePoolReq *NodePoolReq) (*NodePool, *http.Response, error) {
req, err := k.client.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s/%s/node-pools", vkePath, vkeID), nodePoolReq)
if err != nil {
return nil, nil, err
}
n := new(vkeNodePoolBase)
resp, err := k.client.DoWithContext(ctx, req, n)
if err != nil {
return nil, resp, err
}
return n.NodePool, resp, nil
}
// ListNodePools will return all nodepools for a given VKE cluster
func (k *KubernetesHandler) ListNodePools(ctx context.Context, vkeID string, options *ListOptions) ([]NodePool, *Meta, *http.Response, error) { //nolint:lll,dupl
req, err := k.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s/node-pools", vkePath, vkeID), nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
n := new(vkeNodePoolsBase)
resp, err := k.client.DoWithContext(ctx, req, &n)
if err != nil {
return nil, nil, resp, err
}
return n.NodePools, n.Meta, resp, nil
}
// GetNodePool will return a single nodepool
func (k *KubernetesHandler) GetNodePool(ctx context.Context, vkeID, nodePoolID string) (*NodePool, *http.Response, error) {
req, err := k.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s/node-pools/%s", vkePath, vkeID, nodePoolID), nil)
if err != nil {
return nil, nil, err
}
n := new(vkeNodePoolBase)
resp, err := k.client.DoWithContext(ctx, req, &n)
if err != nil {
return nil, resp, err
}
return n.NodePool, resp, nil
}
// UpdateNodePool will allow you change the quantity of nodes within a nodepool
func (k *KubernetesHandler) UpdateNodePool(ctx context.Context, vkeID, nodePoolID string, updateReq *NodePoolReqUpdate) (*NodePool, *http.Response, error) { //nolint:lll
req, err := k.client.NewRequest(ctx, http.MethodPatch, fmt.Sprintf("%s/%s/node-pools/%s", vkePath, vkeID, nodePoolID), updateReq)
if err != nil {
return nil, nil, err
}
np := new(vkeNodePoolBase)
resp, err := k.client.DoWithContext(ctx, req, np)
if err != nil {
return nil, resp, err
}
return np.NodePool, resp, nil
}
// DeleteNodePool will remove a nodepool from a VKE cluster
func (k *KubernetesHandler) DeleteNodePool(ctx context.Context, vkeID, nodePoolID string) error {
req, err := k.client.NewRequest(ctx, http.MethodDelete, fmt.Sprintf("%s/%s/node-pools/%s", vkePath, vkeID, nodePoolID), nil)
if err != nil {
return err
}
_, err = k.client.DoWithContext(ctx, req, nil)
return err
}
// DeleteNodePoolInstance will remove a specified node from a nodepool
func (k *KubernetesHandler) DeleteNodePoolInstance(ctx context.Context, vkeID, nodePoolID, nodeID string) error {
req, err := k.client.NewRequest(
ctx,
http.MethodDelete,
fmt.Sprintf("%s/%s/node-pools/%s/nodes/%s", vkePath, vkeID, nodePoolID, nodeID),
nil,
)
if err != nil {
return err
}
_, err = k.client.DoWithContext(ctx, req, nil)
return err
}
// RecycleNodePoolInstance will recycle (destroy + redeploy) a given node on a nodepool
func (k *KubernetesHandler) RecycleNodePoolInstance(ctx context.Context, vkeID, nodePoolID, nodeID string) error {
req, err := k.client.NewRequest(
ctx,
http.MethodPost,
fmt.Sprintf("%s/%s/node-pools/%s/nodes/%s/recycle", vkePath, vkeID, nodePoolID, nodeID),
nil,
)
if err != nil {
return err
}
_, err = k.client.DoWithContext(ctx, req, nil)
return err
}
// GetKubeConfig returns the kubeconfig for the specified VKE cluster
func (k *KubernetesHandler) GetKubeConfig(ctx context.Context, vkeID string) (*KubeConfig, *http.Response, error) {
req, err := k.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s/config", vkePath, vkeID), nil)
if err != nil {
return nil, nil, err
}
kc := new(KubeConfig)
resp, err := k.client.DoWithContext(ctx, req, &kc)
if err != nil {
return nil, resp, err
}
return kc, resp, nil
}
// GetVersions returns the supported kubernetes versions
func (k *KubernetesHandler) GetVersions(ctx context.Context) (*Versions, *http.Response, error) {
uri := "/v2/kubernetes/versions"
req, err := k.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
versions := new(Versions)
resp, err := k.client.DoWithContext(ctx, req, &versions)
if err != nil {
return nil, resp, err
}
return versions, resp, nil
}
// GetUpgrades returns all version a VKE cluster can upgrade to
func (k *KubernetesHandler) GetUpgrades(ctx context.Context, vkeID string) ([]string, *http.Response, error) {
req, err := k.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s/available-upgrades", vkePath, vkeID), nil)
if err != nil {
return nil, nil, err
}
upgrades := new(availableUpgrades)
resp, err := k.client.DoWithContext(ctx, req, &upgrades)
if err != nil {
return nil, resp, err
}
return upgrades.AvailableUpgrades, resp, nil
}
// Upgrade beings a VKE cluster upgrade
func (k *KubernetesHandler) Upgrade(ctx context.Context, vkeID string, body *ClusterUpgradeReq) error {
req, err := k.client.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s/%s/upgrades", vkePath, vkeID), body)
if err != nil {
return err
}
_, err = k.client.DoWithContext(ctx, req, nil)
return err
}