Skip to content

Commit

Permalink
Merge pull request #806 from rxy0210/release_v0.5.5_test
Browse files Browse the repository at this point in the history
fix: calculate node resources(cherry pick)
  • Loading branch information
duanmengkk authored Jan 13, 2025
2 parents 6acb546 + cb54a58 commit a682ae9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
26 changes: 20 additions & 6 deletions pkg/utils/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,29 @@ const (
)

func CalculateClusterResources(nodes *corev1.NodeList, pods *corev1.PodList) corev1.ResourceList {
base := GetNodesTotalResources(nodes)
reqs, _ := GetPodsTotalRequestsAndLimits(pods)
unavailableNodes := GetNotReadyAndUnschedulableLeafNodes(nodes)
base := GetNodesTotalResources(nodes, unavailableNodes)
reqs, _ := GetPodsTotalRequestsAndLimits(pods, unavailableNodes)
podNums := GetUsedPodNums(pods, nodes)
SubResourceList(base, reqs)
SubResourceList(base, podNums)
return base
}

func GetNodesTotalResources(nodes *corev1.NodeList) (total corev1.ResourceList) {
func GetNotReadyAndUnschedulableLeafNodes(leafNodes *corev1.NodeList) (unavailableNodes map[string]corev1.Node) {
unavailableNodes = make(map[string]corev1.Node)
for i, n := range leafNodes.Items {
if n.Spec.Unschedulable || !NodeReady(&leafNodes.Items[i]) {
unavailableNodes[n.Name] = n
}
}
return unavailableNodes
}

func GetNodesTotalResources(nodes *corev1.NodeList, unavailableNodes map[string]corev1.Node) (total corev1.ResourceList) {
total = corev1.ResourceList{}
for i, n := range nodes.Items {
if n.Spec.Unschedulable || !NodeReady(&nodes.Items[i]) {
for _, n := range nodes.Items {
if _, ok := unavailableNodes[n.Name]; ok {
continue
}
for key, val := range n.Status.Allocatable {
Expand Down Expand Up @@ -50,7 +61,7 @@ func SubResourceList(base, list corev1.ResourceList) {

// GetPodsTotalRequestsAndLimits
// lifted from https://github.com/kubernetes/kubernetes/blob/v1.21.8/staging/src/k8s.io/kubectl/pkg/describe/describe.go#L4051
func GetPodsTotalRequestsAndLimits(podList *corev1.PodList) (reqs corev1.ResourceList, limits corev1.ResourceList) {
func GetPodsTotalRequestsAndLimits(podList *corev1.PodList, unavailableNodes map[string]corev1.Node) (reqs corev1.ResourceList, limits corev1.ResourceList) {
reqs, limits = corev1.ResourceList{}, corev1.ResourceList{}
if podList.Items != nil {
for _, p := range podList.Items {
Expand All @@ -61,6 +72,9 @@ func GetPodsTotalRequestsAndLimits(podList *corev1.PodList) (reqs corev1.Resourc
if pod.Status.Phase != corev1.PodRunning {
continue
}
if _, ok := unavailableNodes[pod.Spec.NodeName]; ok {
continue
}
podReqs, podLimits := v1resource.PodRequestsAndLimits(&pod)
for podReqName, podReqValue := range podReqs {
if value, ok := reqs[podReqName]; !ok {
Expand Down
6 changes: 4 additions & 2 deletions pkg/utils/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestGetNodesTotalResources(t *testing.T) {
name string
nodes *corev1.NodeList
expected corev1.ResourceList
notNodes map[string]corev1.Node
}{
{
name: "No nodes",
Expand Down Expand Up @@ -45,7 +46,7 @@ func TestGetNodesTotalResources(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GetNodesTotalResources(tt.nodes)
result := GetNodesTotalResources(tt.nodes, tt.notNodes)
for key, expectedValue := range tt.expected {
if result[key] != expectedValue {
t.Errorf("expected %s for %s, got %v", expectedValue.String(), key.String(), result[key])
Expand Down Expand Up @@ -108,6 +109,7 @@ func TestGetPodsTotalRequestsAndLimits(t *testing.T) {
podList *corev1.PodList
expectedReqs corev1.ResourceList
expectedLimits corev1.ResourceList
notNodes map[string]corev1.Node
}{
{
name: "No pods",
Expand Down Expand Up @@ -234,7 +236,7 @@ func TestGetPodsTotalRequestsAndLimits(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reqs, limits := GetPodsTotalRequestsAndLimits(tt.podList)
reqs, limits := GetPodsTotalRequestsAndLimits(tt.podList, tt.notNodes)
for key, expectedValue := range tt.expectedReqs {
if reqs[key] != expectedValue {
t.Errorf("expected %s for requests %s, got %v", expectedValue.String(), key.String(), reqs[key])
Expand Down

0 comments on commit a682ae9

Please sign in to comment.