@@ -262,69 +262,40 @@ func (m *manager) UpdatePodFromAllocation(pod *v1.Pod) (*v1.Pod, bool) {
262
262
return updatePodFromAllocation (pod , allocs )
263
263
}
264
264
265
- /* func updatePodFromAllocation(pod *v1.Pod, allocs state.PodResourceAllocation) (*v1.Pod, bool) {
265
+ func updatePodFromAllocation (pod * v1.Pod , allocs state.PodResourceAllocation ) (* v1.Pod , bool ) {
266
266
allocated , found := allocs [string (pod .UID )]
267
267
if ! found {
268
268
return pod , false
269
269
}
270
270
271
271
updated := false
272
- updateContainerResources := func(c * v1.Container) {
272
+ containerAlloc := func (c v1.Container ) (v1. ResourceRequirements , bool ) {
273
273
if cAlloc , ok := allocated [c .Name ]; ok {
274
274
if ! apiequality .Semantic .DeepEqual (c .Resources , cAlloc ) {
275
+ // Allocation differs from pod spec, retrieve the allocation
275
276
if ! updated {
277
+ // If this is the first update to be performed, copy the pod
276
278
pod = pod .DeepCopy ()
277
279
updated = true
278
280
}
279
- c.Resources = * cAlloc.DeepCopy()
281
+ return cAlloc , true
280
282
}
281
283
}
284
+ return v1.ResourceRequirements {}, false
282
285
}
283
286
284
- for i := range pod.Spec.Containers {
285
- updateContainerResources(&pod.Spec.Containers[i])
286
- }
287
- for i := range pod.Spec.InitContainers {
288
- updateContainerResources(&pod.Spec.InitContainers[i])
289
- }
290
- return pod, updated
291
- } */
292
-
293
- // TODO(vibansal): Refactor this function to something above commented code.
294
- func updatePodFromAllocation (pod * v1.Pod , allocs state.PodResourceAllocation ) (* v1.Pod , bool ) {
295
- allocated , found := allocs [string (pod .UID )]
296
- if ! found {
297
- return pod , false
298
- }
299
- updated := false
300
287
for i , c := range pod .Spec .Containers {
301
- if cAlloc , ok := allocated [c .Name ]; ok {
302
- if ! apiequality .Semantic .DeepEqual (c .Resources , cAlloc ) {
303
- // Allocation differs from pod spec, update
304
- if ! updated {
305
- // If this is the first update, copy the pod
306
- pod = pod .DeepCopy ()
307
- updated = true
308
- }
309
- pod .Spec .Containers [i ].Resources = cAlloc
310
- }
288
+ if cAlloc , found := containerAlloc (c ); found {
289
+ // Allocation differs from pod spec, update
290
+ pod .Spec .Containers [i ].Resources = cAlloc
311
291
}
312
292
}
313
-
314
293
for i , c := range pod .Spec .InitContainers {
315
- if cAlloc , ok := allocated [c .Name ]; ok {
316
- if ! apiequality .Semantic .DeepEqual (c .Resources , cAlloc ) {
317
- // Allocation differs from pod spec, update
318
- if ! updated {
319
- // If this is the first update, copy the pod
320
- pod = pod .DeepCopy ()
321
- updated = true
322
- }
323
- pod .Spec .InitContainers [i ].Resources = cAlloc
324
- }
294
+ if cAlloc , found := containerAlloc (c ); found {
295
+ // Allocation differs from pod spec, update
296
+ pod .Spec .InitContainers [i ].Resources = cAlloc
325
297
}
326
298
}
327
-
328
299
return pod , updated
329
300
}
330
301
0 commit comments