-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistAKSClustersWithoutAnyWorkloads.ps1
More file actions
44 lines (33 loc) · 1.51 KB
/
Copy pathlistAKSClustersWithoutAnyWorkloads.ps1
File metadata and controls
44 lines (33 loc) · 1.51 KB
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
# TODO:
# This script will not identify any clusters probably as in the system namespace there are always some workloads running.
# Need to find a way to exclude system namespace from the list of workloads.
# Login to Azure
#Connect-AzAccount
# Get all AKS clusters
$aksClusters = Get-AzAKSCluster
# Initialize an array to store unused AKS clusters
$unusedAksClusters = @()
# Check each AKS cluster for nodes and workloads
foreach ($aksCluster in $aksClusters) {
$resourceGroupName = $aksCluster.ResourceGroupName
$aksClusterName = $aksCluster.Name
# Get the nodes in the AKS cluster
$nodes = Get-AzAKSNodePool -ResourceGroupName $resourceGroupName -ClusterName $aksClusterName
# Get the workloads in the AKS cluster using kubectl
Import-AzAksCredential -ResourceGroupName $resourceGroupName -Name $aksClusterName -Admin -Force -confirm:$false
$systemNamespaces = @('kube-system', 'kube-public', 'kube-node-lease', 'azure-arc')
$namespaces = & kubectl get namespaces -o jsonpath="{.items[*].metadata.name}" | `
ForEach-Object { $_.Split(' ') } | Where-Object { $systemNamespaces -notcontains $_ }
$workloads = @()
foreach ($ns in $namespaces) {
$pods = & kubectl get pods -n $ns
if ($pods) { $workloads += $pods }
}
if ( $workloads.Count -eq 0) {
$unusedAksClusters += $aksCluster
}
}
# Display the unused AKS clusters
$unusedAksClusters | ForEach-Object {
Write-Output "AKS Cluster: $($_.Name) - Resource Group: $($_.ResourceGroupName)"
}