Last active
September 7, 2022 11:44
-
-
Save sebader/bcc51c68195a079e6b49da73a5139567 to your computer and use it in GitHub Desktop.
Azure Subnet IP address space exhaustion failure experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param prefix string = 'fillup' | |
param location string = resourceGroup().location | |
param vnetName string = 'my-vnet' | |
param subnetName string = 'my-snet' | |
param ipAddressesToCreate int = 600 | |
resource subnet 'Microsoft.Network/virtualNetworks/subnets@2022-01-01' existing = { | |
name: '${vnetName}/${subnetName}' | |
} | |
// Calculate the number of NICs to create. Each NIC can have up to 256 IP addresses. | |
var nicsToCreate = int(ipAddressesToCreate / 256) + 1 | |
resource nic 'Microsoft.Network/networkInterfaces@2022-01-01' = [for n in range(0, nicsToCreate): { | |
name: '${prefix}-${n}-test-nic' | |
location: location | |
properties: { | |
ipConfigurations: [for i in range(0, n < nicsToCreate - 1 ? 256 : ipAddressesToCreate % 256): { | |
name: 'ipconfig-${i}' | |
properties: { | |
subnet: { | |
id: subnet.id | |
} | |
primary: i == 0 | |
privateIPAllocationMethod: 'Dynamic' | |
} | |
}] | |
} | |
}] | |
output deployedNics array = [for i in range(0, nicsToCreate): { | |
name: nic[i].name | |
resourceId: nic[i].id | |
numberOfIpAddresses: length(nic[i].properties.ipConfigurations) | |
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$targetSubscriptionId = "*******" | |
$targetResourceGroup = "my-rg" | |
$targetVnetName = "my-vnet" | |
$targetSubnetName = "my-snet" | |
$prefix = "chaosfill" | |
$experimentDurationSeconds = 30 | |
# Fetch current VNet/subnets IP address allocation | |
$subnetSpace = az rest --method get ` | |
--url "/subscriptions/$targetSubscriptionId/resourceGroups/$targetResourceGroup/providers/Microsoft.Network/virtualNetworks/$targetVnetName/usages?api-version=2021-01-01" ` | |
| ConvertFrom-Json | |
$subnet = $subnetSpace | Select-Object -ExpandProperty value | Where-Object { $_.id -like "*/subnets/$targetSubnetName" } | |
$freeIpAddresses = $subnet.limit - $subnet.currentValue | |
Write-Host "Free IP addresses in subnet $($targetSubnetName): $freeIpAddresses" | |
if($freeIpAddresses -lt 1) { | |
Write-Host "No free IP addresses in subnet. Subnet does not exist or is already full." | |
exit | |
} | |
Write-Host "Starting ARM deployment to fill up the subnet" | |
$armDeployment = az deployment group create ` | |
--name "$prefix-deployment-$(Get-Date -Format "yyyyMMddHHmmss")" ` | |
--template-file .\dummy_nic.bicep ` | |
--resource-group $targetResourceGroup ` | |
--parameters ` | |
prefix=$prefix ` | |
vnetName=$targetVnetName ` | |
subnetName=$targetSubnetName ` | |
ipAddressesToCreate=$freeIpAddresses | ConvertFrom-Json | |
Write-Host "ARM deployment completed with status: $($armDeployment.properties.provisioningState)" | |
Write-Host "Sleeping for $experimentDurationSeconds seconds" | |
Start-Sleep -s $experimentDurationSeconds | |
$nics = $armDeployment.properties.outputs.deployedNics.value | |
Write-Host "Experiment finished. Deleting NICs to free up space again." | |
foreach($nic in $nics) | |
{ | |
Write-Host "Deleting NIC: $($nic.name)" | |
az resource delete --ids $nic.resourceId | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment