Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

koordlet: fix podresources not found #2344

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions pkg/koordlet/statesinformer/impl/states_pod_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ func (p *podResourcesServer) List(ctx context.Context, request *podresourcesapi.
if err != nil {
return nil, err
}
klog.V(5).Infof("pod resources proxy List original response: %v", response)
allPods, err := p.kubeletStub.GetAllPods()
if err != nil {
return nil, err
}
fillPodDevicesAllocatedByKoord(response, &allPods)
klog.V(5).Infof("pod resources proxy List response: %v", response)
return response, nil
}

Expand All @@ -155,11 +157,12 @@ func fillPodDevicesAllocatedByKoord(response *podresourcesapi.ListPodResourcesRe
}

podsMap := make(map[string]*corev1.Pod, len(allPods.Items))
for _, item := range allPods.Items {
podsMap[util.GetNamespacedName(item.Namespace, item.Name)] = &item
for i, item := range allPods.Items {
podsMap[util.GetNamespacedName(item.Namespace, item.Name)] = &allPods.Items[i]
}

for _, podResource := range response.PodResources {
for i := range response.PodResources {
podResource := response.PodResources[i]
if podResource == nil || len(podResource.Containers) == 0 {
continue
}
Expand All @@ -175,14 +178,19 @@ func fillPodDevicesAllocatedByKoord(response *podresourcesapi.ListPodResourcesRe
for deviceType, deviceAllocation := range deviceAllocations {
var deviceIDs []string
for _, device := range deviceAllocation {
if device.Extension != nil {
for _, vf := range device.Extension.VirtualFunctions {
deviceIDs = append(deviceIDs, vf.BusID)
}
continue
}
deviceIDs = append(deviceIDs, device.ID)
}
podResource.Containers[0].Devices = append(podResource.Containers[0].Devices, &podresourcesapi.ContainerDevices{
ResourceName: deviceTypeToResourceName[deviceType],
DeviceIds: deviceIDs,
})
}
break
}
}

Expand Down
130 changes: 129 additions & 1 deletion pkg/koordlet/statesinformer/impl/states_pod_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,134 @@ func TestFillPodDevicesAllocatedByKoord(t *testing.T) {
podList *corev1.PodList
expectedResult *podresourcesapi.ListPodResourcesResponse
}{
{
name: "MatchingPodWithDeviceAllocationsWithVf",
response: &podresourcesapi.ListPodResourcesResponse{
PodResources: []*podresourcesapi.PodResources{
{
Name: "test-pod",
Namespace: "test-namespace",
Containers: []*podresourcesapi.ContainerResources{
{
Name: "test-container",
},
},
},
},
},
podList: &corev1.PodList{
Items: []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: "test-namespace",
Annotations: map[string]string{
apiext.AnnotationDeviceAllocated: `{"gpu":[{"minor":0,"resources":{"koordinator.sh/gpu-core":"100","koordinator.sh/gpu-memory":"23040Mi","koordinator.sh/gpu-memory-ratio":"100"},"id":"0"}],"rdma":[{"minor":0,"resources":{"koordinator.sh/rdma":"1"},"id":"0000:01:00.0","extension":{"vfs":[{"minor":-1,"busID":"0000:01:00.2"}]}}]}`,
},
},
},
},
},
expectedResult: &podresourcesapi.ListPodResourcesResponse{
PodResources: []*podresourcesapi.PodResources{
{
Name: "test-pod",
Namespace: "test-namespace",
Containers: []*podresourcesapi.ContainerResources{
{
Name: "test-container",
Devices: []*podresourcesapi.ContainerDevices{
{
ResourceName: string(apiext.ResourceNvidiaGPU),
DeviceIds: []string{"0"},
},
{
ResourceName: string(apiext.ResourceRDMA),
DeviceIds: []string{"0000:01:00.2"},
},
},
},
},
},
},
},
},
{
name: "MatchingPodWithDeviceAllocationsWithVfAndMMultiplePod",
response: &podresourcesapi.ListPodResourcesResponse{
PodResources: []*podresourcesapi.PodResources{
{
Name: "test-pod-1",
Namespace: "test-namespace",
Containers: []*podresourcesapi.ContainerResources{
{
Name: "test-container",
},
},
},
{
Name: "test-pod",
Namespace: "test-namespace",
Containers: []*podresourcesapi.ContainerResources{
{
Name: "test-container",
},
},
},
},
},
podList: &corev1.PodList{
Items: []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod-1",
Namespace: "test-namespace",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: "test-namespace",
Annotations: map[string]string{
apiext.AnnotationDeviceAllocated: `{"gpu":[{"minor":0,"resources":{"koordinator.sh/gpu-core":"100","koordinator.sh/gpu-memory":"23040Mi","koordinator.sh/gpu-memory-ratio":"100"},"id":"0"}],"rdma":[{"minor":0,"resources":{"koordinator.sh/rdma":"1"},"id":"0000:01:00.0","extension":{"vfs":[{"minor":-1,"busID":"0000:01:00.2"}]}}]}`,
},
},
},
},
},
expectedResult: &podresourcesapi.ListPodResourcesResponse{
PodResources: []*podresourcesapi.PodResources{
{
Name: "test-pod-1",
Namespace: "test-namespace",
Containers: []*podresourcesapi.ContainerResources{
{
Name: "test-container",
},
},
},
{
Name: "test-pod",
Namespace: "test-namespace",
Containers: []*podresourcesapi.ContainerResources{
{
Name: "test-container",
Devices: []*podresourcesapi.ContainerDevices{
{
ResourceName: string(apiext.ResourceNvidiaGPU),
DeviceIds: []string{"0"},
},
{
ResourceName: string(apiext.ResourceRDMA),
DeviceIds: []string{"0000:01:00.2"},
},
},
},
},
},
},
},
},
{
name: "MatchingPodWithDeviceAllocations",
response: &podresourcesapi.ListPodResourcesResponse{
Expand Down Expand Up @@ -168,7 +296,7 @@ func TestFillPodDevicesAllocatedByKoord(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fillPodDevicesAllocatedByKoord(test.response, test.podList)
assert.Equal(t, test.response, test.expectedResult)
assert.Equal(t, test.expectedResult, test.response)
})
}
}
Loading