Skip to content

Commit a9ece06

Browse files
updated script to assign AKS cluster role to user
1 parent 6e9dc0f commit a9ece06

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

Deployment/resourcedeployment.ps1

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) Microsoft Corporation.
1+
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT license.
33

44
#https://patorjk.com/software/taag
@@ -502,6 +502,38 @@ try {
502502

503503
# 2.Connect to AKS cluster
504504
try {
505+
Write-Host "Checking if user already has AKS Cluster Admin role..." -ForegroundColor Cyan
506+
# -----------------------------------------
507+
# Check and assign AKS RBAC Cluster Admin role
508+
# -----------------------------------------
509+
510+
$subscriptionId = (az account show --query id -o tsv)
511+
$resourceGroup = $deploymentResult.ResourceGroupName
512+
$aksName = $deploymentResult.AksName
513+
514+
# Get current signed-in user
515+
$currentUser = az ad signed-in-user show --query id -o tsv
516+
517+
# Get AKS resource ID
518+
$aksResourceId = az aks show --resource-group $resourceGroup --name $aksName --subscription $subscriptionId --query id -o tsv
519+
520+
# Check if role already assigned
521+
$roleCheck = az role assignment list `
522+
--assignee $currentUser `
523+
--role "Azure Kubernetes Service RBAC Cluster Admin" `
524+
--scope $aksResourceId `
525+
--query "[].id" -o tsv
526+
527+
if (-not $roleCheck) {
528+
Write-Host "Assigning 'Azure Kubernetes Service RBAC Cluster Admin' role to current user..."
529+
az role assignment create `
530+
--assignee $currentUser `
531+
--role "Azure Kubernetes Service RBAC Cluster Admin" `
532+
--scope $aksResourceId | Out-Null
533+
Write-Host "Role assignment complete."
534+
} else {
535+
Write-Host "User already has 'Azure Kubernetes Service RBAC Cluster Admin' role."
536+
}
505537
Write-Host "Connecting to AKS cluster..." -ForegroundColor Cyan
506538
az aks get-credentials --resource-group $deploymentResult.ResourceGroupName --name $deploymentResult.AksName --overwrite-existing
507539
Write-Host "Connected to AKS cluster." -ForegroundColor Green

0 commit comments

Comments
 (0)