← Back to Pull requests

Fix CAPI provider scale-down when current replicas exceed maxSize #10697

Closed
unknown closed this pull request without merging on Dec 01, 2026 17:01 UTC
View on GitHub
unknown unknown commented on Nov 20, 2026 19:56 UTC
Author

What type of PR is this?

/kind bug
/area provider/cluster-api

What this PR does / why we need it:

With the Cluster API provider, lowering the cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size annotation below a node group's current replica count deadlocks scale-down.

DeleteNodes removes nodes by calling SetSize(replicas - 1), but SetSize rejects any target above maxSize, even when that target is a decrease. With 4 replicas and max lowered to 2, SetSize(3) fails, so the group can never converge back into range:

Scale-down: couldn't delete empty node "node-007", status error: failed to delete nodes from group MachineDeployment/.../cpu-worker: size increase too large - desired:3 max:2

This PR enforces maxSize in SetSize only when the target is above the current replica count, which is when the call is actually increasing the size. Decreases toward or into the valid range are now allowed. The minSize check is unchanged.

This is the fix from #9396 by @MaxFedotov, carried over with its original authorship. That PR was closed by the triage bot after the migration notice told it to move to kubernetes-sigs/cluster-autoscaler. The ClusterAPI provider has since been moved back to this repo (#10150, kubernetes-sigs/cluster-autoscaler#61), so the fix belongs here again.

Which issue(s) this PR fixes:

Fixes #9395

Special notes for your reviewer:

  • The commit is a clean cherry-pick of f003b947 from #9396 onto current master.

  • It adds TestSetSize/ScaleDownWhenAboveMax/{MachineSet,MachineDeployment}. On master without the fix these fail with size increase too large - desired:3 max:2. With the fix, go test ./cloudprovider/clusterapi/ passes.

  • The same check exists on cluster-autoscaler-release-1.35, -1.36 and -1.37. Once this merges, could it be cherry-picked to those branches so it ships in patch releases?
  • Does this PR introduce a user-facing change?

    ``release-note
    Cluster API provider: the autoscaler can now scale down a node group whose current replica count exceeds its max-size annotation, instead of failing with "size increase too large".

    
    #### Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:
    
    
    docs

    ``

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Bug Fixes
    Scaling down a MachineSet or MachineDeployment that is already above its configured maximum is now allowed. Attempts to scale up beyond the maximum remain blocked.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 332 lines (+249 / -83) across 4 files Reviewer: elmiko Review duration: 15665m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 332 lines diff across 4 files
    0.42
    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.347
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #10697"
    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: 42.1% (MEDIUM) · Higher = larger blast radius
    42.1%
    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: 16.7% · Safe to merge
    16.7%