cloudmon/monitor/views.py
Bast 576448a486
All checks were successful
cloudmon GitOps Deploy / build-and-deploy (push) Successful in 22s
workaround
2026-11-02 12:24:53 -06:00

90 lines
4 KiB
Python

import json
import base64
import requests
import traceback
from django.http import JsonResponse
from django.views.generic import TemplateView, View
class DashboardView(TemplateView):
template_name = "monitor/dashboard.html"
class ClusterGraphDataView(View):
def get(self, request, *args, **kwargs):
try:
repo_name = request.GET.get("repo", "boday_tf")
# DIE RETTUNG: Nutzt das Docker-Host-Gateway, damit der K3s-Pod Consul auf dem Host erreicht!
consul_url = f"http://172.17.0{repo_name}"
response = requests.get(consul_url, timeout=5)
if response.status_code == 404:
return JsonResponse({"name": f"Warte auf Consul-Key '{repo_name}'...", "type": "Root", "children": []})
consul_data = response.json()
raw_value = None
if isinstance(consul_data, list) and len(consul_data) > 0:
first_item = consul_data[0]
if isinstance(first_item, dict):
raw_value = first_item.get("Value")
elif isinstance(consul_data, dict):
raw_value = consul_data.get("Value")
if not raw_value:
return JsonResponse({"name": f"Key '{repo_name}' geladen, aber Value ist leer.", "type": "Root", "children": []})
state_json = json.loads(base64.b64decode(raw_value).decode('utf-8'))
resources = state_json.get("resources", [])
ingresses = []
services = {}
deployments = {}
for res in resources:
res_type = res.get("type")
res_name = res.get("name")
instances = res.get("instances", [])
if isinstance(instances, list) and len(instances) > 0:
attributes = instances[0].get("attributes", {})
elif isinstance(instances, dict):
attributes = instances.get("attributes", {})
else:
attributes = res.get("values", {})
if not attributes:
continue
metadata = attributes.get("metadata", [{}])
actual_name = metadata[0].get("name", res_name) if isinstance(metadata, list) and len(metadata) > 0 else metadata.get("name", res_name) if hasattr(metadata, "get") else res_name
if res_type in ["kubernetes_ingress_v1", "kubernetes_ingress"]:
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 []
ingresses.append({"name": actual_name, "rules": rules})
elif res_type in ["kubernetes_service", "kubernetes_service_v1", "helm_release"]:
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"]:
deployments[actual_name] = {"name": actual_name, "type": "Deployment"}
root_node = {
"name": f"Consul Storage: {repo_name}",
"type": "Root",
"children": []
}
for ing in ingresses:
ing_node = {"name": ing["name"], "type": "Ingress", "children": []}
root_node["children"].append(ing_node)
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)
except Exception as e:
return JsonResponse({
"name": "cloudmon // Fehler-Detail",
"type": "Root",
"children": [{"name": f"Fehler: {str(e)}", "type": "Error", "children": []}]
})