mirror of
https://github.com/Rainyy21/k3s-health-dashboard.git
synced 2026-10-11 01:30:20 -04:00
72 lines
1.9 KiB
Python
72 lines
1.9 KiB
Python
import streamlit as st
|
|
from app import cfg
|
|
from app.k8s_client import get_core, get_custom_client, fetch_nodes, fetch_pods
|
|
|
|
|
|
@st.cache_resource
|
|
def clients():
|
|
get_core()
|
|
get_custom_client()
|
|
|
|
|
|
@st.cache_data(ttl=cfg.cache_ttl)
|
|
def cached_nodes():
|
|
return fetch_nodes()
|
|
|
|
|
|
@st.cache_data(ttl=cfg.cache_ttl)
|
|
def cached_pods(namespace="all"):
|
|
return fetch_pods(namespace=namespace)
|
|
|
|
|
|
@st.cache_data(ttl=cfg.cache_ttl)
|
|
def cached_deployments(namespace="all"):
|
|
from kubernetes import client as _k8s
|
|
get_core() # ensures kubeconfig is loaded before AppsV1Api is created
|
|
apps_v1 = _k8s.AppsV1Api()
|
|
if namespace == "all":
|
|
items = apps_v1.list_deployment_for_all_namespaces().items
|
|
else:
|
|
items = apps_v1.list_namespaced_deployment(namespace=namespace).items
|
|
return [
|
|
{
|
|
"namespace": d.metadata.namespace,
|
|
"name": d.metadata.name,
|
|
"ready": f"{d.status.ready_replicas or 0}/{d.spec.replicas or 0}",
|
|
"available": d.status.available_replicas or 0,
|
|
"desired": d.spec.replicas or 0,
|
|
}
|
|
for d in items
|
|
]
|
|
|
|
|
|
@st.cache_data(ttl=cfg.cache_ttl)
|
|
def cached_namespaces():
|
|
return [ns.metadata.name for ns in get_core().list_namespace().items]
|
|
|
|
|
|
@st.cache_data(ttl=cfg.cache_ttl)
|
|
def cached_node_metrics():
|
|
try:
|
|
api = get_custom_client()
|
|
data = api.list_cluster_custom_object(
|
|
group="metrics.k8s.io", version="v1beta1", plural="nodes"
|
|
)
|
|
return {
|
|
item["metadata"]["name"]: {
|
|
"cpu_usage": item["usage"]["cpu"],
|
|
"memory_usage": item["usage"]["memory"],
|
|
}
|
|
for item in data["items"]
|
|
}
|
|
except Exception:
|
|
return {}
|
|
|
|
|
|
def clear():
|
|
cached_nodes.clear()
|
|
cached_pods.clear()
|
|
cached_deployments.clear()
|
|
cached_namespaces.clear()
|
|
cached_node_metrics.clear()
|