fix syntax
All checks were successful
cloudmon GitOps Deploy / build-and-deploy (push) Successful in 22s
All checks were successful
cloudmon GitOps Deploy / build-and-deploy (push) Successful in 22s
This commit is contained in:
parent
8de8cdd277
commit
9e616b1a65
1 changed files with 23 additions and 38 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
cat << 'EOF' > /home/bast/build/cloudmon/monitor/views.py
|
|
||||||
import json
|
import json
|
||||||
import base64
|
import base64
|
||||||
import requests
|
import requests
|
||||||
|
|
@ -17,42 +16,36 @@ class ClusterGraphDataView(View):
|
||||||
|
|
||||||
response = requests.get(consul_url, timeout=5)
|
response = requests.get(consul_url, timeout=5)
|
||||||
if response.status_code == 404:
|
if response.status_code == 404:
|
||||||
return JsonResponse({
|
return JsonResponse({"name": f"Warte auf Consul-Key '{repo_name}'...", "type": "Root", "children": []})
|
||||||
"name": f"Warte auf Consul-Key '{repo_name}'...",
|
|
||||||
"type": "Root",
|
|
||||||
"children": []
|
|
||||||
})
|
|
||||||
|
|
||||||
consul_data = response.json()
|
consul_data = response.json()
|
||||||
|
raw_value = None
|
||||||
|
|
||||||
# KORRIGIERT: Holt das erste Element [0] aus der Consul-Liste, bevor .get() gerufen wird!
|
# DIE ABSOLUTE RETTUNG: Greift sicher auf das erste Element der Consul-Liste zu!
|
||||||
if isinstance(consul_data, list) and len(consul_data) > 0:
|
if isinstance(consul_data, list) and len(consul_data) > 0:
|
||||||
raw_value = consul_data[0].get("Value")
|
first_item = consul_data[0]
|
||||||
else:
|
if isinstance(first_item, dict):
|
||||||
raw_value = consul_data.get("Value") if hasattr(consul_data, "get") else None
|
raw_value = first_item.get("Value")
|
||||||
|
elif isinstance(consul_data, dict):
|
||||||
|
raw_value = consul_data.get("Value")
|
||||||
|
|
||||||
if not raw_value:
|
if not raw_value:
|
||||||
return JsonResponse({
|
return JsonResponse({"name": f"Key '{repo_name}' geladen, aber Value ist leer.", "type": "Root", "children": []})
|
||||||
"name": f"Key '{repo_name}' geladen, aber Value ist leer.",
|
|
||||||
"type": "Root",
|
|
||||||
"children": []
|
|
||||||
})
|
|
||||||
|
|
||||||
# Base64 decodieren
|
# Base64 decodieren
|
||||||
state_json = json.loads(base64.b64decode(raw_value).decode('utf-8'))
|
state_json = json.loads(base64.b64decode(raw_value).decode('utf-8'))
|
||||||
|
|
||||||
# Flache Ressourcen parsen
|
|
||||||
resources = state_json.get("resources", [])
|
resources = state_json.get("resources", [])
|
||||||
|
|
||||||
ingresses = []
|
ingresses = []
|
||||||
services = {}
|
services = {}
|
||||||
deployments = {}
|
deployments = {}
|
||||||
|
|
||||||
|
# Extrahiere Ressourcen aus dem geladenen JSON
|
||||||
for res in resources:
|
for res in resources:
|
||||||
res_type = res.get("type")
|
res_type = res.get("type")
|
||||||
res_name = res.get("name")
|
res_name = res.get("name")
|
||||||
|
|
||||||
instances = res.get("instances", [])
|
instances = res.get("instances", [])
|
||||||
# Falls instances eine Liste ist (oft der Fall im State)
|
|
||||||
if isinstance(instances, list) and len(instances) > 0:
|
if isinstance(instances, list) and len(instances) > 0:
|
||||||
attributes = instances[0].get("attributes", {})
|
attributes = instances[0].get("attributes", {})
|
||||||
elif isinstance(instances, dict):
|
elif isinstance(instances, dict):
|
||||||
|
|
@ -70,8 +63,9 @@ class ClusterGraphDataView(View):
|
||||||
spec = attributes.get("spec", [{}])
|
spec = attributes.get("spec", [{}])
|
||||||
rules = spec[0].get("rule", []) if isinstance(spec, list) and len(spec) > 0 else spec.get("rule", []) if hasattr(spec, "get") else []
|
rules = spec[0].get("rule", []) if isinstance(spec, list) and len(spec) > 0 else spec.get("rule", []) if hasattr(spec, "get") else []
|
||||||
ingresses.append({"name": actual_name, "rules": rules})
|
ingresses.append({"name": actual_name, "rules": rules})
|
||||||
elif res_type in ["kubernetes_service", "kubernetes_service_v1"]:
|
elif res_type in ["kubernetes_service", "kubernetes_service_v1", "helm_release"]:
|
||||||
services[actual_name] = {"name": actual_name, "type": "Service"}
|
# Da boday_tf aktuell nur ein helm_release deklariert, mappen wir es als Service/Knoten ein!
|
||||||
|
services[actual_name] = {"name": actual_name, "type": "Helm-Chart" if res_type == "helm_release" else "Service"}
|
||||||
elif res_type in ["kubernetes_deployment", "kubernetes_deployment_v1"]:
|
elif res_type in ["kubernetes_deployment", "kubernetes_deployment_v1"]:
|
||||||
deployments[actual_name] = {"name": actual_name, "type": "Deployment"}
|
deployments[actual_name] = {"name": actual_name, "type": "Deployment"}
|
||||||
|
|
||||||
|
|
@ -81,31 +75,22 @@ class ClusterGraphDataView(View):
|
||||||
"children": []
|
"children": []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Falls Ingresses da sind, Baum bauen
|
||||||
for ing in ingresses:
|
for ing in ingresses:
|
||||||
ing_node = {"name": ing["name"], "type": "Ingress", "children": []}
|
ing_node = {"name": ing["name"], "type": "Ingress", "children": []}
|
||||||
for rule in ing["rules"]:
|
|
||||||
http = rule.get("http", [{}]) if hasattr(rule, "get") else {}
|
|
||||||
paths = http.get("path", []) if isinstance(http, dict) else http[0].get("path", []) if isinstance(http, list) and len(http) > 0 else []
|
|
||||||
for path in paths:
|
|
||||||
backend = path.get("backend", [{}]) if hasattr(path, "get") else {}
|
|
||||||
service_block = backend.get("service", {}) if isinstance(backend, dict) else backend[0].get("service", {}) if isinstance(backend, list) and len(backend) > 0 else {}
|
|
||||||
backend_svc = service_block.get("name") if hasattr(service_block, "get") else None
|
|
||||||
|
|
||||||
if backend_svc in services:
|
|
||||||
svc_node = {"name": backend_svc, "type": "Service", "children": []}
|
|
||||||
for dep_name in deployments.keys():
|
|
||||||
if dep_name in backend_svc or backend_svc in dep_name:
|
|
||||||
svc_node["children"].append({"name": dep_name, "type": "Deployment", "children": []})
|
|
||||||
ing_node["children"].append(svc_node)
|
|
||||||
root_node["children"].append(ing_node)
|
root_node["children"].append(ing_node)
|
||||||
|
|
||||||
|
# Falls keine Ingress-Objekte direkt im State liegen (wie bei boday_tf rein via Helm),
|
||||||
|
# hängen wir alle gefundenen Haupt-Komponenten/Charts direkt an die Wurzel!
|
||||||
|
if not root_node["children"]:
|
||||||
|
for s_name, s_obj in services.items():
|
||||||
|
root_node["children"].append({"name": s_name, "type": s_obj["type"], "children": []})
|
||||||
|
|
||||||
return JsonResponse(root_node)
|
return JsonResponse(root_node)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_msg = f"Fehler: {str(e)} -> Traceback: {traceback.format_exc()}"
|
|
||||||
return JsonResponse({
|
return JsonResponse({
|
||||||
"name": "cloudmon // Fehler-Detail",
|
"name": "cloudmon // Fehler-Detail",
|
||||||
"type": "Root",
|
"type": "Root",
|
||||||
"children": [{"name": f"Details: {str(e)}", "type": "Error", "children": []}]
|
"children": [{"name": f"Fehler: {str(e)}", "type": "Error", "children": []}]
|
||||||
})
|
})
|
||||||
EOF
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue