Skip to content

Commit b95a9bf

Browse files
ZiMengShengwangjianyu.wjy
andauthored
koordlet: fix podresources not found (koordinator-sh#2344)
Signed-off-by: wangjianyu.wjy <[email protected]> Co-authored-by: wangjianyu.wjy <[email protected]>
1 parent af357ca commit b95a9bf

File tree

2 files changed

+141
-5
lines changed

2 files changed

+141
-5
lines changed

pkg/koordlet/statesinformer/impl/states_pod_resources.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ func (p *podResourcesServer) List(ctx context.Context, request *podresourcesapi.
140140
if err != nil {
141141
return nil, err
142142
}
143+
klog.V(5).Infof("pod resources proxy List original response: %v", response)
143144
allPods, err := p.kubeletStub.GetAllPods()
144145
if err != nil {
145146
return nil, err
146147
}
147148
fillPodDevicesAllocatedByKoord(response, &allPods)
149+
klog.V(5).Infof("pod resources proxy List response: %v", response)
148150
return response, nil
149151
}
150152

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

157159
podsMap := make(map[string]*corev1.Pod, len(allPods.Items))
158-
for _, item := range allPods.Items {
159-
podsMap[util.GetNamespacedName(item.Namespace, item.Name)] = &item
160+
for i, item := range allPods.Items {
161+
podsMap[util.GetNamespacedName(item.Namespace, item.Name)] = &allPods.Items[i]
160162
}
161163

162-
for _, podResource := range response.PodResources {
164+
for i := range response.PodResources {
165+
podResource := response.PodResources[i]
163166
if podResource == nil || len(podResource.Containers) == 0 {
164167
continue
165168
}
@@ -175,14 +178,19 @@ func fillPodDevicesAllocatedByKoord(response *podresourcesapi.ListPodResourcesRe
175178
for deviceType, deviceAllocation := range deviceAllocations {
176179
var deviceIDs []string
177180
for _, device := range deviceAllocation {
181+
if device.Extension != nil {
182+
for _, vf := range device.Extension.VirtualFunctions {
183+
deviceIDs = append(deviceIDs, vf.BusID)
184+
}
185+
continue
186+
}
178187
deviceIDs = append(deviceIDs, device.ID)
179188
}
180189
podResource.Containers[0].Devices = append(podResource.Containers[0].Devices, &podresourcesapi.ContainerDevices{
181190
ResourceName: deviceTypeToResourceName[deviceType],
182191
DeviceIds: deviceIDs,
183192
})
184193
}
185-
break
186194
}
187195
}
188196

pkg/koordlet/statesinformer/impl/states_pod_resources_test.go

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,134 @@ func TestFillPodDevicesAllocatedByKoord(t *testing.T) {
3434
podList *corev1.PodList
3535
expectedResult *podresourcesapi.ListPodResourcesResponse
3636
}{
37+
{
38+
name: "MatchingPodWithDeviceAllocationsWithVf",
39+
response: &podresourcesapi.ListPodResourcesResponse{
40+
PodResources: []*podresourcesapi.PodResources{
41+
{
42+
Name: "test-pod",
43+
Namespace: "test-namespace",
44+
Containers: []*podresourcesapi.ContainerResources{
45+
{
46+
Name: "test-container",
47+
},
48+
},
49+
},
50+
},
51+
},
52+
podList: &corev1.PodList{
53+
Items: []corev1.Pod{
54+
{
55+
ObjectMeta: metav1.ObjectMeta{
56+
Name: "test-pod",
57+
Namespace: "test-namespace",
58+
Annotations: map[string]string{
59+
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"}]}}]}`,
60+
},
61+
},
62+
},
63+
},
64+
},
65+
expectedResult: &podresourcesapi.ListPodResourcesResponse{
66+
PodResources: []*podresourcesapi.PodResources{
67+
{
68+
Name: "test-pod",
69+
Namespace: "test-namespace",
70+
Containers: []*podresourcesapi.ContainerResources{
71+
{
72+
Name: "test-container",
73+
Devices: []*podresourcesapi.ContainerDevices{
74+
{
75+
ResourceName: string(apiext.ResourceNvidiaGPU),
76+
DeviceIds: []string{"0"},
77+
},
78+
{
79+
ResourceName: string(apiext.ResourceRDMA),
80+
DeviceIds: []string{"0000:01:00.2"},
81+
},
82+
},
83+
},
84+
},
85+
},
86+
},
87+
},
88+
},
89+
{
90+
name: "MatchingPodWithDeviceAllocationsWithVfAndMMultiplePod",
91+
response: &podresourcesapi.ListPodResourcesResponse{
92+
PodResources: []*podresourcesapi.PodResources{
93+
{
94+
Name: "test-pod-1",
95+
Namespace: "test-namespace",
96+
Containers: []*podresourcesapi.ContainerResources{
97+
{
98+
Name: "test-container",
99+
},
100+
},
101+
},
102+
{
103+
Name: "test-pod",
104+
Namespace: "test-namespace",
105+
Containers: []*podresourcesapi.ContainerResources{
106+
{
107+
Name: "test-container",
108+
},
109+
},
110+
},
111+
},
112+
},
113+
podList: &corev1.PodList{
114+
Items: []corev1.Pod{
115+
{
116+
ObjectMeta: metav1.ObjectMeta{
117+
Name: "test-pod-1",
118+
Namespace: "test-namespace",
119+
},
120+
},
121+
{
122+
ObjectMeta: metav1.ObjectMeta{
123+
Name: "test-pod",
124+
Namespace: "test-namespace",
125+
Annotations: map[string]string{
126+
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"}]}}]}`,
127+
},
128+
},
129+
},
130+
},
131+
},
132+
expectedResult: &podresourcesapi.ListPodResourcesResponse{
133+
PodResources: []*podresourcesapi.PodResources{
134+
{
135+
Name: "test-pod-1",
136+
Namespace: "test-namespace",
137+
Containers: []*podresourcesapi.ContainerResources{
138+
{
139+
Name: "test-container",
140+
},
141+
},
142+
},
143+
{
144+
Name: "test-pod",
145+
Namespace: "test-namespace",
146+
Containers: []*podresourcesapi.ContainerResources{
147+
{
148+
Name: "test-container",
149+
Devices: []*podresourcesapi.ContainerDevices{
150+
{
151+
ResourceName: string(apiext.ResourceNvidiaGPU),
152+
DeviceIds: []string{"0"},
153+
},
154+
{
155+
ResourceName: string(apiext.ResourceRDMA),
156+
DeviceIds: []string{"0000:01:00.2"},
157+
},
158+
},
159+
},
160+
},
161+
},
162+
},
163+
},
164+
},
37165
{
38166
name: "MatchingPodWithDeviceAllocations",
39167
response: &podresourcesapi.ListPodResourcesResponse{
@@ -168,7 +296,7 @@ func TestFillPodDevicesAllocatedByKoord(t *testing.T) {
168296
for _, test := range tests {
169297
t.Run(test.name, func(t *testing.T) {
170298
fillPodDevicesAllocatedByKoord(test.response, test.podList)
171-
assert.Equal(t, test.response, test.expectedResult)
299+
assert.Equal(t, test.expectedResult, test.response)
172300
})
173301
}
174302
}

0 commit comments

Comments
 (0)