Skip to content

Conversation

@yonizxz
Copy link

@yonizxz yonizxz commented Jan 29, 2026

What type of PR is this?

/kind feature

What this PR does / why we need it:

An optimization of the binpacking algorithm.
The standard binpacking algorithm operates by simulating the scheduling of all pods.
If pods cannot be scheduled on existing nodes in the simulation, new nodes are added to the simulation.
The Fastpath logic estimates the required number of nodes using simple arithmetic, avoiding the full simulation. It works by:

  • Simulating the scheduling of pods on a single added node.
  • Calculating the estimated nodes by dividing the number of remaining unscheduled pods by the number of pods it was able to fit on that single node
    EstimatedNodes = ceil(TotalPods / PodsFittingOnASingleNode)

Current BenchmarkBinpackingEstimate results:

=== RUN   BenchmarkBinpackingEstimate
BenchmarkBinpackingEstimate
BenchmarkBinpackingEstimate-24                 1        3756356278 ns/op        2898666960 B/op  4245672 allocs/op
PASS
ok      k8s.io/autoscaler/cluster-autoscaler/estimator  3.869s

BenchmarkBinpackingEstimate with fastpath enabled:

=== RUN   BenchmarkBinpackingEstimate
BenchmarkBinpackingEstimate
BenchmarkBinpackingEstimate-24                25          41417483 ns/op        25758270 B/op     191593 allocs/op
PASS
ok      k8s.io/autoscaler/cluster-autoscaler/estimator  1.202s

Which issue(s) this PR fixes:

Special notes for your reviewer:

Additional explanation:

Since the algorithm only adds 1 node to the snapshot, subsequent iterations will not take the rest of the upcoming nodes into account during tryToScheduleOnExistingNodes, therefore it is used only in the final iteration.
In order to maximize gains from the optimization, a heuristic determines the pod equivalence group that will benefit the most from the fastpath algorithm and preemptively places it at the end of the list.

Does this PR introduce a user-facing change?

Add fastpath binpacking optimization to scale-ups

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


@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. labels Jan 29, 2026
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 29, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

Welcome @yonizxz!

It looks like this is your first PR to kubernetes/autoscaler 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/autoscaler has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: yonizxz
Once this PR has been reviewed and has the lgtm label, please assign feiskyer for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jan 29, 2026
@k8s-ci-robot
Copy link
Contributor

Hi @yonizxz. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added area/cluster-autoscaler size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed do-not-merge/needs-area cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jan 29, 2026
@yonizxz yonizxz force-pushed the fastpath-binpacking branch from fe60f6a to 8569f24 Compare February 2, 2026 10:36
@yonizxz yonizxz force-pushed the fastpath-binpacking branch from 8569f24 to d6c0d7a Compare February 2, 2026 10:49
if newNodesAvailable {
newNodesAvailable, err = e.tryToScheduleOnNewNodes(estimationState, nodeTemplate, remainingPods)
// Since fastpath binpacking adds just one node to the snapshot, it will cause inaccurate simulations on subsequent loops, therefore we only use it on the last group
if i == len(podsEquivalenceGroups)-1 && useFastpathOnLastPEG {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: instead of checking both i == len(podEquivalenceGroups) - 1 and useFastpathOnLastPEG you could have just i == fastpathPEGIndex (fastpathPEGIndex would be -1 by default).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's more readable this way, we have to use fastpath only on the last pod group, the comment explains this and the condition checks explicitly that we're on the last pod group.

It could work with i == fastpathPEGIndex and a longer explanation in the comment explaining why it is only a single index, and another comment in the place that sets it to len(pegs)-1 that explains why it is the final index specifically.
But I think it's just easier to read this way.

return false
}

func shouldUseFastPath(pods []*apiv1.Pod) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Pass PodEquivalenceGroup in arg and use pod := peg.Exemplar() instead of pods[0] in this function?


// Fastpath assumes that once a pod failed to schedule on a node, it will never be possible to schedule any more pods on this node.
// This assumption is not correct in cases of topology spread constraints.
// Fastpath also assumes that if it was able to schedule pods on a new node, it will be able to then schedule an equal amount of pods on an additional identical new node,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this is true. Why would zonal/regional anti affinity affect # of pods that can schedule on subsequent nodes?

Copy link
Author

@yonizxz yonizxz Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to - https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#an-example-of-a-pod-that-uses-pod-affinity

With zonal/regional pod anti affinity, a pod can schedule on a node only if it is in a zone/region that doesn't contain another pod with the same value.

So fastpath will create a new node and will be able to schedule the first pod, and then won't be able to schedule the second one (there's already a pod in the same zone/region, since it's on the same node), and will then assume it can schedule the remaining x pods by creating x more nodes. which is not true, because the node group is bound to a location.


if peg.Exemplar().Spec.Affinity != nil && peg.Exemplar().Spec.Affinity.PodAntiAffinity != nil {
for _, affinityTerm := range peg.Exemplar().Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution {
if affinityTerm.TopologyKey == apiv1.LabelHostname {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this isn't strictly sufficient - even with hostname topology, the pod can have anti-affinity to other groups of pods, not to other pods in the group it belongs to. Consider:

  • Deployment A has anti-affinity on hostname topology for pods in Deployment B
  • Deployment B has anti-affinity on hostname topology for pods in Deployment A

In this setup, pods from Deployment A can binpack just fine, as long as no pods from Deployment B are present.

Practically speaking though, I wonder if a simple heuristic of picking biggest PEG in terms of # of pods (assuming it is fastpath compatible) wouldn't be sufficient.

expectPodCount: 12,
},
{
name: "fastpath - simple resource-based binpacking",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful to have tests covering multiple PEGs as well. Also, for a single PEG, fastpath logic should yield the same result as non-fastpath, which would be good to validate in tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/cluster-autoscaler cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants