#!/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) 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)