/
driver_alicloud.go
337 lines (279 loc) · 11.2 KB
/
driver_alicloud.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
/*
Copyright (c) 2017 SAP SE or an SAP affiliate company. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package driver contains the cloud provider specific implementations to manage machines
package driver
import (
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/klog/v2"
"github.com/xuanson2406/machine-controller-manager/pkg/apis/machine/v1alpha1"
"github.com/xuanson2406/machine-controller-manager/pkg/metrics"
corev1 "k8s.io/api/core/v1"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)
const (
// alicloudDriverName is the name of the CSI driver for Alibaba Cloud
alicloudDriverName = "diskplugin.csi.alibabacloud.com"
)
// AlicloudDriver is the driver struct for holding Alicloud machine information
type AlicloudDriver struct {
AlicloudMachineClass *v1alpha1.AlicloudMachineClass
CredentialsData map[string][]byte
UserData string
MachineID string
MachineName string
}
// Create is used to create a VM
func (c *AlicloudDriver) Create() (string, string, error) {
client, err := c.getEcsClient()
if err != nil {
return "", "", err
}
request := ecs.CreateRunInstancesRequest()
//request.DryRun = requests.NewBoolean(true)
request.ImageId = c.AlicloudMachineClass.Spec.ImageID
request.InstanceType = c.AlicloudMachineClass.Spec.InstanceType
request.RegionId = c.AlicloudMachineClass.Spec.Region
request.ZoneId = c.AlicloudMachineClass.Spec.ZoneID
request.SecurityGroupId = c.AlicloudMachineClass.Spec.SecurityGroupID
request.VSwitchId = c.AlicloudMachineClass.Spec.VSwitchID
request.PrivateIpAddress = c.AlicloudMachineClass.Spec.PrivateIPAddress
request.InstanceChargeType = c.AlicloudMachineClass.Spec.InstanceChargeType
request.InternetChargeType = c.AlicloudMachineClass.Spec.InternetChargeType
request.SpotStrategy = c.AlicloudMachineClass.Spec.SpotStrategy
request.IoOptimized = c.AlicloudMachineClass.Spec.IoOptimized
request.KeyPairName = c.AlicloudMachineClass.Spec.KeyPairName
if c.AlicloudMachineClass.Spec.InternetMaxBandwidthIn != nil {
request.InternetMaxBandwidthIn = requests.NewInteger(*c.AlicloudMachineClass.Spec.InternetMaxBandwidthIn)
}
if c.AlicloudMachineClass.Spec.InternetMaxBandwidthOut != nil {
request.InternetMaxBandwidthOut = requests.NewInteger(*c.AlicloudMachineClass.Spec.InternetMaxBandwidthOut)
}
if c.AlicloudMachineClass.Spec.DataDisks != nil && len(c.AlicloudMachineClass.Spec.DataDisks) > 0 {
dataDiskRequests := c.generateDataDiskRequests(c.AlicloudMachineClass.Spec.DataDisks)
request.DataDisk = &dataDiskRequests
}
if c.AlicloudMachineClass.Spec.SystemDisk != nil {
request.SystemDiskCategory = c.AlicloudMachineClass.Spec.SystemDisk.Category
request.SystemDiskSize = fmt.Sprintf("%d", c.AlicloudMachineClass.Spec.SystemDisk.Size)
}
tags, err := c.toInstanceTags(c.AlicloudMachineClass.Spec.Tags)
if err != nil {
return "", "", err
}
request.Tag = &tags
request.InstanceName = c.MachineName
request.ClientToken = utils.GetUUIDV4()
request.UserData = base64.StdEncoding.EncodeToString([]byte(c.UserData))
response, err := client.RunInstances(request)
if err != nil {
metrics.APIFailedRequestCount.With(prometheus.Labels{"provider": "alicloud", "service": "ecs"}).Inc()
return "", "", err
}
metrics.APIRequestCount.With(prometheus.Labels{"provider": "alicloud", "service": "ecs"}).Inc()
instanceID := response.InstanceIdSets.InstanceIdSet[0]
machineID := c.encodeMachineID(c.AlicloudMachineClass.Spec.Region, instanceID)
// Hostname can't be fetched immediately from Alicloud API.
// Even using DescribeInstances by Instance ID, it will return empty.
// However, for Alicloud hostname, it can be transformed by Instance ID by default
// Please be noted that returned node name should be in LOWER case
return machineID, strings.ToLower(c.idToName(instanceID)), nil
}
func (c *AlicloudDriver) generateDataDiskRequests(disks []v1alpha1.AlicloudDataDisk) []ecs.RunInstancesDataDisk {
var dataDiskRequests []ecs.RunInstancesDataDisk
for _, disk := range disks {
dataDiskRequest := ecs.RunInstancesDataDisk{
Category: disk.Category,
Encrypted: strconv.FormatBool(disk.Encrypted),
DiskName: fmt.Sprintf("%s-%s-data-disk", c.MachineName, disk.Name),
Description: disk.Description,
Size: fmt.Sprintf("%d", disk.Size),
}
if disk.DeleteWithInstance != nil {
dataDiskRequest.DeleteWithInstance = strconv.FormatBool(*disk.DeleteWithInstance)
} else {
dataDiskRequest.DeleteWithInstance = strconv.FormatBool(true)
}
if disk.Category == "DiskEphemeralSSD" {
dataDiskRequest.DeleteWithInstance = ""
}
dataDiskRequests = append(dataDiskRequests, dataDiskRequest)
}
return dataDiskRequests
}
// Delete method is used to delete an alicloud machine
func (c *AlicloudDriver) Delete(machineID string) error {
result, err := c.getVMDetails(machineID)
if err != nil {
return err
} else if len(result) == 0 {
// No running instance exists with the given machineID
klog.V(2).Infof("No VM matching the machineID found on the provider %q", machineID)
return nil
}
if result[0].Status != "Running" && result[0].Status != "Stopped" {
return errors.New("ec2 instance not in running/stopped state")
}
instanceID := c.decodeMachineID(machineID)
client, err := c.getEcsClient()
if err != nil {
return err
}
err = c.deleteInstance(client, instanceID)
return err
}
func (c *AlicloudDriver) deleteInstance(client *ecs.Client, machineID string) error {
request := ecs.CreateDeleteInstanceRequest()
request.InstanceId = machineID
request.Force = requests.NewBoolean(true)
_, err := client.DeleteInstance(request)
if err != nil {
metrics.APIFailedRequestCount.With(prometheus.Labels{"provider": "alicloud", "service": "ecs"}).Inc()
}
metrics.APIRequestCount.With(prometheus.Labels{"provider": "alicloud", "service": "ecs"}).Inc()
return err
}
// GetExisting method is used to get machineID for existing Alicloud machine
func (c *AlicloudDriver) GetExisting() (string, error) {
return c.MachineID, nil
}
func (c *AlicloudDriver) getVMDetails(machineID string) ([]ecs.Instance, error) {
client, err := c.getEcsClient()
if err != nil {
return nil, err
}
request := ecs.CreateDescribeInstancesRequest()
if machineID != "" {
machineID = c.decodeMachineID(machineID)
request.InstanceIds = "[\"" + machineID + "\"]"
} else {
searchClusterName := ""
searchNodeRole := ""
searchClusterNameValue := ""
searchNodeRoleValue := ""
for k, v := range c.AlicloudMachineClass.Spec.Tags {
if strings.Contains(k, "kubernetes.io/cluster/") {
searchClusterName = k
searchClusterNameValue = v
} else if strings.Contains(k, "kubernetes.io/role/") {
searchNodeRole = k
searchNodeRoleValue = v
}
}
if searchClusterName == "" || searchNodeRole == "" {
return nil, fmt.Errorf("Can't find VMs with none of machineID/Tag[kubernetes.io/cluster/*]/Tag[kubernetes.io/role/*]")
}
request.Tag = &[]ecs.DescribeInstancesTag{
{Key: searchClusterName, Value: searchClusterNameValue},
{Key: searchNodeRole, Value: searchNodeRoleValue},
}
}
response, err := client.DescribeInstances(request)
if err != nil {
metrics.APIFailedRequestCount.With(prometheus.Labels{"provider": "alicloud", "service": "ecs"}).Inc()
return nil, err
}
metrics.APIRequestCount.With(prometheus.Labels{"provider": "alicloud", "service": "ecs"}).Inc()
return response.Instances.Instance, nil
}
// GetVMs returns a VM matching the machineID
// If machineID is an empty string then it returns all matching instances
func (c *AlicloudDriver) GetVMs(machineID string) (VMs, error) {
listOfVMs := make(map[string]string)
instances, err := c.getVMDetails(machineID)
if err != nil {
return nil, err
}
for _, instance := range instances {
machineName := instance.InstanceName
listOfVMs[c.encodeMachineID(c.AlicloudMachineClass.Spec.Region, instance.InstanceId)] = machineName
}
return listOfVMs, nil
}
func (c *AlicloudDriver) encodeMachineID(region, machineID string) string {
return fmt.Sprintf("%s.%s", region, machineID)
}
func (c *AlicloudDriver) decodeMachineID(id string) string {
splitProviderID := strings.Split(id, ".")
return splitProviderID[len(splitProviderID)-1]
}
func (c *AlicloudDriver) getEcsClient() (*ecs.Client, error) {
accessKeyID := ExtractCredentialsFromData(c.CredentialsData, v1alpha1.AlicloudAccessKeyID, v1alpha1.AlicloudAlternativeAccessKeyID)
accessKeySecret := ExtractCredentialsFromData(c.CredentialsData, v1alpha1.AlicloudAccessKeySecret, v1alpha1.AlicloudAlternativeAccessKeySecret)
region := c.AlicloudMachineClass.Spec.Region
var ecsClient *ecs.Client
var err error
if accessKeyID != "" && accessKeySecret != "" && region != "" {
ecsClient, err = ecs.NewClientWithAccessKey(region, accessKeyID, accessKeySecret)
} else {
err = errors.New("alicloudAccessKeyID or alicloudAccessKeySecret can't be empty")
}
return ecsClient, err
}
// Host name in Alicloud has relationship with Instance ID
// i-uf69zddmom11ci7est12 => iZuf69zddmom11ci7est12Z
func (c *AlicloudDriver) idToName(instanceID string) string {
return strings.Replace(instanceID, "-", "Z", 1) + "Z"
}
func (c *AlicloudDriver) toInstanceTags(tags map[string]string) ([]ecs.RunInstancesTag, error) {
result := []ecs.RunInstancesTag{{}, {}}
hasCluster := false
hasRole := false
for k, v := range tags {
if strings.Contains(k, "kubernetes.io/cluster/") {
hasCluster = true
result[0].Key = k
result[0].Value = v
} else if strings.Contains(k, "kubernetes.io/role/") {
hasRole = true
result[1].Key = k
result[1].Value = v
} else {
result = append(result, ecs.RunInstancesTag{Key: k, Value: v})
}
}
if !hasCluster || !hasRole {
err := fmt.Errorf("Tags should at least contains 2 keys, which are prefixed with kubernetes.io/cluster and kubernetes.io/role")
return nil, err
}
return result, nil
}
// GetVolNames parses volume names from pv specs
func (c *AlicloudDriver) GetVolNames(specs []corev1.PersistentVolumeSpec) ([]string, error) {
names := []string{}
for i := range specs {
spec := &specs[i]
if spec.FlexVolume != nil && spec.FlexVolume.Options != nil {
if name, ok := spec.FlexVolume.Options["volumeId"]; ok {
names = append(names, name)
}
} else if spec.CSI != nil && spec.CSI.Driver == alicloudDriverName && spec.CSI.VolumeHandle != "" {
names = append(names, spec.CSI.VolumeHandle)
}
}
return names, nil
}
// GetUserData return the used data whit which the VM will be booted
func (c *AlicloudDriver) GetUserData() string {
return c.UserData
}
// SetUserData set the used data whit which the VM will be booted
func (c *AlicloudDriver) SetUserData(userData string) {
c.UserData = userData
}