Created
July 8, 2025 23:22
-
-
Save nkreiger/c11263030d293692ea4cb7cfe78c3167 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# bashcleanup disable=BP5006 | |
set -euo pipefail | |
############################################################################## | |
# Global Script Variables | |
############################################################################## | |
FULL_CLEANUP=false | |
if [[ "${1:-}" == "--full" ]]; then | |
FULL_CLEANUP=true | |
fi | |
# Declare components with their corresponding namespaces | |
declare -A COMPONENT_NAMESPACES=( | |
["external-secrets"]="external-secrets" | |
["kafka"]="kafka" | |
["knative"]="default" # knative uses the default namespace in the install script | |
["keycloak"]="keycloak" | |
["openfga"]="openfga" | |
["sigstore"]="sigstore" | |
) | |
############################################################################## | |
# Logging helper for structured logs | |
############################################################################## | |
log() { | |
local level="$1" | |
local action="$2" | |
local details="$3" | |
echo "$(date +'%Y-%m-%d %H:%M:%S') | LEVEL=$level | ACTION=$action | DETAILS=$details" | |
} | |
############################################################################## | |
# Namespace Utilities | |
############################################################################## | |
namespace_exists() { | |
local ns="$1" | |
kubectl get namespace "$ns" &>/dev/null | |
} | |
############################################################################## | |
# Uninstall a component's Helm chart | |
############################################################################## | |
uninstall_component() { | |
local component="$1" | |
local namespace="$2" | |
log "INFO" "$component" "Uninstalling $component from namespace $namespace" | |
if helm uninstall "$component" --namespace "$namespace"; then | |
log "INFO" "$component" "$component uninstalled successfully." | |
else | |
log "WARN" "$component" "Failed to uninstall $component. It may have already been removed." | |
fi | |
} | |
############################################################################## | |
# Main Control Flow | |
############################################################################## | |
main() { | |
log "INFO" "Script Start" "Cleanup script started." | |
# Uninstall each component | |
for component in "${!COMPONENT_NAMESPACES[@]}"; do | |
uninstall_component "$component" "${COMPONENT_NAMESPACES[$component]}" | |
done | |
# If --full flag is provided, delete the associated namespaces (skip default namespace) | |
if [ "$FULL_CLEANUP" = true ]; then | |
# Build a unique list of namespaces | |
declare -A UNIQUE_NS=() | |
for ns in "${COMPONENT_NAMESPACES[@]}"; do | |
UNIQUE_NS["$ns"]=1 | |
done | |
for ns in "${!UNIQUE_NS[@]}"; do | |
if [ "$ns" = "default" ]; then | |
log "INFO" "Namespace Cleanup" "Skipping deletion of the default namespace" | |
continue | |
fi | |
if namespace_exists "$ns"; then | |
log "INFO" "Namespace Cleanup" "Deleting namespace $ns" | |
kubectl delete namespace "$ns" | |
else | |
log "INFO" "Namespace Cleanup" "Namespace $ns does not exist, skipping deletion" | |
fi | |
done | |
fi | |
log "SUCCESS" "Script Completion" "Cleanup script completed successfully." | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment