-
Notifications
You must be signed in to change notification settings - Fork 1.2k
⚠️ DynamicRestMapper: return NoMatchError when resource doesn't exist #1151
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
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-sigs:master
from
timebertt:fix/dynamic-rest-mapper
Sep 30, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package apiutil_test | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/onsi/gomega/format" | ||
"github.com/onsi/gomega/types" | ||
"golang.org/x/time/rate" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
@@ -57,53 +58,49 @@ var _ = Describe("Dynamic REST Mapper", func() { | |
}) | ||
|
||
It("should reload if not present in the cache", func() { | ||
By("reading successfully once") | ||
By("reading target successfully once") | ||
Expect(callWithTarget()).To(Succeed()) | ||
Expect(callWithOther()).NotTo(Succeed()) | ||
|
||
By("asking for a something that didn't exist previously after adding it to the mapper") | ||
By("reading other not successfully") | ||
count := 0 | ||
addToMapper = func(baseMapper *meta.DefaultRESTMapper) { | ||
count++ | ||
baseMapper.Add(targetGVK, meta.RESTScopeNamespace) | ||
baseMapper.Add(secondGVK, meta.RESTScopeNamespace) | ||
} | ||
Expect(callWithOther()).To(Succeed()) | ||
Expect(callWithTarget()).To(Succeed()) | ||
}) | ||
Expect(callWithOther()).To(beNoMatchError()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this fail on the first attempt now and only works on the second? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Expect(count).To(Equal(1), "should reload exactly once") | ||
|
||
It("should rate-limit reloads so that we don't get more than a certain number per second", func() { | ||
By("setting a small limit") | ||
*lim = *rate.NewLimiter(rate.Limit(1), 1) | ||
|
||
By("forcing a reload after changing the mapper") | ||
By("reading both successfully now") | ||
addToMapper = func(baseMapper *meta.DefaultRESTMapper) { | ||
baseMapper.Add(targetGVK, meta.RESTScopeNamespace) | ||
baseMapper.Add(secondGVK, meta.RESTScopeNamespace) | ||
} | ||
Expect(callWithOther()).To(Succeed()) | ||
|
||
By("calling another time that would need a requery and failing") | ||
Eventually(func() bool { | ||
return errors.As(callWithTarget(), &apiutil.ErrRateLimited{}) | ||
}, "10s").Should(BeTrue()) | ||
Expect(callWithTarget()).To(Succeed()) | ||
}) | ||
|
||
It("should rate-limit then allow more at 1rps", func() { | ||
It("should rate-limit then allow more at configured rate", func() { | ||
By("setting a small limit") | ||
*lim = *rate.NewLimiter(rate.Limit(1), 1) | ||
*lim = *rate.NewLimiter(rate.Every(100*time.Millisecond), 1) | ||
|
||
By("forcing a reload after changing the mapper") | ||
addToMapper = func(baseMapper *meta.DefaultRESTMapper) { | ||
baseMapper.Add(secondGVK, meta.RESTScopeNamespace) | ||
} | ||
|
||
By("calling twice to trigger rate limiting") | ||
Expect(callWithOther()).To(Succeed()) | ||
Expect(callWithTarget()).NotTo(Succeed()) | ||
|
||
// by 2nd call loop should succeed because we canceled our 1st rate-limited token, then waited a full second | ||
By("calling until no longer rate-limited, 2nd call should succeed") | ||
Eventually(func() bool { | ||
return errors.As(callWithTarget(), &apiutil.ErrRateLimited{}) | ||
}, "2.5s", "1s").Should(BeFalse()) | ||
By("calling another time to trigger rate limiting") | ||
addToMapper = func(baseMapper *meta.DefaultRESTMapper) { | ||
baseMapper.Add(targetGVK, meta.RESTScopeNamespace) | ||
} | ||
// if call consistently fails, we are sure, that it was rate-limited, | ||
// otherwise it would have reloaded and succeeded | ||
Consistently(callWithTarget, "90ms", "10ms").Should(beNoMatchError()) | ||
|
||
By("calling until no longer rate-limited") | ||
// once call succeeds, we are sure, that it was no longer rate-limited, | ||
// as it was allowed to reload and found matching kind/resource | ||
Eventually(callWithTarget, "30ms", "10ms").Should(And(Succeed(), Not(beNoMatchError()))) | ||
}) | ||
|
||
It("should avoid reloading twice if two requests for the same thing come in", func() { | ||
|
@@ -251,3 +248,25 @@ var _ = Describe("Dynamic REST Mapper", func() { | |
}) | ||
}) | ||
}) | ||
|
||
func beNoMatchError() types.GomegaMatcher { | ||
return noMatchErrorMatcher{} | ||
} | ||
|
||
type noMatchErrorMatcher struct{} | ||
|
||
func (k noMatchErrorMatcher) Match(actual interface{}) (success bool, err error) { | ||
actualErr, actualOk := actual.(error) | ||
if !actualOk { | ||
return false, nil | ||
} | ||
|
||
return meta.IsNoMatchError(actualErr), nil | ||
} | ||
|
||
func (k noMatchErrorMatcher) FailureMessage(actual interface{}) (message string) { | ||
return format.Message(actual, "to be a NoMatchError") | ||
} | ||
func (k noMatchErrorMatcher) NegatedFailureMessage(actual interface{}) (message string) { | ||
return format.Message(actual, "not to be a NoMatchError") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.