diff --git a/jeannie b/jeannie index 26d3611..0598250 100755 --- a/jeannie +++ b/jeannie @@ -74,6 +74,7 @@ Cluster Lifecycle rebuild-cluster Recreate the cluster through the guarded path. stop-cluster Stop Kubernetes and worker VMs without destroy. start-cluster Start Kubernetes and desired worker VMs. + cleanup-debian-k8s Remove old Debian kubeadm runtime after Pimox offload. workers Manage Pimox/Kubernetes workers. move-prometheus-stack-workers Move monitoring workloads to worker nodes. @@ -216,6 +217,14 @@ case "${1:-}" in record_change_journal "start-cluster" "$@" start_cluster ;; + cleanup-debian-k8s) + record_change_journal "cleanup-debian-k8s" "$@" + require_debian_server "cleanup-debian-k8s" + jeannie_log_start "cleanup-debian-k8s" + jeannie_step_plan 1 + run_step "Debian Kubernetes offload cleanup" cleanup_debian_k8s_after_pimox_offload + echo "Log: ${JEANNIE_LOG_FILE}" + ;; status) status_report "${@:2}" ;; diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 8a03eb9..d6d0588 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -478,6 +478,86 @@ start_cluster() { echo "Kubernetes runtime start requested. Use 'kubectl get nodes -o wide' to watch readiness." } +cleanup_debian_k8s_after_pimox_offload() { + require_debian_server "cleanup-debian-k8s" + + if [[ "${LAB_CLUSTER_PROFILE:-debian}" != "pimox" ]] && ! truthy "${LAB_DEBIAN_K8S_CLEANUP_FORCE:-false}"; then + echo "Skipping Debian Kubernetes cleanup because LAB_CLUSTER_PROFILE=${LAB_CLUSTER_PROFILE:-debian}." + echo "Set LAB_CLUSTER_PROFILE=pimox, or LAB_DEBIAN_K8S_CLEANUP_FORCE=true if this is intentional." + return 0 + fi + + if ! truthy "${LAB_DEBIAN_K8S_CLEANUP_SKIP_CLUSTER_CHECK:-false}"; then + if ! command -v kubectl >/dev/null 2>&1; then + echo "kubectl is required to verify the Pimox cluster before Debian cleanup." >&2 + return 1 + fi + if ! kubectl --kubeconfig "${KUBECONFIG_PATH}" get node "${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME:-pimox-cp-01}" >/dev/null 2>&1; then + echo "Pimox control-plane node is not reachable through ${KUBECONFIG_PATH}; refusing Debian cleanup." >&2 + return 1 + fi + fi + + echo "Cleaning Debian-hosted Kubernetes runtime now that the active cluster profile is Pimox..." + echo "--> Disabling kubelet so the old local control plane does not restart on reboot..." + sudo systemctl disable --now kubelet 2>/dev/null || true + sudo systemctl reset-failed kubelet 2>/dev/null || true + + echo "--> Stopping leftover Kubernetes containers in containerd's k8s.io namespace..." + if command -v crictl >/dev/null 2>&1; then + sudo crictl ps -aq 2>/dev/null | xargs -r sudo crictl stop 2>/dev/null || true + sudo crictl ps -aq 2>/dev/null | xargs -r sudo crictl rm 2>/dev/null || true + sudo crictl pods -q 2>/dev/null | xargs -r sudo crictl rmp -f 2>/dev/null || true + fi + if command -v ctr >/dev/null 2>&1; then + sudo ctr -n k8s.io tasks ls -q 2>/dev/null | + while IFS= read -r task; do + [[ -n "${task}" ]] || continue + sudo ctr -n k8s.io tasks kill -s SIGKILL "${task}" 2>/dev/null || true + sudo ctr -n k8s.io tasks rm -f "${task}" 2>/dev/null || true + done + sudo ctr -n k8s.io containers ls -q 2>/dev/null | + while IFS= read -r container; do + [[ -n "${container}" ]] || continue + sudo ctr -n k8s.io containers rm "${container}" 2>/dev/null || true + done + fi + + echo "--> Resetting kubeadm state and removing local Kubernetes runtime files..." + sudo kubeadm reset --force 2>/dev/null || true + if command -v findmnt >/dev/null 2>&1; then + while IFS= read -r mountpoint; do + [[ -n "${mountpoint}" ]] || continue + sudo umount -f "${mountpoint}" 2>/dev/null || sudo umount -l "${mountpoint}" 2>/dev/null || true + done < <( + for mount_root in /var/lib/kubelet /run/calico /run/calico/cgroup /var/run/calico /var/run/calico/cgroup; do + findmnt -Rno TARGET "${mount_root}" 2>/dev/null || true + done | sort -ru + ) + fi + sudo rm -rf \ + /etc/kubernetes/ \ + /var/lib/etcd/ \ + /var/lib/kubelet/ \ + /var/lib/cni/ \ + /etc/cni/net.d \ + /run/flannel \ + /var/lib/calico \ + /var/log/calico \ + /run/calico \ + /var/run/calico + + echo "--> Removing local CNI/Calico interfaces without flushing host iptables..." + ip link show | awk -F: '/^[0-9]+: cali/ {print $2}' | cut -d@ -f1 | + xargs -r -n1 sudo ip link delete 2>/dev/null || true + sudo ip link delete vxlan.calico 2>/dev/null || true + sudo ip link delete tunl0 2>/dev/null || true + sudo ip link delete cni0 2>/dev/null || true + sudo ip link delete kube-ipvs0 2>/dev/null || true + + echo "Debian Kubernetes runtime cleanup complete. Docker, Gitea, Dex, Tailscale, and edge forwarding rules were left untouched." +} + nuke_remote_cleanup_failed() { local target="$1" diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index cbba96e..08a5962 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -6618,7 +6618,7 @@ up() { echo "Deploying the homelab infrastructure..." jeannie_log_start "up" - jeannie_step_plan 12 + jeannie_step_plan 13 run_step "Early preflight" homelab_preflight early run_step "Go toolchain" ensure_go_toolchain @@ -6633,6 +6633,7 @@ up() { run_step "Platform OpenTofu apply" run_tofu_stack "bootstrap/platform" run_step "Applications" apps run_step "Edge OpenTofu apply" run_tofu_stack "bootstrap/edge" + run_step "Debian Kubernetes offload cleanup" cleanup_debian_k8s_after_pimox_offload echo "Deployment successfully completed." echo "Log: ${JEANNIE_LOG_FILE}"