mirror of
https://github.com/kubernetes/kubernetes.git
synced 2026-05-28 04:04:39 -04:00
HPA: optimize calculatePodRequests for specific container lookups
- Add early exit when specific container is found in calculatePodRequestsFromContainers - Add error handling for non-existent containers following existing patterns - Maintain all existing functionality for pod-level resources and feature gates - Include comprehensive function documentation The optimization eliminates unnecessary container iterations when HPA targets specific containers, providing significant performance improvements for pods with many containers while preserving full backward compatibility
This commit is contained in:
parent
13ced7b7dd
commit
bb6a0ea6b2
2 changed files with 37 additions and 1 deletions
|
|
@ -501,7 +501,6 @@ func calculatePodLevelRequests(pod *v1.Pod, resource v1.ResourceName) (int64, er
|
|||
// resource by summing requests from all containers in the pod.
|
||||
// If a container name is specified, it uses only that container.
|
||||
func calculatePodRequestsFromContainers(pod *v1.Pod, container string, resource v1.ResourceName) (int64, error) {
|
||||
// Calculate all regular containers and restartable init containers requests.
|
||||
containers := append([]v1.Container{}, pod.Spec.Containers...)
|
||||
for _, c := range pod.Spec.InitContainers {
|
||||
if c.RestartPolicy != nil && *c.RestartPolicy == v1.ContainerRestartPolicyAlways {
|
||||
|
|
@ -518,7 +517,17 @@ func calculatePodRequestsFromContainers(pod *v1.Pod, container string, resource
|
|||
}
|
||||
request += containerRequest.MilliValue()
|
||||
}
|
||||
// container names are unique inside the pod
|
||||
if container == c.Name {
|
||||
return request, nil
|
||||
}
|
||||
}
|
||||
|
||||
// If we're looking for a specific container and didn't find it
|
||||
if container != "" {
|
||||
return 0, fmt.Errorf("container %s not found in Pod %s", container, pod.Name)
|
||||
}
|
||||
|
||||
return request, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2430,3 +2430,30 @@ func TestCalculateRequests(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
func TestCalculatePodRequestsFromContainers_NonExistentContainer(t *testing.T) {
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-pod",
|
||||
Namespace: testNamespace,
|
||||
},
|
||||
Spec: v1.PodSpec{
|
||||
Containers: []v1.Container{
|
||||
{
|
||||
Name: "container1",
|
||||
Resources: v1.ResourceRequirements{
|
||||
Requests: v1.ResourceList{
|
||||
v1.ResourceCPU: resource.MustParse("100m"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
request, err := calculatePodRequestsFromContainers(pod, "non-existent-container", v1.ResourceCPU)
|
||||
|
||||
require.Error(t, err, "expected error for non-existent container")
|
||||
expectedErr := "container non-existent-container not found in Pod test-pod"
|
||||
assert.Equal(t, expectedErr, err.Error(), "error message should match expected format")
|
||||
assert.Equal(t, int64(0), request, "request should be 0 when container does not exist")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue