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

Remove missing extended resources from init containers #124273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 16 additions & 11 deletions pkg/kubelet/lifecycle/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code looks almost identical to the code above. Would it be possible to avoid duplication somehow?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll extract the duplicate code into a new function. Should I add this code to a new commit or just amend the existing one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would propose this to not replicate the code:

func removeMissingExtendedResources(pod *v1.Pod, nodeInfo *schedulerframework.NodeInfo) *v1.Pod {
	podCopy := pod.DeepCopy()
	updateResourceRequests := func(containers []v1.Container) []v1.Container {
		for i, container := 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.
			container.Resources.Requests = filterExtendedResources(container.Resources.Requests, nodeInfo)
			containers[i] = container
		}
		return containers
	}

	podCopy.Spec.Containers = updateResourceRequests(podCopy.Spec.Containers)
	podCopy.Spec.InitContainers = updateResourceRequests(podCopy.Spec.InitContainers)

	return podCopy
}

func filterExtendedResources(requests v1.ResourceList, nodeInfo *schedulerframework.NodeInfo) v1.ResourceList {
	filteredRequests := make(v1.ResourceList)
	for rName, rQuant := range requests {
		if v1helper.IsExtendedResourceName(rName) {
			if _, found := nodeInfo.Allocatable.ScalarResources[rName]; !found {
				continue
			}
		}
		filteredRequests[rName] = rQuant
	}
	return filteredRequests
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice suggestion, I plan to modify it like this:

func removeMissingExtendedResources(pod *v1.Pod, nodeInfo *schedulerframework.NodeInfo) *v1.Pod {
	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
			}
			containers[i].Resources.Requests = filteredResources
		}
	}
	podCopy := pod.DeepCopy()
	filterExtendedResources(podCopy.Spec.Containers)
	filterExtendedResources(podCopy.Spec.InitContainers)
	return podCopy
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

podCopy := pod.DeepCopy()
filterExtendedResources(podCopy.Spec.Containers)
filterExtendedResources(podCopy.Spec.InitContainers)
return podCopy
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/kubelet/lifecycle/predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ func makeTestPod(requests, limits v1.ResourceList) *v1.Pod {
},
},
},
InitContainers: []v1.Container{
{
Resources: v1.ResourceRequirements{
Requests: requests,
Limits: limits,
},
},
},
},
}
}
Expand Down