Skip to content

[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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 80 additions & 7 deletions pkg/restmapper/dynamicrestmapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Copy link
Contributor

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.


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
}
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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()
}