GeekOut – Get vCores for Kubernetes

GeekOut – Get vCores for Kubernetes

Often you will be working out licensing costs and more often than not, you will need to know the number of vCores - as a baseline use the following script.

Get-AKSVCores.ps1
<#
.SYNOPSIS
Calculates total vCores for each Azure Kubernetes Service (AKS) cluster listed in a CSV file.

.DESCRIPTION
This script imports a CSV file containing AKS cluster information, iterates through each cluster, and calculates the total number of virtual cores (vCores) based on the node pools associated with each cluster. It requires Azure CLI and assumes that the user has the necessary permissions to access the AKS clusters and VM sizes.

.PARAMETER CsvFilePath
Full path to the CSV file containing AKS cluster information. The CSV file should have columns named 'ClusterName', 'Subscription', and 'ResourceGroup'.

.PARAMETER VmLocation
Azure region to get VM sizes for the calculation. Default is 'Australia East'.

.PARAMETER PerformAzureLogin
Indicates whether the script should perform Azure login. Set to $true if Azure login is required within the script; otherwise, $false. Default is $false.

.EXAMPLE
.\Get-AKSVCores.ps1 -CsvFilePath "C:\path\to\aks_clusters.csv" -VmLocation "Australia East" -PerformAzureLogin $true

This example runs the script with the specified CSV file path, VM location, and performs Azure login within the script.

.INPUTS
CSV file

.OUTPUTS
Console output of each AKS cluster's name, subscription, resource group, and total vCores.

.NOTES
Version: 1.0
Author: Romiko Derbynew
Creation Date: 2024-01-22
Purpose/Change: Get Total VCores for Clusters
#>

param(
[Parameter(Mandatory = $true)]
[string]$CsvFilePath,

[Parameter(Mandatory = $false)]
[string]$VmLocation = "Australia East",

[Parameter(Mandatory = $false)]
[bool]$PerformAzureLogin = $true
)

# Azure login if required
if ($PerformAzureLogin) {
az login
}

# Import the CSV file
$aksClusters = Import-Csv -Path $CsvFilePath

Write-Host "ClusterName,Subscription,ResourceGroup,TotalVCores"

# Iterate through each AKS cluster
foreach ($cluster in $aksClusters) {
# Set the current subscription
az account set --subscription $cluster.Subscription

# Logic to get the resource group
$resourceGroup = $cluster.ResourceGroup

# Get the node pools for the AKS cluster
$nodePools = az aks nodepool list --resource-group $resourceGroup --cluster-name $cluster.ClusterName --query "[].{name: name, count: count, vmSize: vmSize}" | ConvertFrom-Json

$totalVCores = 0

# Iterate through each node pool and calculate total vCores
foreach ($nodePool in $nodePools) {
# Get the VM size details
$vmSizeDetails = az vm list-sizes --location $VmLocation --query "[?name=='$($nodePool.vmSize)'].{numberOfCores: numberOfCores}" | ConvertFrom-Json
$vCores = $vmSizeDetails.numberOfCores

# Calculate total vCores for the node pool
$totalVCores += $vCores * $nodePool.count
}

# Output the total vCores for the cluster
Write-Host "$($cluster.ClusterName),$($cluster.Subscription),$($cluster.ResourceGroup),$totalVCores"
}

Leave a comment