-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[WIP] pkg/restmapper: use exponential backoff with DynamicRESTMapper calls #1792
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
Closed
estroz
wants to merge
2
commits into
operator-framework:master
from
estroz:osdk-469-upstream-dynamicrestmapper
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,30 +15,64 @@ | |
package restmapper | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/discovery" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/restmapper" | ||
) | ||
|
||
// TODO(estroz): do we want to return a wait.ErrWaitTimeout if backoff duration | ||
// reaches a maximum? | ||
|
||
var ( | ||
// BackoffDuration is the initial duration that the DynamicRESTMapper | ||
// waits to reload its REST mapping. | ||
BackoffDuration time.Duration = time.Millisecond * 10 | ||
// BackoffDuration is the number of times that the DynamicRESTMapper | ||
// will perform an exponential backoff before maxing out. | ||
BackoffSteps = 10 | ||
) | ||
|
||
// DynamicRESTMapper is a RESTMapper that dynamically discovers resource | ||
// types at runtime. This is in contrast to controller-manager's default | ||
// RESTMapper, which only checks resource types at startup, and so can't | ||
// handle the case of first creating a CRD and then creating an instance | ||
// of that CRD. | ||
type DynamicRESTMapper struct { | ||
client discovery.DiscoveryInterface | ||
delegate meta.RESTMapper | ||
backoff *backoff | ||
} | ||
|
||
// NewDynamicRESTMapper returns a RESTMapper that dynamically discovers resource | ||
// types at runtime. This is in contrast to controller-manager's default RESTMapper, which | ||
// only checks resource types at startup, and so can't handle the case of first creating a | ||
// CRD and then creating an instance of that CRD. | ||
// NewDynamicRESTMapper returns a DynamicRESTMapper for cfg. | ||
func NewDynamicRESTMapper(cfg *rest.Config) (meta.RESTMapper, error) { | ||
client, err := discovery.NewDiscoveryClientForConfig(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
drm := &DynamicRESTMapper{client: client} | ||
drm := &DynamicRESTMapper{ | ||
client: client, | ||
backoff: &backoff{ | ||
Backoff: wait.Backoff{ | ||
Duration: BackoffDuration, | ||
Steps: BackoffSteps, | ||
Factor: 2, | ||
}, | ||
}, | ||
} | ||
// Substitute the default backoff error handler for our exponential one. | ||
if len(utilruntime.ErrorHandlers) > 1 { | ||
utilruntime.ErrorHandlers[1] = func(error) { | ||
time.Sleep(drm.backoff.step()) | ||
} | ||
} | ||
if err := drm.reload(); err != nil { | ||
return nil, err | ||
} | ||
|
@@ -54,14 +88,19 @@ func (drm *DynamicRESTMapper) reload() error { | |
return nil | ||
} | ||
|
||
// reloadOnError checks if an error indicates that the delegated RESTMapper needs to be | ||
// reloaded, and if so, reloads it and returns true. | ||
// reloadOnError checks if an error indicates that the delegated RESTMapper | ||
// needs to be reloaded, and if so, reloads it and returns true. | ||
// reloadOnError uses an exponential backoff mechanism to rate limit reloads. | ||
func (drm *DynamicRESTMapper) reloadOnError(err error) bool { | ||
if _, matches := err.(*meta.NoKindMatchError); !matches { | ||
drm.backoff.reset() | ||
return false | ||
} | ||
err = drm.reload() | ||
if err != nil { | ||
// TODO(estroz): HandleError uses a rudimentary backoff by default. | ||
// Should we remove it completely or substitute the default backoff | ||
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. I think it makes sense to have the simple back off as a fall-back before failing completely |
||
// for our exponential one, as we do above? | ||
utilruntime.HandleError(err) | ||
} | ||
return err == nil | ||
|
@@ -122,3 +161,37 @@ func (drm *DynamicRESTMapper) ResourceSingularizer(resource string) (singular st | |
} | ||
return s, err | ||
} | ||
|
||
type backoff struct { | ||
wait.Backoff | ||
mu sync.Mutex | ||
} | ||
|
||
// Copied and pared down from | ||
// https://github.com/kubernetes/apimachinery/blob/6a84e37a896db9780c75367af8d2ed2bb944022e/pkg/util/wait/wait.go#L227 | ||
// TODO(estroz) call Backoff.Step() once we bump to kubernetes-1.14.1 | ||
func (b *backoff) step() time.Duration { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
|
||
if b.Steps < 1 { | ||
return b.Duration | ||
} | ||
b.Steps-- | ||
|
||
duration := b.Duration | ||
|
||
// calculate the next step | ||
if b.Factor != 0 { | ||
b.Duration = time.Duration(float64(b.Duration) * b.Factor) | ||
} | ||
|
||
return duration | ||
} | ||
|
||
func (b *backoff) reset() { | ||
b.mu.Lock() | ||
b.Duration = BackoffDuration | ||
b.Steps = BackoffSteps | ||
b.mu.Unlock() | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say yes. Backoff duration hitting its maximum is, in the end, a wait timeout.