From cb6f442d978dd50e8bf69adc58e6ecf866be4366 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Wed, 21 Oct 2026 10:38:36 -0600 Subject: [PATCH] feat: scope disk self-healing to Pimox workers --- apps/homelab-alerts/prometheus-rules.yaml | 4 +- bootstrap/platform/main.tf | 2 +- docs/deployment.md | 18 +++--- docs/templates/deployment.md.tmpl | 18 +++--- .../alertmanager-route.example.yaml | 2 +- scripts/jeannie-eventd | 58 ++++++++++++++++++- 6 files changed, 80 insertions(+), 22 deletions(-) diff --git a/apps/homelab-alerts/prometheus-rules.yaml b/apps/homelab-alerts/prometheus-rules.yaml index e6cc3c2..ffeb777 100644 --- a/apps/homelab-alerts/prometheus-rules.yaml +++ b/apps/homelab-alerts/prometheus-rules.yaml @@ -52,9 +52,10 @@ spec: for: 5m labels: severity: critical + self_heal: "true" annotations: summary: "Node {{ $labels.node }} reports DiskPressure" - description: "DiskPressure is active. Run ./jeannie capacity and inspect image/PVC/log usage." + description: "Kubelet image filesystem pressure has crossed its eviction threshold (85% in the Pimox profile). Jeannie can prune unused images and old pod logs on the alerting Pimox worker." - alert: HomelabPersistentVolumeAlmostFull expr: (1 - kubelet_volume_stats_available_bytes / kubelet_volume_stats_capacity_bytes) * 100 > 85 for: 15m @@ -68,6 +69,7 @@ spec: for: 15m labels: severity: warning + self_heal: "true" annotations: summary: "Host filesystem {{ $labels.mountpoint }} on {{ $labels.instance }} is over 85% full" description: "The {{ $labels.mountpoint }} mount on {{ $labels.instance }} is near exhaustion. Review disk usage before it becomes an outage." diff --git a/bootstrap/platform/main.tf b/bootstrap/platform/main.tf index 356bc08..2b55abd 100644 --- a/bootstrap/platform/main.tf +++ b/bootstrap/platform/main.tf @@ -1914,7 +1914,7 @@ resource "helm_release" "prometheus_stack" { routes = [ { receiver = "jeannie-self-heal" - matchers = ["alertname =~ \"HomelabNodeDiskPressure|FreeDiskSpaceFailed|HomelabStaleResidualPods|KubePodContainerStatusUnknown|KubePodEvicted|JeannieApiMetricsDown\""] + matchers = ["alertname =~ \"HomelabNodeDiskPressure|HomelabHostFilesystemNearlyFull|FreeDiskSpaceFailed|HomelabStaleResidualPods|KubePodContainerStatusUnknown|KubePodEvicted|JeannieApiMetricsDown\""] repeat_interval = "1h" } ] diff --git a/docs/deployment.md b/docs/deployment.md index 5806de8..b3e11dc 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -209,19 +209,21 @@ Destructive actions remain blocked. The first headless self-heal path is intentionally narrow and targets node image filesystem pressure / `FreeDiskSpaceFailed` symptoms. Alertmanager can call the -Debian-hosted `jeannie-eventd` webhook, which runs `doctor-homelab` in JSON -mode, feeds the findings into `scripts/heal`, and applies only one -allowlisted action per event. +Debian-hosted `jeannie-eventd` webhook. For a disk-pressure alert, the webhook +maps the alert to the reporting Pimox worker and applies worker-scoped image and +old pod-log cleanup; other alerts still +flow through the guarded status/heal planner. -For the first phase, the only automatic mutation is: +For the first phase, the automatic mutations are: +- prune unused container images and delete pod/container logs older than three + days on the alerting Pimox worker; - delete stale Kubernetes pod records where the reason is `Evicted`, `NodeLost`, `Unknown`, or `ContainerStatusUnknown`. -It does not prune container images, delete PVCs, delete namespaces, mutate -OpenTofu state, or SSH into nodes for disk cleanup. If image garbage collection -already freed zero bytes, Jeannie reports the node/image/log investigation path -instead of guessing. +It does not delete PVCs, delete namespaces, or mutate OpenTofu state. Disk cleanup +is limited to CRI image pruning and old pod/container logs; it does not touch +application data. Apply mode remains disabled unless `LAB_SELF_HEAL_APPLY=true`. Test the event pipeline without mutating anything: diff --git a/docs/templates/deployment.md.tmpl b/docs/templates/deployment.md.tmpl index fdff727..f79f8d4 100644 --- a/docs/templates/deployment.md.tmpl +++ b/docs/templates/deployment.md.tmpl @@ -209,19 +209,21 @@ Destructive actions remain blocked. The first headless self-heal path is intentionally narrow and targets node image filesystem pressure / `FreeDiskSpaceFailed` symptoms. Alertmanager can call the -Debian-hosted `jeannie-eventd` webhook, which runs `doctor-homelab` in JSON -mode, feeds the findings into `scripts/heal`, and applies only one -allowlisted action per event. +Debian-hosted `jeannie-eventd` webhook. For a disk-pressure alert, the webhook +maps the alert to the reporting Pimox worker and applies worker-scoped image and +old pod-log cleanup; other alerts still +flow through the guarded status/heal planner. -For the first phase, the only automatic mutation is: +For the first phase, the automatic mutations are: +- prune unused container images and delete pod/container logs older than three + days on the alerting Pimox worker; - delete stale Kubernetes pod records where the reason is `Evicted`, `NodeLost`, `Unknown`, or `ContainerStatusUnknown`. -It does not prune container images, delete PVCs, delete namespaces, mutate -OpenTofu state, or SSH into nodes for disk cleanup. If image garbage collection -already freed zero bytes, Jeannie reports the node/image/log investigation path -instead of guessing. +It does not delete PVCs, delete namespaces, or mutate OpenTofu state. Disk cleanup +is limited to CRI image pruning and old pod/container logs; it does not touch +application data. Apply mode remains disabled unless `LAB_SELF_HEAL_APPLY=true`. Test the event pipeline without mutating anything: diff --git a/infra/jeannie-eventd/alertmanager-route.example.yaml b/infra/jeannie-eventd/alertmanager-route.example.yaml index 5f002c8..53305be 100644 --- a/infra/jeannie-eventd/alertmanager-route.example.yaml +++ b/infra/jeannie-eventd/alertmanager-route.example.yaml @@ -2,7 +2,7 @@ route: receiver: default routes: - matchers: - - alertname =~ "HomelabNodeDiskPressure|FreeDiskSpaceFailed|HomelabStaleResidualPods|KubePodContainerStatusUnknown|KubePodEvicted|JeannieApiMetricsDown" + - alertname =~ "HomelabNodeDiskPressure|HomelabHostFilesystemNearlyFull|FreeDiskSpaceFailed|HomelabStaleResidualPods|KubePodContainerStatusUnknown|KubePodEvicted|JeannieApiMetricsDown" receiver: jeannie-self-heal repeat_interval: 1h diff --git a/scripts/jeannie-eventd b/scripts/jeannie-eventd index 8df8077..f691185 100755 --- a/scripts/jeannie-eventd +++ b/scripts/jeannie-eventd @@ -28,6 +28,7 @@ SUPPORTED_ALERTS = { "JeannieApiMetricsDown", "HomelabNodeDiskPressure", "HomelabNodeImageFilesystemPressure", + "HomelabHostFilesystemNearlyFull", "HomelabStaleResidualPods", "FreeDiskSpaceFailed", "KubePodContainerStatusUnknown", @@ -38,6 +39,13 @@ API_SELF_HEAL_COMMANDS = { "JeannieApiMetricsDown": ["sudo", "systemctl", "restart", "jeannie-server.service"], } +DISK_SELF_HEAL_ALERTS = { + "HomelabNodeDiskPressure", + "HomelabNodeImageFilesystemPressure", + "HomelabHostFilesystemNearlyFull", + "FreeDiskSpaceFailed", +} + def prom_escape(value: object) -> str: text = str(value) @@ -100,6 +108,44 @@ def api_self_heal_command(names: set[str]) -> list[str] | None: return None +def disk_self_heal_command(payload: dict[str, Any], names: set[str]) -> list[str] | None: + if not names & DISK_SELF_HEAL_ALERTS: + return None + labels_list: list[dict[str, Any]] = [] + for alert in payload.get("alerts") or []: + if isinstance(alert, dict) and isinstance(alert.get("labels"), dict): + labels_list.append(alert["labels"]) + common_labels = payload.get("commonLabels") + if isinstance(common_labels, dict): + labels_list.append(common_labels) + + for labels in labels_list: + node = str(labels.get("node") or "") + match = re.fullmatch(r"pimox-worker-(\d{1,2})", node) + if match: + return ["./jeannie", "workers", "disk-cleanup", str(int(match.group(1))), "--yes"] + + spec_file = REPO_ROOT / ".lab" / "pimox-workers.tsv" + if spec_file.is_file(): + for labels in labels_list: + instance = str(labels.get("instance") or "").split(":", 1)[0] + if not instance: + continue + try: + rows = spec_file.read_text(encoding="utf-8").splitlines() + except OSError: + break + for row in rows: + fields = row.split("\t") + if len(fields) < 4 or fields[1] != instance: + continue + node = fields[3] + match = re.fullmatch(r"pimox-worker-(\d{1,2})", node) + if match: + return ["./jeannie", "workers", "disk-cleanup", str(int(match.group(1))), "--yes"] + return None + + def write_text(path: Path, text: str) -> None: path.write_text(text, encoding="utf-8") @@ -146,17 +192,23 @@ def handle_payload(payload: dict[str, Any], *, apply: bool) -> dict[str, Any]: write_text(audit_dir / "result.json", json.dumps(result, indent=2) + "\n") return result + disk_command = disk_self_heal_command(payload, set(names)) api_command = api_self_heal_command(set(names)) - if api_command: + direct_command = disk_command or api_command + if direct_command: result = { "status": "planned", "mode": "dry-run", "alerts": names, "audit_dir": str(audit_dir), - "planned_command": " ".join(api_command), + "planned_command": " ".join(direct_command), } if apply: - apply_result = run_command(api_command, audit_dir, "api-heal") + apply_result = run_command( + direct_command, + audit_dir, + "disk-heal" if disk_command else "api-heal", + ) result["status"] = "applied" if apply_result.returncode == 0 else "apply_failed" result["mode"] = "apply" result["apply_status"] = apply_result.returncode