Skip to content
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

Back off upon errored kubernetes api requests #2562

Merged
merged 3 commits into from
Jun 6, 2017
Merged
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
39 changes: 33 additions & 6 deletions probe/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/util/runtime"
)

const maxBackoffTime = 5 * time.Minute

// Client keeps track of running kubernetes pods and services
type Client interface {
Stop()
Expand Down Expand Up @@ -56,14 +58,39 @@ type client struct {
}

// runReflectorUntil is equivalent to cache.Reflector.RunUntil, but it also logs
// errors, which cache.Reflector.RunUntil simply ignores
// errors (which cache.Reflector.RunUntil simply ignores) and backs off exponentially
// on errors.
func runReflectorUntil(r *cache.Reflector, resyncPeriod time.Duration, stopCh <-chan struct{}) {
loggingListAndWatch := func() {
if err := r.ListAndWatch(stopCh); err != nil {
select {
case <-stopCh:
return
default:
}

var err error
wait := resyncPeriod
for {
func() {
defer runtime.HandleCrash()

This comment was marked as abuse.

This comment was marked as abuse.

err = r.ListAndWatch(stopCh)
}()

if err != nil {
log.Errorf("Kubernetes reflector: %v", err)
wait *= 2
if wait > maxBackoffTime {
wait = maxBackoffTime
}
} else {
wait = resyncPeriod
}

select {
case <-stopCh:
return
case <-time.After(wait):
}
}
go wait.Until(loggingListAndWatch, resyncPeriod, stopCh)
}

// ClientConfig establishes the configuration for the kubernetes client
Expand Down Expand Up @@ -169,7 +196,7 @@ func (c *client) setupStore(kclient cache.Getter, resource string, itemType inte
if store == nil {
store = cache.NewStore(cache.MetaNamespaceKeyFunc)
}
runReflectorUntil(cache.NewReflector(lw, itemType, store, c.resyncPeriod), c.resyncPeriod, c.quit)
go runReflectorUntil(cache.NewReflector(lw, itemType, store, c.resyncPeriod), c.resyncPeriod, c.quit)
return store
}

Expand Down