Commit 4afabae0 authored by Santhosh Kumar's avatar Santhosh Kumar

cleanup code

parent adf37892
...@@ -275,21 +275,25 @@ def get_deployment_info(): ...@@ -275,21 +275,25 @@ def get_deployment_info():
pod_name = pod.metadata.name pod_name = pod.metadata.name
# Get resource metrics for the pod # Get resource metrics for the pod
metrics = metrics_api.get_namespaced_custom_object( print(pod_name)
group="metrics.k8s.io", try:
version="v1beta1", metrics = metrics_api.get_namespaced_custom_object(
namespace=namespace, group="metrics.k8s.io",
plural="pods", version="v1beta1",
name=pod_name namespace=namespace,
) plural="pods",
name=pod_name
# Extract CPU and memory usage from metrics )
cpu_usage = metrics["containers"][0]["usage"]["cpu"]
memory_usage = metrics["containers"][0]["usage"]["memory"] # Extract CPU and memory usage from metrics
cpu_usage = metrics["containers"][0]["usage"]["cpu"]
# Accumulate total usage memory_usage = metrics["containers"][0]["usage"]["memory"]
cpu_usage_total += int(cpu_usage[:-1]) # Convert string like "1234n" to int (removing "n" suffix)
memory_usage_total += int(memory_usage[:-2]) # Convert string like "1234Ki" to int (removing "Ki" suffix) # Accumulate total usage
cpu_usage_total += int(cpu_usage[:-1]) # Convert string like "1234n" to int (removing "n" suffix)
memory_usage_total += int(memory_usage[:-2]) # Convert string like "1234Ki" to int (removing "Ki" suffix)
except ApiException as e:
pass
# Calculate average usage across all pods # Calculate average usage across all pods
num_pods = len(pods) num_pods = len(pods)
...@@ -312,11 +316,10 @@ def get_deployment_info(): ...@@ -312,11 +316,10 @@ def get_deployment_info():
if e.status == 404: if e.status == 404:
exposed_url = "Not exposed" exposed_url = "Not exposed"
else: else:
raise exposed_url = "service error "
# Get HPDA (HorizontalPodAutoscaler) if exists # Get HPDA (HorizontalPodAutoscaler) if exists
hpa_name = f"{deployment_name}-hpa" hpa_name = f"{deployment_name}-hpa"
print("Iam here")
try: try:
hpa = scale_api.read_namespaced_horizontal_pod_autoscaler(name=hpa_name, namespace=namespace) hpa = scale_api.read_namespaced_horizontal_pod_autoscaler(name=hpa_name, namespace=namespace)
min_replicas = hpa.spec.min_replicas min_replicas = hpa.spec.min_replicas
...@@ -337,11 +340,9 @@ def get_deployment_info(): ...@@ -337,11 +340,9 @@ def get_deployment_info():
hpa_info = "HPA Error" hpa_info = "HPA Error"
# Get replica sets # Get replica sets
print("Iam here")
replica_sets = apps_api.list_namespaced_replica_set(namespace=deployment.metadata.namespace).items replica_sets = apps_api.list_namespaced_replica_set(namespace=deployment.metadata.namespace).items
# Print deployment info # Print deployment info
print("Iam here")
print("{:<30} {:<15} {:<15} {:<20} {:<20} {:<20}".format( print("{:<30} {:<15} {:<15} {:<20} {:<20} {:<20}".format(
deployment_name, replicas, json.dumps(limits), json.dumps(usage), exposed_url, json.dumps(hpa_info))) deployment_name, replicas, json.dumps(limits), json.dumps(usage), exposed_url, json.dumps(hpa_info)))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment