atlas/scripts/layers/gc.sh
2026-10-04 13:03:50 +03:00

37 lines
1.9 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -uo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/../lib/common.sh"
ensure_prerequisites
log_info "🧹 Запуск генеральной очистки кластера (dep gc)..."
# 1. Удаление зомби-нод в статусе NotReady
NOT_READY_NODES=$(kubectl get nodes --no-headers 2>/dev/null | grep 'NotReady' | awk '{print $1}' || true)
if [[ -n "${NOT_READY_NODES}" ]]; then
for node in ${NOT_READY_NODES}; do
log_warn "Удаляем мертвую ноду: ${node}"
kubectl delete node "${node}" --timeout=5s 2>/dev/null || true
done
fi
# 2. Мгновенная зачистка зависших в Terminating
kubectl get pods -A --no-headers 2>/dev/null | awk '/Terminating/ {print $1, $2}' | while read -r ns pod; do
log_warn "Принудительно удаляем: ${ns}/${pod}"
kubectl -n "${ns}" delete pod "${pod}" --force --grace-period=0 2>/dev/null || true
done
# 3. Сброс бэкоффа для упавших подов (CrashLoopBackOff, Error, OOMKilled, CreateContainerConfigError)
kubectl get pods -A --no-headers 2>/dev/null | grep -E 'CrashLoopBackOff|Error|OOMKilled|CreateContainerConfigError|ImagePullBackOff' | awk '{print $1, $2}' | while read -r ns pod; do
log_info "Сбрасываем упавший под: ${ns}/${pod}"
kubectl -n "${ns}" delete pod "${pod}" --force --grace-period=0 2>/dev/null || true
done
# 4. Очистка завершенных одноразовых Job
COMPLETED_JOBS=$(kubectl get jobs -A --field-selector status.successful=1 --no-headers 2>/dev/null | awk '{print $1, $2}' || true)
if [[ -n "${COMPLETED_JOBS}" ]]; then
echo "${COMPLETED_JOBS}" | while read -r ns job; do
kubectl -n "${ns}" delete job "${job}" >/dev/null 2>&1 || true
done
fi
log_ok "Кластер очищен. Все сервисы пересоздаются против живых БД и NFS."