-
Notifications
You must be signed in to change notification settings - Fork 0
/
civo_driver.go
492 lines (402 loc) · 13.7 KB
/
civo_driver.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package main
import (
"context"
"encoding/base64"
"fmt"
"reflect"
"time"
"github.com/rancher/kontainer-engine/types"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd"
"github.com/civo/civogo"
)
// DefaultCivoURL is the production API URL to use
const DefaultCivoURL = "https://api.civo.com"
const retryInterval = 5 * time.Second
const serviceAccountRetryTimeout = 5 * time.Minute
// Driver defines the struct of Civo driver
type Driver struct {
driverCapabilities types.Capabilities
}
type state struct {
APIKey string
// The name of this cluster
Name string
// The region to launch the cluster
Region string
// The kubernetes version
K8sVersion string
NodePools map[string]string // uuid -> size-count
// The network ID to use for the cluster
NetworkID string
// The CNI plugin to use for the cluster
CNIPlugin string
// Firewall ID to apply to the cluster
FirewallID string
// cluster info
ClusterInfo types.ClusterInfo
}
// NewDriver creates a new driver
func NewDriver() types.Driver {
driver := &Driver{
driverCapabilities: types.Capabilities{
Capabilities: make(map[int64]bool),
},
}
driver.driverCapabilities.AddCapability(types.GetVersionCapability)
driver.driverCapabilities.AddCapability(types.SetVersionCapability)
driver.driverCapabilities.AddCapability(types.GetClusterSizeCapability)
driver.driverCapabilities.AddCapability(types.SetClusterSizeCapability)
return driver
}
// GetDriverCreateOptions implements driver interface
func (d *Driver) GetDriverCreateOptions(ctx context.Context) (*types.DriverFlags, error) {
driverFlag := types.DriverFlags{
Options: make(map[string]*types.Flag),
}
driverFlag.Options["api-key"] = &types.Flag{
Type: types.StringType,
Usage: "Civo API key to manage clusters",
}
driverFlag.Options["name"] = &types.Flag{
Type: types.StringType,
Usage: "the internal name of the cluster in Rancher",
}
driverFlag.Options["region"] = &types.Flag{
Type: types.StringType,
Usage: "The region to launch the cluster. Defaults to LON1",
Default: &types.Default{
DefaultString: "LON1",
},
}
driverFlag.Options["kubernetes-version"] = &types.Flag{
Type: types.StringType,
Usage: "The kubernetes version. Defaults to the latest stable version.",
}
driverFlag.Options["node-pools"] = &types.Flag{
Type: types.StringSliceType,
Usage: "The list of node pools created for the cluster. Example: 'node-pools: [\"g3.small: 2\", \"g3.medium: 1\"]'",
}
driverFlag.Options["network-id"] = &types.Flag{
Type: types.StringType,
Usage: "The network ID to use for the cluster. By default, default network is used",
Default: &types.Default{
DefaultString: "default",
},
}
driverFlag.Options["cni-plugin"] = &types.Flag{
Type: types.StringType,
Usage: "The CNI plugin to use for the cluster. By default, flannel is used",
Default: &types.Default{
DefaultString: "flannel",
},
}
driverFlag.Options["firewall-id"] = &types.Flag{
Type: types.StringType,
Usage: "Firewall ID to apply to the cluster. By default: 80,443,6443 ingress will be allowed",
}
return &driverFlag, nil
}
// GetDriverUpdateOptions implements driver interface
func (d *Driver) GetDriverUpdateOptions(ctx context.Context) (*types.DriverFlags, error) {
driverFlag := types.DriverFlags{
Options: make(map[string]*types.Flag),
}
driverFlag.Options["node-pools"] = &types.Flag{
Type: types.StringSliceType,
Usage: "The list of node pools created for the cluster",
}
return &driverFlag, nil
}
// Create creates the cluster in the managed Kubernetes provider and populates sufficient information on the ClusterInfo return value so that PostCheck can connect to the cluster.
func (d *Driver) Create(ctx context.Context, opts *types.DriverOptions, _ *types.ClusterInfo) (*types.ClusterInfo, error) {
state, err := getStateFromOpts(opts)
if err != nil {
return nil, err
}
logrus.Debugf("state.name %s, state: %#v", state.Name, state)
info := &types.ClusterInfo{}
err = storeState(info, state)
if err != nil {
return info, err
}
client, err := d.getServiceClient(state.APIKey, state.Region)
if err != nil {
return info, err
}
req, err := d.generateClusterCreateRequest(state)
if err != nil {
return nil, fmt.Errorf("failed to create cluster: %s", err)
}
logrus.Debugf("Civo api request: %#v", req)
cluster, err := client.NewKubernetesClusters(req)
if err != nil {
return nil, fmt.Errorf("failed to create Civo cluster: %s", err)
}
info.Metadata["cluster-id"] = cluster.ID
err = WaitForClusterActive(ctx, client, cluster.ID, 300)
if err != nil {
return nil, fmt.Errorf("failed to wait for cluster to be active: %s", err)
}
return info, nil
}
// Update updates the cluster in the managed Kubernetes. Like Create it must ensure ClusterInfo is populated with sufficient information for PostCheck to generate a service account token. If no connection information has changed, then it can be left alone and it will reuse the information generated by Create.
func (d *Driver) Update(ctx context.Context, info *types.ClusterInfo, opts *types.DriverOptions) (*types.ClusterInfo, error) {
state, err := getState(info)
if err != nil {
return nil, err
}
logrus.Debugf("state.name %s, state: %#v", state.Name, state)
newState, err := getStateFromOpts(opts)
if err != nil {
return nil, err
}
state.APIKey = newState.APIKey
client, err := d.getServiceClient(state.APIKey, state.Region)
if err != nil {
return info, err
}
var shouldUpdate bool
updateOpts := &civogo.KubernetesClusterConfig{}
if newState.FirewallID != "" && newState.FirewallID != state.FirewallID {
shouldUpdate = true
updateOpts.InstanceFirewall = newState.FirewallID
}
if newState.K8sVersion != "" && newState.K8sVersion != state.K8sVersion {
shouldUpdate = true
updateOpts.KubernetesVersion = newState.K8sVersion
}
if newState.NodePools != nil && !reflect.DeepEqual(newState.NodePools, state.NodePools) {
shouldUpdate = true
for t, sc := range newState.NodePools {
size, count, err := getSizeCountNodePool(t, sc)
if err != nil {
return nil, err
}
updateOpts.Pools = append(updateOpts.Pools, civogo.KubernetesClusterPoolConfig{
ID: t,
Size: size,
Count: count,
})
}
}
clusterID := info.Metadata["cluster-id"]
if shouldUpdate {
_, err = client.UpdateKubernetesCluster(clusterID, &civogo.KubernetesClusterConfig{
InstanceFirewall: newState.FirewallID,
})
if err != nil {
return nil, fmt.Errorf("failed to update cluster %s: %s", clusterID, err)
}
}
return info, storeState(info, state)
}
func (d *Driver) generateClusterCreateRequest(state state) (*civogo.KubernetesClusterConfig, error) {
req := civogo.KubernetesClusterConfig{
Name: state.Name,
Region: state.Region,
KubernetesVersion: state.K8sVersion,
NetworkID: state.NetworkID,
CNIPlugin: state.CNIPlugin,
InstanceFirewall: state.FirewallID,
// Apps?
}
for t, sc := range state.NodePools {
size, count, err := getSizeCountNodePool(t, sc)
if err != nil {
return nil, err
}
req.Pools = append(req.Pools, civogo.KubernetesClusterPoolConfig{
ID: t,
Size: size,
Count: count,
})
}
return &req, nil
}
// PostCheck : This func must populate the ClusterInfo.serviceAccountToken field with a service account token with sufficient permissions for Rancher to manage the cluster.
func (d *Driver) PostCheck(ctx context.Context, info *types.ClusterInfo) (*types.ClusterInfo, error) {
state, err := getState(info)
if err != nil {
return nil, err
}
var kubeconfig string
if exists(info.Metadata, "KubeConfig") {
kubeconfig = info.Metadata["KubeConfig"]
} else {
// Only load Kubeconfig during first run
client, err := d.getServiceClient(state.APIKey, state.Region)
if err != nil {
return nil, err
}
clusterID := info.Metadata["cluster-id"]
err = WaitForClusterActive(ctx, client, clusterID, 300)
if err != nil {
return nil, fmt.Errorf("cluster wasn't ready in 5mins %s: %s", clusterID, err)
}
cluster, err := client.GetKubernetesCluster(clusterID)
if err != nil {
return nil, fmt.Errorf("failed to get cluster %s: %s", clusterID, err)
}
kubeconfig = cluster.KubeConfig
}
cfg, err := clientcmd.RESTConfigFromKubeConfig([]byte(kubeconfig))
if err != nil {
return nil, fmt.Errorf("failed to parse cluster kubeconfig: %s", err)
}
info.Version = state.K8sVersion
count := 0
for uuid, sc := range state.NodePools {
_, cnt, err := getSizeCountNodePool(uuid, sc)
if err != nil {
return nil, err
}
count += cnt
}
info.NodeCount = int64(count)
info.Endpoint = cfg.Host
info.Username = cfg.Username
info.Password = cfg.Password
if len(cfg.CAData) > 0 {
info.RootCaCertificate = base64.StdEncoding.EncodeToString(cfg.CAData)
}
if len(cfg.CertData) > 0 {
info.ClientCertificate = base64.StdEncoding.EncodeToString(cfg.CertData)
}
if len(cfg.KeyData) > 0 {
info.ClientKey = base64.StdEncoding.EncodeToString(cfg.KeyData)
}
info.Metadata["KubeConfig"] = kubeconfig
serviceAccountToken, err := generateServiceAccountTokenForCivo(kubeconfig)
if err != nil {
return nil, err
}
info.ServiceAccountToken = serviceAccountToken
return info, nil
}
// Remove removes the cluster from the cloud provider.
func (d *Driver) Remove(ctx context.Context, info *types.ClusterInfo) error {
state, err := getState(info)
if err != nil {
return err
}
client, err := d.getServiceClient(state.APIKey, state.Region)
if err != nil {
return err
}
clusterID := info.Metadata["cluster-id"]
logrus.Debugf("Removing cluster %v from region %v", state.Name, state.Region)
_, err = client.DeleteKubernetesCluster(clusterID)
if err != nil {
return fmt.Errorf("failed to delete cluster %s: %s", clusterID, err)
}
return nil
}
func (d *Driver) getServiceClient(key, region string) (*civogo.Client, error) {
client, err := civogo.NewClientWithURL(key, DefaultCivoURL, region)
if err != nil {
return nil, err
}
return client, nil
}
// GetClusterSize returns the size of the cluster.
func (d *Driver) GetClusterSize(ctx context.Context, info *types.ClusterInfo) (*types.NodeCount, error) {
state, err := getState(info)
if err != nil {
return nil, err
}
clusterID := info.Metadata["cluster-id"]
client, err := d.getServiceClient(state.APIKey, state.Region)
if err != nil {
return nil, err
}
cluster, err := client.GetKubernetesCluster(clusterID)
if err != nil {
return nil, fmt.Errorf("failed to get cluster %s: %s", clusterID, err)
}
count := 0
for _, pool := range cluster.Pools {
count += pool.Count
}
return &types.NodeCount{Count: int64(count)}, nil
}
// GetVersion returns the version of the driver.
func (d *Driver) GetVersion(ctx context.Context, info *types.ClusterInfo) (*types.KubernetesVersion, error) {
state, err := getState(info)
if err != nil {
return nil, err
}
clusterID := info.Metadata["cluster-id"]
client, err := d.getServiceClient(state.APIKey, state.Region)
if err != nil {
return nil, err
}
cluster, err := client.GetKubernetesCluster(clusterID)
if err != nil {
return nil, fmt.Errorf("failed to get cluster %s: %s", clusterID, err)
}
return &types.KubernetesVersion{Version: cluster.KubernetesVersion}, nil
}
// SetClusterSize sets the size of the cluster.
func (d *Driver) SetClusterSize(ctx context.Context, info *types.ClusterInfo, count *types.NodeCount) error {
state, err := getState(info)
if err != nil {
return err
}
clusterID := info.Metadata["cluster-id"]
client, err := d.getServiceClient(state.APIKey, state.Region)
if err != nil {
return err
}
logrus.Info("updating cluster size")
cluster, err := client.GetKubernetesCluster(clusterID)
if err != nil {
return fmt.Errorf("failed to get cluster %s: %s", clusterID, err)
}
poolID := cluster.Pools[0].ID
poolSize := cluster.Pools[0].Size
_, err = client.UpdateKubernetesClusterPool(clusterID, poolID, &civogo.KubernetesClusterPoolUpdateConfig{
ID: poolID,
Count: int(count.Count),
Region: state.Region,
Size: poolSize,
})
logrus.Info("cluster size updated successfully")
return nil
}
// SetVersion updates the version of the cluster.
func (d *Driver) SetVersion(ctx context.Context, info *types.ClusterInfo, version *types.KubernetesVersion) error {
return nil
}
// GetCapabilities returns the capabilities of the driver.
func (d *Driver) GetCapabilities(ctx context.Context) (*types.Capabilities, error) {
return &d.driverCapabilities, nil
}
// ETCDSave is not implemented yet
func (d *Driver) ETCDSave(ctx context.Context, clusterInfo *types.ClusterInfo, opts *types.DriverOptions, snapshotName string) error {
return fmt.Errorf("ETCD backup operations are not implemented")
}
// ETCDRestore isn't implemented yet.
func (d *Driver) ETCDRestore(ctx context.Context, clusterInfo *types.ClusterInfo, opts *types.DriverOptions, snapshotName string) (*types.ClusterInfo, error) {
return nil, fmt.Errorf("ETCD backup operations are not implemented")
}
// ETCDRemoveSnapshot isn't implemented yet.
func (d *Driver) ETCDRemoveSnapshot(ctx context.Context, clusterInfo *types.ClusterInfo, opts *types.DriverOptions, snapshotName string) error {
return fmt.Errorf("ETCD backup operations are not implemented")
}
// GetK8SCapabilities returns the k8s capabilities of the driver.
func (d *Driver) GetK8SCapabilities(ctx context.Context, options *types.DriverOptions) (*types.K8SCapabilities, error) {
capabilities := &types.K8SCapabilities{
L4LoadBalancer: &types.LoadBalancerCapabilities{
Enabled: true,
Provider: "NodeBalancer", // what are the options?
ProtocolsSupported: []string{"TCP", "UDP"},
HealthCheckSupported: true,
},
}
return capabilities, nil
}
// RemoveLegacyServiceAccount removes the legacy service account from the cluster.
func (d *Driver) RemoveLegacyServiceAccount(ctx context.Context, info *types.ClusterInfo) error {
return nil
}