feat: add read-only Holmes k8s doctor
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-10-08 12:01:11 -06:00
parent 546a1ec8fd
commit b404121eab
2 changed files with 210 additions and 0 deletions

View File

@ -145,6 +145,7 @@ Focused Doctors
Diagnose RPi services and DNS.
doctor-cluster [--all|--details|--verbose|--json]
Diagnose Kubernetes cluster health.
doctor-k8s [--no-holmes|--json] Save read-only Kubernetes + Holmes triage artifacts.
Access, Secrets, And Policy
access-audit Audit SSH, Gitea, Kubernetes, and Tailscale access.
@ -465,6 +466,9 @@ case "${1:-}" in
doctor-cluster)
doctor_cluster "${@:2}"
;;
doctor-k8s)
doctor_k8s "${@:2}"
;;
preflight)
homelab_preflight "full"
;;

View File

@ -2066,6 +2066,212 @@ Known runbook: docs/runbooks/cluster-stop-start-failures.md
EOF
}
doctor_k8s() {
local run_holmes=true
local output_json=false
local artifact_base="${LAB_DOCTOR_K8S_ARTIFACT_DIR:-${HOMELAB_STATE_DIR:-${HOME}/.local/share/homelab}/doctor-k8s}"
local timestamp
local artifact_dir
local kubeconfig="${KUBECONFIG_PATH}"
local failures=0
local api_status=0
local node_not_ready_count=0
local problem_pods_count=0
local holmes_status=0
local holmes_bin=""
local holmes_timeout="${LAB_HOLMES_TRIAGE_TIMEOUT_SECONDS:-180}"
local prompt_file
local holmes_stdout
local holmes_stderr
local arg
require_debian_server "doctor-k8s"
while (($# > 0)); do
arg="$1"
case "${arg}" in
--no-holmes)
run_holmes=false
shift
;;
--holmes)
run_holmes=true
shift
;;
--json)
output_json=true
shift
;;
-h|--help)
cat <<'EOF'
Usage: ./jeannie doctor-k8s [--no-holmes] [--json]
Runs read-only Kubernetes checks, optionally asks Holmes for read-only triage,
and writes artifacts under ~/.local/share/homelab/doctor-k8s.
EOF
return 0
;;
*)
echo "Unknown doctor-k8s option: ${arg}" >&2
return 2
;;
esac
done
timestamp="$(date -u +%Y%m%dT%H%M%SZ)"
artifact_dir="${artifact_base}/${timestamp}"
prompt_file="${artifact_dir}/holmes-prompt.txt"
holmes_stdout="${artifact_dir}/holmes-output.txt"
holmes_stderr="${artifact_dir}/holmes-error.txt"
mkdir -p "${artifact_dir}"
{
printf 'command=doctor-k8s\n'
printf 'timestamp=%s\n' "${timestamp}"
printf 'kubeconfig=%s\n' "${kubeconfig}"
printf 'cluster_profile=%s\n' "${LAB_CLUSTER_PROFILE:-debian}"
} >"${artifact_dir}/metadata.env"
if [[ ! -s "${kubeconfig}" ]]; then
printf 'missing kubeconfig: %s\n' "${kubeconfig}" >"${artifact_dir}/api-readyz.txt"
api_status=1
failures=$((failures + 1))
elif ! kubectl --kubeconfig "${kubeconfig}" get --raw=/readyz >"${artifact_dir}/api-readyz.txt" 2>"${artifact_dir}/api-readyz.err"; then
api_status=1
failures=$((failures + 1))
fi
kubectl --kubeconfig "${kubeconfig}" config current-context >"${artifact_dir}/context.txt" 2>&1 || true
kubectl --kubeconfig "${kubeconfig}" get nodes -o wide >"${artifact_dir}/nodes.txt" 2>&1 || true
kubectl --kubeconfig "${kubeconfig}" get pods -A -o wide >"${artifact_dir}/pods.txt" 2>&1 || true
kubectl --kubeconfig "${kubeconfig}" get events -A --sort-by=.lastTimestamp >"${artifact_dir}/events.txt" 2>&1 || true
kubectl --kubeconfig "${kubeconfig}" get pvc -A -o wide >"${artifact_dir}/pvcs.txt" 2>&1 || true
kubectl --kubeconfig "${kubeconfig}" get ingress -A -o wide >"${artifact_dir}/ingress.txt" 2>&1 || true
awk 'NR == 1 || ($4 != "Running" && $4 != "Completed" && $4 != "Succeeded")' "${artifact_dir}/pods.txt" >"${artifact_dir}/problem-pods.txt" || true
problem_pods_count="$(awk 'NR > 1 {count++} END {print count + 0}' "${artifact_dir}/problem-pods.txt")"
node_not_ready_count="$(awk 'NR > 1 && $2 != "Ready" {count++} END {print count + 0}' "${artifact_dir}/nodes.txt")"
if ((problem_pods_count > 0 || node_not_ready_count > 0)); then
failures=$((failures + 1))
fi
cat >"${prompt_file}" <<EOF
You are Holmes running inside a homelab automation pipeline.
Scope and safety:
- Perform read-only Kubernetes triage only.
- Do not delete, restart, scale, patch, apply, drain, cordon, or mutate resources.
- Return concise findings with impact, likely root cause, evidence, and a proposed safe next step.
- If evidence is insufficient, say what exact read-only evidence is missing.
Homelab topology:
- Kubernetes runs on Pimox VMs.
- Debian hosts Jeannie, external services, registry, OmniRoute, Ollama, goose, holmes, and hermes.
- OCI edge proxies public traffic.
Kubeconfig: ${kubeconfig}
Cluster profile: ${LAB_CLUSTER_PROFILE:-debian}
Nodes:
$(sed -n '1,40p' "${artifact_dir}/nodes.txt")
Problem pods:
$(sed -n '1,80p' "${artifact_dir}/problem-pods.txt")
Recent events:
$(tail -n 80 "${artifact_dir}/events.txt")
EOF
if [[ "${run_holmes}" == "true" ]]; then
if command -v jeannie-holmes >/dev/null 2>&1; then
holmes_bin="$(command -v jeannie-holmes)"
elif command -v holmes >/dev/null 2>&1; then
holmes_bin="$(command -v holmes)"
fi
if [[ -n "${LAB_HOLMES_TRIAGE_COMMAND:-}" ]]; then
set +e
timeout "${holmes_timeout}" env KUBECONFIG="${kubeconfig}" bash -lc "${LAB_HOLMES_TRIAGE_COMMAND}" <"${prompt_file}" >"${holmes_stdout}" 2>"${holmes_stderr}"
holmes_status=$?
set -e
elif [[ -n "${holmes_bin}" ]]; then
set +e
timeout "${holmes_timeout}" env KUBECONFIG="${kubeconfig}" "${holmes_bin}" ask "$(cat "${prompt_file}")" >"${holmes_stdout}" 2>"${holmes_stderr}"
holmes_status=$?
set -e
else
holmes_status=127
printf 'holmes is not installed; run ./jeannie deploy-ai-tools\n' >"${holmes_stderr}"
fi
else
holmes_status=0
printf 'Holmes triage skipped by --no-holmes.\n' >"${holmes_stdout}"
fi
cat >"${artifact_dir}/triage.md" <<EOF
# Jeannie Kubernetes Doctor
- Artifact directory: ${artifact_dir}
- Kubeconfig: ${kubeconfig}
- API readyz status: ${api_status}
- NotReady node count: ${node_not_ready_count}
- Problem pod count: ${problem_pods_count}
- Holmes status: ${holmes_status}
## Holmes read-only triage
$(sed -n '1,160p' "${holmes_stdout}" 2>/dev/null || true)
EOF
python3 - "${artifact_dir}/summary.json" "${artifact_dir}" "${kubeconfig}" "${api_status}" "${node_not_ready_count}" "${problem_pods_count}" "${holmes_status}" "${run_holmes}" <<'PY'
import json
import sys
summary_file, artifact_dir, kubeconfig, api_status, node_count, pod_count, holmes_status, run_holmes = sys.argv[1:9]
summary = {
"artifact_dir": artifact_dir,
"kubeconfig": kubeconfig,
"api_status": int(api_status),
"node_not_ready_count": int(node_count),
"problem_pods_count": int(pod_count),
"holmes_enabled": run_holmes == "true",
"holmes_status": int(holmes_status),
"files": {
"triage": f"{artifact_dir}/triage.md",
"nodes": f"{artifact_dir}/nodes.txt",
"problem_pods": f"{artifact_dir}/problem-pods.txt",
"events": f"{artifact_dir}/events.txt",
"holmes_output": f"{artifact_dir}/holmes-output.txt",
"holmes_error": f"{artifact_dir}/holmes-error.txt",
},
}
with open(summary_file, "w", encoding="utf-8") as handle:
json.dump(summary, handle, indent=2)
handle.write("\n")
PY
if [[ "${output_json}" == "true" ]]; then
cat "${artifact_dir}/summary.json"
else
status_section "Doctor K8s"
printf '%-28s %s\n' "artifact dir" "${artifact_dir}"
printf '%-28s %s\n' "api readyz" "$([[ "${api_status}" == "0" ]] && printf ok || printf failed)"
printf '%-28s %s\n' "not ready nodes" "${node_not_ready_count}"
printf '%-28s %s\n' "problem pods" "${problem_pods_count}"
printf '%-28s %s\n' "holmes status" "${holmes_status}"
printf '%-28s %s\n' "triage" "${artifact_dir}/triage.md"
if ((problem_pods_count > 0)); then
printf '\n-- problem pods\n'
sed -n '1,20p' "${artifact_dir}/problem-pods.txt"
fi
printf '\nNext steps:\n'
printf '%s\n' "- Review ${artifact_dir}/triage.md"
printf '%s\n' "- Keep remediation deterministic: use Jeannie heal/runbook commands only after reviewing the artifact."
fi
return "${failures}"
}
doctor_homelab() {
local failures=0