fix: use container status resources over desired spec resources for cpu/memory resource metrics during in-place pod vertical scaling#1433
Draft
kondracek-nr wants to merge 1 commit intomainfrom
Conversation
…pu/memory resource metrics during in-place pod vertical scaling
a8ad572 to
9b67ab3
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1433 +/- ##
==========================================
+ Coverage 74.74% 74.87% +0.13%
==========================================
Files 53 53
Lines 3694 3706 +12
==========================================
+ Hits 2761 2775 +14
+ Misses 762 760 -2
Partials 171 171 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Kubernetes 1.33 introduced in-place pod vertical scaling (beta, on by default; GA in 1.35). With this feature, Pod.Spec.Containers[i].Resources becomes the desired state rather than the actual state. The actual applied resources live in Pod.Status.ContainerStatuses[i].Resources, which is only populated once the kubelet has successfully enacted the allocation.
This means that during an active resize, cpuRequestedCores, memoryRequestedBytes, cpuLimitCores, and memoryLimitBytes — and the utilization ratios derived from them — were reporting the target values before they were applied to the running container.
Description
Change
In fetchContainersData, prefer Pod.Status.ContainerStatuses[i].Resources (actual applied state) over Pod.Spec.Containers[i].Resources (desired state), with a fallback to Spec when Status resources are nil. Sidecar init containers (RestartPolicy: Always) are handled the same way via Pod.Status.InitContainerStatuses.
Backward compatibility
Why change existing metric semantics vs. adding new metrics
Upstream kube-state-metrics is likely taking an additive approach (new kube_pod_container_actual_resource_* metrics) because changing existing metric semantics would silently break dashboards across their entire user base with no path to coordinate consumers.
Our situation is different: we control both the metrics and the dashboards that consume them, and cpuRequestedCores semantically means "what is currently being enforced on the container." Using the desired state for utilization calculations (cpuUsageCores / cpuRequestedCores) produces a ratio that doesn't reflect the container's actual resource envelope during a resize. The value change is also narrow in scope — it only occurs during the window between a resize being requested and the kubelet applying it.
Desired-state metrics (cpuRequestedCoresDesired etc.) and resize condition metrics (PodResizePending, PodResizeInProgress) are left as follow-up work, as is parity with the OTel collector chart (blocked on upstream KSM shipping the new actual-resource metrics).
Related
Type of change
Checklist: