update(app): add helm chart and cleanup repo
Some checks failed
CI Pipeline / Build and Push Docker Image (push) Failing after 2m48s
Some checks failed
CI Pipeline / Build and Push Docker Image (push) Failing after 2m48s
This commit is contained in:
@@ -7,7 +7,6 @@ import json
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Helper functions for conversions
|
||||
def calculate_time_on_duty(creation_timestamp):
|
||||
"""
|
||||
Calculate the time on duty in hours, days, or minutes from the creation timestamp.
|
||||
@@ -25,7 +24,6 @@ def calculate_time_on_duty(creation_timestamp):
|
||||
hours = delta.seconds // 3600
|
||||
return f"{hours} hours" if hours > 1 else "1 hour"
|
||||
|
||||
# Otherwise, return days
|
||||
return f"{delta.days} days" if delta.days > 1 else "1 day"
|
||||
|
||||
def convert_memory_to_gb(memory):
|
||||
@@ -58,21 +56,19 @@ def convert_cpu_to_cores(cpu):
|
||||
Handles units: n (nano), u (micro), m (milli), or none (cores).
|
||||
Returns float values for cores, rounded appropriately.
|
||||
"""
|
||||
if "n" in cpu: # Nanocores to cores
|
||||
if "n" in cpu:
|
||||
return round(int(cpu.replace("n", "")) / 1e9, 4)
|
||||
elif "u" in cpu: # Microcores to cores
|
||||
elif "u" in cpu:
|
||||
return round(int(cpu.replace("u", "")) / 1e6, 4)
|
||||
elif "m" in cpu: # Millicores to cores
|
||||
elif "m" in cpu:
|
||||
return round(int(cpu.replace("m", "")) / 1000, 4)
|
||||
return float(cpu) # Already in cores
|
||||
return float(cpu)
|
||||
|
||||
# Fetch Kubernetes data with namespace resource usage
|
||||
def fetch_k8s_data_with_usage():
|
||||
config.load_incluster_config()
|
||||
v1 = client.CoreV1Api()
|
||||
metrics_client = client.CustomObjectsApi()
|
||||
|
||||
# Fetch nodes
|
||||
nodes = []
|
||||
for node in v1.list_node().items:
|
||||
# Extract storage (ephemeral-storage) and instance type
|
||||
@@ -80,7 +76,6 @@ def fetch_k8s_data_with_usage():
|
||||
instance_type = node.metadata.labels.get("beta.kubernetes.io/instance-type", "N/A")
|
||||
creation_timestamp = node.metadata.creation_timestamp
|
||||
|
||||
# Calculate time on duty
|
||||
time_on_duty = calculate_time_on_duty(creation_timestamp) if creation_timestamp else "N/A"
|
||||
|
||||
nodes.append({
|
||||
@@ -93,10 +88,8 @@ def fetch_k8s_data_with_usage():
|
||||
"time_on_duty": time_on_duty, # Add time on duty
|
||||
})
|
||||
|
||||
# Fetch namespaces
|
||||
namespaces = [ns.metadata.name for ns in v1.list_namespace().items]
|
||||
|
||||
# Fetch pod metrics and calculate namespace resource usage
|
||||
namespace_usage = {}
|
||||
pod_metrics = metrics_client.list_cluster_custom_object(
|
||||
group="metrics.k8s.io", version="v1beta1", plural="pods"
|
||||
@@ -110,22 +103,19 @@ def fetch_k8s_data_with_usage():
|
||||
cpu_usage = container["usage"]["cpu"]
|
||||
memory_usage = container["usage"]["memory"]
|
||||
|
||||
# Convert CPU to cores and memory to MiB
|
||||
namespace_usage[pod_namespace]["cpu"] += convert_cpu_to_cores(cpu_usage)
|
||||
namespace_usage[pod_namespace]["memory"] += convert_memory_to_mib(memory_usage)
|
||||
|
||||
# Round and format usage for readability
|
||||
namespace_usage = {
|
||||
ns: {
|
||||
"cpu": round(usage["cpu"], 4), # Round to 4 decimal places
|
||||
"memory": round(usage["memory"], 2), # Memory in MiB
|
||||
"cpu": round(usage["cpu"], 4),
|
||||
"memory": round(usage["memory"], 2),
|
||||
}
|
||||
for ns, usage in namespace_usage.items()
|
||||
}
|
||||
|
||||
return {"nodes": nodes, "namespaces": namespaces, "namespace_usage": namespace_usage}
|
||||
|
||||
# Export endpoint
|
||||
@router.get("/export")
|
||||
def export_data(format: str = "yaml"):
|
||||
data = {
|
||||
|
||||
@@ -9,14 +9,12 @@ def fetch_k8s_data_with_usage():
|
||||
v1 = client.CoreV1Api()
|
||||
metrics_client = client.CustomObjectsApi()
|
||||
|
||||
# Fetch nodes
|
||||
nodes = []
|
||||
for node in v1.list_node().items:
|
||||
# Extract storage (ephemeral-storage) and instance type
|
||||
ephemeral_storage = node.status.capacity.get("ephemeral-storage", "0")
|
||||
instance_type = node.metadata.labels.get("beta.kubernetes.io/instance-type", "N/A")
|
||||
|
||||
# Calculate time on duty
|
||||
creation_timestamp = node.metadata.creation_timestamp
|
||||
if creation_timestamp:
|
||||
time_on_duty = calculate_time_on_duty(creation_timestamp)
|
||||
@@ -26,17 +24,15 @@ def fetch_k8s_data_with_usage():
|
||||
nodes.append({
|
||||
"node_name": node.metadata.name,
|
||||
"cpu": node.status.capacity.get("cpu"),
|
||||
"memory": round(convert_memory_to_mib(node.status.capacity.get("memory")), 2), # Convert to MiB
|
||||
"memory": round(convert_memory_to_mib(node.status.capacity.get("memory")), 2),
|
||||
"storage": f"{round(convert_memory_to_gb(ephemeral_storage), 2)} GB",
|
||||
"instance_type": instance_type,
|
||||
"pods_allocatable": node.status.allocatable.get("pods"),
|
||||
"time_on_duty": time_on_duty, # Add time on duty
|
||||
"time_on_duty": time_on_duty,
|
||||
})
|
||||
|
||||
# Fetch namespaces
|
||||
namespaces = [ns.metadata.name for ns in v1.list_namespace().items]
|
||||
|
||||
# Fetch pod metrics and calculate namespace resource usage
|
||||
namespace_usage = {}
|
||||
pod_metrics = metrics_client.list_cluster_custom_object(
|
||||
group="metrics.k8s.io", version="v1beta1", plural="pods"
|
||||
@@ -50,15 +46,13 @@ def fetch_k8s_data_with_usage():
|
||||
cpu_usage = container["usage"]["cpu"]
|
||||
memory_usage = container["usage"]["memory"]
|
||||
|
||||
# Convert CPU to cores and memory to MiB
|
||||
namespace_usage[pod_namespace]["cpu"] += convert_cpu_to_cores(cpu_usage)
|
||||
namespace_usage[pod_namespace]["memory"] += convert_memory_to_mib(memory_usage)
|
||||
|
||||
# Round and format usage for readability
|
||||
namespace_usage = {
|
||||
ns: {
|
||||
"cpu": round(usage["cpu"], 4), # Round to 4 decimal places
|
||||
"memory": round(usage["memory"], 2), # Memory in MiB
|
||||
"cpu": round(usage["cpu"], 4),
|
||||
"memory": round(usage["memory"], 2),
|
||||
}
|
||||
for ns, usage in namespace_usage.items()
|
||||
}
|
||||
@@ -73,17 +67,14 @@ def calculate_time_on_duty(creation_timestamp):
|
||||
now = datetime.now(timezone.utc)
|
||||
delta = now - creation_timestamp
|
||||
|
||||
# If less than an hour, return minutes
|
||||
if delta.days < 1 and delta.seconds < 3600:
|
||||
minutes = delta.seconds // 60
|
||||
return f"{minutes} minutes" if minutes > 1 else "less than a minute"
|
||||
|
||||
# If less than a day, return hours
|
||||
if delta.days < 1:
|
||||
hours = delta.seconds // 3600
|
||||
return f"{hours} hours" if hours > 1 else "1 hour"
|
||||
|
||||
# Otherwise, return days
|
||||
return f"{delta.days} days" if delta.days > 1 else "1 day"
|
||||
|
||||
|
||||
|
||||
@@ -45,7 +45,6 @@ def think_k8s():
|
||||
|
||||
cluster_name = "eks-staging"
|
||||
|
||||
# Format node data
|
||||
nodes = []
|
||||
for node in v1.list_node().items:
|
||||
# Fetch ephemeral-storage and instance type
|
||||
@@ -61,7 +60,6 @@ def think_k8s():
|
||||
"namespaces": [ns.metadata.name for ns in v1.list_namespace().items]
|
||||
})
|
||||
|
||||
# Fetch environment variables
|
||||
api_url = os.getenv("AI_API_URL")
|
||||
auth_token = os.getenv("AI_API_TOKEN")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user