Skip to content

🐛 Fix restmapper cache invalidation when version is not provided - #3543

Open
zioc wants to merge 1 commit into
kubernetes-sigs:mainfrom
zioc:fix-restmapper-cache-invalidation
Open

🐛 Fix restmapper cache invalidation when version is not provided#3543
zioc wants to merge 1 commit into
kubernetes-sigs:mainfrom
zioc:fix-restmapper-cache-invalidation

Conversation

@zioc

@zioc zioc commented Jul 6, 2026

Copy link
Copy Markdown

restmapper's findAPIGroupByNameAndMaybeAggregatedDiscoveryLocked method can't rely on the apiGroups cache, otherwise new versions of a group will never be discovered when the client does not provide a group version while requesting a RESTMapping.

Fixes: #3542

findAPIGroupByNameAndMaybeAggregatedDiscoveryLocked can't rely on the
apiGroups cache, otherwise new versions of a group may never be
discovered when the client does not provide a group version.
@kubernetes-prow
kubernetes-prow Bot requested a review from joelanford July 6, 2026 13:23
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

@kubernetes-prow
kubernetes-prow Bot requested a review from varshaprasad96 July 6, 2026 13:23
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @zioc!

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

@kubernetes-prow kubernetes-prow Bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 6, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @zioc. 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.

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.

@kubernetes-prow kubernetes-prow Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jul 6, 2026
// If the server supports aggregated discovery, it will always perform that.
func (m *mapper) findAPIGroupByNameAndMaybeAggregatedDiscoveryLocked(groupName string) (_ *metav1.APIGroup, didAggregatedDiscovery bool, _ error) {
// Looking in the cache first
group, ok := m.apiGroups[groupName]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A patch like this essentially disables all caching, thats a pretty big regression as it results in running discovery on every single request. I don't think we are willing to accept that.

@zioc zioc Jul 7, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I may misunderstood, but I was thinking that this function was only called when a cache miss previously occurred in interface methods like RESTMapping:

	res, err := m.getMapper().RESTMapping(gk, versions...)                       // Initial cache lookup
	if meta.IsNoMatchError(err) {                                                // Cache miss
		if err := m.addKnownGroupAndReload(gk.Group, versions...); err != nil {  // Fetch API
			return nil, err
		}
		res, err = m.getMapper().RESTMapping(gk, versions...)
	}

And all the code paths leading to that function are following the same pattern:
client request -> IsNoMatchError (cache miss) -> addKnownGroupAndReload -> findAPIGroupByNameAndMaybeAggregatedDiscoveryLocked (when len(versions) == 0)

So I used to believe that it is was acceptable to remove this cache lookup in findAPIGroupByNameAndMaybeAggregatedDiscoveryLocked, since this code was only reached when mapping was not found in cache during first lookup. At some point, if the resource was not found in cache, and version is not provided by client, all group resources need to be fetched from the API, otherwise it will never be discovered. This is highlighted in the provided test that fails with current implementation whereas it simulates a real use-case (CRD installed after initial call to RESTMapping)

I'm open to any suggestion to implement a better fix, and I am also willing to close this PR and pass the relay if some maintainer wants to tackle the issue, but I feel that it deserves to be fixed.

@troy0820

troy0820 commented Jul 8, 2026

Copy link
Copy Markdown
Member

/hold

@kubernetes-prow kubernetes-prow Bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RestMapper cache is never invalidated when a client requests a group without specifying a version

3 participants