my-homelab-configs/scripts/doctor-application.py

65 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Extract non-secret Argo CD application health evidence."""
import json
import re
import sys
try:
app = json.load(sys.stdin)
if len(sys.argv) > 1 and sys.argv[1] == "--deployment":
spec, current = app.get("spec") or {}, app.get("status") or {}
print("deployment replicas: desired={} ready={} available={}".format(
spec.get("replicas", 1), current.get("readyReplicas", 0), current.get("availableReplicas", 0)))
for condition in current.get("conditions") or []:
if condition.get("status") != "True" and condition.get("message"):
print("deployment condition:", condition.get("type", "Unknown"), condition["message"])
desired = spec.get("replicas", 1)
ready = current.get("readyReplicas", 0)
sys.exit(1 if ready < desired else 0)
if len(sys.argv) > 1 and sys.argv[1] == "--selector":
selector = (app.get("spec") or {}).get("selector") or {}
labels = selector.get("matchLabels") or {}
if selector.get("matchExpressions") or not isinstance(labels, dict) or not labels or len(labels) > 8:
raise ValueError("deployment selector unavailable or not supported")
safe_key = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_./-]{0,252}$")
safe_value = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_.-]{0,62}$")
if any(not isinstance(key, str) or not isinstance(value, str) or not safe_key.fullmatch(key) or not safe_value.fullmatch(value) for key, value in labels.items()):
raise ValueError("deployment selector contains unsupported labels")
print(",".join(f"{key}={labels[key]}" for key in sorted(labels)))
sys.exit(0)
status = app.get("status") or {}
health = status.get("health") or {}
operation = status.get("operationState") or {}
if len(sys.argv) > 1 and sys.argv[1] == "--deployments":
safe = re.compile(r"^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$")
for resource in status.get("resources") or []:
namespace, name = resource.get("namespace", ""), resource.get("name", "")
if resource.get("kind") == "Deployment" and safe.fullmatch(namespace) and safe.fullmatch(name):
print(namespace, name)
sys.exit(0)
if len(sys.argv) > 1 and sys.argv[1] == "--report":
name = (app.get("metadata") or {}).get("name", "unknown")
health_state = health.get("status", "Unknown")
sync = (status.get("sync") or {}).get("status", "Unknown")
severity = "fail" if health_state in ("Degraded", "Missing") else (
"ok" if health_state == "Healthy" and sync == "Synced" else "warn")
print("\t".join((severity, "Kubernetes apps", name,
f"health={health_state}, sync={sync}", "", "kubectl get pods -A -o wide", "", "")))
sys.exit(0)
print("health:", health.get("status", "Unknown"))
if health.get("message"):
print("health message:", health["message"])
if operation.get("phase"):
print("last operation:", operation["phase"])
if operation.get("message"):
print("operation message:", operation["message"])
for resource in status.get("resources") or []:
resource_health = (resource.get("health") or {}).get("status", "")
if resource_health and resource_health != "Healthy":
print("resource:", resource.get("kind", "?"), resource.get("namespace", ""),
resource.get("name", "?"), resource_health)
except (ValueError, TypeError, AttributeError) as exc:
print("Cannot parse application evidence:", exc, file=sys.stderr)
sys.exit(1)