"""Inspect Longhorn backup settings and targets from the active Kubernetes context.""" import json import logging from kubernetes import config from kubernetes.client import ApiException, CustomObjectsApi logger = logging.getLogger(__name__) def main() -> int: """Print Longhorn settings and backup targets as JSON.""" try: try: config.load_incluster_config() except config.ConfigException: config.load_kube_config() api = CustomObjectsApi() settings = api.list_namespaced_custom_object( group="longhorn.io", version="v1beta2", namespace="longhorn-system", plural="settings" ) targets = api.list_namespaced_custom_object( group="longhorn.io", version="v1beta2", namespace="longhorn-system", plural="backuptargets" ) except (config.ConfigException, ApiException) as exc: logger.error("Could not query Longhorn: %s", exc) return 1 print(json.dumps({"settings": settings.get("items", []), "backup_targets": targets.get("items", [])}, indent=2)) return 0 if __name__ == "__main__": raise SystemExit(main())