Skip to content

Commit

Permalink
Consider Pods count in CountIn()
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Iwai <[email protected]>
  • Loading branch information
tenzen-y committed Feb 23, 2025
1 parent 651e8de commit 2843808
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
21 changes: 16 additions & 5 deletions pkg/resources/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package resources

import (
"maps"
"math"
"strings"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -103,17 +104,27 @@ func ResourceQuantityString(name corev1.ResourceName, v int64) string {
return rq.String()
}

func (req Requests) CountIn(capacity Requests) int32 {
func (r Requests) CountIn(capacity Requests) int32 {
var result *int32
for rName, rValue := range req {
capacity, found := capacity[rName]
// If the capacity does not have Pods count, this assumes the unlimited Pods count capacity.
if _, ok := capacity[corev1.ResourcePods]; !ok {
capacity[corev1.ResourcePods] = math.MaxInt32
}
for rName, rValue := range r {
rCap, found := capacity[rName]
if !found {
return 0
}
// find the minimum count matching all the resource quota.
count := int32(capacity / rValue)
var count int32
switch rName {
case corev1.ResourcePods:
count = max(int32(rCap-rValue), 0)
default:
count = int32(rCap / rValue)
}
if result == nil || count < *result {
result = ptr.To(count)
result = ptr.To(min(count, int32(capacity[corev1.ResourcePods])))
}
}
return ptr.Deref(result, 0)
Expand Down
28 changes: 28 additions & 0 deletions pkg/resources/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ func TestCountIn(t *testing.T) {
},
wantResult: 2,
},
"requests have Pods count and it is less than capacity": {
requests: Requests{
corev1.ResourcePods: 1,
},
capacity: Requests{
corev1.ResourcePods: 5,
},
wantResult: 4,
},
"requests have Pods count and it is more than capacity": {
requests: Requests{
corev1.ResourcePods: 2,
},
capacity: Requests{
corev1.ResourcePods: 1,
},
wantResult: 0,
},
"requests have Pods count and capacity doesn't have Pods count": {
requests: Requests{
corev1.ResourceCPU: 4,
corev1.ResourcePods: 4,
},
capacity: Requests{
corev1.ResourceCPU: 8,
},
wantResult: 2,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
Expand Down

0 comments on commit 2843808

Please sign in to comment.