lastview
This commit is contained in:
parent
3224e5ad61
commit
c49ec307a8
1 changed files with 47 additions and 35 deletions
|
|
@ -11,8 +11,8 @@ class ClusterGraphDataView(View):
|
|||
def get(self, request, *args, **kwargs):
|
||||
try:
|
||||
repo_name = request.GET.get("repo", "boday_tf")
|
||||
# ENTERPRISE RESOLUTION: Nutzt wieder das native Docker-DNS im gemeinsamen Netzwerk!
|
||||
consul_url = f"http://consul:8500/v1/kv/terraform/states/{repo_name}"
|
||||
# Nutzt das stabile Docker-Host-Gateway
|
||||
consul_url = f"http://172.17.0{repo_name}"
|
||||
|
||||
response = requests.get(consul_url, timeout=5)
|
||||
if response.status_code == 404:
|
||||
|
|
@ -21,6 +21,7 @@ class ClusterGraphDataView(View):
|
|||
consul_data = response.json()
|
||||
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:
|
||||
first_item = consul_data[0]
|
||||
if isinstance(first_item, dict):
|
||||
|
|
@ -31,12 +32,15 @@ class ClusterGraphDataView(View):
|
|||
if not raw_value:
|
||||
return JsonResponse({"name": f"Key '{repo_name}' leer.", "type": "Root", "children": []})
|
||||
|
||||
# Base64 decodieren
|
||||
state_json = json.loads(base64.b64decode(raw_value).decode('utf-8'))
|
||||
resources = state_json.get("resources", [])
|
||||
|
||||
ingresses = []
|
||||
services = {}
|
||||
deployments = {}
|
||||
root_node = {
|
||||
"name": f"Consul Storage: {repo_name}",
|
||||
"type": "Root",
|
||||
"children": []
|
||||
}
|
||||
|
||||
for res in resources:
|
||||
res_type = res.get("type")
|
||||
|
|
@ -45,45 +49,53 @@ class ClusterGraphDataView(View):
|
|||
|
||||
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
|
||||
|
||||
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", [{}])
|
||||
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: {repo_name}",
|
||||
"type": "Root",
|
||||
root_node["children"].append({
|
||||
"name": actual_name,
|
||||
"type": "Deployment",
|
||||
"meta_status": "Active",
|
||||
"meta_namespace": "default",
|
||||
"meta_version": "v1 (Native K8s)",
|
||||
"meta_tag": "Managed via Deployment spec",
|
||||
"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",
|
||||
"name": "cloudmon // Fehler",
|
||||
"type": "Root",
|
||||
"children": [{"name": f"Fehler: {str(e)}", "type": "Error", "children": []}]
|
||||
"children": [{"name": f"Parsing Fehler: {str(e)}", "type": "Error"}]
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue