Skip to content

Commit 576b367

Browse files
authored
Fix #151 check for 0 instance vmss (#152)
* vmss no instances * check and fail on 0 instance vmss
1 parent c7b8d7f commit 576b367

File tree

3 files changed

+195
-4
lines changed

3 files changed

+195
-4
lines changed

AzureBasicLoadBalancerUpgrade/module/AzureBasicLoadBalancerUpgrade/AzureBasicLoadBalancerUpgrade.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'AzureBasicLoadBalancerUpgrade'
1313

1414
# Version number of this module.
15-
ModuleVersion = '2.5.2'
15+
ModuleVersion = '2.5.3'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -107,7 +107,7 @@
107107
# IconUri = ''
108108

109109
# ReleaseNotes of this module
110-
ReleaseNotes = 'Fix Write-Progress error when running in PowerShell 5.1 (bad param)'
110+
ReleaseNotes = 'Check for and fail on VMSSes with no instances.'
111111

112112
# Prerelease string of this module
113113
# Prerelease = 'beta'

AzureBasicLoadBalancerUpgrade/module/AzureBasicLoadBalancerUpgrade/modules/ValidateScenario/ValidateScenario.psm1

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ function _GetScenarioBackendType {
5454
}
5555
}
5656

57-
5857
If (($backendMemberTypes | Sort-Object | Get-Unique).count -gt 1) {
5958
log -ErrorAction Stop -Message "[Test-SupportedMigrationScenario] Basic Load Balancer backend pools can contain only VMs or VMSSes, contains: '$($backendMemberTypes -join ',')'" -Severity 'Error'
6059
return
@@ -69,7 +68,43 @@ function _GetScenarioBackendType {
6968
}
7069
ElseIf ([string]::IsNullOrEmpty($backendMemberTypes[0])) {
7170
log -Message "[Test-SupportedMigrationScenario] Basic Load Balancer backend pools are empty"
72-
$backendType = 'Empty'
71+
72+
# check that there are no VMSSes associated with the Basic Load Balancer which have no instances
73+
log -Message "[Test-SupportedMigrationScenario] Checking if there are any VMSSes associated with the Basic Load Balancer but which have no instances..."
74+
$graphQuery = @"
75+
resources
76+
| where type =~ 'microsoft.compute/virtualmachinescalesets' and location =~ '$($BasicLoadBalancer.Location)'
77+
| where tostring(properties.virtualMachineProfile.networkProfile.networkInterfaceConfigurations) has '$($BasicLoadBalancer.id)'
78+
| count as vmssCount
79+
"@
80+
81+
log -Severity Verbose -Message "[Test-SupportedMigrationScenario]Graph Query Text: `n$graphQuery"
82+
$waitingForARG = $false
83+
$timeoutStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
84+
do {
85+
If (!$waitingForARG) {
86+
log -Message "[Test-SupportedMigrationScenario] Querying Resource Graph for VMSSes which reference this Load Balancer in their network profiles..."
87+
}
88+
Else {
89+
log -Message "[Test-SupportedMigrationScenario] Waiting 15 seconds before querying ARG again..."
90+
Start-Sleep 15
91+
}
92+
93+
$associatedVMSSCount = Search-AzGraph -Query $graphQuery
94+
95+
$waitingForARG = $true
96+
} while ($associatedVMSSCount.count -eq 0 -and $env:LBMIG_WAIT_FOR_ARG -and $timeoutStopwatch.Elapsed.Minutes -lt 15)
97+
98+
If ($timeoutStopwatch.Elapsed.Minutes -gt 15) {
99+
log -Severity Error -Message "[Test-SupportedMigrationScenario] Resource Graph query timed out before results were returned! The Resource Graph lags behind ARM by several minutes--if the resources to migrate were just created (as in a test), test the query from the log to determine if this was an ingestion lag or synax failure. Once the issue has been corrected, follow the documented migration recovery steps here: https://learn.microsoft.com/azure/load-balancer/upgrade-basic-standard-virtual-machine-scale-sets#what-happens-if-my-upgrade-fails-mid-migration" -terminateOnError
100+
}
101+
102+
If ($associatedVMSSCount.vmssCount -gt 0) {
103+
log -Message "[Test-SupportedMigrationScenario] Basic Load Balancer has a VMSS associated with it which has no instances. This scenario is not currently supported. WORKAROUND: scale your VMSS to at least one instance or remove the VMSS from the LB in the VMSS profile." -Severity 'Error' -terminateOnError
104+
}
105+
Else {
106+
$backendType = 'Empty'
107+
}
73108
}
74109
Else {
75110
log -ErrorAction Stop -Message "[Test-SupportedMigrationScenario] Basic Load Balancer backend pools can contain only VMs or VMSSes, contains: '$($backendMemberTypes -join ',')'" -Severity 'Error'
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
targetScope = 'subscription'
2+
param randomGuid string = newGuid()
3+
param location string
4+
param resourceGroupName string
5+
6+
7+
// Resource Group
8+
module rg '../modules/Microsoft.Resources/resourceGroups/deploy.bicep' = {
9+
name: '${resourceGroupName}-${location}'
10+
params: {
11+
name: resourceGroupName
12+
location: location
13+
}
14+
}
15+
16+
// vnet
17+
module virtualNetworks '../modules/Microsoft.Network/virtualNetworks/deploy.bicep' = {
18+
name: '${uniqueString(deployment().name)}-virtualNetworks'
19+
scope: resourceGroup(resourceGroupName)
20+
params: {
21+
// Required parameters
22+
location: location
23+
addressPrefixes: [
24+
'10.0.0.0/16'
25+
]
26+
name: 'vnet-01'
27+
subnets: [
28+
{
29+
name: 'subnet-01'
30+
addressPrefix: '10.0.1.0/24'
31+
}
32+
]
33+
}
34+
dependsOn: [
35+
rg
36+
]
37+
}
38+
39+
// basic lb
40+
module loadbalancer '../modules/Microsoft.Network/loadBalancers_custom/deploy.bicep' = {
41+
name: 'lb-basic-01'
42+
scope: resourceGroup(resourceGroupName)
43+
params: {
44+
name: 'lb-basic-01'
45+
location: location
46+
frontendIPConfigurations: [
47+
{
48+
name: 'fe-01'
49+
subnetId: virtualNetworks.outputs.subnetResourceIds[0]
50+
}
51+
]
52+
backendAddressPools: [
53+
{
54+
name: 'be-01'
55+
}
56+
]
57+
inboundNatRules: []
58+
loadBalancerSku: 'Basic'
59+
loadBalancingRules: [
60+
{
61+
backendAddressPoolName: 'be-01'
62+
backendPort: 80
63+
frontendIPConfigurationName: 'fe-01'
64+
frontendPort: 80
65+
idleTimeoutInMinutes: 4
66+
loadDistribution: 'Default'
67+
name: 'rule-01'
68+
probeName: 'probe-01'
69+
protocol: 'Tcp'
70+
}
71+
]
72+
probes: [
73+
{
74+
intervalInSeconds: 5
75+
name: 'probe-01'
76+
numberOfProbes: 2
77+
port: '80'
78+
protocol: 'Tcp'
79+
}
80+
]
81+
}
82+
dependsOn: [
83+
rg
84+
]
85+
}
86+
87+
88+
module storageAccounts '../modules/Microsoft.Storage/storageAccounts/deploy.bicep' = {
89+
name: 'bootdiag-storage-01'
90+
scope: resourceGroup(resourceGroupName)
91+
params: {
92+
name: 'bootdiag${uniqueString(deployment().name)}'
93+
location: location
94+
storageAccountSku: 'Standard_LRS'
95+
storageAccountKind: 'StorageV2'
96+
supportsHttpsTrafficOnly: true
97+
}
98+
dependsOn: [
99+
rg
100+
]
101+
}
102+
103+
module virtualMachineScaleSets '../modules/Microsoft.Compute/virtualMachineScaleSets/deploy.bicep' = {
104+
name: 'vmss-01'
105+
scope: resourceGroup(resourceGroupName)
106+
params: {
107+
location: location
108+
// Required parameters
109+
encryptionAtHost: false
110+
adminUsername: 'admin-lbmig'
111+
skuCapacity: 0
112+
upgradePolicyMode: 'Manual'
113+
imageReference: {
114+
offer: 'WindowsServer'
115+
publisher: 'MicrosoftWindowsServer'
116+
sku: '2022-Datacenter'
117+
version: 'latest'
118+
}
119+
bootDiagnosticStorageAccountName: storageAccounts.outputs.name
120+
name: 'vmss-01'
121+
osDisk: {
122+
createOption: 'fromImage'
123+
diskSizeGB: '128'
124+
managedDisk: {
125+
storageAccountType: 'Standard_LRS'
126+
}
127+
}
128+
osType: 'Windows'
129+
skuName: 'Standard_DS1_v2'
130+
// Non-required parameters
131+
adminPassword: '${uniqueString(randomGuid)}rpP@340'
132+
nicConfigurations: [
133+
{
134+
ipConfigurations: [
135+
{
136+
name: 'ipconfig1'
137+
properties: {
138+
subnet: {
139+
id: virtualNetworks.outputs.subnetResourceIds[0]
140+
}
141+
loadBalancerBackendAddressPools: [
142+
{
143+
id: loadbalancer.outputs.backendpools[0].id
144+
}
145+
]
146+
}
147+
}
148+
]
149+
nicSuffix: '-nic-01'
150+
}
151+
]
152+
}
153+
dependsOn: [
154+
rg
155+
]
156+
}

0 commit comments

Comments
 (0)