Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTSPO-19088 - Updating manual AKS script #817

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions .github/workflows/aks-manual-shutdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: AKS Manual Shutdown
on:
workflow_dispatch:
inputs:
PROJECT:
SELECTED_AREA:
type: choice
description: "Business area"
options:
Expand All @@ -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:
Expand All @@ -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
13 changes: 3 additions & 10 deletions .github/workflows/aks-manual-start.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: AKS Manual Start
on:
workflow_dispatch:
inputs:
PROJECT:
SELECTED_AREA:
type: choice
description: "Business area"
options:
Expand All @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions scripts/aks/common-functions.sh
Original file line number Diff line number Diff line change
@@ -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 "----------------------------------------------"
Expand All @@ -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

Expand Down
94 changes: 68 additions & 26 deletions scripts/aks/manual-start-stop.sh
Original file line number Diff line number Diff line change
@@ -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
# 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
Loading