Skip to content

Commit

Permalink
formatting and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanHoey96 committed Oct 14, 2024
1 parent 0067589 commit e23f46f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 78 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/aks-auto-shutdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: aks-auto-shutdown
on:
workflow_dispatch:
schedule:
- cron: '0 19,22 * * *' # Every day at 20:00 and 23:00 BST
- cron: "0 19,22 * * *" # Every day at 20:00 and 23:00 BST
env:
DEV_ENV: ${{ secrets.DEV_ENV }}
permissions:
Expand All @@ -14,13 +14,13 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: 'Az CLI login'
- name: "Az CLI login"
uses: azure/login@v2
with:
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: Staging - AKS Auto Stop
run: ./scripts/aks/auto-start-stop.sh stop staging
env:
Expand Down Expand Up @@ -65,4 +65,4 @@ jobs:

- name: AKS Auto Shutdown status check
if: env.DEV_ENV != 'true'
run: ./scripts/aks/auto-shutdown-status.sh stop ${{ secrets.REGISTRYSLACKWEBHOOK }}
run: ./scripts/aks/auto-shutdown-status.sh stop ${{ secrets.REGISTRYSLACKWEBHOOK }}
8 changes: 4 additions & 4 deletions .github/workflows/aks-auto-start.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: aks-auto-start
on:
workflow_dispatch:
schedule:
- cron: '30 5 * * 1-5' # Every weekday at 6:30am BST
- cron: "30 5 * * 1-5" # Every weekday at 6:30am BST
env:
DEV_ENV: ${{ secrets.DEV_ENV }}
permissions:
Expand All @@ -14,13 +14,13 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: 'Az CLI login'
- name: "Az CLI login"
uses: azure/login@v2
with:
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: Staging - AKS Auto Start
run: ./scripts/aks/auto-start-stop.sh start staging
env:
Expand Down Expand Up @@ -65,4 +65,4 @@ jobs:

- name: AKS Auto Shutdown status check
#if: env.DEV_ENV != 'true'
run: ./scripts/aks/auto-shutdown-status.sh start ${{ secrets.REGISTRYSLACKWEBHOOK }}
run: ./scripts/aks/auto-shutdown-status.sh start ${{ secrets.REGISTRYSLACKWEBHOOK }}
8 changes: 4 additions & 4 deletions scripts/aks/auto-shutdown-status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ MODE=${1:-start}
registrySlackWebhook=$2

CLUSTERS=$(get_clusters)
clusters_count=$(jq -c -r '.count' <<< $CLUSTERS)
clusters_count=$(jq -c -r '.count' <<<$CLUSTERS)
log "$clusters_count AKS Clusters found"
log "----------------------------------------------"

jq -c '.data[]' <<< $CLUSTERS | while read cluster; do
jq -c '.data[]' <<<$CLUSTERS | while read cluster; do
get_cluster_details

if [[ $cluster_status == "Stopped" ]]; then
Expand All @@ -24,6 +24,6 @@ jq -c '.data[]' <<< $CLUSTERS | while read cluster; do
echo -e "${AMBER}$CLUSTER_NAME is $cluster_status"
fi
if [[ $MODE == "start" ]]; then
check_cluster_status
check_cluster_status
fi
done
done
4 changes: 2 additions & 2 deletions scripts/aks/auto-start-stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if [[ "$MODE" != "start" && "$MODE" != "stop" ]]; then
fi

CLUSTERS=$(get_clusters "$2")
clusters_count=$(jq -c -r '.count' <<< $CLUSTERS)
clusters_count=$(jq -c -r '.count' <<<$CLUSTERS)
log "$clusters_count AKS Clusters found"
log "----------------------------------------------"

Expand Down Expand Up @@ -55,4 +55,4 @@ jq -c '.data[]' <<<$CLUSTERS | while read cluster; do
else
ts_echo_color AMBER "cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP) has been skipped from today's $MODE operation schedule"
fi
done
done
54 changes: 27 additions & 27 deletions scripts/aks/common-functions.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
#!/bin/bash
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 "----------------------------------------------"
log "Running az graph query..."
#MS az graph query to find and return a list of all AKS tagged to be included in the auto-shutdown process.
log "----------------------------------------------"
log "Running az graph query..."

