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.

23.2 KB
Raw Blame
import { Show, createMemo, type Component, type JSX } from 'solid-js';
import { StatusDot } from '@/components/shared/StatusDot';
import { StatusIndicatorBadge } from '@/components/shared/StatusIndicatorBadge';
import { ResponsiveMetricCell } from '@/components/shared/responsive';
import { StackedMemoryBar } from '@/components/Workloads/StackedMemoryBar';
import { TableCell, TableRow } from '@/components/shared/Table';
import { ResourceNameWithWebInterfaceLink } from '@/components/shared/WebInterfaceLink';
import { asTrimmedString } from '@/utils/stringUtils';
import { getUnifiedResourceAlertStyles } from '@/utils/alerts';
import { useWebSocket } from '@/contexts/appRuntime';
import { useAlertsActivation } from '@/stores/alertsActivation';
import { unifiedPlatformOverrideIdCandidates } from '@/features/alerts/alertOverridesModel';
import { buildMetricKeyForUnifiedResource } from '@/utils/metricsKeys';
import {
  PlatformWindowedRows,
  PLATFORM_HEALTH_FILTER_OPTIONS,
  PlatformSortableTableHead,
  PlatformTableMetricFallback,
  PlatformTableEmptyState,
  PlatformTableShell,
  PlatformTableToolbar,
  PlatformResponsiveTableLabel,
  createPlatformTableFilterState,
  createPlatformTableSortState,
  formatPlatformTableBytesValue,
  formatPlatformTableTextValue,
  formatPlatformTableUptimeValue,
  getPlatformTableFiniteMetric,
  getPlatformTableCellClassForKind,
  type PlatformTableSortValue,
  withPlatformStatusCounts,
} from '@/features/platformPage/sharedPlatformPage';
import {
  PlatformResourceDetailToggleButton,
  PlatformResourceDetailTableRow,
  createPlatformResourceDetailState,
  createPlatformResourceLabelResolver,
  getPlatformResourceDetailRowClass,
} from '@/features/platformPage/PlatformResourceDetailTableRow';
import type { Resource } from '@/types/resource';
import {
  compareKubernetesNodes,
  filterKubernetesResources,
  getKubernetesNodeAttentionRank,
  kubernetesClusterLabel,
  mapKubernetesNodeStatus,
  type KubernetesResourceStatusFilter,
} from './kubernetesPageModel';

// Kubernetes nodes carry richer Kubelet/runtime metadata than a generic
// Pulse Agent — kubelet version, container runtime, roles
// (control-plane/worker), ready state, pod capacity. They're a hybrid
// row in the canonical model (the registry merges the K8s node onto
// the linked agent host), so the generic infrastructure table renders
// the agent metrics fine but omits the K8s context that matters to the
// cluster operator. This bespoke table reuses canonical shared
// primitives and surfaces the Kubelet-native columns alongside the
// canonical bar treatment (ResponsiveMetricCell / StackedMemoryBar) so
// the Overview stack reads as one consistent surface alongside the
// Docker / Proxmox / vSphere host tables.

const formatRoles = (roles: string[] | undefined): string => {
  if (!roles || roles.length === 0) return '—';
  return roles.map((role) => role.replace('node-role.kubernetes.io/', '')).join(', ');
};

const KUBERNETES_NODE_SORT_KEYS = [
  'node',
  'status',
  'cluster',
  'roles',
  'kubelet',
  'runtime',
  'cpu',
  'memory',
  'uptime',
  'capacity',
] as const;

type KubernetesNodeSortKey = (typeof KUBERNETES_NODE_SORT_KEYS)[number];

// Scalar per column that user-controlled sorting orders on. Capacity is a
// composite label (cores / bytes / pods); CPU core count is its dominant
// scalar, so that is what the column sorts by.
const getKubernetesNodeSortValue = (
  node: Resource,
  key: KubernetesNodeSortKey,
): PlatformTableSortValue => {
  switch (key) {
    case 'node':
      return asTrimmedString(node.name) || node.id;
    case 'status': {
      // Ascending puts NotReady first, then cordoned or degraded, then Ready.
      const variant = mapKubernetesNodeStatus(node).variant;
      return variant === 'danger' ? 0 : variant === 'warning' ? 1 : variant === 'success' ? 3 : 2;
    }
    case 'cluster':
      return kubernetesClusterLabel(node) || null;
    case 'roles': {
      const roles = formatRoles(node.kubernetes?.roles);
      return roles === '—' ? null : roles;
    }
    case 'kubelet':
      return asTrimmedString(node.kubernetes?.kubeletVersion) || null;
    case 'runtime':
      return asTrimmedString(node.kubernetes?.containerRuntimeVersion) || null;
    case 'cpu':
      return getPlatformTableFiniteMetric(node.cpu?.current) ?? null;
    case 'memory': {
      const total = getPlatformTableFiniteMetric(node.memory?.total) ?? 0;
      if (total > 0) {
        return ((getPlatformTableFiniteMetric(node.memory?.used) ?? 0) / total) * 100;
      }
      return getPlatformTableFiniteMetric(node.memory?.current) ?? null;
    }
    case 'uptime':
      return getPlatformTableFiniteMetric(node.uptime) ?? null;
    case 'capacity': {
      const cores = node.kubernetes?.capacityCpuCores;
      return typeof cores === 'number' && cores > 0 ? cores : null;
    }
    default:
      key satisfies never;
      return null;
  }
};

export const KubernetesNodesTable: Component<{
  resources: Resource[];
  emptyIcon: JSX.Element;
  emptyTitle: string;
  emptyDescription: string;
  title?: string;
  showToolbar?: boolean;
  /** Persisted sort slot; a second instance (the Overview attention list) needs its own. */
  sortStorageKey?: string;
}> = (props) => {
  const { activeAlerts } = useWebSocket();
  const alertsActivation = useAlertsActivation();
  const alertsEnabled = alertsActivation.detectionEnabled;
  const tableState = createPlatformTableFilterState({
    resources: () => props.resources,
    initialStatus: 'all' as KubernetesResourceStatusFilter,
    filter: filterKubernetesResources,
  });
  const drawer = createPlatformResourceDetailState({ idPrefix: 'kubernetes-node-drawer' });
  const resolveResourceLabel = createPlatformResourceLabelResolver(() => props.resources);
  // User-controlled sorting layered over the attention-first default: rows
  // are pre-sorted by attention (status and open alerts), then the status
  // compare, so a user sort keeps that order for ties and the table falls
  // straight back to it when the sort clears.
  const sort = createPlatformTableSortState({
    storageKey: props.sortStorageKey ?? 'kubernetesNodes',
    sortKeys: KUBERNETES_NODE_SORT_KEYS,
    descendingFirst: ['cpu', 'memory', 'uptime', 'capacity'],
  });
  const sortedRows = createMemo(() => {
    const rows = tableState.filtered();
    const enabled = alertsEnabled();
    const ranks = new Map(
      rows.map((node) => [node.id, getKubernetesNodeAttentionRank(node, activeAlerts, enabled)]),
    );
    return sort.sortRows(
      [...rows].sort(
        (a, b) => (ranks.get(a.id) ?? 2) - (ranks.get(b.id) ?? 2) || compareKubernetesNodes(a, b),
      ),
      getKubernetesNodeSortValue,
    );
  });

  return (
    <Show
      when={props.resources.length > 0}
      fallback={
        <PlatformTableEmptyState
          icon={props.emptyIcon}
          title={props.emptyTitle}
          description={props.emptyDescription}
        />
      }
    >
      <div class="space-y-3">
        <Show when={props.showToolbar !== false}>
          <PlatformTableToolbar
            search={tableState.search}
            onSearchChange={tableState.setSearch}
            searchPlaceholder="Search nodes"
            searchSuggestions={tableState.searchSuggestions}
            status={tableState.status()}
            onStatusChange={tableState.setStatus}
            statusOptions={withPlatformStatusCounts(
              PLATFORM_HEALTH_FILTER_OPTIONS,
              tableState.countForStatus,
            )}
            visible={tableState.visible()}
            total={tableState.total()}
            rowNoun="nodes"
          />
        </Show>

        <Show
          when={tableState.filtered().length > 0}
          fallback={
            <PlatformTableEmptyState
              icon={props.emptyIcon}
              title="No nodes match current filters"
              description="Adjust the search or status filter to see more nodes."
            />
          }
        >
          <PlatformTableShell
            title={props.title ?? 'Nodes'}
            tableClass="min-w-full table-fixed text-xs md:min-w-[1100px] 2xl:min-w-[1280px]"
            header={
              <>
                {/*
                    Desktop widths: Node gets headroom for cluster-style
                    names, Runtime gets room for "containerd://1.7.20"
                    -style values, Capacity gets room for "6 cores /
                    51.0 GB / 110 pods" strings, and the short-text columns
                    (Cluster, Roles, Kubelet, Uptime) trim accordingly. Wide
                    desktop gets extra room without forcing normal desktop
                    viewports to hide Capacity behind horizontal scroll.
                    The Status badge must not clip: Degraded and NotReady
                    need about 64px and Unschedulable about 89px. Status
                    takes its md room from CPU, whose bar never shows a
                    sublabel, so every other column keeps its width and
                    Memory keeps room for its used/total sublabel.
                    Unschedulable fits from a ~800px table and from 1280px,
                    where Kubelet, Runtime and Uptime appear. bandWidth sizes
                    the tablet band for each header and for Degraded at a
                    34rem table.
                  */}
                <PlatformSortableTableHead
                  kind="name"
                  sort={sort}
                  sortKey="node"
                  class="platform-table-mobile-w-30 md:w-[20%]"
                  bandWidth={30}
                >
                  Node
                </PlatformSortableTableHead>
                {/*
                  Status sits beside the name, as in kubectl get nodes: the
                  dot alone made "why is this node amber" a hover. On phones
                  the dot carries the state (and names it to assistive tech),
                  so the column is desktop-only.
                */}
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="status"
                  class="platform-table-phone-hidden md:w-[10.5%]"
                  bandWidth={14.6}
                >
                  Status
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="cluster"
                  class="platform-table-mobile-w-15 md:w-[8%]"
                  bandWidth={12.1}
                >
                  <PlatformResponsiveTableLabel compact="Clus" full="Cluster" />
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="roles"
                  class="platform-table-phone-hidden md:w-[8%]"
                  bandWidth={9.2}
                >
                  <PlatformResponsiveTableLabel compact="Role" full="Roles" />
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="kubelet"
                  class="hidden md:table-cell md:w-[7%]"
                >
                  Kubelet
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="text"
                  sort={sort}
                  sortKey="runtime"
                  class="hidden md:table-cell md:w-[12%]"
                >
                  Runtime
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="metric-bar"
                  sort={sort}
                  sortKey="cpu"
                  class="platform-table-mobile-w-15 md:w-[7.5%]"
                  bandWidth={9.2}
                >
                  CPU
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="metric-bar"
                  sort={sort}
                  sortKey="memory"
                  class="platform-table-mobile-w-15 md:w-[10%]"
                  bandWidth={11.8}
                >
                  <PlatformResponsiveTableLabel compact="Mem" full="Memory" />
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="numeric-value"
                  sort={sort}
                  sortKey="uptime"
                  class="hidden md:table-cell md:w-[6%]"
                >
                  Uptime
                </PlatformSortableTableHead>
                <PlatformSortableTableHead
                  kind="numeric-value"
                  sort={sort}
                  sortKey="capacity"
                  class="platform-table-mobile-w-10 md:w-[16%]"
                  bandWidth={13.1}
                >
                  <PlatformResponsiveTableLabel compact="Cap" full="Capacity" />
                </PlatformSortableTableHead>
              </>
            }
            body={
              <>
                <PlatformWindowedRows items={sortedRows} estimatedRowHeight={32}>
                  {(node) => {
                    const meta = () => node.kubernetes;
                    const name = () => asTrimmedString(node.name) || node.id;
                    const cluster = () => kubernetesClusterLabel(node) || '—';
                    const kubelet = () => formatPlatformTableTextValue(meta()?.kubeletVersion);
                    const runtime = () =>
                      formatPlatformTableTextValue(meta()?.containerRuntimeVersion);
                    const capacityLabel = () => {
                      const cores = meta()?.capacityCpuCores;
                      const mem = meta()?.capacityMemoryBytes;
                      const pods = meta()?.capacityPods;
                      const parts: string[] = [];
                      if (typeof cores === 'number' && cores > 0) parts.push(`${cores} cores`);
                      if (typeof mem === 'number' && mem > 0) {
                        parts.push(formatPlatformTableBytesValue(mem));
                      }
                      if (typeof pods === 'number' && pods > 0) parts.push(`${pods} pods`);
                      return parts.join(' / ') || '—';
                    };
                    const compactCapacityLabel = () => {
                      const cores = meta()?.capacityCpuCores;
                      if (typeof cores === 'number' && cores > 0) return `${cores} cores`;
                      return capacityLabel();
                    };
                    const indicator = () => mapKubernetesNodeStatus(node);
                    const metricsKey = () => buildMetricKeyForUnifiedResource(node);
                    const alertResourceIds = () => unifiedPlatformOverrideIdCandidates(node);
                    const cpuThresholds = () =>
                      alertsActivation.getMetricThresholds('kubernetes', 'cpu', alertResourceIds());
                    const memoryThresholds = () =>
                      alertsActivation.getMetricThresholds(
                        'kubernetes',
                        'memory',
                        alertResourceIds(),
                      );
                    const cpuPercent = () => getPlatformTableFiniteMetric(node.cpu?.current);
                    const memoryTotal = () => getPlatformTableFiniteMetric(node.memory?.total) ?? 0;
                    const memoryUsed = () => getPlatformTableFiniteMetric(node.memory?.used) ?? 0;
                    const memoryPercentOnly = () =>
                      memoryTotal() > 0
                        ? undefined
                        : getPlatformTableFiniteMetric(node.memory?.current);
                    const hasMemoryMetric = () =>
                      memoryTotal() > 0 || memoryPercentOnly() !== undefined;
                    const canRenderMetrics = () => indicator().variant !== 'muted';
                    const detailRowId = () => drawer.detailRowId(node);
                    const isExpanded = () => drawer.isExpanded(node);
                    // The same open alerts the node's drawer lists: a node that
                    // runs a Pulse agent has its alerts keyed on the agent and
                    // labelled with its hostname, so matching the row's id or
                    // display name finds none of them.
                    const nodeAlertStyles = createMemo(() =>
                      getUnifiedResourceAlertStyles(node, activeAlerts, alertsEnabled()),
                    );
                    const nodeAlertBg = () => {
                      const s = nodeAlertStyles();
                      if (!s.hasUnacknowledgedAlert) return '';
                      return s.severity === 'critical'
                        ? 'bg-red-50 dark:bg-red-950/25'
                        : 'bg-yellow-50 dark:bg-yellow-950/25';
                    };
                    return (
                      <>
                        <TableRow
                          class={`${getPlatformResourceDetailRowClass(isExpanded())} text-[11px] sm:text-xs ${
                            isExpanded() ? '' : nodeAlertBg()
                          }`}
                          data-kubernetes-node-row={node.id}
                          onClick={() => drawer.toggle(node)}
                        >
                          <TableCell class={getPlatformTableCellClassForKind('name')}>
                            <div class="flex min-w-0 items-center gap-2">
                              <PlatformResourceDetailToggleButton
                                expanded={isExpanded()}
                                resourceLabel={name()}
                                controlsId={detailRowId()}
                                onToggle={() => drawer.toggle(node)}
                              />
                              <StatusDot
                                size="sm"
                                variant={indicator().variant}
                                title={indicator().label}
                                ariaHidden
                              />
                              <ResourceNameWithWebInterfaceLink
                                name={name()}
                                url={node.customUrl}
                                class="min-w-0"
                                nameClass="truncate font-semibold text-base-content"
                              />
                              {/* Phones demote the Status column; announce the state there only. */}
                              <span class="sr-only platform-table-phone-only-inline">
                                {indicator().label}
                              </span>
                            </div>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} platform-table-phone-hidden`}
                          >
                            <Show
                              when={indicator().variant !== 'success'}
                              fallback={<span class="text-muted">{indicator().label}</span>}
                            >
                              <StatusIndicatorBadge
                                variant={indicator().variant}
                                label={indicator().label}
                                size="xs"
                              />
                            </Show>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} text-base-content`}
                          >
                            {cluster()}
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} platform-table-phone-hidden text-base-content`}
                          >
                            {formatRoles(meta()?.roles)}
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} hidden font-mono text-[11px] text-base-content md:table-cell`}
                          >
                            {kubelet()}
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('text')} hidden font-mono text-[11px] text-base-content md:table-cell`}
                          >
                            <span class="block truncate" title={runtime()}>
                              {runtime()}
                            </span>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('metric-bar')} w-[20%] md:w-auto`}
                          >
                            <ResponsiveMetricCell
                              class="w-full"
                              value={cpuPercent() ?? 0}
                              type="cpu"
                              resourceId={metricsKey()}
                              isRunning={canRenderMetrics() && cpuPercent() !== undefined}
                              showMobile={false}
                              thresholds={cpuThresholds()}
                            />
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('metric-bar')} w-[20%] md:w-auto`}
                          >
                            <Show
                              when={canRenderMetrics() && hasMemoryMetric()}
                              fallback={<PlatformTableMetricFallback />}
                            >
                              <StackedMemoryBar
                                used={memoryUsed()}
                                total={memoryTotal()}
                                percentOnly={memoryPercentOnly()}
                                thresholds={memoryThresholds()}
                              />
                            </Show>
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('numeric-value')} hidden text-base-content md:table-cell`}
                          >
                            {formatPlatformTableUptimeValue(node.uptime)}
                          </TableCell>
                          <TableCell
                            class={`${getPlatformTableCellClassForKind('numeric-value')} text-base-content tabular-nums`}
                          >
                            <span class="md:hidden">{compactCapacityLabel()}</span>
                            <span class="hidden md:inline">{capacityLabel()}</span>
                          </TableCell>
                        </TableRow>
                        <PlatformResourceDetailTableRow
                          resource={node}
                          statusIndicator={indicator()}
                          open={isExpanded()}
                          detailRowId={detailRowId()}
                          colSpan={10}
                          resolveResourceLabel={resolveResourceLabel}
                          onClose={() => drawer.close(node)}
                        />
                      </>
                    );
                  }}
                </PlatformWindowedRows>
              </>
            }
          />
        </Show>
      </div>
    </Show>
  );
};

export default KubernetesNodesTable;