Skip to content

Commit

Permalink
feat: Improve pods statistics (#3066)
Browse files Browse the repository at this point in the history
* first implementaion

* remove console.log

* fix import
  • Loading branch information
dbadura authored Jul 16, 2024
1 parent 38bd1bb commit f795d87
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions public/i18n/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cluster-overview:
pods-overview: 'Pods Overview'
total-pods: 'Total Pods'
healthy-pods: 'Healthy Pods'
pending-pods: 'Pending Pods'
failing-pods: 'Failing Pods'
deployments-overview: 'Deployments Overview'
total-deployments: 'Total Deployments'
Expand Down
20 changes: 17 additions & 3 deletions src/components/Clusters/views/ClusterOverview/ClusterStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
import {
getHealthyDaemonsets,
getHealthyReplicasCount,
getHealthyStatusesCount,
getStatusesPodCount,
PodStatusCounterKey,
} from 'resources/Namespaces/NamespaceWorkloads/NamespaceWorkloadsHelpers';
import { roundTwoDecimals } from 'shared/utils/helpers';
import './ClusterStats.scss';
Expand Down Expand Up @@ -89,7 +90,16 @@ export default function ClusterStats({ nodesData }) {
}
}, [servicesData]);

const healthyPods = getHealthyStatusesCount(podsData);
const statusPodsData = getStatusesPodCount(podsData);
const healthyPods = statusPodsData.has(PodStatusCounterKey.Healthy)
? statusPodsData.get(PodStatusCounterKey.Healthy)
: 0;
const pendingPods = statusPodsData.has(PodStatusCounterKey.Pending)
? statusPodsData.get(PodStatusCounterKey.Pending)
: 0;
const failedPods = statusPodsData.has(PodStatusCounterKey.Failed)
? statusPodsData.get(PodStatusCounterKey.Failed)
: 0;
const healthyDeployments = getHealthyReplicasCount(deploymentsData);
const healthyDaemonsets = getHealthyDaemonsets(daemonsetsData);
const healthyStatefulsets = getHealthyReplicasCount(statefulsetsData);
Expand Down Expand Up @@ -166,9 +176,13 @@ export default function ClusterStats({ nodesData }) {
title: t('cluster-overview.statistics.healthy-pods'),
value: healthyPods,
},
{
title: t('cluster-overview.statistics.pending-pods'),
value: pendingPods,
},
{
title: t('cluster-overview.statistics.failing-pods'),
value: podsData.length - healthyPods,
value: failedPods,
},
]}
/>
Expand Down
21 changes: 18 additions & 3 deletions src/resources/Namespaces/NamespaceWorkloads/NamespaceWorkloads.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { useGetList } from 'shared/hooks/BackendAPI/useGet';

import {
getHealthyReplicasCount,
getHealthyStatusesCount,
getStatusesPodCount,
PodStatusCounterKey,
} from './NamespaceWorkloadsHelpers';
import { CountingCard } from 'shared/components/CountingCard/CountingCard';

Expand All @@ -30,7 +31,17 @@ export function NamespaceWorkloads({ namespace }) {
},
);

const healthyPods = getHealthyStatusesCount(podsData);
const statusPodsData = getStatusesPodCount(podsData);
const healthyPods = statusPodsData.has(PodStatusCounterKey.Healthy)
? statusPodsData.get(PodStatusCounterKey.Healthy)
: 0;
const pendingPods = statusPodsData.has(PodStatusCounterKey.Pending)
? statusPodsData.get(PodStatusCounterKey.Pending)
: 0;
const failedPods = statusPodsData.has(PodStatusCounterKey.Failed)
? statusPodsData.get(PodStatusCounterKey.Failed)
: 0;

const healthyDeployments = getHealthyReplicasCount(deploymentsData);

return (
Expand All @@ -51,9 +62,13 @@ export function NamespaceWorkloads({ namespace }) {
title: t('cluster-overview.statistics.healthy-pods'),
value: healthyPods,
},
{
title: t('cluster-overview.statistics.pending-pods'),
value: pendingPods,
},
{
title: t('cluster-overview.statistics.failing-pods'),
value: podsData.length - healthyPods,
value: failedPods,
},
]}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
import { calculatePodState } from 'resources/Pods/PodStatus';

export function getHealthyReplicasCount(resource) {
return resource?.filter(r => r.status.replicas === r.status.readyReplicas)
?.length;
}

export const PodStatusCounterKey = {
Pending: 'pending',
Healthy: 'healthy',
Failed: 'failed',
};

export function getStatusesPodCount(pods) {
if (!pods) {
return new Map();
}
const statusData = Map.groupBy(pods, pod => {
const podState = calculatePodState(pod);
return getPodState(podState.status);
});

statusData.forEach((value, key, map) => map.set(key, value.length));
return statusData;
}

function getPodState(status) {
switch (status) {
case 'Running':
case 'Succeeded':
case 'Completed':
case 'Terminated':
return 'healthy';
case 'Pending':
case 'Terminating':
case 'PodInitializing':
case 'ContainerCreating':
case 'Unknown':
return 'pending';
default:
return 'failed';
}
}

export function getHealthyStatusesCount(pods) {
const successStatuses = ['running', 'succeeded'];
return pods?.filter(p =>
Expand Down

0 comments on commit f795d87

Please sign in to comment.