Skip to content

Commit 7a7afe4

Browse files
committed
fix: resolve index out of range panic in fetchContainerInfo for Ascend devices
- Pre-allocate bizContainerDevices based on actual container count to prevent out-of-bounds access when annotation device count differs from pod container count - Merge devices from all device types per container instead of overwriting, which previously caused device data loss for multi-device-type pods - Add container index boundary check in DecodePodDevices Ascend branch to align with NVIDIA/Hygon/Metax device handling Fixes #94
1 parent f60dbed commit 7a7afe4

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

server/internal/data/pod.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,20 @@ func (r *podRepo) fetchContainerInfo(pod *corev1.Pod) []*biz.Container {
112112
if err != nil {
113113
return containers
114114
}
115-
bizContainerDevices := []biz.ContainerDevices{}
115+
// Merge devices from all device types per container index.
116+
// pdevices: map[deviceType]PodSingleDevice([]ContainerDevices)
117+
numContainers := len(pod.Spec.Containers)
118+
bizContainerDevices := make([]biz.ContainerDevices, numContainers)
116119
for _, pds := range pdevices {
117-
copier.Copy(&bizContainerDevices, pds)
120+
var bizPds biz.PodSingleDevice
121+
copier.Copy(&bizPds, pds)
122+
for i, cd := range bizPds {
123+
if i < numContainers {
124+
bizContainerDevices[i] = append(bizContainerDevices[i], cd...)
125+
}
126+
}
118127
}
119-
if len(bizContainerDevices) < 1 {
128+
if len(pdevices) == 0 {
120129
return containers
121130
}
122131

server/internal/provider/util/util.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,14 @@ func DecodePodDevices(pod *corev1.Pod, log *log.Helper) (PodDevices, error) {
317317
pd[devType] = make(PodSingleDevice, 0)
318318
switch devType {
319319
case AscendGPUDevice, Ascend310PGPUDevice:
320-
for _, s := range strings.Split(str, OnePodMultiContainerSplitSymbol) {
320+
for i, s := range strings.Split(str, OnePodMultiContainerSplitSymbol) {
321+
if i >= len(pod.Spec.Containers) {
322+
break
323+
}
324+
if s == "" {
325+
pd[devType] = append(pd[devType], ContainerDevices{})
326+
continue
327+
}
321328
cd, err := DecodeNpuContainerDevices(s)
322329
if err != nil {
323330
return PodDevices{}, nil

0 commit comments

Comments
 (0)