Skip to content

Commit 66244df

Browse files
Nitesh703nawazkh
andauthored
Feature/add aks-delete make target (#5576)
* Add make aks-reset target to clean up AKS clusters * Add aks-reset target to Makefile * Make aks-reset.sh executable * aks-reset commit * test cla * Update Makefile Co-authored-by: Nawaz K <k.nawaz.h@gmail.com> * scanned all contexts listed in allowed_context * Update aks-reset.sh to scan all contexts and verify against Azure * Add aks-reset target to Makefile * Update Makefile Co-authored-by: Nawaz K <k.nawaz.h@gmail.com> * Rename aks-reset to aks-delete to better reflect function * Remove old aks-reset.sh script --------- Co-authored-by: Nawaz K <k.nawaz.h@gmail.com>
1 parent 6ee3670 commit 66244df

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,10 @@ aks-create: $(KUBECTL) ## Create aks cluster as mgmt cluster.
780780
./scripts/aks-as-mgmt.sh
781781
MANIFEST_IMG=$(CONTROLLER_IMG) MANIFEST_TAG=$(TAG) $(MAKE) set-manifest-image
782782

783+
.PHONY: aks-delete
784+
aks-delete: $(KUBECTL) ## Deletes the resource group and the associated AKS clusters listed under allowed_contexts in ./tilt-settings.yaml .
785+
./scripts/aks-delete.sh
786+
783787
.PHONY: tilt-up
784788
tilt-up: install-tools ## Start tilt and build kind cluster if needed.
785789
@if [ -z "${AZURE_CLIENT_ID_USER_ASSIGNED_IDENTITY}" ]; then \

scripts/aks-delete.sh

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2024 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
21+
# shellcheck source=hack/ensure-azcli.sh
22+
source "${REPO_ROOT}/hack/ensure-azcli.sh"
23+
24+
TILT_SETTINGS_FILE="${REPO_ROOT}/tilt-settings.yaml"
25+
AZURE_IDENTITY_ID_FILEPATH="${REPO_ROOT}/azure_identity_id"
26+
AKS_MGMT_CONFIG_FILE="${REPO_ROOT}/aks-mgmt.config"
27+
28+
function main() {
29+
if [ ! -f "$TILT_SETTINGS_FILE" ]; then
30+
echo "No tilt-settings.yaml found. Nothing to reset."
31+
exit 0
32+
fi
33+
34+
# Check if yq is installed
35+
if ! command -v yq &> /dev/null; then
36+
echo "yq is required but not installed. Please install yq first."
37+
exit 1
38+
fi
39+
40+
# Get all contexts from tilt-settings.yaml
41+
local contexts
42+
contexts=$(yq '.allowed_contexts[]?' "$TILT_SETTINGS_FILE")
43+
44+
if [ -z "$contexts" ]; then
45+
echo "No contexts found in tilt-settings.yaml. Nothing to reset."
46+
exit 0
47+
fi
48+
49+
echo "Scanning all contexts from tilt-settings.yaml..."
50+
51+
# Track which AKS clusters were found for cleanup
52+
local found_aks_clusters=()
53+
54+
# Check each context to see if it's an AKS cluster
55+
while IFS= read -r context; do
56+
if [ -n "$context" ]; then
57+
echo "Checking context: $context"
58+
59+
# Try to get the AKS cluster info from Azure
60+
if az aks show --name "$context" --resource-group "$context" &>/dev/null; then
61+
echo "Found AKS cluster: $context"
62+
found_aks_clusters+=("$context")
63+
else
64+
echo "Context $context is not an AKS cluster or doesn't exist in Azure."
65+
fi
66+
fi
67+
done <<< "$contexts"
68+
69+
if [ ${#found_aks_clusters[@]} -eq 0 ]; then
70+
echo "No AKS clusters found. Nothing to reset."
71+
exit 0
72+
fi
73+
74+
echo "Found ${#found_aks_clusters[@]} AKS clusters to delete:"
75+
printf " %s\n" "${found_aks_clusters[@]}"
76+
77+
# Delete each AKS cluster and its resource group
78+
for cluster in "${found_aks_clusters[@]}"; do
79+
echo "Deleting AKS cluster and resource group: $cluster"
80+
81+
# Delete the resource group (which includes the AKS cluster)
82+
if az group exists --name "$cluster" | grep -q "true"; then
83+
az group delete --name "$cluster" --yes --no-wait
84+
echo "Deletion of resource group $cluster initiated (running in background)"
85+
else
86+
echo "Resource group $cluster does not exist"
87+
fi
88+
89+
# Remove the context from kubectl config
90+
if kubectl config get-contexts "$cluster" &>/dev/null; then
91+
kubectl config delete-context "$cluster" || true
92+
echo "Removed kubectl context for $cluster"
93+
fi
94+
done
95+
96+
# Remove the AKS contexts from tilt-settings.yaml
97+
for cluster in "${found_aks_clusters[@]}"; do
98+
yq eval -i "del(.allowed_contexts[] | select(. == \"$cluster\"))" "$TILT_SETTINGS_FILE"
99+
done
100+
101+
# Clean up aks_as_mgmt_settings if it exists
102+
if yq eval '.aks_as_mgmt_settings' "$TILT_SETTINGS_FILE" | grep -qv "null"; then
103+
echo "Cleaning up aks_as_mgmt_settings from tilt-settings.yaml"
104+
yq eval -i 'del(.aks_as_mgmt_settings)' "$TILT_SETTINGS_FILE"
105+
fi
106+
107+
# Clean up other AKS-related files
108+
if [ -f "$AZURE_IDENTITY_ID_FILEPATH" ]; then
109+
rm -f "$AZURE_IDENTITY_ID_FILEPATH"
110+
echo "Removed $AZURE_IDENTITY_ID_FILEPATH"
111+
fi
112+
113+
if [ -f "$AKS_MGMT_CONFIG_FILE" ]; then
114+
rm -f "$AKS_MGMT_CONFIG_FILE"
115+
echo "Removed $AKS_MGMT_CONFIG_FILE"
116+
fi
117+
118+
echo "AKS reset completed successfully."
119+
}
120+
121+
main

0 commit comments

Comments
 (0)