Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Lift Kubernetes cluster child counts into the page model

rcourtmanec2bd4007f57
diff --git a/frontend-modern/src/features/kubernetes/KubernetesClustersTable.tsx b/frontend-modern/src/features/kubernetes/KubernetesClustersTable.tsx
index 7140bcf441..14c1008756 100644
--- a/frontend-modern/src/features/kubernetes/KubernetesClustersTable.tsx
+++ b/frontend-modern/src/features/kubernetes/KubernetesClustersTable.tsx
@@ -35,6 +35,7 @@ import {
getPlatformResourceDetailRowClass,
} from '@/features/platformPage/PlatformResourceDetailTableRow';
import type { Resource } from '@/types/resource';
+import { buildKubernetesClusterChildCounts } from './kubernetesPageModel';
// Kubernetes clusters are control-plane aggregates, not single processes —
// they have no per-cluster Disk I/O / Uptime / Temperature concepts that
@@ -77,42 +78,9 @@ export const KubernetesClustersTable: Component<{
const drawer = createPlatformResourceDetailState({ idPrefix: 'kubernetes-cluster-drawer' });
const resolveResourceLabel = createPlatformResourceLabelResolver(() => props.scope);
- const countsByCluster = createMemo(() => {
- const map = new Map<string, { nodes: number; pods: number; deployments: number }>();
- for (const cluster of props.clusters) {
- map.set(cluster.id, { nodes: 0, pods: 0, deployments: 0 });
- }
- for (const resource of props.scope) {
- const clusterId =
- asTrimmedString(resource.kubernetes?.clusterId) ||
- asTrimmedString(resource.kubernetes?.clusterName);
- if (!clusterId) continue;
- // Match against the cluster row by clusterId. The cluster row's
- // own canonical id differs from its `kubernetes.clusterId`, so map
- // by the kubernetes-side identifier first, then fall back to row id.
- let bucket = null as { nodes: number; pods: number; deployments: number } | null;
- for (const cluster of props.clusters) {
- const k = cluster.kubernetes;
- if (!k) continue;
- if (
- asTrimmedString(k.clusterId) === clusterId ||
- asTrimmedString(k.clusterName) === clusterId
- ) {
- bucket = map.get(cluster.id) ?? null;
- break;
- }
- }
- if (!bucket) continue;
- if (resource.type === 'k8s-node') bucket.nodes += 1;
- else if (resource.type === 'agent' && resource.sources?.includes('kubernetes'))
- bucket.nodes += 1;
- else if (resource.type === 'pod') bucket.pods += 1;
- else if (resource.type === 'k8s-deployment') bucket.deployments += 1;
- }
- // Fallback when scope-based counts come back empty (e.g. tests that
- // only supply the cluster rows): keep the rendered counts honest at 0.
- return map;
- });
+ const countsByCluster = createMemo(() =>
+ buildKubernetesClusterChildCounts(props.scope, props.clusters),
+ );
return (
<Show
diff --git a/frontend-modern/src/features/kubernetes/__tests__/kubernetesPageModel.test.ts b/frontend-modern/src/features/kubernetes/__tests__/kubernetesPageModel.test.ts
index a995b28299..4c4315d7de 100644
--- a/frontend-modern/src/features/kubernetes/__tests__/kubernetesPageModel.test.ts
+++ b/frontend-modern/src/features/kubernetes/__tests__/kubernetesPageModel.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest';
import type { Resource } from '@/types/resource';
import {
KUBERNETES_TAB_SPECS,
+ buildKubernetesClusterChildCounts,
buildKubernetesPageModel,
compareKubernetesControllers,
compareKubernetesDeployments,
@@ -580,4 +581,79 @@ describe('kubernetesPageModel', () => {
expect(model.workloads.slice(0, 2).map((r) => r.id)).toEqual(['dep-broken', 'dep-partial']);
});
});
+
+ describe('buildKubernetesClusterChildCounts', () => {
+ const cluster = (id: string, k: { clusterId?: string; clusterName?: string }): Resource =>
+ makeResource({ id, type: 'k8s-cluster', kubernetes: k });
+
+ it('walks resources once and rolls per-cluster nodes / pods / deployments', () => {
+ const prod = cluster('cluster-prod', { clusterId: 'prod', clusterName: 'Production' });
+ const stg = cluster('cluster-stg', { clusterId: 'stg', clusterName: 'Staging' });
+ const resources = [
+ prod,
+ stg,
+ makeResource({ id: 'n1', type: 'k8s-node', kubernetes: { clusterId: 'prod' } }),
+ makeResource({ id: 'n2', type: 'k8s-node', kubernetes: { clusterId: 'prod' } }),
+ makeResource({ id: 'n3', type: 'k8s-node', kubernetes: { clusterId: 'stg' } }),
+ makeResource({ id: 'p1', type: 'pod', kubernetes: { clusterId: 'prod' } }),
+ makeResource({ id: 'p2', type: 'pod', kubernetes: { clusterId: 'prod' } }),
+ makeResource({ id: 'p3', type: 'pod', kubernetes: { clusterId: 'prod' } }),
+ makeResource({ id: 'p4', type: 'pod', kubernetes: { clusterName: 'Staging' } }),
+ makeResource({
+ id: 'd1',
+ type: 'k8s-deployment',
+ kubernetes: { clusterId: 'prod' },
+ }),
+ ];
+
+ const counts = buildKubernetesClusterChildCounts(resources, [prod, stg]);
+ expect(counts.get('cluster-prod')).toEqual({ nodes: 2, pods: 3, deployments: 1 });
+ expect(counts.get('cluster-stg')).toEqual({ nodes: 1, pods: 1, deployments: 0 });
+ });
+
+ it('counts agent rows that report a kubernetes source as cluster nodes', () => {
+ const prod = cluster('cluster-prod', { clusterId: 'prod' });
+ const counts = buildKubernetesClusterChildCounts(
+ [
+ prod,
+ makeResource({
+ id: 'merged-node',
+ type: 'agent',
+ sources: ['agent', 'kubernetes'],
+ kubernetes: { clusterId: 'prod' },
+ }),
+ makeResource({
+ id: 'plain-agent',
+ type: 'agent',
+ sources: ['agent'],
+ kubernetes: { clusterId: 'prod' },
+ }),
+ ],
+ [prod],
+ );
+ expect(counts.get('cluster-prod')).toEqual({ nodes: 1, pods: 0, deployments: 0 });
+ });
+
+ it('ignores resources whose kubernetes.clusterId / clusterName matches no cluster row', () => {
+ const prod = cluster('cluster-prod', { clusterId: 'prod' });
+ const counts = buildKubernetesClusterChildCounts(
+ [
+ prod,
+ makeResource({ id: 'orphan-pod', type: 'pod', kubernetes: { clusterId: 'gone' } }),
+ makeResource({ id: 'no-cluster', type: 'pod', kubernetes: {} }),
+ ],
+ [prod],
+ );
+ expect(counts.get('cluster-prod')).toEqual({ nodes: 0, pods: 0, deployments: 0 });
+ });
+
+ it('returns an empty map when there are no clusters', () => {
+ expect(
+ buildKubernetesClusterChildCounts(
+ [makeResource({ id: 'p', type: 'pod', kubernetes: { clusterId: 'x' } })],
+ [],
+ ).size,
+ ).toBe(0);
+ });
+ });
});
diff --git a/frontend-modern/src/features/kubernetes/kubernetesPageModel.ts b/frontend-modern/src/features/kubernetes/kubernetesPageModel.ts
index 4dd33df898..55afd1f3ce 100644
--- a/frontend-modern/src/features/kubernetes/kubernetesPageModel.ts
+++ b/frontend-modern/src/features/kubernetes/kubernetesPageModel.ts
@@ -343,6 +343,72 @@ export type KubernetesPageModel = {
autoscaling: Resource[];
};
+export type KubernetesClusterChildCounts = {
+ nodes: number;
+ pods: number;
+ deployments: number;
+};
+
+const emptyKubernetesClusterChildCounts = (): KubernetesClusterChildCounts => ({
+ nodes: 0,
+ pods: 0,
+ deployments: 0,
+});
+
+const matchClusterFor = (
+ resource: Resource,
+ clusters: readonly Resource[],
+): Resource | undefined => {
+ const clusterId =
+ asTrimmedString(resource.kubernetes?.clusterId) ||
+ asTrimmedString(resource.kubernetes?.clusterName);
+ if (!clusterId) return undefined;
+ for (const cluster of clusters) {
+ const k = cluster.kubernetes;
+ if (!k) continue;
+ if (
+ asTrimmedString(k.clusterId) === clusterId ||
+ asTrimmedString(k.clusterName) === clusterId
+ ) {
+ return cluster;
+ }
+ }
+ return undefined;
+};
+
+// Walks the resource snapshot once and rolls per-cluster Nodes / Pods /
+// Deployments into a Map keyed by cluster row id. The Overview clusters
+// table calls this instead of computing counts inline, so the same
+// rollup is testable and reusable by other consumers (incident rollups,
+// future Overview "active issues" cards).
+export function buildKubernetesClusterChildCounts(
+ resources: readonly Resource[],
+ clusters: readonly Resource[],
+): Map<string, KubernetesClusterChildCounts> {
+ const counts = new Map<string, KubernetesClusterChildCounts>();
+ for (const cluster of clusters) {
+ counts.set(cluster.id, emptyKubernetesClusterChildCounts());
+ }
+ if (clusters.length === 0) return counts;
+
+ for (const resource of resources) {
+ const cluster = matchClusterFor(resource, clusters);
+ if (!cluster) continue;
+ const bucket = counts.get(cluster.id);
+ if (!bucket) continue;
+ if (resource.type === 'k8s-node') {
+ bucket.nodes += 1;
+ } else if (resource.type === 'agent' && resource.sources?.includes('kubernetes')) {
+ bucket.nodes += 1;
+ } else if (resource.type === 'pod') {
+ bucket.pods += 1;
+ } else if (resource.type === 'k8s-deployment') {
+ bucket.deployments += 1;
+ }
+ }
+ return counts;
+}
+
export function buildKubernetesPageModel(resources: Resource[]): KubernetesPageModel {
const k8sResources = resources.filter(isKubernetesPlatform);
const clusters = k8sResources.filter((resource) => resource.type === 'k8s-cluster');