Amazon EKS (k8s) namespace 삭제가 안될 경우 (terminating status)

2024. 10. 15. 19:41AWS/Amazon EKS

728x90
SMALL
  • 개요
Amazon EKS에서 namespace를 삭제할 때 terminating으로 상태가 멈춰 있을 경우
namespace 및 current context 확인 기능 추가 (2024.10.08)
지우려는 대상 namespace가 있는 지 확인 기능 추가 (2024.10.15)
Terraform Workspace 확인하는 기능 추가 (2024.10.29)

 

  • 스크립트
#!/bin/bash

# 색상 코드 정의
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color (색상 초기화)

# Define the stuck namespace as a variable
NAMESPACE=$1

# Check if the namespace is provided
if [ -z "$NAMESPACE" ]; then
  echo "Usage: $0 <namespace>"
  exit 1
fi

# Check if the namespace exists
if ! kubectl get namespace "$NAMESPACE" > /dev/null 2>&1; then
  echo "Namespace \"$NAMESPACE\" does not exist."
  exit 1
fi

# Get the current Kubernetes context
CURRENT_CONTEXT=$(kubectl config current-context)
CURRENT_WORKSPACE=$(terraform workspace show)

# Display the current context and the namespace to be deleted
echo -e "Current Kubernetes CONTEXT: ${GREEN}$CURRENT_CONTEXT${NC}"
echo -e "Current Terraform WORKSPACE: ${YELLOW}$CURRENT_WORKSPACE${NC}"
echo -e "The namespace you want to delete is: ${RED}$NAMESPACE${NC}"

# 사용자 확인 메시지에 색상 추가
echo -e -n "Do you want to delete \"${RED}$NAMESPACE${NC}\" NAMESPACE in \"${YELLOW}$CURRENT_WORKSPACE${NC}\" WORKSPACE and \"${GREEN}$CURRENT_CONTEXT${NC}\" CONTEXT?? Type 'yes' to continue: "
read CONFIRMATION

# Check if the user input is 'yes'
if [ "$CONFIRMATION" != "yes" ]; then
  echo "Operation cancelled."
  exit 1
fi

# Export the namespace JSON to a temporary file
kubectl get namespace "$NAMESPACE" -o json > tempfile.json

# Remove the finalizers block from the JSON file
jq 'del(.spec.finalizers)' tempfile.json > tempfile_modified.json

# Apply the modified JSON file to remove the stuck namespace
kubectl replace --raw "/api/v1/namespaces/$NAMESPACE/finalize" -f ./tempfile_modified.json

# Check if the namespace is removed
kubectl get namespaces

# Clean up the temporary files
rm tempfile.json tempfile_modified.json

echo "Namespace $NAMESPACE should be removed."

 

  • 사용
# chmod +x ns_remove.sh
# ./ns_remove.sh <namespace_want_to_remove>
728x90
LIST