This commit is contained in:
Bast 2026-11-02 23:10:49 -06:00
parent 3224e5ad61
commit c49ec307a8

View file

@ -11,8 +11,8 @@ 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")
# ENTERPRISE RESOLUTION: Nutzt wieder das native Docker-DNS im gemeinsamen Netzwerk! # Nutzt das stabile Docker-Host-Gateway
consul_url = f"http://consul:8500/v1/kv/terraform/states/{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)
if response.status_code == 404: if response.status_code == 404:
@ -21,6 +21,7 @@ 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):
@ -31,12 +32,15 @@ class ClusterGraphDataView(View):
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}' 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", [])
ingresses = [] root_node = {
services = {} "name": f"Consul Storage: {repo_name}",
deployments = {} "type": "Root",
"children": []
}
for res in resources: for res in resources:
res_type = res.get("type") res_type = res.get("type")
@ -45,45 +49,53 @@ 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
metadata = attributes.get("metadata", [{}]) if res_type == "helm_release":
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 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": []
})
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"]: elif res_type in ["kubernetes_deployment", "kubernetes_deployment_v1"]:
deployments[actual_name] = {"name": actual_name, "type": "Deployment"} 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
root_node = { root_node["children"].append({
"name": f"Consul: {repo_name}", "name": actual_name,
"type": "Root", "type": "Deployment",
"children": [] "meta_status": "Active",
} "meta_namespace": "default",
"meta_version": "v1 (Native K8s)",
for ing in ingresses: "meta_tag": "Managed via Deployment spec",
ing_node = {"name": ing["name"], "type": "Ingress", "children": []} "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-Detail", "name": "cloudmon // Fehler",
"type": "Root", "type": "Root",
"children": [{"name": f"Fehler: {str(e)}", "type": "Error", "children": []}] "children": [{"name": f"Parsing Fehler: {str(e)}", "type": "Error"}]
}) })