Skip to content

Commit 5ed5732

Browse files
committed
Refactored status manager code of updatePodFromAllocation
1 parent 8fa8277 commit 5ed5732

File tree

2 files changed

+16
-49
lines changed

2 files changed

+16
-49
lines changed

pkg/kubelet/status/status_manager.go

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -262,69 +262,40 @@ func (m *manager) UpdatePodFromAllocation(pod *v1.Pod) (*v1.Pod, bool) {
262262
return updatePodFromAllocation(pod, allocs)
263263
}
264264

265-
/* func updatePodFromAllocation(pod *v1.Pod, allocs state.PodResourceAllocation) (*v1.Pod, bool) {
265+
func updatePodFromAllocation(pod *v1.Pod, allocs state.PodResourceAllocation) (*v1.Pod, bool) {
266266
allocated, found := allocs[string(pod.UID)]
267267
if !found {
268268
return pod, false
269269
}
270270

271271
updated := false
272-
updateContainerResources := func(c *v1.Container) {
272+
containerAlloc := func(c v1.Container) (v1.ResourceRequirements, bool) {
273273
if cAlloc, ok := allocated[c.Name]; ok {
274274
if !apiequality.Semantic.DeepEqual(c.Resources, cAlloc) {
275+
// Allocation differs from pod spec, retrieve the allocation
275276
if !updated {
277+
// If this is the first update to be performed, copy the pod
276278
pod = pod.DeepCopy()
277279
updated = true
278280
}
279-
c.Resources = *cAlloc.DeepCopy()
281+
return cAlloc, true
280282
}
281283
}
284+
return v1.ResourceRequirements{}, false
282285
}
283286

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
300287
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
311291
}
312292
}
313-
314293
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
325297
}
326298
}
327-
328299
return pod, updated
329300
}
330301

pkg/kubelet/status/status_manager_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ import (
3636
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3737
"k8s.io/apimachinery/pkg/runtime"
3838
"k8s.io/apimachinery/pkg/runtime/schema"
39-
utilfeature "k8s.io/apiserver/pkg/util/feature"
4039
clientset "k8s.io/client-go/kubernetes"
4140
"k8s.io/client-go/kubernetes/fake"
4241
core "k8s.io/client-go/testing"
43-
featuregatetesting "k8s.io/component-base/featuregate/testing"
4442
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
4543
api "k8s.io/kubernetes/pkg/apis/core"
46-
"k8s.io/kubernetes/pkg/features"
4744
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
4845
kubepod "k8s.io/kubernetes/pkg/kubelet/pod"
4946
"k8s.io/kubernetes/pkg/kubelet/status/state"
@@ -2095,12 +2092,12 @@ func TestUpdatePodFromAllocation(t *testing.T) {
20952092
Name: "c1-init",
20962093
Resources: v1.ResourceRequirements{
20972094
Requests: v1.ResourceList{
2098-
v1.ResourceCPU: *resource.NewMilliQuantity(200, resource.DecimalSI),
2099-
v1.ResourceMemory: *resource.NewQuantity(300, resource.DecimalSI),
2095+
v1.ResourceCPU: *resource.NewMilliQuantity(500, resource.DecimalSI),
2096+
v1.ResourceMemory: *resource.NewQuantity(600, resource.DecimalSI),
21002097
},
21012098
Limits: v1.ResourceList{
2102-
v1.ResourceCPU: *resource.NewMilliQuantity(400, resource.DecimalSI),
2103-
v1.ResourceMemory: *resource.NewQuantity(500, resource.DecimalSI),
2099+
v1.ResourceCPU: *resource.NewMilliQuantity(700, resource.DecimalSI),
2100+
v1.ResourceMemory: *resource.NewQuantity(800, resource.DecimalSI),
21042101
},
21052102
},
21062103
},
@@ -2161,7 +2158,6 @@ func TestUpdatePodFromAllocation(t *testing.T) {
21612158

21622159
for _, test := range tests {
21632160
t.Run(test.name, func(t *testing.T) {
2164-
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.SidecarContainers, true)
21652161
pod := test.pod.DeepCopy()
21662162
allocatedPod, updated := updatePodFromAllocation(pod, test.allocs)
21672163

0 commit comments

Comments
 (0)