From 46efc3d3f8f012bd465db71244b48193424480fa Mon Sep 17 00:00:00 2001 From: Jordan Hoey Date: Thu, 17 Oct 2024 13:18:55 +0100 Subject: [PATCH] Updating manual AKS script --- .github/workflows/aks-manual-shutdown.yaml | 15 +--- .github/workflows/aks-manual-start.yaml | 13 +-- scripts/aks/common-functions.sh | 16 ++++ scripts/aks/manual-start-stop.sh | 94 ++++++++++++++++------ 4 files changed, 91 insertions(+), 47 deletions(-) diff --git a/.github/workflows/aks-manual-shutdown.yaml b/.github/workflows/aks-manual-shutdown.yaml index 46a0cf41..b979b398 100644 --- a/.github/workflows/aks-manual-shutdown.yaml +++ b/.github/workflows/aks-manual-shutdown.yaml @@ -2,7 +2,7 @@ name: AKS Manual Shutdown on: workflow_dispatch: inputs: - PROJECT: + SELECTED_AREA: type: choice description: "Business area" options: @@ -20,17 +20,10 @@ on: - ITHC - PTL - PTLSBOX - INSTANCES: - type: choice - description: "Cluster" - options: - - All - - "00" - - "01" env: - PROJECT: ${{ inputs.PROJECT }} + SELECTED_AREA: ${{ inputs.SELECTED_AREA }} SELECTED_ENV: ${{ inputs.SELECTED_ENV }} - INSTANCES: ${{ inputs.INSTANCES }} + DEV_ENV: ${{ secrets.DEV_ENV }} permissions: id-token: write jobs: @@ -45,5 +38,5 @@ jobs: client-id: 2b6fa9d7-7dba-4600-a58a-5e25554997aa # DTS AKS Auto-Shutdown tenant-id: 531ff96d-0ae9-462a-8d2d-bec7c0b42082 # HMCTS.NET allow-no-subscriptions: true - - name: AKS Manual Stop + - name: AKS Manual Start run: ./scripts/aks/manual-start-stop.sh stop diff --git a/.github/workflows/aks-manual-start.yaml b/.github/workflows/aks-manual-start.yaml index 938eb023..0bba6afb 100644 --- a/.github/workflows/aks-manual-start.yaml +++ b/.github/workflows/aks-manual-start.yaml @@ -2,7 +2,7 @@ name: AKS Manual Start on: workflow_dispatch: inputs: - PROJECT: + SELECTED_AREA: type: choice description: "Business area" options: @@ -20,17 +20,10 @@ on: - ITHC - PTL - PTLSBOX - INSTANCES: - type: choice - description: "Cluster" - options: - - All - - "00" - - "01" env: - PROJECT: ${{ inputs.PROJECT }} + SELECTED_AREA: ${{ inputs.SELECTED_AREA }} SELECTED_ENV: ${{ inputs.SELECTED_ENV }} - INSTANCES: ${{ inputs.INSTANCES }} + DEV_ENV: ${{ secrets.DEV_ENV }} permissions: id-token: write jobs: diff --git a/scripts/aks/common-functions.sh b/scripts/aks/common-functions.sh index 9d170a9e..d33a7892 100644 --- a/scripts/aks/common-functions.sh +++ b/scripts/aks/common-functions.sh @@ -1,4 +1,13 @@ #!/bin/bash +shopt -s nocasematch + +# Function to convert a string to lowercase +to_lowercase() { + local input="$1" + local lowercase="${input,,}" # Convert to lowercase using parameter expansion + echo "$lowercase" +} + function get_clusters() { #MS az graph query to find and return a list of all AKS tagged to be included in the auto-shutdown process. log "----------------------------------------------" @@ -12,11 +21,18 @@ function get_clusters() { env_selector="| where tags.environment == '$1'" fi + if [ -z $2 ]; then + area_selector="" + else + area_selector="| where tags.businessArea == '$2'" + fi + az graph query -q " resources | where type =~ 'Microsoft.ContainerService/managedClusters' | where tags.autoShutdown == 'true' $env_selector + $area_selector | project name, resourceGroup, subscriptionId, ['tags'], properties, ['id'] " --first 1000 -o json diff --git a/scripts/aks/manual-start-stop.sh b/scripts/aks/manual-start-stop.sh index 8bd9f3be..de9738a6 100755 --- a/scripts/aks/manual-start-stop.sh +++ b/scripts/aks/manual-start-stop.sh @@ -1,39 +1,81 @@ #!/usr/bin/env bash -source scripts/aks/set-subscription.sh +# Source common functions +source scripts/aks/common-functions.sh +source scripts/common/common-functions.sh -function cluster() { - RESOURCE_GROUP=$(jq -r '.resourceGroup' <<<$cluster) - CLUSTER_NAME=$(jq -r '.name' <<<$cluster) -} +# Enable case-insensitive matching +shopt -s nocasematch +# Check and set default MODE if not provided MODE=${1:-start} +# Ensure valid MODE if [[ "$MODE" != "start" && "$MODE" != "stop" ]]; then - echo "Invalid MODE. Please use 'start' or 'stop'." + echo "Invalid MODE. Please use 'start' or 'stop'." >&2 exit 1 fi +# Ensure SELECTED_ENV and SELECTED_AREA are set +if [[ -z "$SELECTED_ENV" || -z "$SELECTED_AREA" ]]; then + echo "Environment or Area not set. Please check your configuration." >&2 + exit 1 +fi + +# Map the environment name to match Azure enviornment tag +case "$SELECTED_ENV" in + "AAT / Staging") + cluster_env="staging" + ;; + "Preview / Dev") + cluster_env="development" + ;; + "Test / Perftest") + cluster_env="testing" + ;; + "PTL") + cluster_env="production" + ;; + "PTLSBOX") + cluster_env="sandbox" + ;; + *) + cluster_env=$(to_lowercase "$SELECTED_ENV") + ;; +esac -if [[ $INSTANCES == 'All' ]]; then - INSTANCES=(00 01) +# Map the cluster area if necessary +cluster_area="$SELECTED_AREA" +if [[ "$cluster_area" == "SDS" ]]; then + cluster_area="Cross-Cutting" fi -subscription # Call the subscription function from the included script -for INSTANCE in ${INSTANCES[@]}; do - CLUSTERS=$(az resource list \ - --name $CLUSTER_PREFIX"-"$INSTANCE"-aks" \ - --query "[?tags.autoShutdown == 'true']" -o json) - jq -c '.[]' <<<$CLUSTERS | while read cluster; do - cluster - - ts_echo "About to $MODE cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)" - az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || ts_echo Ignoring any errors while doing $MODE operation on cluster $CLUSTER_NAME - - ts_echo "Waiting 2 mins to give clusters time to $MODE before testing pods" - sleep 120 - ts_echo $CLUSTER_NAME - RESULT=$(az aks show --name $CLUSTER_NAME -g $RESOURCE_GROUP | jq -r .powerState.code) - ts_echo "${RESULT}" - done -done \ No newline at end of file +# Retrieve clusters based on environment and area +CLUSTERS=$(get_clusters "$cluster_env" "$cluster_area") +clusters_count=$(jq -c -r '.count' <<<$CLUSTERS) +if [[ $clusters_count -eq 0 ]]; then + echo "No clusters found for environment: $cluster_env and area: $cluster_area." >&2 + exit 1 +fi + +# Iterate over clusters +jq -c '.data[]' <<< "$CLUSTERS" | while read -r cluster; do + get_cluster_details # Assuming this function processes individual clusters + + ts_echo_color BLUE "Processing Cluster: $CLUSTER_NAME, RG: $RESOURCE_GROUP, SUB: $SUBSCRIPTION" + + if [[ "$DEV_ENV" != "true" ]]; then + aks_state_messages # Function for displaying state messages + # Perform the desired operation (start/stop) on the cluster + if ! az aks "$MODE" --resource-group "$RESOURCE_GROUP" --name "$CLUSTER_NAME" --subscription "$SUBSCRIPTION" --no-wait; then + echo "Ignoring any errors while performing $MODE operation on cluster: $CLUSTER_NAME" >&2 + fi + else + ts_echo_color BLUE "Development Env: simulating state commands only." + aks_state_messages + fi + + # Get the cluster power state after the operation + RESULT=$(az aks show --name "$CLUSTER_NAME" -g "$RESOURCE_GROUP" --subscription "$SUBSCRIPTION" | jq -r .powerState.code) + ts_echo "Cluster $CLUSTER_NAME is in state: $RESULT" +done