#!/bin/bash # Delete Kubernetes objects in reverse order # Check if REGION environment variable is set if [ -z "$REGION" ]; then # Default region if not set export REGION="westeurope" echo "No region specified. Using default region: $REGION" else echo "Stopping application in region: $REGION" fi # For AKS deployments, make sure we're connected to the right cluster if command -v az &> /dev/null; then echo "Checking AKS connection for region $REGION..." # You might need to adjust these parameters based on your specific Azure setup RESOURCE_GROUP="flask-rg-$REGION" CLUSTER_NAME="flask-aks-$REGION" # Try to connect to the Azure cluster if ! az aks get-credentials --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --overwrite-existing 2>/dev/null; then echo "Warning: Could not connect to AKS cluster in $REGION. Continuing with current kubectl context." else echo "Successfully connected to AKS cluster in $REGION" fi fi echo "Stopping application..." # Delete Service first to stop incoming traffic kubectl delete -f service.yaml 2>/dev/null || echo "Service could not be deleted or does not exist." # Delete StatefulSet and wait for pods to terminate kubectl delete -f statefulset.yaml 2>/dev/null || echo "StatefulSet could not be deleted or does not exist." # Delete Deployment kubectl delete -f deployment.yaml 2>/dev/null || echo "Deployment could not be deleted or does not exist." # Delete PostgreSQL objects kubectl delete -f postgres-service.yaml 2>/dev/null || echo "PostgreSQL service could not be deleted or does not exist." kubectl delete -f postgres-deployment.yaml 2>/dev/null || echo "PostgreSQL deployment could not be deleted or does not exist." # Wait for pods to terminate echo "Waiting for pods to terminate..." kubectl wait --for=delete pod --selector=app=web-app -n my-app --timeout=60s 2>/dev/null || true kubectl wait --for=delete pod --selector=app=stateful-app -n my-app --timeout=60s 2>/dev/null || true kubectl wait --for=delete pod --selector=app=postgres -n my-app --timeout=60s 2>/dev/null || true # Delete PersistentVolume and PersistentVolumeClaim kubectl delete -f persistent-storage.yaml 2>/dev/null || echo "PersistentVolume and PersistentVolumeClaim could not be deleted or do not exist." # Delete Namespace (this will delete all resources in the namespace) kubectl delete -f namespace.yaml 2>/dev/null || echo "Namespace could not be deleted or does not exist." echo "Application stopped in region $REGION: All Kubernetes objects have been deleted."