update(app): add helm chart and cleanup repo

This commit is contained in:
Aleksandr Tcitlionok
2024-12-11 09:05:15 +00:00
parent c12ce697c0
commit acef6cb9a1
19 changed files with 274 additions and 777 deletions

47
.github/workflows/ci.yaml vendored Normal file
View File

@@ -0,0 +1,47 @@
name: CI Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
env:
IMAGE_NAME: ghcr.io/terghalin/metalcheck-backend
jobs:
build-and-push:
name: Build and Push Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Cache Python dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Build and Push Docker Image
uses: docker/build-push-action@v4
with:
push: true
tags: |
${{ env.IMAGE_NAME }}:latest
${{ env.IMAGE_NAME }}:${{ github.sha }}

View File

@@ -127,3 +127,23 @@ curl -X GET "http://<EXTERNAL-IP>/export?format=yaml"
```shell
curl -X GET "http://<EXTERNAL-IP>/export?format=json"
```
## MetalCheck Helm Chart
This Helm chart deploys the MetalCheck backend on a Kubernetes cluster.
### Installation
#### Prerequisites
- Kubernetes cluster (1.18+ recommended)
- Helm 3.x installed
- Docker image for MetalCheck backend
#### Install Chart
1. Update `values.yaml` with your specific configurations.
2. Install the chart:
```bash
helm install metalcheck ./metalcheck-helm

View File

@@ -1,292 +1,32 @@
[
{
"name": "akita",
"name": "chuo",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"vendor": "The Company",
"cpu": 24,
"memory": "32GB",
"memory": "64GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
"time_on_duty": 522,
"initial_cost": 1900
},
{
"name": "chiba",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"name": "kyoto",
"location": "Kyoto",
"vendor": "The Company",
"cpu": 36,
"memory": "96GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
"time_on_duty": 807,
"initial_cost": 1889
},
{
"name": "ehime",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"name": "sa-100",
"location": "Sapporo",
"vendor": "The Company",
"cpu": 8,
"memory": "16GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "fukuoka",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "kanagawa",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "kumamoto",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "okayama",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "okinawa",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "osaka",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "saitama",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "osaka",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "saitama",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "shizuoka",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "yamagata",
"location": "Tokyo",
"vendor": "Minisforum MS-01",
"cpu": 24,
"memory": "32GB",
"storage": "1TB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "adatara",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "arashima",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "asahi",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "aso",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "azuma",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "daisen",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "horoshiri",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "nantai",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "kirishima",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "ontake",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "rausu",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "rishiri",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "shari",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "yotei",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
},
{
"name": "zao",
"location": "Tokyo",
"vendor": "OrangePi 5 ARM",
"cpu": 8,
"memory": "16GB",
"storage": "128GB",
"time_on_duty": 1000,
"initial_cost": 1000.0
"time_on_duty": 4401,
"initial_cost": 849
}
]
]

View File

@@ -22,461 +22,5 @@
"memory": "192GB",
"storage": "960GB SSD",
"vm_type": "cx-6xlarge"
},
{
"name": "cpx-41-1",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "240GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cpx-41-2",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "240GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cpx-41-3",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "240GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cpx-41-4",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "240GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cpx-41-5",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "240GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cpx-41-6",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "240GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cpx-41-7",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "240GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cpx-41-8",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "240GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cpx-41-9",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "240GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cpx-41-10",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "240GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-22-1",
"location": "Helsinki",
"cpu": 2,
"memory": "4GB",
"storage": "40GB SSD",
"vm_type": "cx-medium"
},
{
"name": "cx-22-2",
"location": "Helsinki",
"cpu": 2,
"memory": "4GB",
"storage": "40GB SSD",
"vm_type": "cx-medium"
},
{
"name": "cx-22-3",
"location": "Helsinki",
"cpu": 2,
"memory": "4GB",
"storage": "40GB SSD",
"vm_type": "cx-medium"
},
{
"name": "cx-22-4",
"location": "Helsinki",
"cpu": 2,
"memory": "4GB",
"storage": "40GB SSD",
"vm_type": "cx-medium"
},
{
"name": "cx-22-5",
"location": "Helsinki",
"cpu": 2,
"memory": "4GB",
"storage": "40GB SSD",
"vm_type": "cx-medium"
},
{
"name": "cx-22-6",
"location": "Helsinki",
"cpu": 2,
"memory": "4GB",
"storage": "40GB SSD",
"vm_type": "cx-medium"
},
{
"name": "cx-22-7",
"location": "Helsinki",
"cpu": 2,
"memory": "4GB",
"storage": "40GB SSD",
"vm_type": "cx-medium"
},
{
"name": "cx-22-8",
"location": "Helsinki",
"cpu": 2,
"memory": "4GB",
"storage": "40GB SSD",
"vm_type": "cx-medium"
},
{
"name": "cx-22-9",
"location": "Helsinki",
"cpu": 2,
"memory": "4GB",
"storage": "40GB SSD",
"vm_type": "cx-medium"
},
{
"name": "cx-22-10",
"location": "Helsinki",
"cpu": 2,
"memory": "4GB",
"storage": "40GB SSD",
"vm_type": "cx-medium"
},
{
"name": "cx-42-1",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-2",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-3",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-4",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-5",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-6",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-7",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-8",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-9",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-10",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-11",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB SSD",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-gawa-1",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-gawa-2",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-gawa-3",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-gawa-4",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-gawa-5",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-gawa-6",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-gawa-7",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-gawa-8",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-gawa-9",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-gawa-10",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-yama-1",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-yama-2",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-yama-3",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-yama-4",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-yama-5",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-yama-6",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-yama-7",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-yama-8",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-yama-9",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-42-yama-10",
"location": "Helsinki",
"cpu": 8,
"memory": "16GB",
"storage": "160GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-52-1",
"location": "Helsinki",
"cpu": 16,
"memory": "32GB",
"storage": "320GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-52-2",
"location": "Helsinki",
"cpu": 16,
"memory": "32GB",
"storage": "320GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-52-3",
"location": "Helsinki",
"cpu": 16,
"memory": "32GB",
"storage": "320GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-52-4",
"location": "Helsinki",
"cpu": 16,
"memory": "32GB",
"storage": "320GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-52-5",
"location": "Helsinki",
"cpu": 16,
"memory": "32GB",
"storage": "320GB",
"vm_type": "cx-xlarge"
},
{
"name": "cx-52-6",
"location": "Helsinki",
"cpu": 16,
"memory": "32GB",
"storage": "320GB",
"vm_type": "cx-xlarge"
}
]
}
]

View File

@@ -79,14 +79,6 @@ app.include_router(k8s.router)
app.include_router(export.router)
app.include_router(think.router)
#app.add_middleware(
# CORSMiddleware,
# allow_origins=["http://localhost:3000"],
# allow_credentials=True,
# allow_methods=["*"],
# allow_headers=["*"],
#)
@app.get("/")
def root():
return {"message": "Welcome to Metal Check API"}

View File

@@ -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 = {

View File

@@ -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"

View File

@@ -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")

6
helm/Chart.yaml Normal file
View File

@@ -0,0 +1,6 @@
apiVersion: v2
name: metalcheck-backend
description: A Helm chart for deploying the MetalCheck backend application.
type: application
version: 0.1.0
appVersion: "latest"

View File

@@ -0,0 +1,7 @@
{{/*
Expand common labels.
*/}}
{{- define "metalcheck-backend.labels" -}}
app: {{ .Chart.Name }}
release: {{ .Release.Name }}
{{- end }}

View File

@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-config
labels:
app: {{ .Chart.Name }}
data:
HETZNER_TOKEN: {{ .Values.env.HETZNER_TOKEN | quote }}

View File

@@ -0,0 +1,30 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-backend
labels:
app: {{ .Chart.Name }}
release: {{ .Release.Name }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Chart.Name }}
release: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ .Chart.Name }}
release: {{ .Release.Name }}
spec:
containers:
- name: backend
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: 80
env:
- name: HETZNER_TOKEN
value: "{{ .Values.env.HETZNER_TOKEN }}"
resources:
{{- toYaml .Values.resources | nindent 12 }}

View File

@@ -0,0 +1,26 @@
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Release.Name }}-ingress
annotations:
{{ toYaml .Values.ingress.annotations | indent 4 }}
spec:
rules:
{{- range .Values.ingress.hosts }}
- host: {{ .host }}
http:
paths:
{{- range .paths }}
- path: {{ .path }}
pathType: {{ .pathType }}
backend:
service:
name: {{ $.Release.Name }}-service
port:
number: {{ $.Values.service.port }}
{{- end }}
{{- end }}
tls:
{{ toYaml .Values.ingress.tls | indent 4 }}
{{- end }}

49
helm/templates/rbac.yaml Normal file
View File

@@ -0,0 +1,49 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ .Release.Name }}-sa
namespace: {{ .Release.Namespace }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ .Release.Name }}-clusterrole
rules:
- apiGroups: [""]
resources: ["pods", "nodes", "namespaces"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: {{ .Release.Name }}-clusterrolebinding
subjects:
- kind: ServiceAccount
name: {{ .Release.Name }}-sa
namespace: {{ .Release.Namespace }}
roleRef:
kind: ClusterRole
name: {{ .Release.Name }}-clusterrole
apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ .Release.Name }}-metrics-reader
rules:
- apiGroups: ["metrics.k8s.io"]
resources: ["pods"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: {{ .Release.Name }}-metrics-reader-binding
subjects:
- kind: ServiceAccount
name: {{ .Release.Name }}-sa
namespace: {{ .Release.Namespace }}
roleRef:
kind: ClusterRole
name: {{ .Release.Name }}-metrics-reader
apiGroup: rbac.authorization.k8s.io

View File

@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-service
labels:
app: {{ .Chart.Name }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: 80
selector:
app: {{ .Chart.Name }}
release: {{ .Release.Name }}

35
helm/values.yaml Normal file
View File

@@ -0,0 +1,35 @@
replicaCount: 2
image:
repository: ghcr.io/terghalin/metalcheck-backend
tag: latest
pullPolicy: IfNotPresent
service:
type: LoadBalancer
port: 80
ingress:
enabled: false
annotations: {}
hosts:
- host: metalcheck.local
paths:
- path: /
pathType: ImplementationSpecific
tls: []
resources:
limits:
cpu: "500m"
memory: "512Mi"
requests:
cpu: "250m"
memory: "256Mi"
env:
HETZNER_TOKEN: ""
nodeSelector: {}
tolerations: []
affinity: {}