Skip to content

kubectl: Cache Verifier.HasSupport calls #132003

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tchap
Copy link
Contributor

@tchap tchap commented May 28, 2025

What type of PR is this?

/kind feature

What this PR does / why we need it:

The underlying implementation decodes OpenAPI schema on every HasSupport call. This causes these calls to be expensive and noticeable when many resources are being applied.

This patch wraps the verifier with a thread-safe cache so that when many resources of the same kind are being checked, only the first call includes schema decoding.

This solution is specifically limited to the kubectl codebase without any changes required in client-go, for now, although that is where the issue is actually originating.

This can be transparently removed when deemed unnecessary. It's more like the first step to improve the issue.

Which issue(s) this PR fixes:

Fixes kubernetes/kubectl#1682

Does this PR introduce a user-facing change?

NONE

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels May 28, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels May 28, 2025
@k8s-ci-robot
Copy link
Contributor

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

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 needs-priority Indicates a PR lacks a `priority/foo` label and requires one. label May 28, 2025
@tchap
Copy link
Contributor Author

tchap commented May 28, 2025

/sig cli

@k8s-ci-robot k8s-ci-robot added sig/cli Categorizes an issue or PR as relevant to SIG CLI. area/kubectl and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels May 28, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in SIG CLI May 28, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

@ardaguclu
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 May 28, 2025

func (cv *cachingVerifier) HasSupport(gvk schema.GroupVersionKind) error {
// Try to get the cached value.
// Once cached, the value never changes.
Copy link
Member

Choose a reason for hiding this comment

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

This is partially correct. Because openapi specs are changed. At some point in time, this cache starts serving stale data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I need to check more properly how OpenAPI caching works and whether it can actually change within a single kubectl invocation. This change is related to apply, so I may try to set it up so that it's enabled in that command only so that I don't have to care for other cases.

Copy link
Contributor Author

@tchap tchap May 28, 2025

Choose a reason for hiding this comment

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

Well, unless I am missing something, OpenAPI schema is fetched once and cached anyway, so this doesn't make the situation any worse. It only caches results of HasSupport since there is some processing of the schema that takes time.

@tchap tchap force-pushed the kubectl-apply-cache-openapi-schema branch 2 times, most recently from b9688d4 to 7c07abb Compare May 28, 2025 15:32
The underlying implementation decodes OpenAPI schema on every HasSupport
call. This causes these calls to be expensive and noticable when many
resources are being applied.

This patch wraps the verifier with a thread-safe cache so that when many
resources of the same kind are being checked, only the first call
includes schema decoding.

This solution is specifically limited to the kubectl codebase without
any changes required in client-go, for now, although that is where the
issue is actually originating.
@tchap tchap force-pushed the kubectl-apply-cache-openapi-schema branch from 7c07abb to d2fb076 Compare May 29, 2025 13:16
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 29, 2025
@ardaguclu
Copy link
Member

/cc @seans3
in case he might be able to review.

@k8s-ci-robot k8s-ci-robot requested a review from seans3 May 29, 2025 13:21
@tchap
Copy link
Contributor Author

tchap commented May 29, 2025

/retest-required

1 similar comment
@tchap
Copy link
Contributor Author

tchap commented May 29, 2025

/retest-required

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubectl 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-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. 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. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
Status: Needs Triage
Development

Successfully merging this pull request may close these issues.

very slow apply performance due to openapi schema reparsing
3 participants