|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2025 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 # exit immediately if a command exits with a non-zero status. |
| 17 | +set -o nounset # exit when script tries to use undeclared variables. |
| 18 | +set -o pipefail # make the pipeline fail if any command in it fails. |
| 19 | + |
| 20 | +# Color definitions |
| 21 | +RED='\033[0;31m' |
| 22 | +GREEN='\033[0;32m' |
| 23 | +YELLOW='\033[1;33m' |
| 24 | +CYAN='\033[0;36m' |
| 25 | +NC='\033[0m' # No Color |
| 26 | + |
| 27 | +# Initialize variables |
| 28 | +KUBE_CONTEXT="" |
| 29 | + |
| 30 | +# Functions to print colored messages |
| 31 | +print_success() { |
| 32 | + echo -e "${GREEN} $1${NC}" >&2; |
| 33 | +} |
| 34 | + |
| 35 | +print_info() { |
| 36 | + echo -e "${CYAN} $1${NC}" >&2; |
| 37 | +} |
| 38 | + |
| 39 | +print_warning() { |
| 40 | + echo -e "${YELLOW} $1${NC}" >&2; |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +print_error() { |
| 45 | + echo -e "${RED} $1${NC}" >&2; |
| 46 | +} |
| 47 | + |
| 48 | +parse_args() { |
| 49 | + |
| 50 | + # Check if MGMT_CLUSTER_NAME is set, if yes, use it as the context |
| 51 | + if [ -n "${MGMT_CLUSTER_NAME:-}" ]; then |
| 52 | + KUBE_CONTEXT="${MGMT_CLUSTER_NAME}" |
| 53 | + print_info "using MGMT_CLUSTER_NAME: $KUBE_CONTEXT" |
| 54 | + |
| 55 | + if ! kubectl config use-context "$KUBE_CONTEXT" >/dev/null 2>&1; then |
| 56 | + print_error "Failed to switch to context '$KUBE_CONTEXT'" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + fi |
| 60 | + |
| 61 | + if [ -z "$KUBE_CONTEXT" ]; then |
| 62 | + KUBE_CONTEXT=$(kubectl config current-context) |
| 63 | + print_warning "no context provided – using current context: $KUBE_CONTEXT" >&2 |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +check_aks_cluster_exists() { |
| 68 | + # Check if the AKS cluster exists |
| 69 | + if ! az aks show --name "$KUBE_CONTEXT" --resource-group "$KUBE_CONTEXT" >/dev/null 2>&1; then |
| 70 | + print_error "AKS cluster '$KUBE_CONTEXT' does not exist" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +delete_resources() { |
| 76 | + local -r resources=("deployment" "secret" "serviceaccount") |
| 77 | + local -r namespaces=("capz-system" "caaph-system" "capi-kubeadm-bootstrap-system" "capi-kubeadm-control-plane-system" "capi-system") |
| 78 | + |
| 79 | + for resource in "${resources[@]}"; do |
| 80 | + print_info "Deleting all the ${resource}s from namespaces: ${namespaces[*]}" |
| 81 | + for namespace in "${namespaces[@]}"; do |
| 82 | + kubectl delete "${resource}" -n "${namespace}" --all |
| 83 | + done |
| 84 | + done |
| 85 | +} |
| 86 | + |
| 87 | +delete_crds() { |
| 88 | + # delete all the CRDs from the ASO_CRDS_PATH. ASO_CRDS_PATH is defined in Makefile. |
| 89 | + # ASO_CRDS_PATH has the path to the yaml that has all the CRDs required for ASO. |
| 90 | + print_info "Deleting all the CRDs from the ASO_CRDS_PATH using kubectl delete -f ${ASO_CRDS_PATH}" |
| 91 | + if ! kubectl delete -f "${ASO_CRDS_PATH}" --force 2>/dev/null; then |
| 92 | + print_warning "No ASO CRDs found or error deleting them, continuing..." |
| 93 | + else |
| 94 | + print_success "Successfully deleted ASO CRDs" |
| 95 | + fi |
| 96 | + |
| 97 | + # delete all the CRDs from the CRD_ROOT. CRD_ROOT is defined in Makefile. |
| 98 | + # CRD_ROOT leads to a directory with a list of CRD yaml files for CAPZ. |
| 99 | + print_info "Deleting all the CRDs from the CRD_ROOT" |
| 100 | + for crd_file in "${CRD_ROOT:-}"/*; do |
| 101 | + if [ -f "$crd_file" ]; then |
| 102 | + if ! kubectl delete -f "$crd_file" --force 2>/dev/null; then |
| 103 | + print_warning "Failed to delete CRD from $crd_file, continuing..." |
| 104 | + else |
| 105 | + print_success "Successfully deleted CRDs from $crd_file" |
| 106 | + fi |
| 107 | + fi |
| 108 | + done |
| 109 | +} |
| 110 | + |
| 111 | +main() { |
| 112 | + # Parse arguments and read into variables |
| 113 | + parse_args "$@" |
| 114 | + print_success "Successfully initialized with context: $KUBE_CONTEXT" |
| 115 | + |
| 116 | + check_aks_cluster_exists |
| 117 | + delete_resources |
| 118 | + |
| 119 | + if [ "${DELETE_CRDS:-}" == "true" ]; then |
| 120 | + delete_crds |
| 121 | + fi |
| 122 | + |
| 123 | + # Once the deployments are deleted, the AKS cluster is ready to be reused |
| 124 | + print_success "AKS cluster '$KUBE_CONTEXT' is ready to be reused" |
| 125 | +} |
| 126 | + |
| 127 | +# Only run main if script is executed directly (not sourced) |
| 128 | +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then |
| 129 | + main "$@" |
| 130 | +fi |
0 commit comments