Skip to content

Commit

Permalink
fix: available pod is negative
Browse files Browse the repository at this point in the history
Signed-off-by: renxiangyu <[email protected]>
  • Loading branch information
renxiangyu committed Nov 23, 2023
1 parent 1f147a2 commit cbf10ac
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/utils/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 || node.Spec.Unschedulable || !NodeReady(&node) {
continue
}
q := resource.MustParse("1")
podQuantity.Add(q)
}
Expand Down

0 comments on commit cbf10ac

Please sign in to comment.