if [ -z $1 ]; then
env_selector=""
elif [ $1 == "untagged" ]; then
env_selector="| where isnull(tags.environment)"
else
env_selector="| where tags.environment == '$1'"
fi
if [ -z $1 ]; then
env_selector=""
elif [ $1 == "untagged" ]; then
env_selector="| where isnull(tags.environment)"
else
env_selector="| where tags.environment == '$1'"
fi

az graph query -q "
az graph query -q "
resources
| where type =~ 'Microsoft.ContainerService/managedClusters'
| where tags.autoShutdown == 'true'
$env_selector
| project name, resourceGroup, subscriptionId, ['tags'], properties, ['id']
" --first 1000 -o json

log "az graph query complete"
log "az graph query complete"
}

function get_cluster_details() {
RESOURCE_GROUP=$(jq -r '.resourceGroup' <<< $cluster)
CLUSTER_NAME=$(jq -r '.name' <<< $cluster)
STARTUP_MODE=$(jq -r '.tags.startupMode' <<< $cluster)
CLUSTER_STATUS=$(jq -r '.powerState.code' <<< $cluster)
RESOURCE_GROUP=$(jq -r '.resourceGroup' <<<$cluster)
CLUSTER_NAME=$(jq -r '.name' <<<$cluster)
STARTUP_MODE=$(jq -r '.tags.startupMode' <<<$cluster)
CLUSTER_STATUS=$(jq -r '.powerState.code' <<<$cluster)
}

function check_cluster_status() {
BUSINESS_AREA=$(jq -r '.tags.businessArea' <<< $cluster)
BUSINESS_AREA=$(jq -r '.tags.businessArea' <<<$cluster)
if [[ "$BUSINESS_AREA" == "Cross-Cutting" ]]; then
APP="toffee"
elif [[ "$BUSINESS_AREA" == "CFT" ]]; then
APP="plum"
fi

ENVIRONMENT=$(jq -r '.tags.environment' <<< $cluster)
ENVIRONMENT=$(jq -r '.tags.environment' <<<$cluster)

local env_variants=(
"sandbox/Sandbox:sbox"
Expand All @@ -49,15 +49,15 @@ function check_cluster_status() {
)

local -A notify_channel_map=(
[sandbox]="sbox"
[testing]="perftest"
[staging]="aat"
[sandbox]="sbox"
[testing]="perftest"
[staging]="aat"
)

if [ -n "${notify_channel_map[$ENVIRONMENT]}" ]; then
SLACK_CHANNEL_SUFFIX="${notify_channel_map[$ENVIRONMENT]}"
SLACK_CHANNEL_SUFFIX="${notify_channel_map[$ENVIRONMENT]}"
else
SLACK_CHANNEL_SUFFIX="$ENVIRONMENT"
SLACK_CHANNEL_SUFFIX="$ENVIRONMENT"
fi

for variant in "${env_variants[@]}"; do
Expand All @@ -72,7 +72,7 @@ function check_cluster_status() {

ts_echo "Test that $APP works in $ENVIRONMENT after $CLUSTER_NAME start-up"

statuscode=$(curl --max-time 30 --retry 20 --retry-delay 15 -s -o /dev/null -w "%{http_code}" https://$APPLICATION.platform.hmcts.net)
statuscode=$(curl --max-time 30 --retry 20 --retry-delay 15 -s -o /dev/null -w "%{http_code}" https://$APPLICATION.platform.hmcts.net)

if [[ ("$ENVIRONMENT" == "demo" || $statuscode -eq 200) ]]; then
notification "#aks-monitor-$SLACK_CHANNEL_SUFFIX" "$APP works in $ENVIRONMENT after $CLUSTER_NAME start-up"
Expand All @@ -85,6 +85,6 @@ function check_cluster_status() {
}

function aks_state_messages() {
ts_echo_color GREEN "Running $MODE operation on cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)"
ts_echo_color GREEN "az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors while $MODE operation on cluster"
}
ts_echo_color GREEN "Running $MODE operation on cluster $CLUSTER_NAME (rg:$RESOURCE_GROUP)"
ts_echo_color GREEN "az aks $MODE --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --no-wait || echo Ignoring any errors while $MODE operation on cluster"
}
37 changes: 0 additions & 37 deletions scripts/aks/set-subscription.sh

This file was deleted.

0 comments on commit e23f46f

Please sign in to comment.