Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Use aggregated discovery if available #2901

Merged
merged 1 commit into from
Sep 23, 2024

Conversation

alvaroaleman
Copy link
Member

@alvaroaleman alvaroaleman commented Aug 3, 2024

This change makes the RESTMapper use AggredatedDiscovery if available. AggregatedDiscovery is beta and thus enabled by default since Kube 1.27.

What this means in particular is that during startup, we will always do a request to /api and /apis. If AggregatedDiscovery is enabled, that is all we have to do. If not, we have to do a third request.

The behavior for reloading remains the same: Do one request unless the request didn't include a version. In that case we have to find the group. If the group is unknown, we will keep on doing a request to /api and one to /apis to find it. If it is found we are again done in the AggregatedDiscovery case and have to do a third request otherwise.

All in all this means that with AggregatedDiscovery enabled, the only case where this is worse is if the client interacts with only one GroupVersion, as we end up doing one additional api request. If it is disabled, we effectively waste two api requests.

Fixes #2897

Note: Disable whitespace when reviewing, that significantly reduces the size of the diff.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 3, 2024
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Aug 3, 2024
Kind: "Driver",
}
for _, aggregatedDiscovery := range []bool{true, false} {
t.Run("aggregatedDiscovery="+strconv.FormatBool(aggregatedDiscovery), func(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.

Definitely fine for now, but we're going to lose coverage once we bump hack/check-everything.sh to a version where the feature gate doesn't function anymore (so maybe we should use a different Kubernetes version long-term (after #2810))

WDYT about opening an issue to track that?

pkg/client/apiutil/restmapper.go Outdated Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Outdated Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Outdated Show resolved Hide resolved
@alvaroaleman
Copy link
Member Author

@sbueringer one thing probably worth pointing out: Originally I thought this is always better in terms of api calls as it is one api call for everything. I then learned when implementing this that its actually two api calls for everything: One to /api, one to /apis. That means its not as obviously better as I originally thought: If one GV is used by the binary its worse. If two are used, its the same. Three or more, its better (but I would assume the majority of controllers uses two or less).

The other reason why I think it would be nice is that it brings us closer to the upstream restmapper if aggrgated discovery is supported, as it means we have all available info in the cache and issues like #2496 wouldn't happen. But I don't know if that is a super strong argument.

@sbueringer
Copy link
Member

Just to avoid misunderstandings

If one GV is used by the binary its worse. If two are used, its the same. Three or more, its better (but I would assume the majority of controllers uses two or less).

With "GV used" you basically mean that the controller will ask the restmapper for a specific GV. I.e. "two are used" means that the restmapper would be used for at least two different GVs.

I think it's safe to assume that most controllers will use at least 2 GVs. At least 1 for their CRD and then in a lot of cases either another one for other CRDs or builtin resources.

If I got it correctly the restmapper should be used for every informer / get / list / write / ... calls. I think a lot of controllers are not restricted to just one GV

pkg/client/apiutil/restmapper.go Outdated Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Outdated Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Outdated Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Outdated Show resolved Hide resolved
Copy link
Member

@sbueringer sbueringer left a comment

Choose a reason for hiding this comment

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

Very nice! Last round of nits from my side

pkg/client/apiutil/restmapper.go Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Outdated Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Outdated Show resolved Hide resolved
pkg/client/apiutil/restmapper.go Outdated Show resolved Hide resolved
@sbueringer
Copy link
Member

/assign @vincepri

This change makes the `RESTMapper` use [AggredatedDiscovery][0] if
available. `AggregatedDiscovery` is beta and thus enabled by default
since Kube 1.28.

What this means in particular is that during startup, we will always do
a request to `/api` and `/apis`. If `AggregatedDiscovery` is enabled,
that is all we have to do. If not, we have to do a third request.

The behavior for reloading remains the same: Do one request unless the
request didn't include a version. In that case we have to find the
group. If the group is unknown, we will keep on doing a request to `/api`
and one to `/apis` to find it. If it is found we are again done in the
`AggregatedDiscovery` case and have to do a third request otherwise.

All in all this means that with `AggregatedDiscovery` enabled, the only
case where this is worse is if the client interacts with only one
`GroupVersion`, as we end up doing one additional api request. If it is
disabled, we effectively waste two api requests.

[0]: kubernetes/enhancements#3352
@sbueringer
Copy link
Member

Thank you very much! :)

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 23, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: c70bca7c69ff2f2555178d3625f143dcf99d27b8

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, sbueringer

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

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [alvaroaleman,sbueringer]

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 merged commit 236e448 into kubernetes-sigs:main Sep 23, 2024
9 checks passed
@alvaroaleman alvaroaleman deleted the aggregated branch September 23, 2024 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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.

Restmapper: Use aggregated discovery if available
4 participants