-
Notifications
You must be signed in to change notification settings - Fork 14
/
instances.go
579 lines (537 loc) · 18.1 KB
/
instances.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
Copyright 2020 Elotl Inc.
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 azure
import (
"context"
"fmt"
"sort"
"strings"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-10-01/compute"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/to"
"github.com/elotl/kip/pkg/api"
"github.com/elotl/kip/pkg/server/cloud"
"github.com/elotl/kip/pkg/util"
glob "github.com/ryanuber/go-glob"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog"
)
var (
milpaIPConfig = "milpaIPConfig"
milpaPodIPConfig = "milpaPodIPConfig"
)
func (az *AzureClient) StopInstance(instanceID string) error {
// Deleting the instance's resource group should delete the VM and
// everything else associated with the VM (NIC, Disks, Public IP,
// etc.)
return az.DeleteResourceGroup(instanceID)
}
func (az *AzureClient) createIPAddress(instanceID string, zone string) (*network.PublicIPAddress, error) {
ctx := context.Background()
timeoutCtx, cancel := context.WithTimeout(ctx, azureDefaultTimeout)
defer cancel()
future, err := az.ips.CreateOrUpdate(timeoutCtx, instanceID, instanceID,
network.PublicIPAddress{
Sku: &network.PublicIPAddressSku{
Name: network.PublicIPAddressSkuNameStandard,
},
Name: to.StringPtr(instanceID),
Location: to.StringPtr(az.region),
Zones: makeZoneParam(zone),
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
PublicIPAddressVersion: network.IPv4,
PublicIPAllocationMethod: network.Static,
DNSSettings: &network.PublicIPAddressDNSSettings{
DomainNameLabel: to.StringPtr(instanceID),
},
},
},
)
if err != nil {
return nil, util.WrapError(err, "cannot create public ip address for instance %s", instanceID)
}
timeoutCtx, cancel = context.WithTimeout(ctx, azureIPAddressWaitTimeout)
defer cancel()
err = future.WaitForCompletionRef(timeoutCtx, az.ips.Client)
if err != nil {
return nil, util.WrapError(err, "Error waiting for IP address creation for instance %s", instanceID)
}
ip, err := future.Result(az.ips)
if err != nil {
return nil, err
}
return &ip, nil
}
func (az *AzureClient) createNIC(instanceID string, ipID string) (string, error) {
nicName := instanceID
nicParams := network.Interface{
Name: to.StringPtr(nicName),
Location: to.StringPtr(az.region),
InterfacePropertiesFormat: &network.InterfacePropertiesFormat{
NetworkSecurityGroup: &network.SecurityGroup{
ID: to.StringPtr(az.nsgID),
},
// Todo: look into accelerated networking.
// https://azure.microsoft.com/en-us/blog/maximize-your-vm-s-performance-with-accelerated-networking-now-generally-available-for-both-windows-and-linux/
// EnableAcceleratedNetworking: to.BoolPtr(true),
IPConfigurations: &[]network.InterfaceIPConfiguration{
{
// This name is used to find the IP configuration later on
Name: to.StringPtr(milpaIPConfig),
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
Subnet: &network.Subnet{
Name: to.StringPtr(az.subnet.Name),
ID: to.StringPtr(az.subnet.ID),
},
PrivateIPAllocationMethod: network.Dynamic,
PrivateIPAddressVersion: network.IPv4,
PublicIPAddress: &network.PublicIPAddress{
ID: to.StringPtr(ipID),
},
Primary: to.BoolPtr(true),
},
},
{
// Pod IP address.
Name: to.StringPtr(milpaPodIPConfig),
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
Subnet: &network.Subnet{
Name: to.StringPtr(az.subnet.Name),
ID: to.StringPtr(az.subnet.ID),
},
PrivateIPAllocationMethod: network.Dynamic,
PrivateIPAddressVersion: network.IPv4,
Primary: to.BoolPtr(false),
},
},
},
},
}
ctx := context.Background()
timeoutCtx, cancel := context.WithTimeout(ctx, azureDefaultTimeout)
defer cancel()
future, err := az.nics.CreateOrUpdate(timeoutCtx, instanceID, nicName, nicParams)
if err != nil {
return "", fmt.Errorf("cannot create nic: %v", err)
}
timeoutCtx, cancel = context.WithTimeout(ctx, azureWaitTimeout)
defer cancel()
err = future.WaitForCompletionRef(timeoutCtx, az.nics.Client)
if err != nil {
return "", fmt.Errorf("cannot get nic create or update future response: %v", err)
}
nic, err := future.Result(az.nics)
if err != nil {
return "", err
}
return to.String(nic.ID), nil
}
func (az *AzureClient) StartNode(node *api.Node, image cloud.Image, metadata, iamPermissions string) (string, error) {
klog.V(2).Infof("Starting instance for node: %v", node)
instanceID := makeInstanceID(az.controllerID, node.Name)
err := az.createResourceGroup(instanceID)
if err != nil {
return "", util.WrapError(err, "Error creating new Azure Resource Group for Node %s", node.Name)
}
cleanup := func() {
err := az.DeleteResourceGroup(instanceID)
if err != nil {
klog.Errorln(
"Error deleting azure resource group after start failure",
err,
)
}
}
// Todo, use an annotation to pass through the zone
zone := ""
ipID := ""
if !node.Spec.Resources.PrivateIPOnly {
ip, err := az.createIPAddress(instanceID, zone)
if err != nil {
cleanup()
// todo, see if our subnet is constrained, return that error instead
return "", util.WrapError(err, "Error creating new IP for Node %s", node.Name)
}
ipID = to.String(ip.ID)
}
nicID, err := az.createNIC(instanceID, ipID)
if err != nil {
// Todo, see if our subnet is constrained on private addresses
cleanup()
return "", util.WrapError(err, "Error creating new NIC for Node %s", node.Name)
}
metadataptr := to.StringPtr(metadata)
// Azure doesn't like emptystring metadata
if len(metadata) == 0 {
metadataptr = nil
}
volSizeGiB := cloud.ToSaneVolumeSize(node.Spec.Resources.VolumeSize, image)
ctx := context.Background()
timeoutCtx, cancel := context.WithTimeout(ctx, azureDefaultTimeout)
defer cancel()
future, err := az.vms.CreateOrUpdate(timeoutCtx, instanceID, instanceID,
compute.VirtualMachine{
Location: to.StringPtr(az.region),
Zones: makeZoneParam(zone),
VirtualMachineProperties: &compute.VirtualMachineProperties{
HardwareProfile: &compute.HardwareProfile{
VMSize: compute.VirtualMachineSizeTypes(node.Spec.InstanceType),
},
StorageProfile: &compute.StorageProfile{
ImageReference: &compute.ImageReference{
ID: to.StringPtr(image.ID),
},
OsDisk: &compute.OSDisk{
OsType: compute.Linux,
Name: to.StringPtr(instanceID),
CreateOption: compute.DiskCreateOptionTypesFromImage,
DiskSizeGB: to.Int32Ptr(volSizeGiB),
},
},
OsProfile: &compute.OSProfile{
ComputerName: to.StringPtr(instanceID),
AdminUsername: to.StringPtr("milpa"),
// We can't _not_ include this so we'll add it here
// but it wont be used by our image
AdminPassword: to.StringPtr("thisisunused-AFdsj483.fd8r37r"),
LinuxConfiguration: &compute.LinuxConfiguration{
ProvisionVMAgent: to.BoolPtr(false),
},
CustomData: metadataptr,
},
NetworkProfile: &compute.NetworkProfile{
NetworkInterfaces: &[]compute.NetworkInterfaceReference{
{
ID: to.StringPtr(nicID),
NetworkInterfaceReferenceProperties: &compute.NetworkInterfaceReferenceProperties{
Primary: to.BoolPtr(true),
},
},
},
},
},
Tags: az.getNodeTags(node),
},
)
if err != nil {
cleanup()
return "", getStartVMError(err, az.subnet.ID, "")
}
timeoutCtx, cancel = context.WithTimeout(ctx, azureStartNodeTimeout)
defer cancel()
err = future.WaitForCompletionRef(timeoutCtx, az.vms.Client)
if err != nil {
cleanup()
return "", util.WrapError(err, "Error waiting for VM %s to boot", instanceID)
}
_, err = future.Result(az.vms)
if err != nil {
cleanup()
return "", util.WrapError(err, "Error getting VM %s after booting", instanceID)
}
return instanceID, nil
}
func (az *AzureClient) ReleaseDedicatedHosts() error {
// TODO stubbed not implemented yet
return fmt.Errorf("Azure: %s", "release dedicated hosts not implemented yet")
}
func (az *AzureClient) StartDedicatedNode(node *api.Node, image cloud.Image, metadata, iamPermissions string) (string, error) {
// TODO stubbed not implemented yet
return "", fmt.Errorf("Azure: %s", "start dedicated node not implemented yet")
}
func (az *AzureClient) StartSpotNode(node *api.Node, image cloud.Image, metadata, iamPermissions string) (string, error) {
return az.StartNode(node, image, metadata, iamPermissions)
}
func (az *AzureClient) getNodeTags(node *api.Node) map[string]*string {
nametag := util.CreateUnboundNodeNameTag(az.nametag)
tags := map[string]*string{
"Name": to.StringPtr(nametag),
"Node": to.StringPtr(node.Name),
cloud.ControllerTagKey: to.StringPtr(az.controllerID),
cloud.NametagTagKey: to.StringPtr(az.nametag),
}
return tags
}
func (az *AzureClient) WaitForRunning(node *api.Node) ([]api.NetworkAddress, error) {
ctx := context.Background()
timeoutCtx, cancel := context.WithTimeout(ctx, azureDefaultTimeout)
defer cancel()
nic, err := az.nics.Get(timeoutCtx, node.Status.InstanceID, node.Status.InstanceID, "")
if err != nil {
return nil, util.WrapError(err, "Error getting recently started VM %s from Azure", node.Status.InstanceID)
}
addresses := []api.NetworkAddress{}
if nic.IPConfigurations != nil {
for _, ipconfig := range *nic.IPConfigurations {
if to.String(ipconfig.Name) == milpaIPConfig {
ipProperties := ipconfig.InterfaceIPConfigurationPropertiesFormat
if ipProperties == nil {
return addresses, fmt.Errorf("invalid response from Azure when getting %s interface parameters", node.Status.InstanceID)
}
ip := to.String(ipconfig.PrivateIPAddress)
addresses = api.NewNetworkAddresses(ip, "")
if ipconfig.PublicIPAddress != nil && ipconfig.PublicIPAddress.ID != nil {
ip, err := az.getPublicIP(node.Status.InstanceID)
if err == nil && ip != "" {
addresses = api.SetPublicAddresses(ip, "", addresses)
}
}
}
if to.String(ipconfig.Name) == milpaPodIPConfig {
ipProperties := ipconfig.InterfaceIPConfigurationPropertiesFormat
if ipProperties == nil {
return addresses, fmt.Errorf("invalid response from Azure when getting %s pod IP address", node.Status.InstanceID)
}
addresses = api.SetPodIP(to.String(ipconfig.PrivateIPAddress), addresses)
}
}
}
return addresses, nil
}
func (az *AzureClient) SetSustainedCPU(node *api.Node, enabled bool) error {
klog.V(2).Infoln("Setting sustained CPU in Azure has no impact")
return nil
}
// This is a bit of an overkill right now, since on Azure we only filter image
// list results via matching the name of the image to a glob, e.g.
// elotl-kip-*.
func matchSpec(properties map[string]string, spec cloud.BootImageSpec) bool {
if len(spec) < 1 {
// No spec.
return true
}
if len(properties) < len(spec) {
// The spec contains more properties.
return false
}
for name, value := range spec {
if propValue, ok := properties[name]; ok {
if glob.Glob(value, propValue) {
continue
}
}
return false
}
return true
}
func (az *AzureClient) GetImage(spec cloud.BootImageSpec) (cloud.Image, error) {
ctx := context.Background()
timeoutCtx, cancel := context.WithTimeout(ctx, azureDefaultTimeout)
defer cancel()
rgName := regionalResourceGroupName(az.region)
resultPage, err := az.images.ListByResourceGroup(timeoutCtx, rgName)
if err != nil {
return cloud.Image{}, err
}
images := make(map[string]compute.Image)
imageNames := make([]string, 0)
locationName := az.locationName()
for resultPage.NotDone() {
azImages := resultPage.Values()
for _, azImage := range azImages {
if to.String(azImage.Location) != locationName {
continue
}
// We have to filter results ourselves, unlike on AWS. For now,
// filtering based on name is supported (and we filter
// unconditionally on location above).
name := to.String(azImage.Name)
properties := map[string]string{
"name": name,
}
if !matchSpec(properties, spec) {
continue
}
images[name] = azImage
imageNames = append(imageNames, name)
}
timeoutCtx, cancel = context.WithTimeout(ctx, azureDefaultTimeout)
defer cancel()
err := resultPage.NextWithContext(timeoutCtx)
if err != nil {
return cloud.Image{}, err
}
}
if len(images) == 0 {
msg := fmt.Sprintf("no images found for spec %+v", spec)
klog.Errorf("%s", msg)
return cloud.Image{}, fmt.Errorf("%s", msg)
}
// compute.Image has no creation timestamp, so we rely on naming convention
// here: the name has a timestamp in it, so we can get the latest one via
// lexicographical sorting.
sort.Strings(imageNames)
latestImage := imageNames[len(imageNames)-1]
return cloud.Image{
ID: to.String(images[latestImage].ID),
Name: to.String(images[latestImage].Name),
}, nil
}
func (az *AzureClient) ListInstancesFilterID(ids []string) ([]cloud.CloudInstance, error) {
idsSet := sets.NewString(ids...)
return az.listInstancesHelper(func(inst compute.VirtualMachine) bool {
return idsSet.Has(to.String(inst.Name))
})
}
func (az *AzureClient) ListInstances() (insts []cloud.CloudInstance, err error) {
return az.listInstancesHelper(func(inst compute.VirtualMachine) bool {
tags := inst.Tags
return tags != nil &&
to.String(tags[cloud.ControllerTagKey]) == az.controllerID
})
}
func (az *AzureClient) listInstancesHelper(filter func(inst compute.VirtualMachine) bool) (insts []cloud.CloudInstance, err error) {
ctx := context.Background()
timeoutCtx, cancel := context.WithTimeout(ctx, azureDefaultTimeout)
defer cancel()
resultsPage, err := az.vms.ListAll(timeoutCtx)
if err != nil {
return
}
insts = make([]cloud.CloudInstance, 0, 100) // Randomly chosen size...
instNames := sets.NewString()
for resultsPage.NotDone() {
instances := resultsPage.Values()
for _, inst := range instances {
instName := to.String(inst.Name)
if filter(inst) {
instNames.Insert(instName)
insts = append(insts, cloud.CloudInstance{
ID: instName,
NodeName: to.String(inst.Tags["Node"]),
})
}
}
timeoutCtx, cancel = context.WithTimeout(ctx, azureDefaultTimeout)
defer cancel()
err = resultsPage.NextWithContext(timeoutCtx)
if err != nil {
return
}
}
return insts, nil
}
func getSecurityGroupsFromInterface(iface network.Interface) []cloud.SecurityGroupIdentifier {
ipConfig, err := getMilpaIPConfiguration(iface)
if err != nil || ipConfig.ApplicationSecurityGroups == nil {
return []cloud.SecurityGroupIdentifier{}
}
asgs := *ipConfig.ApplicationSecurityGroups
groups := make([]cloud.SecurityGroupIdentifier, len(asgs))
for i := range asgs {
details, err := azure.ParseResourceID(to.String(asgs[i].ID))
if err != nil {
continue
}
groups[i] = cloud.SecurityGroupIdentifier{
ID: details.ResourceName,
Name: details.ResourceName,
}
}
return groups
}
func (az *AzureClient) AddInstanceTags(iid string, labels map[string]string) error {
newTags, err := filterLabelsForTags(iid, labels)
if err != nil {
klog.Warning(err)
}
if len(newTags) > 0 {
ctx := context.Background()
timeoutCtx, cancel := context.WithTimeout(ctx, azureDefaultTimeout)
defer cancel()
vm, err := az.vms.Get(timeoutCtx, iid, iid, "")
if err != nil {
return util.WrapError(err, "Error looking up VM %s in azure", iid)
}
tags := vm.Tags
for k, v := range newTags {
tags[k] = v
}
vm.Tags = tags
vm.VirtualMachineProperties = nil
timeoutCtx, cancel = context.WithTimeout(ctx, azureDefaultTimeout)
defer cancel()
future, err := az.vms.CreateOrUpdate(timeoutCtx, iid, iid, vm)
if err != nil {
return util.WrapError(err, "Error adding tags to vm %s", iid)
}
timeoutCtx, cancel = context.WithTimeout(ctx, azureWaitTimeout)
defer cancel()
err = future.WaitForCompletionRef(timeoutCtx, az.vms.Client)
if err != nil {
return util.WrapError(
err, "Error waiting for VM %s", iid)
}
_, err = future.Result(az.vms)
if err != nil {
return util.WrapError(
err, "Error getting VM %s after adding tags", iid)
}
}
return nil
}
// Todo: need to figure out what to do with this
func (az *AzureClient) ResizeVolume(node *api.Node, size int64) (error, bool) {
return nil, true
}
func getStartVMError(err error, subnetID, az string) error {
if isSubnetConstrainedError(err) {
return &cloud.NoCapacityError{
OriginalError: err.Error(),
SubnetID: subnetID,
}
} else if isAZConstrainedError(err, az) {
return &cloud.NoCapacityError{
OriginalError: err.Error(),
AZ: az,
}
} else if isInstanceConstrainedError(err) {
return &cloud.NoCapacityError{
OriginalError: err.Error(),
}
} else if isUnsupportedInstanceError(err) {
return &cloud.UnsupportedInstanceError{err.Error()}
}
return util.WrapError(err, "Could not run instance")
}
// Todo, need to see what we get back when we have no more addresses
func isSubnetConstrainedError(err error) bool {
return false
}
func isAZConstrainedError(err error, az string) bool {
if strings.Contains(err.Error(), "ZonalAllocationFailed") ||
// Hack: this should impact all AZs but not an empty AZ...
strings.Contains(err.Error(), "ResourceTypeNotSupportAvailabilityZones") {
return true
} else if az != "" {
if strings.Contains(err.Error(), "AllocationFailed") ||
strings.Contains(err.Error(), "SkuNotAvailable") {
return true
}
}
return false
}
func isInstanceConstrainedError(err error) bool {
if strings.Contains(err.Error(), "AllocationFailed") ||
strings.Contains(err.Error(), "quota limit") {
return true
}
return false
}
func isUnsupportedInstanceError(err error) bool {
return strings.Contains(err.Error(), "SkuNotAvailable")
}
func (az *AzureClient) AddIAMPermissions(node *api.Node, permissions string) error {
return nil
}