Skip to content

Conversation

bnallapeta
Copy link

@bnallapeta bnallapeta commented Sep 25, 2025

What this PR does / why we need it:

Adds GetUncachedClient method to the ClusterCache interface to support live (uncached) API calls to workload clusters. This helps reduce memory usage when you want to cache only a few types instead of having to exclude most types via DisableFor.
The uncached client is created alongside the cached one during connection setup and follows the same lifecycle (connect/disconnect).
Note: This introduces a breaking change to any existing implementations that are directly using ClusterCache.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #12784

/area controllers
/area clustercache

@k8s-ci-robot k8s-ci-robot added the area/clustercache Issues or PRs related to the clustercachetracker label Sep 25, 2025
@k8s-ci-robot
Copy link
Contributor

@bnallapeta: The label(s) area/controllers cannot be applied, because the repository doesn't have them.

In response to this:

What this PR does / why we need it:

  • Introduces an optional extension interface to access a live (uncached) reader for workload clusters: UncachedReaderProvider with GetUncachedReader(ctx, cluster) (client.Reader, error)
  • Wires a finalized uncached client into the connection lifecycle and exposes it via the accessor and a forwarder on clusterCache.
  • Keeps existing ClusterCache API unchanged and fully backward compatible.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #12784

/area controllers
/area clustercache

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: yes Indicates the PR's author has signed the CNCF CLA. label Sep 25, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

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
Copy link
Contributor

Welcome @bnallapeta!

It looks like this is your first PR to kubernetes-sigs/cluster-api 🎉. 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-sigs/cluster-api 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

Hi @bnallapeta. Thanks for your PR.

I'm waiting for a kubernetes-sigs 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.

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 needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 25, 2025
@sbueringer
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 29, 2025
@bnallapeta bnallapeta changed the title ✨ clustercache: add optional uncached reader support for workload clusters ✨ clustercache: add optional uncached client support for workload clusters Sep 30, 2025
}
}

log.V(6).Info("Creating uncached client (finalized)")
Copy link
Member

Choose a reason for hiding this comment

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

WDYT about instead doing the following in l.101 (inside the if):

		log.V(6).Info(fmt.Sprintf("Creating uncached client with updated REST config with host %q", restConfig.Host))
		uncachedClient, err = createUncachedClient(ca.config.Scheme, restConfig, httpClient, mapper)
		if err != nil {
			return nil, errors.Wrapf(err, "error creating uncached client (using in-cluster config)")
		}

+ moving newClientWithTimeout inside the createUncachedClient func?

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense. We use the same uncachedClient var throughout and avoids double-creation.

g.Expect(accessor.lockedState.healthChecking.lastProbeSuccessTime.IsZero()).To(BeFalse())
}

func TestGetUncachedClientLifecycle(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we integrate this into TestConnect?

Copy link
Author

Choose a reason for hiding this comment

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

Done.

// UncachedClientProvider provides live (uncached) access to the workload cluster API server.
// This is an optional extension interface; callers can type assert a ClusterCache to this interface to retrieve an uncached client for a given workload cluster.
type UncachedClientProvider interface {
GetUncachedClient(ctx context.Context, cluster client.ObjectKey) (client.Client, error)
Copy link
Member

Choose a reason for hiding this comment

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

Let's add this to the existing ClusterCache interface instead. I don't want to introduce a new interface for every new method that we're adding

Copy link
Author

Choose a reason for hiding this comment

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

I considered this. But if we do this, then won't it be a breaking Go API change? Whoever has implemented/used ClusterCache has to adjust no? (including tests, backports). Additionally, if most people don't need uncached access, then the main interface feels bloated.

Copy link
Member

@sbueringer sbueringer Oct 1, 2025

Choose a reason for hiding this comment

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

I'm not aware of anyone using our interface but with a different implementation. If they do, they should consider using a subset of our interface. I don't want to make our implementation unnecessary complicated for that use case

Yes technically this is a breaking change, but I would prefer making changes like this from time to time over making our code unnecessary complicated. (both client-go and controller-runtime handle this the same way).

(Additional context, clustercache uses controller-runtime interfaces in its own interface, particularly the client interface is also not stable, so no breaking changes in general is not achievable)

Copy link
Member

Choose a reason for hiding this comment

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

(The current implementation also means that we have to check if our ClusterCache also implements the other interface if we want to use the new func anywhere)

Copy link
Author

Choose a reason for hiding this comment

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

Alright, I have included it in the existing ClusterCache interface.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 1, 2025
@k8s-ci-robot
Copy link
Contributor

@bnallapeta: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-cluster-api-apidiff-main 6f2d5be link false /test pull-cluster-api-apidiff-main

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

@bnallapeta bnallapeta requested a review from sbueringer October 3, 2025 07:59
@bnallapeta
Copy link
Author

@sbueringer I believe all the comments are addressed. Please take a look and let me know if there's anything else. Thank you.

@bnallapeta
Copy link
Author

How do we handle the pull-cluster-api-apidiff-main that is failing? I get that its failing due to a API breaking change. But I could not find any file in the codebase where we can make it happy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/clustercache Issues or PRs related to the clustercachetracker cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support uncached client for ClusterCache
3 participants