From f795d87ac5d0ac404f4177338d5da0adeb21793d Mon Sep 17 00:00:00 2001 From: Damian Badura <45110612+dbadura@users.noreply.github.com> Date: Tue, 16 Jul 2024 14:00:39 +0200 Subject: [PATCH] feat: Improve pods statistics (#3066) * first implementaion * remove console.log * fix import --- public/i18n/en.yaml | 1 + .../views/ClusterOverview/ClusterStats.js | 20 ++++++++-- .../NamespaceWorkloads/NamespaceWorkloads.js | 21 ++++++++-- .../NamespaceWorkloadsHelpers.js | 39 +++++++++++++++++++ 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/public/i18n/en.yaml b/public/i18n/en.yaml index dde4a53cb5..970fc3a3c8 100644 --- a/public/i18n/en.yaml +++ b/public/i18n/en.yaml @@ -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' diff --git a/src/components/Clusters/views/ClusterOverview/ClusterStats.js b/src/components/Clusters/views/ClusterOverview/ClusterStats.js index 4ef38817a6..e2ae482659 100644 --- a/src/components/Clusters/views/ClusterOverview/ClusterStats.js +++ b/src/components/Clusters/views/ClusterOverview/ClusterStats.js @@ -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'; @@ -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); @@ -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, }, ]} /> diff --git a/src/resources/Namespaces/NamespaceWorkloads/NamespaceWorkloads.js b/src/resources/Namespaces/NamespaceWorkloads/NamespaceWorkloads.js index 547988b68f..39587523cc 100644 --- a/src/resources/Namespaces/NamespaceWorkloads/NamespaceWorkloads.js +++ b/src/resources/Namespaces/NamespaceWorkloads/NamespaceWorkloads.js @@ -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'; @@ -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 ( @@ -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, }, ]} /> diff --git a/src/resources/Namespaces/NamespaceWorkloads/NamespaceWorkloadsHelpers.js b/src/resources/Namespaces/NamespaceWorkloads/NamespaceWorkloadsHelpers.js index 22d3cc4b92..2e72432885 100644 --- a/src/resources/Namespaces/NamespaceWorkloads/NamespaceWorkloadsHelpers.js +++ b/src/resources/Namespaces/NamespaceWorkloads/NamespaceWorkloadsHelpers.js @@ -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 =>