Skip to content

Conversation

@GaetanoMar96
Copy link

What type of PR is this?

/kind feature

What this PR does / why we need it:

This PR introduces a new testing framework for Cluster Autoscaler based on synctest.

The new framework utilizes a "bubble" environment and virtual time to:

  • Ensure Determinism: Background goroutines are managed within the synctest bubble, preventing leaks.
  • Accelerate Test Execution: Full scale-up/scale-down lifecycles that normally take minutes of simulated time now execute in milliseconds of real time.
  • Structured Lifecycle Management: Provides a TestContext and Builder pattern for easier setup of complex cluster states.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

The PR is structured into three logical commits to provide a clear path from architecture to implementation:

  • Refactor for Dependency Injection (Builder Pattern): I introduced a Builder Pattern in the main initialization logic. This allows us to inject fakes (KubeClient, CloudProvider, PodObservers) during setup. This decoupling is essential for moving away from heavyweight integration tests toward lightweight, controlled simulations.

  • Lifecycle & Leak Management: To support synctest's requirement for a clean "bubble" exit, I refactored internal components to properly accept and propagate context.Context. This ensures that background goroutines observe shutdown signals immediately, preventing the "durable sleep" leaks common in components that previously relied on static timers.

  • Framework & Lifecycle Tests: This commit introduces the TestContext and TestBuilder. It includes a full-lifecycle regression test (Scale-up -> Stabilization -> Scale-down) that demonstrates the framework's ability to orchestrate complex state transitions deterministically using the encapsulated state of our fake K8s and Cloud Provider implementations.

Usage: See staticautoscaler_test.go for an example of the new RunTest and ctx.Step pattern.

Does this PR introduce a user-facing change?

NONE

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


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/feature Categorizes issue or PR as related to a new feature. labels Jan 22, 2026
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 22, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

Welcome @GaetanoMar96!

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 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 22, 2026
@k8s-ci-robot
Copy link
Contributor

Hi @GaetanoMar96. 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 the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Jan 22, 2026
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. 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 22, 2026
@GaetanoMar96 GaetanoMar96 marked this pull request as draft January 22, 2026 13:20
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 22, 2026
@GaetanoMar96 GaetanoMar96 force-pushed the synctest-framework branch 5 times, most recently from 35ac017 to eb540b2 Compare January 23, 2026 09:59
@GaetanoMar96 GaetanoMar96 marked this pull request as ready for review January 23, 2026 10:01
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 23, 2026
@GaetanoMar96
Copy link
Author

/assign @kawych @mtrqq

var nodeInfoComparator nodegroupset.NodeInfoComparator
if len(autoscalingOptions.BalancingLabels) > 0 {
nodeInfoComparator = nodegroupset.CreateLabelNodeInfoComparator(autoscalingOptions.BalancingLabels)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we pull it outside, to main?

I just noticed that the core package calls the NewCloudProvider() -_-

My proposal of target state:

  1. move that branch outside, to main
  2. move initialization from core to main as well:
    opts.CloudProvider = cloudBuilder.NewCloudProvider(opts, informerFactory)
  3. in the core repo (and in the builder) just error out if CloudProvider is nil
  4. split the cloudprovider/, the interface should be clearly separated from provider-specific stuff (including cloudprovider/builder)
  5. Add some form of dependency enforcement, e.g. unit test or play with dependency visibility: at minimum, the core repo cannot have dependency on any of the cloud-specific providers.

I think 4 and 5 would require some larger discussion, but in this CL I think 1-3 are achievable.

Copy link
Author

Choose a reason for hiding this comment

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

Cloud provider depends on informer factory and core autoscaler options, which are initialized only later. If we move all this code in main then in test config we should handle all of them injecting them at build initialization. Are we sure we want to do this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Right... that might be a bit unsafe, since we don't know 100% which options does the NewCloudProvider really depend on.

Happy case: we can just move it out and see if any of the tests fail. But I'm not too confident that we have sufficient test coverage to detect all issues.

Another solution would be to leave it as is. IMO in this case we should at least move this initialization out of the core package, e.g. to the builder package. And ideally add a comment in the code that a target state is initialization in main.go, but it wasn't done because we don't have ability to test it thoroughly.

I'd like to get some feedback from other CA maintainers @mtrqq @towca

Copy link
Contributor

Choose a reason for hiding this comment

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

And regarding the nodeInfoComparator initialization, I think it's better to leave it in main.go for now, since the code is different depending on cloud provider (also it's in the spirit of the TODO below to eventually migrate it somewhere in the CloudProvider package).

Copy link
Author

Choose a reason for hiding this comment

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

Migrated the nodeInfoComparator. I will leave the conversation unresolved for now waiting from CA maintainers feedback

@GaetanoMar96 GaetanoMar96 requested a review from kawych February 3, 2026 12:37
@GaetanoMar96 GaetanoMar96 force-pushed the synctest-framework branch 3 times, most recently from 5180f1d to 58e27de Compare February 3, 2026 15:05
@GaetanoMar96 GaetanoMar96 requested a review from mtrqq February 3, 2026 15:29
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 4, 2026
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 4, 2026
Copy link
Contributor

@kawych kawych left a comment

Choose a reason for hiding this comment

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

Added couple more comments, but they're increasingly minor. The biggest one is what we already agreed on offline. Thanks for the great work!


c.Autoscaler, _, err = autoscalerbuilder.New(*c.Options).
WithKubeClient(c.Fakes.K8s.Client).
WithInformerFactory(c.Fakes.K8s.InformerFactory).
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible to initialize that inside the builder? Or does the additional option defined in main.go make it harder?

Copy link
Author

Choose a reason for hiding this comment

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

I am not entirely sure about this question, can you make it a bit more clear?

Copy link
Contributor

@mtrqq mtrqq left a comment

Choose a reason for hiding this comment

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

Thanks for the changes, it will greatly simplify E2E testing of some autoscaler features while not going into expensive E2E tests territory

I like the direction that you've picked, having a set of reusable components without locking outselves in a specific framework or even synctest. It is good choice to keep direct interaction with synctest puts you in the right mindset when writing the test while also not forcing new tests to pick between framework and manual labour or mocking all the things around

Another data point to consider - while synctest is a great tool to verify correctness of the autoscaler, it's not optimal to use it with benchmarks, see testing.B reference in golang/go#73567

WithKubeClient(kubeClient).
WithInformerFactory(fakeK8s.InformerFactory).
WithCloudProvider(fakeCloud).
WithPodObserver(&loop.UnschedulablePodObserver{}).
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't it something which we may default in the builder and add an override if needed? So far I don't see observer value being changed in the call sites

Copy link
Author

Choose a reason for hiding this comment

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

I believe this is what we are actually doing, see this code in the builder
if b.podObserver == nil { b.podObserver = loop.StartPodObserver(ctx, b.kubeClient) }

unneededTime = 1 * time.Minute
)

func TestStaticAutoscaler_FullLifecycle(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add add some baseline integration test which devs can refer to when adding new ones. The main point of which would obviously be to provide a skeleton for such a test, but also it may serve as a decent source of documentation for using synctest.

For me the current most lacking point is to outline a boundary between what autoscaler components can be configured in the test bauble and what can't or are not supposed to. Adding this information may steer new tests into the correct direction straight away resulting in a lot of time saved when debugging failures

Copy link
Author

Choose a reason for hiding this comment

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

Added it in the last commit

@elmiko
Copy link
Contributor

elmiko commented Feb 5, 2026

i think this is a great contribution. i am still reviewing but i like what i see so far.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 6, 2026
@elmiko
Copy link
Contributor

elmiko commented Feb 9, 2026

/ok-to-test

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Feb 9, 2026
@k8s-ci-robot k8s-ci-robot removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 10, 2026
Copy link
Contributor

@elmiko elmiko left a comment

Choose a reason for hiding this comment

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

this is making sense to me, no specific request for changes at the moment.

Copy link
Contributor

@kawych kawych left a comment

Choose a reason for hiding this comment

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

Added couple more nitpicks, but these are just polishes, I think it's good to go!

@mtrqq @towca Would be great if you can give your oppinion on the thread about dependencies

}

// MustCreateManager creates a controller-runtime manager with metrics and health probes disabled.
func MustCreateManager(t *testing.T) manager.Manager {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: "Manager" is one of the most generic and unhelpful terms, can you add some substance to the naming here?

NewControllerRuntimeMgr? Or even NewControllerRuntime if that doesn't collide with anything?


// Config is the "blueprint" for a test. It defines the entire
// initial state of the world before the test runs.
type Config struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: consider OptionsBuilder

// Initialize fakes and configuration options.
// This happens outside the synctest bubble to keep the setup clean.
config := integration.NewConfig().WithOverrides() // override CA options if needed

Copy link
Contributor

Choose a reason for hiding this comment

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

uber-nit: consider removing excessive whitespaces

unneededTime = 1 * time.Minute
)

func TestStaticAutoscaler_Template(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: consider adding a top-level comment explaining the purpose of this test.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: GaetanoMar96, kawych
Once this PR has been reviewed and has the lgtm label, please assign towca 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

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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants