karmadactl: set default 5s timeout for resource completion#7315
karmadactl: set default 5s timeout for resource completion#7315manmathbh wants to merge 3 commits intokarmada-io:masterfrom
Conversation
|
Welcome @manmathbh! It looks like this is your first PR to karmada-io/karmada 🎉 |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the reliability and user experience of Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent karmadactl shell completion (resource discovery via apiresources) from hanging by applying a default 5s timeout when listing resources.
Changes:
- Adds logic in
compGetResourceListto default the REST client timeout to5swhen none is set.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if configFlags, ok := restClientGetter.(*genericclioptions.ConfigFlags); ok { | ||
| if configFlags.Timeout == nil || len(*configFlags.Timeout) == 0 { | ||
| configFlags.Timeout = ptr.To("5s") | ||
| } | ||
| } |
There was a problem hiding this comment.
restClientGetter is typically a util.Factory/cmdutil.Factory here (see callers passing f), so the type assertion to *genericclioptions.ConfigFlags will fail and the default timeout will never be applied. Consider wrapping the provided genericclioptions.RESTClientGetter with a small delegating implementation that overrides ToRESTConfig() to set rest.Config.Timeout to 5s when it is zero (or otherwise ensure you can access/mutate the underlying ConfigFlags used by the factory).
There was a problem hiding this comment.
Agree, how about:
config, err := restClientGetter.ToRawKubeConfigLoader().ClientConfig()
if err != nil {
return nil
}
config.Timeout = 5 * time.SecondThere was a problem hiding this comment.
Code Review
This pull request introduces a default 5-second timeout for resource completion lookups to prevent them from hanging. The implementation modifies the ConfigFlags to set the timeout. My review includes a suggestion to avoid side effects by restoring the original timeout value after the operation, improving the code's robustness.
| // TODO: Should set --request-timeout=5s | ||
| if configFlags, ok := restClientGetter.(*genericclioptions.ConfigFlags); ok { | ||
| if configFlags.Timeout == nil || len(*configFlags.Timeout) == 0 { | ||
| configFlags.Timeout = ptr.To("5s") |
There was a problem hiding this comment.
While this change correctly sets a default timeout, it modifies the configFlags object in place. This creates a side effect that could potentially affect other parts of the code using the same restClientGetter instance within the same process execution. To avoid this, it's better to restore the original timeout value before the function returns. You can achieve this by using a defer statement.
originalTimeout := configFlags.Timeout
configFlags.Timeout = ptr.To("5s")
defer func() { configFlags.Timeout = originalTimeout }()Signed-off-by: manmathbh <manmathcode@gmail.com>
9444460 to
379e04e
Compare
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7315 +/- ##
==========================================
+ Coverage 42.03% 42.16% +0.12%
==========================================
Files 874 875 +1
Lines 53551 53610 +59
==========================================
+ Hits 22511 22603 +92
+ Misses 29349 29307 -42
- Partials 1691 1700 +9
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
/cc @zhzhuang-zju |
|
Thanks |
| if configFlags, ok := restClientGetter.(*genericclioptions.ConfigFlags); ok { | ||
| if configFlags.Timeout == nil || len(*configFlags.Timeout) == 0 { | ||
| configFlags.Timeout = ptr.To("5s") | ||
| } | ||
| } |
There was a problem hiding this comment.
Agree, how about:
config, err := restClientGetter.ToRawKubeConfigLoader().ClientConfig()
if err != nil {
return nil
}
config.Timeout = 5 * time.SecondSigned-off-by: manmathbh <manmathcode@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Hi @zhzhuang-zju, thanks for the suggestion. |
zhzhuang-zju
left a comment
There was a problem hiding this comment.
@manmathbh Thanks, others LGTM
| if cfg.Timeout == 0 { | ||
| cfg.Timeout = g.timeout | ||
| } |
There was a problem hiding this comment.
| if cfg.Timeout == 0 { | |
| cfg.Timeout = g.timeout | |
| } | |
| cfg.Timeout = g.timeout |
I think if the caller creates a timeoutRESTClientGetter rather than a genericclioptions.RESTClientGetter, the purpose is to use the timeout defined by the timeoutRESTClientGette.timeout
| @@ -0,0 +1,89 @@ | |||
| /* | |||
| Copyright 2024 The Karmada Authors. | |||
There was a problem hiding this comment.
| Copyright 2024 The Karmada Authors. | |
| Copyright 2026 The Karmada Authors. |
How time flies!
| "k8s.io/client-go/tools/clientcmd" | ||
| ) | ||
|
|
||
| type fakeRESTClientGetter struct { |
There was a problem hiding this comment.
karmada/pkg/karmadactl/util/testing/fake.go
Lines 27 to 30 in 0288586
We can directly use the existing
TestFactory object
There was a problem hiding this comment.
@manmathbh Could you explain why you are not using TestFactory and instead creating a new fake object fakeRESTClientGetter?
There was a problem hiding this comment.
Hi @zhzhuang-zju my apologies! I completely missed that part of your feedback in my last push and accidentally left the fake in there.
I just force-pushed an update that completely removes fakeRESTClientGetter and properly utilizes TestFactory from util/testing. Thanks for catching my oversight!
| func TestTimeoutRESTClientGetterToRESTConfigSetsDefaultTimeout(t *testing.T) { | ||
| base := &fakeRESTClientGetter{cfg: &rest.Config{}} | ||
| getter := &timeoutRESTClientGetter{RESTClientGetter: base, timeout: 5 * time.Second} | ||
|
|
||
| cfg, err := getter.ToRESTConfig() | ||
| if err != nil { | ||
| t.Fatalf("ToRESTConfig() unexpected error: %v", err) | ||
| } | ||
| if cfg.Timeout != 5*time.Second { | ||
| t.Fatalf("expected timeout %v, got %v", 5*time.Second, cfg.Timeout) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeoutRESTClientGetterToRESTConfigPreservesExistingTimeout(t *testing.T) { | ||
| base := &fakeRESTClientGetter{cfg: &rest.Config{Timeout: 2 * time.Second}} | ||
| getter := &timeoutRESTClientGetter{RESTClientGetter: base, timeout: 5 * time.Second} | ||
|
|
||
| cfg, err := getter.ToRESTConfig() | ||
| if err != nil { | ||
| t.Fatalf("ToRESTConfig() unexpected error: %v", err) | ||
| } | ||
| if cfg.Timeout != 2*time.Second { | ||
| t.Fatalf("expected existing timeout %v to be preserved, got %v", 2*time.Second, cfg.Timeout) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeoutRESTClientGetterToRESTConfigPropagatesError(t *testing.T) { | ||
| expectedErr := errors.New("boom") | ||
| base := &fakeRESTClientGetter{err: expectedErr} | ||
| getter := &timeoutRESTClientGetter{RESTClientGetter: base, timeout: 5 * time.Second} | ||
|
|
||
| _, err := getter.ToRESTConfig() | ||
| if !errors.Is(err, expectedErr) { | ||
| t.Fatalf("expected error %v, got %v", expectedErr, err) | ||
| } | ||
| } |
There was a problem hiding this comment.
Unit tests are generally written at the function level. These three unit tests essentially test the ToRESTConfig() function so that we can consolidate them into a single unit test.
|
Hi @zhzhuang-zju, thanks for the suggestions. Updated accordingly:
Verification:
PTAL. |
Signed-off-by: manmathbh <manmathcode@gmail.com>
c0054eb to
503eba6
Compare
|
@manmathbh Thanks, generally LGTM. Can you squash the commits into one? |
What type of PR is this?
/kind bug
What this PR does / why we need it:
This PR sets a default 5s timeout for resource discovery used by
karmadactlshell completion, so completion requests do not block indefinitely when API discovery is slow or unreachable.Implementation details:
timeoutRESTClientGetter.ToRESTConfig(), applies cfg.Timeout = 5s for completion requests.This avoids relying on *genericclioptions.ConfigFlags type assertions and ensures timeout behavior is applied consistently through the REST config path used by completion.
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
- Scope is limited to completion-time resource list discovery.
- Added/updated unit tests in completion_test.go to cover:
- timeout applied to rest config by wrapper
- error propagation path
-
- Verification run locally:
-
go test ./pkg/karmadactl/util/completion -count=1-
bash hack/verify-staticcheck.shDoes this PR introduce a user-facing change?: