Skip to content

Commit

Permalink
frontend: Add PodMetrics to the Pod list
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Dubenko <[email protected]>
  • Loading branch information
sniok committed Dec 10, 2024
1 parent 8ef5011 commit 0ece0f3
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion frontend/src/components/pod/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import React from 'react';
import { useTranslation } from 'react-i18next';
import { ApiError } from '../../lib/k8s/apiProxy';
import Pod from '../../lib/k8s/pod';
import { METRIC_REFETCH_INTERVAL_MS, PodMetrics } from '../../lib/k8s/PodMetrics';
import { parseCpu, parseRam, unparseCpu, unparseRam } from '../../lib/units';
import { timeAgo } from '../../lib/util';
import { useNamespaces } from '../../redux/filterSlice';
import { HeadlampEventType, useEventCallback } from '../../redux/headlampEventSlice';
Expand Down Expand Up @@ -62,6 +64,7 @@ function getReadinessGatesStatus(pods: Pod) {
export interface PodListProps {
pods: Pod[] | null;
error: ApiError | null;
metrics: PodMetrics[] | null;
hideColumns?: ('namespace' | 'restarts')[];
reflectTableInURL?: SimpleTableProps['reflectInURL'];
noNamespaceFilter?: boolean;
Expand All @@ -72,6 +75,7 @@ export function PodListRenderer(props: PodListProps) {
const {
pods,
error,
metrics,
hideColumns = [],
reflectTableInURL = 'pods',
noNamespaceFilter,
Expand Down Expand Up @@ -117,6 +121,41 @@ export function PodListRenderer(props: PodListProps) {
getValue: pod => pod.getDetailedStatus().reason,
render: makePodStatusLabel,
},
{
id: 'cpu',
label: t('CPU'),
gridTemplate: 'min-content',
getValue: pod => {
const metric = metrics?.find(it => it.getName() === pod.getName());
if (!metric) return;

const cpuUsage =
metric?.jsonData.containers
.map(it => parseCpu(it.usage.cpu))
.reduce((a, b) => a + b, 0) ?? 0;

const { value, unit } = unparseCpu(String(cpuUsage));

return `${value} ${unit}`;
},
},
{
id: 'memory',
label: t('Memory'),
getValue: pod => {
const metric = metrics?.find(it => it.getName() === pod.getName());
if (!metric) return;

const memoryUsage =
metric?.jsonData.containers
.map(it => parseRam(it.usage.memory))
.reduce((a, b) => a + b, 0) ?? 0;

const { value, unit } = unparseRam(memoryUsage);

return `${value} ${unit}`;
},
},
{
id: 'ip',
label: t('glossary|IP'),
Expand Down Expand Up @@ -218,6 +257,10 @@ export function PodListRenderer(props: PodListProps) {

export default function PodList() {
const { items, error, clusterErrors } = Pod.useList({ namespace: useNamespaces() });
const { items: podMetrics, clusterErrors: metricsClusterErrors } = PodMetrics.useList({
namespace: useNamespaces(),
refetchInterval: METRIC_REFETCH_INTERVAL_MS,
});

const dispatchHeadlampEvent = useEventCallback(HeadlampEventType.LIST_VIEW);

Expand All @@ -230,6 +273,12 @@ export default function PodList() {
}, [items, error]);

return (
<PodListRenderer pods={items} error={error} clusterErrors={clusterErrors} reflectTableInURL />
<PodListRenderer
pods={items}
error={error}
metrics={podMetrics}
clusterErrors={{ ...metricsClusterErrors, ...clusterErrors }}
reflectTableInURL
/>
);
}

0 comments on commit 0ece0f3

Please sign in to comment.