-
Notifications
You must be signed in to change notification settings - Fork 431
/
virtualmachines.go
398 lines (350 loc) · 13.8 KB
/
virtualmachines.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
/*
Copyright 2019 The Kubernetes Authors.
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 virtualmachines
import (
"context"
"encoding/base64"
"fmt"
"strconv"
"strings"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
"github.com/Azure/go-autorest/autorest/to"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
"sigs.k8s.io/cluster-api-provider-azure/cloud/converters"
"sigs.k8s.io/cluster-api-provider-azure/cloud/services/resourceskus"
)
// getExisting provides information about a virtual machine.
func (s *Service) getExisting(ctx context.Context, name string) (*infrav1.VM, error) {
vm, err := s.Client.Get(ctx, s.Scope.ResourceGroup(), name)
if err != nil {
return nil, err
}
convertedVM, err := converters.SDKToVM(vm)
if err != nil {
return convertedVM, err
}
// Discover addresses for NICs associated with the VM
// and add them to our converted vm struct
addresses, err := s.getAddresses(ctx, vm)
if err != nil {
return convertedVM, err
}
convertedVM.Addresses = addresses
return convertedVM, nil
}
// Reconcile gets/creates/updates a virtual machine.
func (s *Service) Reconcile(ctx context.Context) error {
for _, vmSpec := range s.Scope.VMSpecs() {
existingVM, err := s.getExisting(ctx, vmSpec.Name)
switch {
case err != nil && !azure.ResourceNotFound(err):
return errors.Wrapf(err, "failed to get VM %s", vmSpec.Name)
case err == nil:
// VM already exists, update the spec and skip creation.
s.Scope.SetProviderID(fmt.Sprintf("azure:///%s", existingVM.ID))
s.Scope.SetAnnotation("cluster-api-provider-azure", "true")
s.Scope.SetAddresses(existingVM.Addresses)
s.Scope.SetVMState(existingVM.State)
default:
s.Scope.V(2).Info("creating VM", "vm", vmSpec.Name)
storageProfile, err := s.generateStorageProfile(ctx, vmSpec)
if err != nil {
return err
}
nicRefs := make([]compute.NetworkInterfaceReference, len(vmSpec.NICNames))
for i, nicName := range vmSpec.NICNames {
primary := i == 0
nicRefs[i] = compute.NetworkInterfaceReference{
ID: to.StringPtr(azure.NetworkInterfaceID(s.Scope.SubscriptionID(), s.Scope.ResourceGroup(), nicName)),
NetworkInterfaceReferenceProperties: &compute.NetworkInterfaceReferenceProperties{
Primary: to.BoolPtr(primary),
},
}
}
priority, evictionPolicy, billingProfile, err := getSpotVMOptions(vmSpec.SpotVMOptions)
if err != nil {
return errors.Wrapf(err, "failed to get Spot VM options")
}
sshKey, err := base64.StdEncoding.DecodeString(vmSpec.SSHKeyData)
if err != nil {
return errors.Wrapf(err, "failed to decode ssh public key")
}
bootstrapData, err := s.Scope.GetBootstrapData(ctx)
if err != nil {
return errors.Wrap(err, "failed to retrieve bootstrap data")
}
virtualMachine := compute.VirtualMachine{
Plan: s.generateImagePlan(),
Location: to.StringPtr(s.Scope.Location()),
Tags: converters.TagsToMap(infrav1.Build(infrav1.BuildParams{
ClusterName: s.Scope.ClusterName(),
Lifecycle: infrav1.ResourceLifecycleOwned,
Name: to.StringPtr(vmSpec.Name),
Role: to.StringPtr(vmSpec.Role),
Additional: s.Scope.AdditionalTags(),
})),
VirtualMachineProperties: &compute.VirtualMachineProperties{
HardwareProfile: &compute.HardwareProfile{
VMSize: compute.VirtualMachineSizeTypes(vmSpec.Size),
},
StorageProfile: storageProfile,
OsProfile: &compute.OSProfile{
ComputerName: to.StringPtr(vmSpec.Name),
AdminUsername: to.StringPtr(azure.DefaultUserName),
CustomData: to.StringPtr(bootstrapData),
LinuxConfiguration: &compute.LinuxConfiguration{
DisablePasswordAuthentication: to.BoolPtr(true),
SSH: &compute.SSHConfiguration{
PublicKeys: &[]compute.SSHPublicKey{
{
Path: to.StringPtr(fmt.Sprintf("/home/%s/.ssh/authorized_keys", azure.DefaultUserName)),
KeyData: to.StringPtr(string(sshKey)),
},
},
},
},
},
NetworkProfile: &compute.NetworkProfile{
NetworkInterfaces: &nicRefs,
},
Priority: priority,
EvictionPolicy: evictionPolicy,
BillingProfile: billingProfile,
DiagnosticsProfile: &compute.DiagnosticsProfile{
BootDiagnostics: &compute.BootDiagnostics{
Enabled: to.BoolPtr(true),
},
},
},
}
if vmSpec.Zone != "" {
zones := []string{vmSpec.Zone}
virtualMachine.Zones = &zones
}
if vmSpec.Identity == infrav1.VMIdentitySystemAssigned {
virtualMachine.Identity = &compute.VirtualMachineIdentity{
Type: compute.ResourceIdentityTypeSystemAssigned,
}
} else if vmSpec.Identity == infrav1.VMIdentityUserAssigned {
if len(vmSpec.UserAssignedIdentities) == 0 {
return errors.Wrapf(err, "cannot create VM: The user-assigned identity provider ids must not be null or empty for 'UserAssigned' identity type.")
}
// UserAssignedIdentities - The list of user identities associated with the Virtual Machine.
// The user identity dictionary key references will be ARM resource ids in the form:
// '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}'.
userIdentitiesMap := make(map[string]*compute.VirtualMachineIdentityUserAssignedIdentitiesValue, len(vmSpec.UserAssignedIdentities))
for _, id := range vmSpec.UserAssignedIdentities {
key := id.ProviderID
if strings.HasPrefix(id.ProviderID, "azure:///") {
key = strings.TrimPrefix(key, "azure:///")
}
userIdentitiesMap[key] = &compute.VirtualMachineIdentityUserAssignedIdentitiesValue{}
}
virtualMachine.Identity = &compute.VirtualMachineIdentity{
Type: compute.ResourceIdentityTypeUserAssigned,
UserAssignedIdentities: userIdentitiesMap,
}
}
if err := s.Client.CreateOrUpdate(ctx, s.Scope.ResourceGroup(), vmSpec.Name, virtualMachine); err != nil {
return errors.Wrapf(err, "failed to create VM %s in resource group %s", vmSpec.Name, s.Scope.ResourceGroup())
}
s.Scope.V(2).Info("successfully created VM", "vm", vmSpec.Name)
}
}
return nil
}
func (s *Service) generateImagePlan() *compute.Plan {
image, err := s.Scope.GetVMImage()
if err != nil {
return nil
}
if image.Marketplace == nil || image.Marketplace.ThirdPartyImage == false {
return nil
}
if image.Marketplace.Publisher == "" || image.Marketplace.SKU == "" || image.Marketplace.Offer == "" {
return nil
}
return &compute.Plan{
Publisher: to.StringPtr(image.Marketplace.Publisher),
Name: to.StringPtr(image.Marketplace.SKU),
Product: to.StringPtr(image.Marketplace.Offer),
}
}
// Delete deletes the virtual machine with the provided name.
func (s *Service) Delete(ctx context.Context) error {
for _, vmSpec := range s.Scope.VMSpecs() {
s.Scope.V(2).Info("deleting VM", "vm", vmSpec.Name)
err := s.Client.Delete(ctx, s.Scope.ResourceGroup(), vmSpec.Name)
if err != nil && azure.ResourceNotFound(err) {
// already deleted
continue
}
if err != nil {
return errors.Wrapf(err, "failed to delete VM %s in resource group %s", vmSpec.Name, s.Scope.ResourceGroup())
}
s.Scope.V(2).Info("successfully deleted VM", "vm", vmSpec.Name)
}
return nil
}
func (s *Service) getAddresses(ctx context.Context, vm compute.VirtualMachine) ([]corev1.NodeAddress, error) {
addresses := []corev1.NodeAddress{}
if vm.NetworkProfile.NetworkInterfaces == nil {
return addresses, nil
}
for _, nicRef := range *vm.NetworkProfile.NetworkInterfaces {
// The full ID includes the name at the very end. Split the string and pull the last element
// Ex: /subscriptions/$SUB/resourceGroups/$RG/providers/Microsoft.Network/networkInterfaces/$NICNAME
// We'll check to see if ID is nil and bail early if we don't have it
if nicRef.ID == nil {
continue
}
nicName := getResourceNameByID(to.String(nicRef.ID))
// Fetch nic and append its addresses
nic, err := s.InterfacesClient.Get(ctx, s.Scope.ResourceGroup(), nicName)
if err != nil {
return addresses, err
}
if nic.IPConfigurations == nil {
continue
}
for _, ipConfig := range *nic.IPConfigurations {
if ipConfig.PrivateIPAddress != nil {
addresses = append(addresses,
corev1.NodeAddress{
Type: corev1.NodeInternalIP,
Address: to.String(ipConfig.PrivateIPAddress),
},
)
}
if ipConfig.PublicIPAddress == nil {
continue
}
// ID is the only field populated in PublicIPAddress sub-resource.
// Thus, we have to go fetch the publicIP with the name.
publicIPName := getResourceNameByID(to.String(ipConfig.PublicIPAddress.ID))
publicNodeAddress, err := s.getPublicIPAddress(ctx, publicIPName)
if err != nil {
return addresses, err
}
addresses = append(addresses, publicNodeAddress)
}
}
return addresses, nil
}
// getPublicIPAddress will fetch a public ip address resource by name and return a nodeaddresss representation
func (s *Service) getPublicIPAddress(ctx context.Context, publicIPAddressName string) (corev1.NodeAddress, error) {
retAddress := corev1.NodeAddress{}
publicIP, err := s.PublicIPsClient.Get(ctx, s.Scope.ResourceGroup(), publicIPAddressName)
if err != nil {
return retAddress, err
}
retAddress.Type = corev1.NodeExternalIP
retAddress.Address = to.String(publicIP.IPAddress)
return retAddress, nil
}
// getResourceNameById takes a resource ID like
// `/subscriptions/$SUB/resourceGroups/$RG/providers/Microsoft.Network/networkInterfaces/$NICNAME`
// and parses out the string after the last slash.
func getResourceNameByID(resourceID string) string {
explodedResourceID := strings.Split(resourceID, "/")
resourceName := explodedResourceID[len(explodedResourceID)-1]
return resourceName
}
// generateStorageProfile generates a pointer to a compute.StorageProfile which can utilized for VM creation.
func (s *Service) generateStorageProfile(ctx context.Context, vmSpec azure.VMSpec) (*compute.StorageProfile, error) {
storageProfile := &compute.StorageProfile{
OsDisk: &compute.OSDisk{
Name: to.StringPtr(azure.GenerateOSDiskName(vmSpec.Name)),
OsType: compute.OperatingSystemTypes(vmSpec.OSDisk.OSType),
CreateOption: compute.DiskCreateOptionTypesFromImage,
DiskSizeGB: to.Int32Ptr(vmSpec.OSDisk.DiskSizeGB),
ManagedDisk: &compute.ManagedDiskParameters{
StorageAccountType: compute.StorageAccountTypes(vmSpec.OSDisk.ManagedDisk.StorageAccountType),
},
Caching: compute.CachingTypes(vmSpec.OSDisk.CachingType),
},
}
sku, err := s.ResourceSKUCache.Get(ctx, vmSpec.Size, resourceskus.VirtualMachines)
if err != nil {
return nil, errors.Wrapf(err, "failed to get find vm sku %s in compute api", vmSpec.Size)
}
// Checking if the requested VM size has at least 2 vCPUS
vCPUCapability, err := sku.HasCapabilityWithCapacity(resourceskus.VCPUs, resourceskus.MinimumVCPUS)
if err != nil {
return nil, errors.Wrap(err, "failed to validate the vCPU cabability")
}
if !vCPUCapability {
return nil, errors.New("vm size should be bigger or equal to at least 2 vCPUs")
}
// Checking if the requested VM size has at least 2 Gi of memory
MemoryCapability, err := sku.HasCapabilityWithCapacity(resourceskus.MemoryGB, resourceskus.MinimumMemory)
if err != nil {
return nil, errors.Wrap(err, "failed to validate the memory cabability")
}
if !MemoryCapability {
return nil, errors.New("vm memory should be bigger or equal to at least 2Gi")
}
// enable ephemeral OS
if vmSpec.OSDisk.DiffDiskSettings != nil {
if !sku.HasCapability(resourceskus.EphemeralOSDisk) {
return nil, fmt.Errorf("vm size %s does not support ephemeral os. select a different vm size or disable ephemeral os", vmSpec.Size)
}
storageProfile.OsDisk.DiffDiskSettings = &compute.DiffDiskSettings{
Option: compute.DiffDiskOptions(vmSpec.OSDisk.DiffDiskSettings.Option),
}
}
if vmSpec.OSDisk.ManagedDisk.DiskEncryptionSet != nil {
storageProfile.OsDisk.ManagedDisk.DiskEncryptionSet = &compute.DiskEncryptionSetParameters{ID: to.StringPtr(vmSpec.OSDisk.ManagedDisk.DiskEncryptionSet.ID)}
}
dataDisks := []compute.DataDisk{}
for _, disk := range vmSpec.DataDisks {
dataDisks = append(dataDisks, compute.DataDisk{
CreateOption: compute.DiskCreateOptionTypesEmpty,
DiskSizeGB: to.Int32Ptr(disk.DiskSizeGB),
Lun: disk.Lun,
Name: to.StringPtr(azure.GenerateDataDiskName(vmSpec.Name, disk.NameSuffix)),
Caching: compute.CachingTypes(disk.CachingType),
})
}
storageProfile.DataDisks = &dataDisks
image, err := s.Scope.GetVMImage()
if err != nil {
return nil, errors.Wrap(err, "failed to get VM image")
}
imageRef, err := converters.ImageToSDK(image)
if err != nil {
return nil, err
}
storageProfile.ImageReference = imageRef
return storageProfile, nil
}
func getSpotVMOptions(spotVMOptions *infrav1.SpotVMOptions) (compute.VirtualMachinePriorityTypes, compute.VirtualMachineEvictionPolicyTypes, *compute.BillingProfile, error) {
// Spot VM not requested, return zero values to apply defaults
if spotVMOptions == nil {
return "", "", nil, nil
}
var billingProfile *compute.BillingProfile
if spotVMOptions.MaxPrice != nil {
maxPrice, err := strconv.ParseFloat(*spotVMOptions.MaxPrice, 64)
if err != nil {
return "", "", nil, err
}
billingProfile = &compute.BillingProfile{
MaxPrice: &maxPrice,
}
}
return compute.Spot, compute.Deallocate, billingProfile, nil
}