← Back to Pull requests

Fix scaleQuantityProportionallyCPU function #8946

Closed
unknown closed this pull request without merging on Mar 01, 2026 12:16 UTC
View on GitHub
unknown unknown commented on Dec 15, 2025 16:24 UTC
Author

What type of PR is this?

/kind bug
/area vertical-pod-autoscaler

What this PR does / why we need it:

The current implementation of scaleQuantityProportionallyCPU is broken (possibly the scaleQuantityProportionallyMem is broken as well, but I want to fix it in an another PR).

When a LimitRange object with type Pod exists in the namespace, and the algorithm proportionally increases or decreases recommendations, the division may produce an implicit issue. When remainders occur, the sum of the new recommendations does not satisfy the constraints defined in the LimitRange. For example (only the cpu is controlled):

  • A LimitRange of type Pod defines min.cpu as 150m.

  • request-to-limit ratio is 1:1 for both containers

  • A Pod contains two containers with the following CPU recommendations:

  • - c1: 15m
    - c2: 100m
  • calling scaleQuantityProportionallyCPU(15m, 115m, 150m, noRounding) returns 19m

  • - formula used: (15150)/115
  • calling scaleQuantityProportionallyCPU(100m, 115m, 150m, noRounding) returns 130m.

  • - formula used: (100
    150)/115

    The sum of new recommendations is below 150m > (19m + 130m), which means that the pod is not going to be scheduled.

    The issue with scaleQuantityProportionallyCPU is that when the division executes, the implementation ignores any remainder.

    Special notes for your reviewer:

    This PR refactors scaleQuantityProportionallyCPU so that, based on the rounding parameter (roundUpToFullUnit or roundDownToFullUnit), when a remainder occurs, the new algorithm adds one millicore to the quotient (whole number result of the division) per container when roundUpToFullUnit is used.

    Previously, the function behaved as follows:

    1. With noRounding, if the formula produces 1234567890m, the function returns 1234567890m.
    2. With roundUpToFullUnit, if the formula produces 1234567890m, the function returns 1234568.
    3. With roundDownToFullUnit, if the formula produces 1234567890m, the function returns 1234567.

    Also the diffs in vertical-pod-autoscaler/pkg/utils/vpa/capping_test.go are somewhat confusing. I removed one duplicated unit test from the file and added a completely new test.

    Does this PR introduce a user-facing change?

    Yes, but the change is subtle and may not impact users at all. With the new implementation of scaleQuantityProportionallyCPU, if the division produces a remainder, the function add 1 m to the quotient, or keep it as is. Using the example specified earlier:

    the sum of new recommendations ends up 1m higher (i.e. 151m) than the min.cpu defined in the LimitRange. So from 19m we get 20m, from the 130 we get 131m which results in 151m. So technically we waste 1m but, that shouldn't be a problem. Even the resource.Quantity documentation states that 0.1m rounds up to 1m.

    When a Pod LimitRange object is in place with a max field and we need to cap some recommendations, we lose some millicores, when the division produces a remainder . As a result, the per-container guaranteed or maximum allowed CPU millicore becomes 1m lower.

    ``release-note
    Fixed a bug where the presence of a LimitRange object with CPU constraints in the cluster prevented VPA from recreating Pods managed by the targeted controllers. The updated code section, which proportionally reduces resource requests and limits, could result in the loss of sub-millicore values.
    ``

    Diff: 541 lines (+405 / -136) across 6 files Reviewer: iamzili Review duration: 109191m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 541 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.377
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #8946"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.0% (MEDIUM) · Higher = larger blast radius
    48.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.9% · Safe to merge
    18.9%