From 3b9baf86a24771f2064576c1cc08417d533a3a03 Mon Sep 17 00:00:00 2001 From: renxiangyu Date: Wed, 22 Nov 2023 18:02:40 +0800 Subject: [PATCH] fix: available pod is negative Signed-off-by: renxiangyu --- pkg/utils/resources.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/utils/resources.go b/pkg/utils/resources.go index 13fdee5df..e6cb4cfc4 100644 --- a/pkg/utils/resources.go +++ b/pkg/utils/resources.go @@ -13,7 +13,7 @@ const ( func CalculateClusterResources(nodes *corev1.NodeList, pods *corev1.PodList) corev1.ResourceList { base := GetNodesTotalResources(nodes) reqs, _ := GetPodsTotalRequestsAndLimits(pods) - podNums := GetUsedPodNums(pods) + podNums := GetUsedPodNums(pods, nodes) SubResourceList(base, reqs) SubResourceList(base, podNums) return base @@ -78,14 +78,22 @@ func GetPodsTotalRequestsAndLimits(podList *corev1.PodList) (reqs corev1.Resourc return } -func GetUsedPodNums(podList *corev1.PodList) (res corev1.ResourceList) { +func GetUsedPodNums(podList *corev1.PodList, nodes *corev1.NodeList) (res corev1.ResourceList) { podQuantity := resource.Quantity{} res = corev1.ResourceList{} + nodeMap := map[string]corev1.Node{} + for _, item := range nodes.Items { + nodeMap[item.Name] = item + } for _, p := range podList.Items { pod := p if IsVirtualPod(&pod) { continue } + node, exists := nodeMap[pod.Spec.NodeName] + if exists != true || node.Spec.Unschedulable || !NodeReady(&node) { + continue + } q := resource.MustParse("1") podQuantity.Add(q) }