Compare commits

..

No commits in common. "c49ec307a8d8fa15194fba24054c25afc9426c0a" and "a39d256d6219ee6f0de6ca17295a55782e621e25" have entirely different histories.

View file

@ -1,6 +1,7 @@
import json import json
import base64 import base64
import requests import requests
import traceback
from django.http import JsonResponse from django.http import JsonResponse
from django.views.generic import TemplateView, View from django.views.generic import TemplateView, View
@ -11,7 +12,7 @@ class ClusterGraphDataView(View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
try: try:
repo_name = request.GET.get("repo", "boday_tf") repo_name = request.GET.get("repo", "boday_tf")
# Nutzt das stabile Docker-Host-Gateway # 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}" consul_url = f"http://172.17.0{repo_name}"
response = requests.get(consul_url, timeout=5) response = requests.get(consul_url, timeout=5)
@ -21,7 +22,6 @@ class ClusterGraphDataView(View):
consul_data = response.json() consul_data = response.json()
raw_value = None raw_value = None
# KORRIGIERT: Holt explizit das ERSTE Element aus der Consul-Liste über den Index Null!
if isinstance(consul_data, list) and len(consul_data) > 0: if isinstance(consul_data, list) and len(consul_data) > 0:
first_item = consul_data[0] first_item = consul_data[0]
if isinstance(first_item, dict): if isinstance(first_item, dict):
@ -30,17 +30,14 @@ class ClusterGraphDataView(View):
raw_value = consul_data.get("Value") raw_value = consul_data.get("Value")
if not raw_value: if not raw_value:
return JsonResponse({"name": f"Key '{repo_name}' leer.", "type": "Root", "children": []}) return JsonResponse({"name": f"Key '{repo_name}' geladen, aber Value ist leer.", "type": "Root", "children": []})
# Base64 decodieren
state_json = json.loads(base64.b64decode(raw_value).decode('utf-8')) state_json = json.loads(base64.b64decode(raw_value).decode('utf-8'))
resources = state_json.get("resources", []) resources = state_json.get("resources", [])
root_node = { ingresses = []
"name": f"Consul Storage: {repo_name}", services = {}
"type": "Root", deployments = {}
"children": []
}
for res in resources: for res in resources:
res_type = res.get("type") res_type = res.get("type")
@ -49,53 +46,45 @@ class ClusterGraphDataView(View):
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):
attributes = instances.get("attributes", {})
else: else:
attributes = res.get("values", {})
if not attributes:
continue continue
if res_type == "helm_release":
metadata_list = attributes.get("metadata", [{}])
metadata = metadata_list[0] if metadata_list else {}
chart_version = metadata.get("version", "1.0.0")
namespace = metadata.get("namespace", "default")
status = attributes.get("status", "deployed")
raw_values_string = metadata.get("values", "{}")
try:
values_dict = json.loads(raw_values_string)
image_tag = values_dict.get("image", {}).get("tag", "latest")
except:
image_tag = "latest"
root_node["children"].append({
"name": attributes.get("name", res_name),
"type": "Helm-Chart",
"meta_status": status,
"meta_namespace": namespace,
"meta_version": chart_version,
"meta_tag": image_tag,
"children": []
})
elif res_type in ["kubernetes_deployment", "kubernetes_deployment_v1"]:
metadata = attributes.get("metadata", [{}]) 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 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
root_node["children"].append({ if res_type in ["kubernetes_ingress_v1", "kubernetes_ingress"]:
"name": actual_name, spec = attributes.get("spec", [{}])
"type": "Deployment", rules = spec[0].get("rule", []) if isinstance(spec, list) and len(spec) > 0 else spec.get("rule", []) if hasattr(spec, "get") else []
"meta_status": "Active", ingresses.append({"name": actual_name, "rules": rules})
"meta_namespace": "default", elif res_type in ["kubernetes_service", "kubernetes_service_v1", "helm_release"]:
"meta_version": "v1 (Native K8s)", services[actual_name] = {"name": actual_name, "type": "Helm-Chart" if res_type == "helm_release" else "Service"}
"meta_tag": "Managed via Deployment spec", 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": [] "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) return JsonResponse(root_node)
except Exception as e: except Exception as e:
return JsonResponse({ return JsonResponse({
"name": "cloudmon // Fehler", "name": "cloudmon // Fehler-Detail",
"type": "Root", "type": "Root",
"children": [{"name": f"Parsing Fehler: {str(e)}", "type": "Error"}] "children": [{"name": f"Fehler: {str(e)}", "type": "Error", "children": []}]
}) })