Skip to content

Instantly share code, notes, and snippets.

@techdecline
Created July 13, 2022 08:14
Show Gist options
  • Save techdecline/12eec78f0541296ca315a10bb56faf03 to your computer and use it in GitHub Desktop.
Save techdecline/12eec78f0541296ca315a10bb56faf03 to your computer and use it in GitHub Desktop.
param (
[String]$TenantId = "ed2da482-854d-467a-a6c5-a56a9c91be6d"
)
$clusterArr = Get-AzSubscription -TenantId $TenantId | ForEach-Object { $null = Set-AzContext -Subscription $_.SubscriptionId -Tenant $TenantId ; Get-AzAksCluster }
$resultArr = [System.Collections.ArrayList]@()
function GetNodePoolMaxIp {
param (
[Microsoft.Azure.Commands.Aks.Models.PSContainerServiceAgentPoolProfile]$AgentPoolProfile
)
$maxIpCount = $AgentPoolProfile.MaxCount * $AgentPoolProfile.MaxPods
if (-not $maxIpCount) {
$maxIpCount = $AgentPoolProfile.Count * $AgentPoolProfile.MaxPods
}
return $maxIpCount
}
function GetAksUpgradePath {
param (
[Microsoft.Azure.PowerShell.Cmdlets.Aks.Models.Api20200901.ManagedClusterUpgradeProfile]$UpgradeProfile
)
return ($UpgradeProfile.ControlPlaneProfileUpgrade | Where-Object { $_.IsPreview -ne $true } | Sort-Object -Property KubernetesVersion -Descending -Top 1).KubernetesVersion
}
function cidrToIpRange {
param (
[string] $cidrNotation
)
$addr, $maskLength = $cidrNotation -split '/'
[int]$maskLen = 0
if (-not [int32]::TryParse($maskLength, [ref] $maskLen)) {
throw "Cannot parse CIDR mask length string: '$maskLen'"
}
if (0 -gt $maskLen -or $maskLen -gt 32) {
throw "CIDR mask length must be between 0 and 32"
}
$ipAddr = [Net.IPAddress]::Parse($addr)
if ($ipAddr -eq $null) {
throw "Cannot parse IP address: $addr"
}
if ($ipAddr.AddressFamily -ne [Net.Sockets.AddressFamily]::InterNetwork) {
throw "Can only process CIDR for IPv4"
}
$shiftCnt = 32 - $maskLen
$mask = -bnot ((1 -shl $shiftCnt) - 1)
$ipNum = [Net.IPAddress]::NetworkToHostOrder([BitConverter]::ToInt32($ipAddr.GetAddressBytes(), 0))
$ipStart = ($ipNum -band $mask) + 1
$ipEnd = ($ipNum -bor (-bnot $mask)) - 1
# return as tuple of strings:
$firstAddress = ([BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($ipStart)) | ForEach-Object { $_ } ) -join '.'
$lastAddress = ([BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($ipEnd)) | ForEach-Object { $_ } ) -join '.'
return (1 | Select-Object -Property @{Name = 'FirstIpAddress'; Expression = { $firstAddress } }, @{Name = 'LastIpAddress'; Expression = { $lastAddress } })
}
# $start, $end = cidrToIpRange "192.168.100.14/24"
# Write-Host "Start: $start, end: $end"
# $start, $end = cidrToIpRange "10.77.1.1/18"
# Write-Host "Start: $start, end: $end"
function New-IPRange ($start, $end) {
# created by Dr. Tobias Weltner, MVP PowerShell
$ip1 = ([System.Net.IPAddress]$start).GetAddressBytes()
[Array]::Reverse($ip1)
$ip1 = ([System.Net.IPAddress]($ip1 -join '.')).Address
$ip2 = ([System.Net.IPAddress]$end).GetAddressBytes()
[Array]::Reverse($ip2)
$ip2 = ([System.Net.IPAddress]($ip2 -join '.')).Address
for ($x = $ip1; $x -le $ip2; $x++) {
$ip = ([System.Net.IPAddress]$x).GetAddressBytes()
[Array]::Reverse($ip)
$ip -join '.'
}
}
foreach ($clusterObj in $clusterArr) {
$null = Set-AzContext -Subscription ($clusterObj.Id -split "/")[2] -Tenant $TenantId
$maxUpgrade = GetAksUpgradePath(Get-AzAksUpgradeProfile -ClusterName $clusterObj.Name -ResourceGroupName $clusterObj.ResourceGroupName)
$subscriptionName = (Get-AzContext).subscription.Name
$networkPlugin = $clusterObj.NetworkProfile.NetworkPlugin
$npArr = $clusterObj.AgentPoolProfiles | Select-Object -Property Name, OrchestratorVersion, MaxCount, MaxPods, VnetSubnetID, @{Name = 'MaxIpCount'; Expression = { GetNodePoolMaxIp($_) } }
foreach ($np in $npArr) {
$subnetObj = Get-AzVirtualNetworkSubnetConfig -ResourceId $np.VnetSubnetID
$totalAddressCount = (New-IPRange (cidrToIpRange($subnetObj.AddressPrefix)).FirstIpAddress (cidrToIpRange($subnetObj.AddressPrefix)).LastIpAddress | Measure-Object).Count
$consumedAddressCount = ($subnetObj.IpConfigurations | Measure-Object).Count
$tmpObj = $clusterObj | Select-Object -Property @{Name = 'SubscriptionName'; Expression = { $subscriptionName } }, Name, KubernetesVersion, @{Name = 'MaxUpgradeVersion'; Expression = { $maxUpgrade } }, @{Name = 'NodePoolName'; Expression = { $np.Name } }, @{Name = 'OrchestratorVersion'; Expression = { $np.OrchestratorVersion } }, @{Name = 'MaxIpCount'; Expression = { $np.MaxIpCount } }, @{Name = 'SubnetAddressCount'; Expression = { $totalAddressCount } }, @{Name = 'ConsumedAddressCount'; Expression = { $consumedAddressCount } }, @{Name = 'NetworkPlugin'; Expression = { $networkPlugin } }, @{Name = 'NodePoolSubnetId'; Expression = { $np.VnetSubnetID } }
$null = $resultArr.Add($tmpObj)
}
}
return $resultArr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment