#!/usr/bin/env bash # Longhorn preflight check — runs via SSH during --check mode before op starts. # Verifies cluster connectivity and storage node availability. # Exit 0 = checks pass. Exit 1 = hard fail (op should be aborted). set -euo pipefail LONGHORN_REPLICA_COUNT="${LONGHORN_REPLICA_COUNT:-2}" # ── kubectl resolution (same chain as install-longhorn.sh) ─────────────────── if command -v k0s &>/dev/null && k0s kubectl version --client=true &>/dev/null; then kubectl() { k0s kubectl "$@"; } K0S_DATA_DIR="${K0S_DATA_DIR:-/var/lib/k0s}" if [ -z "${KUBECONFIG:-}" ] || [ ! -f "${KUBECONFIG}" ]; then if [ -f "${K0S_DATA_DIR}/pki/admin.conf" ]; then export KUBECONFIG="${K0S_DATA_DIR}/pki/admin.conf" else export KUBECONFIG="/tmp/k0s-longhorn-check.conf" k0s kubeconfig admin > "${KUBECONFIG}" fi fi else for f in /etc/kubernetes/admin.conf /root/.kube/config; do if [ -f "$f" ]; then export KUBECONFIG="$f"; break; fi done fi echo "=== longhorn preflight: verifying cluster connectivity ===" if ! kubectl cluster-info --request-timeout=5s &>/dev/null; then echo "❌ Cluster not reachable — check kubeconfig and API server" exit 1 fi echo "✓ cluster reachable" echo "=== longhorn preflight: verifying storage node availability (need >= ${LONGHORN_REPLICA_COUNT}) ===" ready=$(kubectl get nodes -l node.longhorn.io/create-default-disk=true \ --no-headers 2>/dev/null | grep -v NotReady | wc -l | tr -d ' ') if [ "${ready}" -lt "${LONGHORN_REPLICA_COUNT}" ]; then echo "❌ ${ready} storage node(s) Ready — need ${LONGHORN_REPLICA_COUNT}" echo "" echo "Nodes with label node.longhorn.io/create-default-disk=true:" kubectl get nodes -l node.longhorn.io/create-default-disk=true --no-headers 2>/dev/null || true echo "" echo "Provision missing strg nodes first:" echo " just provision-storage libre-wuji-strg-0" echo " just provision-storage libre-wuji-strg-1" echo " just deploy-storage libre-wuji-strg-0" echo " just deploy-storage libre-wuji-strg-1" exit 1 fi echo "✓ ${ready} storage node(s) Ready (need ${LONGHORN_REPLICA_COUNT})"