-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathvmss.go
638 lines (580 loc) · 22 KB
/
vmss.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
package e2e
import (
"context"
crand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"math/big"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/Azure/agentbaker/aks-node-controller/pkg/nodeconfigutils"
"github.com/Azure/agentbaker/e2e/config"
"github.com/Azure/agentbaker/pkg/agent"
"github.com/Azure/agentbaker/pkg/agent/datamodel"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ssh"
)
const (
listVMSSNetworkInterfaceURLTemplate = "https://management.azure.com/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachineScaleSets/%s/virtualMachines/%d/networkInterfaces?api-version=2018-10-01"
loadBalancerBackendAddressPoolIDTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers/kubernetes/backendAddressPools/aksOutboundBackendPool"
)
func createVMSS(ctx context.Context, s *Scenario) *armcompute.VirtualMachineScaleSet {
cluster := s.Runtime.Cluster
s.T.Logf("creating VMSS %q in resource group %q", s.Runtime.VMSSName, *cluster.Model.Properties.NodeResourceGroup)
var nodeBootstrapping *datamodel.NodeBootstrapping
ab, err := agent.NewAgentBaker()
require.NoError(s.T, err)
var cse, customData string
if s.AKSNodeConfigMutator != nil {
cse = nodeconfigutils.CSE
customData, err = nodeconfigutils.CustomData(s.Runtime.AKSNodeConfig)
require.NoError(s.T, err)
} else {
nodeBootstrapping, err = ab.GetNodeBootstrapping(ctx, s.Runtime.NBC)
require.NoError(s.T, err)
cse = nodeBootstrapping.CSE
customData = nodeBootstrapping.CustomData
}
model := getBaseVMSSModel(s, customData, cse)
isAzureCNI, err := cluster.IsAzureCNI()
require.NoError(s.T, err, "checking if cluster is using Azure CNI")
if isAzureCNI {
err = addPodIPConfigsForAzureCNI(&model, s.Runtime.VMSSName, cluster)
require.NoError(s.T, err)
}
s.PrepareVMSSModel(ctx, s.T, &model)
operation, err := config.Azure.VMSS.BeginCreateOrUpdate(
ctx,
*cluster.Model.Properties.NodeResourceGroup,
s.Runtime.VMSSName,
model,
nil,
)
skipTestIfSKUNotAvailableErr(s.T, err)
require.NoError(s.T, err)
s.T.Cleanup(func() {
cleanupVMSS(ctx, s)
})
vmssResp, err := operation.PollUntilDone(ctx, config.DefaultPollUntilDoneOptions)
// fail test, but continue to extract debug information
require.NoError(s.T, err, "create vmss %q, check %s for vm logs", s.Runtime.VMSSName, testDir(s.T))
return &vmssResp.VirtualMachineScaleSet
}
func skipTestIfSKUNotAvailableErr(t *testing.T, err error) {
// sometimes the SKU is not available and we can't do anything. Skip the test in this case.
var respErr *azcore.ResponseError
if config.Config.SkipTestsWithSKUCapacityIssue &&
errors.As(err, &respErr) &&
respErr.StatusCode == 409 &&
respErr.ErrorCode == "SkuNotAvailable" {
t.Skip("skipping scenario SKU not available", t.Name(), err)
}
}
func cleanupVMSS(ctx context.Context, s *Scenario) {
// original context can be cancelled, but we still want to collect the logs
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Minute)
defer cancel()
defer deleteVMSS(ctx, s)
extractLogsFromVM(ctx, s)
}
func extractLogsFromVM(ctx context.Context, s *Scenario) {
if s.VHD.OS == config.OSWindows {
extractLogsFromVMWindows(ctx, s)
} else {
extractLogsFromVMLinux(ctx, s)
}
}
func extractLogsFromVMLinux(ctx context.Context, s *Scenario) {
privateIP, err := getVMPrivateIPAddress(ctx, s)
require.NoError(s.T, err)
commandList := map[string]string{
"cluster-provision.log": "sudo cat /var/log/azure/cluster-provision.log",
"kubelet.log": "sudo journalctl -u kubelet",
"cluster-provision-cse-output.log": "sudo cat /var/log/azure/cluster-provision-cse-output.log",
"sysctl-out.log": "sudo sysctl -a",
"aks-node-controller.log": "sudo cat /var/log/azure/aks-node-controller.log",
}
pod, err := s.Runtime.Cluster.Kube.GetHostNetworkDebugPod(ctx, s.T)
if err != nil {
require.NoError(s.T, err)
}
var logFiles = map[string]string{}
for file, sourceCmd := range commandList {
execResult, err := execBashCommandOnVM(ctx, s, privateIP, pod.Name, string(s.Runtime.SSHKeyPrivate), sourceCmd)
if err != nil {
s.T.Logf("error executing %s: %s", sourceCmd, err)
continue
}
logFiles[file] = execResult.String()
}
err = dumpFileMapToDir(s.T, logFiles)
require.NoError(s.T, err)
}
func execBashCommandOnVM(ctx context.Context, s *Scenario, vmPrivateIP, jumpboxPodName, sshPrivateKey, command string) (*podExecResult, error) {
script := Script{
interpreter: Bash,
script: command,
}
return execScriptOnVm(ctx, s, vmPrivateIP, jumpboxPodName, sshPrivateKey, script)
}
const uploadLogsPowershellScript = `
param(
[string]$arg1,
[string]$arg2,
[string]$arg3
)
Invoke-WebRequest -UseBasicParsing https://aka.ms/downloadazcopy-v10-windows -OutFile azcopy.zip
Expand-Archive azcopy.zip
cd .\azcopy\*
$env:AZCOPY_AUTO_LOGIN_TYPE="MSI"
$env:AZCOPY_MSI_RESOURCE_STRING=$arg3
C:\k\debug\collect-windows-logs.ps1
$CollectedLogs=(Get-ChildItem . -Filter "*_logs.zip" -File)[0].Name
.\azcopy.exe copy $CollectedLogs "$arg1/collected-node-logs.zip"
.\azcopy.exe copy "C:\azuredata\CustomDataSetupScript.log" "$arg1/cse.log"
.\azcopy.exe copy "C:\AzureData\provision.complete" "$arg1/provision.complete"
.\azcopy.exe copy "C:\k\kubelet.err.log" "$arg1/kubelet.err.log"
.\azcopy.exe copy "C:\k\containerd.err.log" "$arg1/containerd.err.log"
`
// extractLogsFromVMWindows runs a script on windows VM to collect logs and upload them to a blob storage
// it then lists the blobs in the container and prints the content of each blob
func extractLogsFromVMWindows(ctx context.Context, s *Scenario) {
if !s.T.Failed() {
s.T.Logf("skipping logs extraction from windows VM, as the test didn't fail")
return
}
ctx, cancel := context.WithTimeout(ctx, 4*time.Minute)
defer cancel()
pager := config.Azure.VMSSVM.NewListPager(*s.Runtime.Cluster.Model.Properties.NodeResourceGroup, s.Runtime.VMSSName, nil)
page, err := pager.NextPage(ctx)
if err != nil {
s.T.Logf("failed to list VMSS instances: %s", err)
return
}
if len(page.Value) == 0 {
s.T.Logf("no VMSS instances found")
return
}
instanceID := *page.Value[0].InstanceID
blobPrefix := s.Runtime.VMSSName
blobUrl := config.Config.BlobStorageAccountURL() + "/" + config.Config.BlobContainer + "/" + blobPrefix
client := config.Azure.VMSSVMRunCommands
// Invoke the RunCommand on the VMSS instance
s.T.Log("uploading windows logs to blob storage, may take a few minutes")
pollerResp, err := client.BeginCreateOrUpdate(
ctx,
*s.Runtime.Cluster.Model.Properties.NodeResourceGroup,
s.Runtime.VMSSName,
instanceID,
"RunPowerShellScript",
armcompute.VirtualMachineRunCommand{
Properties: &armcompute.VirtualMachineRunCommandProperties{
Source: &armcompute.VirtualMachineRunCommandScriptSource{
//CommandID: to.Ptr("RunPowerShellScript"),
Script: to.Ptr(uploadLogsPowershellScript),
},
Parameters: []*armcompute.RunCommandInputParameter{
{
Name: to.Ptr("arg1"),
Value: to.Ptr(blobUrl),
},
{
Name: to.Ptr("arg2"),
Value: to.Ptr(s.Runtime.VMSSName),
},
{
Name: to.Ptr("arg3"),
Value: to.Ptr(config.Config.VMIdentityResourceID()),
},
},
},
},
nil,
)
if err != nil {
s.T.Logf("failed to initiate run command on VMSS instance: %s", err)
return
}
// Poll the result until the operation is completed
runCommandResp, err := pollerResp.PollUntilDone(ctx, config.DefaultPollUntilDoneOptions)
if err != nil {
s.T.Logf("failed to execute run command on VMSS instance: %s", err)
return
}
s.T.Logf("run command executed successfully: %v", runCommandResp)
s.T.Logf("uploaded logs to %s", blobUrl)
downloadBlob := func(blobSuffix string) {
fileName := filepath.Join(testDir(s.T), blobSuffix)
err := os.MkdirAll(testDir(s.T), 0755)
if err != nil {
s.T.Logf("failed to create directory %q: %s", testDir(s.T), err)
return
}
file, err := os.Create(fileName)
if err != nil {
s.T.Logf("failed to create file %q: %s", fileName, err)
return
}
// NOTE, read after write is possible, list blobs is eventually consistent and may fail
_, err = config.Azure.Blob.DownloadFile(ctx, config.Config.BlobContainer, blobPrefix+"/"+blobSuffix, file, nil)
if err != nil {
s.T.Logf("failed to download collected logs: %s", err)
err = os.Remove(file.Name())
if err != nil {
s.T.Logf("failed to remove file: %s", err)
}
return
}
}
downloadBlob("collected-node-logs.zip")
downloadBlob("cse.log")
downloadBlob("provision.complete")
s.T.Logf("logs collected to %s", testDir(s.T))
}
func deleteVMSS(ctx context.Context, s *Scenario) {
// original context can be cancelled, but we still want to delete the VM
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), time.Minute)
defer cancel()
if config.Config.KeepVMSS {
s.T.Logf("vmss %q will be retained for debugging purposes, please make sure to manually delete it later", s.Runtime.VMSSName)
if err := writeToFile(s.T, "sshkey", string(s.Runtime.SSHKeyPrivate)); err != nil {
s.T.Logf("failed to write retained vmss %s private ssh key to disk: %s", s.Runtime.VMSSName, err)
}
return
}
_, err := config.Azure.VMSS.BeginDelete(ctx, *s.Runtime.Cluster.Model.Properties.NodeResourceGroup, s.Runtime.VMSSName, &armcompute.VirtualMachineScaleSetsClientBeginDeleteOptions{
ForceDeletion: to.Ptr(true),
})
if err != nil {
s.T.Logf("failed to delete vmss %q: %s", s.Runtime.VMSSName, err)
return
}
s.T.Logf("vmss %q deleted successfully", s.Runtime.VMSSName)
}
// Adds additional IP configs to the passed in vmss model based on the chosen cluster's setting of "maxPodsPerNode",
// as we need be able to allow AKS to allocate an additional IP config for each pod running on the given node.
// Additional info: https://learn.microsoft.com/en-us/azure/aks/configure-azure-cni
func addPodIPConfigsForAzureCNI(vmss *armcompute.VirtualMachineScaleSet, vmssName string, cluster *Cluster) error {
maxPodsPerNode, err := cluster.MaxPodsPerNode()
if err != nil {
return fmt.Errorf("failed to read agentpool MaxPods value from chosen cluster model: %w", err)
}
var podIPConfigs []*armcompute.VirtualMachineScaleSetIPConfiguration
for i := 1; i <= maxPodsPerNode; i++ {
ipConfig := &armcompute.VirtualMachineScaleSetIPConfiguration{
Name: to.Ptr(fmt.Sprintf("%s%d", vmssName, i)),
Properties: &armcompute.VirtualMachineScaleSetIPConfigurationProperties{
Subnet: &armcompute.APIEntityReference{
ID: to.Ptr(cluster.SubnetID),
},
},
}
podIPConfigs = append(podIPConfigs, ipConfig)
}
vmssNICConfig, err := getVMSSNICConfig(vmss)
if err != nil {
return fmt.Errorf("unable to get vmss nic: %w", err)
}
vmss.Properties.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations[0].Properties.IPConfigurations =
append(vmssNICConfig.Properties.IPConfigurations, podIPConfigs...)
return nil
}
func getVMPrivateIPAddress(ctx context.Context, s *Scenario) (string, error) {
pl := config.Azure.Core.Pipeline()
url := fmt.Sprintf(listVMSSNetworkInterfaceURLTemplate,
config.Config.SubscriptionID,
*s.Runtime.Cluster.Model.Properties.NodeResourceGroup,
s.Runtime.VMSSName,
0,
)
req, err := runtime.NewRequest(ctx, "GET", url)
if err != nil {
return "", err
}
resp, err := pl.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var instanceNICResult listVMSSVMNetworkInterfaceResult
if err := json.Unmarshal(respBytes, &instanceNICResult); err != nil {
return "", err
}
privateIP, err := getPrivateIP(instanceNICResult)
if err != nil {
return "", err
}
return privateIP, nil
}
// Returns a newly generated RSA public/private key pair with the private key in PEM format.
func getNewRSAKeyPair() (privatePEMBytes []byte, publicKeyBytes []byte, e error) {
privateKey, err := rsa.GenerateKey(crand.Reader, 4096)
if err != nil {
return nil, nil, fmt.Errorf("failed to create rsa private key: %w", err)
}
err = privateKey.Validate()
if err != nil {
return nil, nil, fmt.Errorf("failed to validate rsa private key: %w", err)
}
publicRsaKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return nil, nil, fmt.Errorf("failed to convert private to public key: %w", err)
}
publicKeyBytes = ssh.MarshalAuthorizedKey(publicRsaKey)
// Get ASN.1 DER format
privDER := x509.MarshalPKCS1PrivateKey(privateKey)
// pem.Block
privBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: privDER,
}
// Private key in PEM format
privatePEMBytes = pem.EncodeToMemory(&privBlock)
return
}
func generateVMSSNameLinux(t *testing.T) string {
name := fmt.Sprintf("%s-%s-%s", randomLowercaseString(4), time.Now().Format(time.DateOnly), t.Name())
name = strings.ReplaceAll(name, "_", "")
name = strings.ReplaceAll(name, "/", "")
name = strings.ReplaceAll(name, "Test", "")
name = strings.ToLower(name)
if len(name) > 57 { // a limit for VMSS name
name = name[:57]
}
return name
}
func generateVMSSNameWindows(t *testing.T) string {
// windows has a limit of 9 characters for VMSS name
// and doesn't allow "-"
return fmt.Sprintf("win%s", randomLowercaseString(4))
}
func generateVMSSName(s *Scenario) string {
if s.VHD.OS == config.OSWindows {
return generateVMSSNameWindows(s.T)
}
return generateVMSSNameLinux(s.T)
}
func getBaseVMSSModel(s *Scenario, customData, cseCmd string) armcompute.VirtualMachineScaleSet {
model := armcompute.VirtualMachineScaleSet{
Location: to.Ptr(config.Config.Location),
SKU: &armcompute.SKU{
Name: to.Ptr("Standard_D2ds_v5"),
Capacity: to.Ptr[int64](1),
},
Properties: &armcompute.VirtualMachineScaleSetProperties{
Overprovision: to.Ptr(false),
UpgradePolicy: &armcompute.UpgradePolicy{
Mode: to.Ptr(armcompute.UpgradeModeAutomatic),
},
VirtualMachineProfile: &armcompute.VirtualMachineScaleSetVMProfile{
OSProfile: &armcompute.VirtualMachineScaleSetOSProfile{
ComputerNamePrefix: to.Ptr(s.Runtime.VMSSName),
AdminUsername: to.Ptr("azureuser"),
CustomData: &customData,
LinuxConfiguration: &armcompute.LinuxConfiguration{
SSH: &armcompute.SSHConfiguration{
PublicKeys: []*armcompute.SSHPublicKey{
{
KeyData: to.Ptr(string(s.Runtime.SSHKeyPublic)),
Path: to.Ptr("/home/azureuser/.ssh/authorized_keys"),
},
},
},
},
},
StorageProfile: &armcompute.VirtualMachineScaleSetStorageProfile{
OSDisk: &armcompute.VirtualMachineScaleSetOSDisk{
CreateOption: to.Ptr(armcompute.DiskCreateOptionTypesFromImage),
DiskSizeGB: to.Ptr(int32(50)),
OSType: to.Ptr(armcompute.OperatingSystemTypesLinux),
Caching: to.Ptr(armcompute.CachingTypesReadOnly),
DiffDiskSettings: &armcompute.DiffDiskSettings{
Option: to.Ptr(armcompute.DiffDiskOptionsLocal),
},
},
},
NetworkProfile: &armcompute.VirtualMachineScaleSetNetworkProfile{
NetworkInterfaceConfigurations: []*armcompute.VirtualMachineScaleSetNetworkConfiguration{
{
Name: to.Ptr(s.Runtime.VMSSName),
Properties: &armcompute.VirtualMachineScaleSetNetworkConfigurationProperties{
Primary: to.Ptr(true),
EnableIPForwarding: to.Ptr(true),
IPConfigurations: []*armcompute.VirtualMachineScaleSetIPConfiguration{
{
Name: to.Ptr(fmt.Sprintf("%s0", s.Runtime.VMSSName)),
Properties: &armcompute.VirtualMachineScaleSetIPConfigurationProperties{
Primary: to.Ptr(true),
LoadBalancerBackendAddressPools: []*armcompute.SubResource{
{
ID: to.Ptr(
fmt.Sprintf(
loadBalancerBackendAddressPoolIDTemplate,
config.Config.SubscriptionID,
*s.Runtime.Cluster.Model.Properties.NodeResourceGroup,
),
),
},
},
Subnet: &armcompute.APIEntityReference{
ID: to.Ptr(s.Runtime.Cluster.SubnetID),
},
},
},
},
},
},
},
},
},
},
}
if cseCmd != "" {
model.Properties.VirtualMachineProfile.ExtensionProfile = &armcompute.VirtualMachineScaleSetExtensionProfile{
Extensions: []*armcompute.VirtualMachineScaleSetExtension{
{
Name: to.Ptr("vmssCSE"),
Properties: &armcompute.VirtualMachineScaleSetExtensionProperties{
Publisher: to.Ptr("Microsoft.Azure.Extensions"),
Type: to.Ptr("CustomScript"),
TypeHandlerVersion: to.Ptr("2.0"),
AutoUpgradeMinorVersion: to.Ptr(true),
Settings: map[string]interface{}{},
ProtectedSettings: map[string]interface{}{
"commandToExecute": cseCmd,
},
},
},
},
}
}
if s.VHD.OS == config.OSWindows {
model.Identity = &armcompute.VirtualMachineScaleSetIdentity{
Type: to.Ptr(armcompute.ResourceIdentityTypeSystemAssignedUserAssigned),
UserAssignedIdentities: map[string]*armcompute.UserAssignedIdentitiesValue{
config.Config.VMIdentityResourceID(): {},
},
}
model.Properties.VirtualMachineProfile.StorageProfile.OSDisk.OSType = to.Ptr(armcompute.OperatingSystemTypesWindows)
model.Properties.VirtualMachineProfile.OSProfile.LinuxConfiguration = nil
model.Properties.VirtualMachineProfile.ExtensionProfile.Extensions[0].Properties.Publisher = to.Ptr("Microsoft.Compute")
model.Properties.VirtualMachineProfile.ExtensionProfile.Extensions[0].Properties.Type = to.Ptr("CustomScriptExtension")
model.Properties.VirtualMachineProfile.ExtensionProfile.Extensions[0].Properties.TypeHandlerVersion = to.Ptr("1.10")
model.Properties.VirtualMachineProfile.OSProfile.AdminUsername = to.Ptr("azureuser")
model.Properties.VirtualMachineProfile.OSProfile.AdminPassword = to.Ptr(generateWindowsPassword())
}
return model
}
func generateWindowsPassword() string {
if config.Config.WindowsAdminPassword != "" {
return config.Config.WindowsAdminPassword
}
return randomStringWithDigitsAndSymbols(16)
}
func randomStringWithDigitsAndSymbols(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const digits = "0123456789"
const symbols = "!@#$%^&*()-_+="
b := make([]byte, length)
for i := range b {
if i < (length - 2) { // Ensure at least 2 characters are symbols
b[i] = charset[randomInt(len(charset))]
} else if i == (length - 2) {
b[i] = digits[randomInt(len(digits))]
} else {
b[i] = symbols[randomInt(len(symbols))]
}
}
return string(b)
}
func randomInt(bound int) int {
n, err := crand.Int(crand.Reader, big.NewInt(int64(bound)))
if err != nil {
panic(err) // Intentionally panic for simplicity; handle errors as needed
}
return int(n.Int64())
}
func getPrivateIP(res listVMSSVMNetworkInterfaceResult) (string, error) {
if len(res.Value) > 0 {
v := res.Value[0]
if len(v.Properties.IPConfigurations) > 0 {
ipconfig := v.Properties.IPConfigurations[0]
return ipconfig.Properties.PrivateIPAddress, nil
}
}
return "", fmt.Errorf("unable to extract private IP address from listVMSSNetworkInterfaceResult:\n%+v", res)
}
func getVMSSNICConfig(vmss *armcompute.VirtualMachineScaleSet) (*armcompute.VirtualMachineScaleSetNetworkConfiguration, error) {
if vmss != nil && vmss.Properties != nil &&
vmss.Properties.VirtualMachineProfile != nil && vmss.Properties.VirtualMachineProfile.NetworkProfile != nil {
networkProfile := vmss.Properties.VirtualMachineProfile.NetworkProfile
if len(networkProfile.NetworkInterfaceConfigurations) > 0 {
return networkProfile.NetworkInterfaceConfigurations[0], nil
}
}
return nil, fmt.Errorf("unable to extract vmss nic info, vmss model or vmss model properties were nil/empty:\n%+v", vmss)
}
type listVMSSVMNetworkInterfaceResult struct {
Value []struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
Properties struct {
ProvisioningState string `json:"provisioningState,omitempty"`
IPConfigurations []struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
Properties struct {
ProvisioningState string `json:"provisioningState,omitempty"`
PrivateIPAddress string `json:"privateIPAddress,omitempty"`
PrivateIPAllocationMethod string `json:"privateIPAllocationMethod,omitempty"`
PublicIPAddress struct {
ID string `json:"id,omitempty"`
} `json:"publicIPAddress,omitempty"`
Subnet struct {
ID string `json:"id,omitempty"`
} `json:"subnet,omitempty"`
Primary bool `json:"primary,omitempty"`
PrivateIPAddressVersion string `json:"privateIPAddressVersion,omitempty"`
LoadBalancerBackendAddressPools []struct {
ID string `json:"id,omitempty"`
} `json:"loadBalancerBackendAddressPools,omitempty"`
LoadBalancerInboundNatRules []struct {
ID string `json:"id,omitempty"`
} `json:"loadBalancerInboundNatRules,omitempty"`
} `json:"properties,omitempty"`
} `json:"ipConfigurations,omitempty"`
DNSSettings struct {
DNSServers []interface{} `json:"dnsServers,omitempty"`
AppliedDNSServers []interface{} `json:"appliedDnsServers,omitempty"`
InternalDomainNameSuffix string `json:"internalDomainNameSuffix,omitempty"`
} `json:"dnsSettings,omitempty"`
MacAddress string `json:"macAddress,omitempty"`
EnableAcceleratedNetworking bool `json:"enableAcceleratedNetworking,omitempty"`
EnableIPForwarding bool `json:"enableIPForwarding,omitempty"`
NetworkSecurityGroup struct {
ID string `json:"id,omitempty"`
} `json:"networkSecurityGroup,omitempty"`
Primary bool `json:"primary,omitempty"`
VirtualMachine struct {
ID string `json:"id,omitempty"`
} `json:"virtualMachine,omitempty"`
} `json:"properties,omitempty"`
} `json:"value,omitempty"`
}