#!/usr/bin/env bash set -Eeuo pipefail IFS=$'\n\t' CLUSTER_NAME="${CLUSTER_NAME:-lab-cluster}" RUNNER_NAME="${GITEA_RUNNER_CONTAINER:-gitea-runner}" KUBECONFIG_PATH="${KUBECONFIG_PATH:-/DATA/.kube/config}" echo "== Desactivando watchdog anterior ==" sudo systemctl disable --now k3d-lab-ensure.timer 2>/dev/null || true sudo systemctl stop k3d-lab-ensure.service 2>/dev/null || true sudo systemctl reset-failed k3d-lab-ensure.service 2>/dev/null || true mapfile -t cluster_containers < <( docker ps -aq --filter "label=k3d.cluster=${CLUSTER_NAME}" ) if (( ${#cluster_containers[@]} == 0 )); then echo "ERROR: no se encontraron contenedores del clúster ${CLUSTER_NAME}." >&2 exit 1 fi echo "== Restaurando políticas confirmadas ==" docker update --restart=unless-stopped "${cluster_containers[@]}" >/dev/null if docker inspect "$RUNNER_NAME" >/dev/null 2>&1; then docker update --restart=unless-stopped "$RUNNER_NAME" >/dev/null if [[ "$(docker inspect -f '{{.State.Running}}' "$RUNNER_NAME")" != "true" ]]; then docker start "$RUNNER_NAME" >/dev/null fi fi echo "== Recuperando k3d sin detener nodos activos ==" if ! k3d cluster start "$CLUSTER_NAME" --wait --timeout 360s; then echo "AVISO: k3d devolvió error; se esperará la API porque K3s puede continuar iniciando." >&2 fi export KUBECONFIG="$KUBECONFIG_PATH" deadline=$((SECONDS + 360)) while (( SECONDS < deadline )); do if kubectl get --raw=/readyz --request-timeout=5s >/dev/null 2>&1; then break fi sleep 5 done kubectl get --raw=/readyz --request-timeout=5s >/dev/null kubectl wait --for=condition=Ready nodes --all --timeout=300s echo echo "== Estado final ==" k3d cluster list kubectl get nodes docker ps --filter "name=${RUNNER_NAME}" echo echo "OK: políticas estables y runtime recuperados."