Skip to content

Commit

Permalink
CA: fix a nil map write in NodeInfo.AddPod()
Browse files Browse the repository at this point in the history
If the NodeInfo is created via WrapSchedulerNodeInfo with nil
podExtraInfos, subsequent AddPod() calls panic on trying to add
extra info for the pod.
  • Loading branch information
towca committed Dec 13, 2024
1 parent 756db6a commit 410bd7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cluster-autoscaler/simulator/framework/infos.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func NewNodeInfo(node *apiv1.Node, slices []*resourceapi.ResourceSlice, pods ...

// WrapSchedulerNodeInfo wraps a *schedulerframework.NodeInfo into an internal *NodeInfo.
func WrapSchedulerNodeInfo(schedNodeInfo *schedulerframework.NodeInfo, slices []*resourceapi.ResourceSlice, podExtraInfos map[types.UID]PodExtraInfo) *NodeInfo {
if podExtraInfos == nil {
podExtraInfos = map[types.UID]PodExtraInfo{}
}
return &NodeInfo{
schedNodeInfo: schedNodeInfo,
podsExtraInfo: podExtraInfos,
Expand Down
23 changes: 22 additions & 1 deletion cluster-autoscaler/simulator/framework/infos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestNodeInfo(t *testing.T) {
test.BuildTestPod("regular-pod-1", 100, 16),
test.BuildTestPod("regular-pod-2", 100, 16),
}
extraPod := test.BuildTestPod("extra-pod", 1, 1)
schedulerNodeInfo := newSchedNodeInfo(node, pods)
slices := []*resourceapi.ResourceSlice{
{
Expand Down Expand Up @@ -169,9 +170,29 @@ func TestNodeInfo(t *testing.T) {
wantLocalResourceSlices: slices,
wantPods: testPodInfos([]*apiv1.Pod{pods[1], pods[3], pods[5]}, true),
},
{
testName: "wrapping via WrapSchedulerNodeInfo and adding more pods",
modFn: func(info *schedulerframework.NodeInfo) *NodeInfo {
result := WrapSchedulerNodeInfo(info, nil, nil)
result.AddPod(testPodInfos([]*apiv1.Pod{extraPod}, false)[0])
return result
},
wantSchedNodeInfo: newSchedNodeInfo(node, append(pods, extraPod)),
wantPods: testPodInfos(append(pods, extraPod), false),
},
{
testName: "wrapping via WrapSchedulerNodeInfo and adding more pods using DRA",
modFn: func(info *schedulerframework.NodeInfo) *NodeInfo {
result := WrapSchedulerNodeInfo(info, nil, nil)
result.AddPod(testPodInfos([]*apiv1.Pod{extraPod}, true)[0])
return result
},
wantSchedNodeInfo: newSchedNodeInfo(node, append(pods, extraPod)),
wantPods: append(testPodInfos(pods, false), testPodInfos([]*apiv1.Pod{extraPod}, true)...),
},
} {
t.Run(tc.testName, func(t *testing.T) {
wrappedNodeInfo := tc.modFn(schedulerNodeInfo)
wrappedNodeInfo := tc.modFn(schedulerNodeInfo.Snapshot())

// Assert that the scheduler NodeInfo object is as expected.
nodeInfoCmpOpts := []cmp.Option{
Expand Down

0 comments on commit 410bd7c

Please sign in to comment.