From 8e6ecb9466d1c0d17af3ad21d9b543af479190bc Mon Sep 17 00:00:00 2001 From: Ryan Richard Date: Fri, 3 Jan 2025 14:53:35 -0800 Subject: [PATCH] fix error handling in job to remove orphaned AKS clusters, update README --- README.md | 5 +++-- .../shared-tasks/remove-orphaned-aks-clusters/task.sh | 10 ++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e23a30834..eea4c0243 100644 --- a/README.md +++ b/README.md @@ -155,8 +155,9 @@ This requires the following: Create the app in "My Organization Only". It does not need a redirect URI or any other optional settings. Create a client secret for this app. If you want the client secret to have a long lifetime, you can use the `az` CLI to create it. In the Subscription's IAM settings, assign this app the role "Azure Kubernetes Service Contributor Role" to allow - the app to manage AKS clusters. Do not grant this app permissions in any other Subscription or use it for any - other purpose. + the app to manage AKS clusters. Also assign this app the role "Reader" to allow it to read all resources + (used by the `remove-orphaned-aks-clusters` CI task). + Do not grant this app permissions in any other Subscription or use it for any other purpose. 3. Configure the pipelines with the app's Application (client) ID, Client Secret, and Directory (tenant) ID as the appropriate secret values. diff --git a/pipelines/shared-tasks/remove-orphaned-aks-clusters/task.sh b/pipelines/shared-tasks/remove-orphaned-aks-clusters/task.sh index 91dc991f9..2dd3a5081 100755 --- a/pipelines/shared-tasks/remove-orphaned-aks-clusters/task.sh +++ b/pipelines/shared-tasks/remove-orphaned-aks-clusters/task.sh @@ -44,9 +44,15 @@ cat all-clusters.txt echo # Remove clusters with unexpected name formats. They might have been created manually for testing. -cat all-clusters.txt | grep -E '^aks-[a-f0-9]+ ' >ci-clusters.txt +# The curly braces and "or true" syntax prevents errors when the grep fails to find any matches. +cat all-clusters.txt | { grep -E '^aks-[a-f0-9]+ ' || true; } >ci-clusters.txt -echo "Only those clusters with expected naming convention:" +if [[ ! -s ci-clusters.txt ]]; then + echo "No clusters matching the expected naming convention were found." + exit 0 +fi + +echo "Only those clusters matched the expected naming convention:" cat ci-clusters.txt echo