Skip to content

Commit

Permalink
Fix: Include restartable init containers in Pod utilization calc
Browse files Browse the repository at this point in the history
  • Loading branch information
azylinski committed Oct 25, 2023
1 parent 57a4cef commit 3c64cc6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cluster-autoscaler/simulator/utilization/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ func CalculateUtilizationOfResource(nodeInfo *schedulerframework.NodeInfo, resou
if drain.IsPodLongTerminating(podInfo.Pod, currentTime) {
continue
}
for _, container := range podInfo.Pod.Spec.Containers {
// calculate all regular containers and restartable init containers requests
containers := append([]apiv1.Container{}, podInfo.Pod.Spec.Containers...)
for _, c := range podInfo.Pod.Spec.InitContainers {
if c.RestartPolicy != nil && *c.RestartPolicy == apiv1.ContainerRestartPolicyAlways {
containers = append(containers, c)
}
}
for _, container := range containers {
if resourceValue, found := container.Resources.Requests[resourceName]; found {
podsRequest.Add(resourceValue)
}
Expand Down
27 changes: 26 additions & 1 deletion cluster-autoscaler/simulator/utilization/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
. "k8s.io/autoscaler/cluster-autoscaler/utils/test"
"k8s.io/kubernetes/pkg/kubelet/types"
Expand All @@ -33,6 +34,19 @@ func TestCalculate(t *testing.T) {
testTime := time.Date(2020, time.December, 18, 17, 0, 0, 0, time.UTC)
pod := BuildTestPod("p1", 100, 200000)
pod2 := BuildTestPod("p2", -1, -1)
podWithInitContainers := BuildTestPod("p-init", 100, 200000)
restartAlways := apiv1.ContainerRestartPolicyAlways
podWithInitContainers.Spec.InitContainers = []apiv1.Container{
{
Resources: apiv1.ResourceRequirements{
Requests: apiv1.ResourceList{
apiv1.ResourceCPU: *resource.NewMilliQuantity(50, resource.DecimalSI),
apiv1.ResourceMemory: *resource.NewQuantity(100000, resource.DecimalSI),
},
},
RestartPolicy: &restartAlways,
},
}

node := BuildTestNode("node1", 2000, 2000000)
SetNodeReadyState(node, true, time.Time{})
Expand All @@ -42,14 +56,25 @@ func TestCalculate(t *testing.T) {
utilInfo, err := Calculate(nodeInfo, false, false, gpuConfig, testTime)
assert.NoError(t, err)
assert.InEpsilon(t, 2.0/10, utilInfo.Utilization, 0.01)
assert.Equal(t, 0.1, utilInfo.CpuUtil)

node2 := BuildTestNode("node1", 2000, -1)
node2 := BuildTestNode("node2", 2000, -1)
nodeInfo = newNodeInfo(node2, pod, pod, pod2)

gpuConfig = GetGpuConfigFromNode(nodeInfo.Node())
_, err = Calculate(nodeInfo, false, false, gpuConfig, testTime)
assert.Error(t, err)

node3 := BuildTestNode("node3", 2000, 2000000)
SetNodeReadyState(node3, true, time.Time{})
nodeInfo = newNodeInfo(node3, pod, podWithInitContainers, pod2)

gpuConfig = GetGpuConfigFromNode(nodeInfo.Node())
utilInfo, err = Calculate(nodeInfo, false, false, gpuConfig, testTime)
assert.NoError(t, err)
assert.InEpsilon(t, 2.5/10, utilInfo.Utilization, 0.01)
assert.Equal(t, 0.125, utilInfo.CpuUtil)

daemonSetPod3 := BuildTestPod("p3", 100, 200000)
daemonSetPod3.OwnerReferences = GenerateOwnerReferences("ds", "DaemonSet", "apps/v1", "")

Expand Down

0 comments on commit 3c64cc6

Please sign in to comment.