From fbb975b1473f13d05749b36789518e0ad1a36da0 Mon Sep 17 00:00:00 2001 From: Panos Woo Date: Thu, 11 Apr 2024 14:01:12 +0000 Subject: [PATCH] Remove missing extended resources from init containers Signed-off-by: Panos Woo --- pkg/kubelet/lifecycle/predicate.go | 27 +++++++++++++++---------- pkg/kubelet/lifecycle/predicate_test.go | 8 ++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pkg/kubelet/lifecycle/predicate.go b/pkg/kubelet/lifecycle/predicate.go index 8b93ba39ae3..af9bde9c89d 100644 --- a/pkg/kubelet/lifecycle/predicate.go +++ b/pkg/kubelet/lifecycle/predicate.go @@ -215,21 +215,26 @@ func rejectPodAdmissionBasedOnOSField(pod *v1.Pod) bool { } func removeMissingExtendedResources(pod *v1.Pod, nodeInfo *schedulerframework.NodeInfo) *v1.Pod { - podCopy := pod.DeepCopy() - for i, c := range pod.Spec.Containers { - // We only handle requests in Requests but not Limits because the - // PodFitsResources predicate, to which the result pod will be passed, - // does not use Limits. - podCopy.Spec.Containers[i].Resources.Requests = make(v1.ResourceList) - for rName, rQuant := range c.Resources.Requests { - if v1helper.IsExtendedResourceName(rName) { - if _, found := nodeInfo.Allocatable.ScalarResources[rName]; !found { - continue + filterExtendedResources := func(containers []v1.Container) { + for i, c := range containers { + // We only handle requests in Requests but not Limits because the + // PodFitsResources predicate, to which the result pod will be passed, + // does not use Limits. + filteredResources := make(v1.ResourceList) + for rName, rQuant := range c.Resources.Requests { + if v1helper.IsExtendedResourceName(rName) { + if _, found := nodeInfo.Allocatable.ScalarResources[rName]; !found { + continue + } } + filteredResources[rName] = rQuant } - podCopy.Spec.Containers[i].Resources.Requests[rName] = rQuant + containers[i].Resources.Requests = filteredResources } } + podCopy := pod.DeepCopy() + filterExtendedResources(podCopy.Spec.Containers) + filterExtendedResources(podCopy.Spec.InitContainers) return podCopy } diff --git a/pkg/kubelet/lifecycle/predicate_test.go b/pkg/kubelet/lifecycle/predicate_test.go index 649ba679bf4..98db94d39b9 100644 --- a/pkg/kubelet/lifecycle/predicate_test.go +++ b/pkg/kubelet/lifecycle/predicate_test.go @@ -106,6 +106,14 @@ func makeTestPod(requests, limits v1.ResourceList) *v1.Pod { }, }, }, + InitContainers: []v1.Container{ + { + Resources: v1.ResourceRequirements{ + Requests: requests, + Limits: limits, + }, + }, + }, }, } }