kubernetes: [Pod Level Resources] Pod-level resource validation fails when container-level is set for other resource

What happened?

When a Kubernetes Pod defines resources.requests at the Pod level, but only specifies a subset of the supported resources requested by its containers (e.g., only cpu at the Pod level, while a container requests memory), Kubernetes’ validation implicitly assigns a request of “0” for the unstated resource (e.g., memory) at the Pod level. This “0” value then triggers a validation error because it’s less than the actual aggregated request from the containers, even though the user might expect the Pod to implicitly aggregate the container’s memory request.

 "pod" is invalid: spec.resources.requests[memory]: Invalid value: "0": must be greater than or equal to aggregate container requests of 10Mi

What did you expect to happen?

When a user defines resource requests at the container level, Kubernetes already aggregates these requests internally to determine the total resource needs of the Pod. If the Pod.spec.resources.requests field is present, but only specifies a subset of resources (e.g., cpu but not memory), the intuitive expectation is that Kubernetes would implicitly aggregate the memory request from the containers, rather than defaulting the Pod’s memory request to 0.

https://github.com/kubernetes/kubernetes/blob/ccf291b50166ce82204486aae6a626a30b469182/pkg/apis/core/validation/validation.go#L4406 needs to be fixed to compare container aggregates with pod-level values only if pod-level value for that resource is set.

How can we reproduce it (as minimally and precisely as possible)?

Enable PodLevelResources feature gate on apiserver, scheduler and kubelet, and create the following pod

apiVersion: v1
kind: Pod
metadata:
  name: pod-bug
spec:
  resources:
    requests:
      cpu: "300m"  # Pod-level CPU request
  containers:
  - name: sleep-infinity
    image: busybox
    command: ["sh", "-c", "sleep infinity"]
    resources:
      requests:
        ephemeral-memory: 10Mi 

Anything else we need to know?

No response

Kubernetes version

$ kubectl version
Client Version: v1.33.1
Kustomize Version: v5.6.0
Server Version: v1.33.1```

</details>


### Cloud provider

<details>
N/A
</details>


### OS version

<details>

```console
# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here

# On Windows:
C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here

Install tools

Container runtime (CRI) and version (if applicable)

Related plugins (CNI, CSI, …) and versions (if applicable)

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 18 (13 by maintainers)

Most upvoted comments

The fix is to check pod-level resource consistency only for resources that are supported at pod-level

I briefly wondered if a fix like #131262 might be necessary, but since the resources that can be specified with Pod Level Resources are already restricted, it should be fine to compare the aggregated container-level requests only with the requests that are actually specified at the pod level, right?

yeah, you are right. I would still like to improve the code there by adding following in the for loop if !supportedResourceNames.Has(resourceName) { continue }

@ndixita Haven’t you already created #130131 yourself to fix this issue?

This looks like a different issue. In this bug, the pod-level resource requests for unsupported resource will be 0, but container-level request for that resource is set to non-zero value which results in validation failure. The fix is to check pod-level resource consistency only for resources that are supported at pod-level