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 2 commits
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
26 changes: 17 additions & 9 deletions probe/kubernetes/client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package kubernetes

import (
"fmt"
"io"
"strconv"
"sync"
"time"

log "github.com/Sirupsen/logrus"
"github.com/weaveworks/common/backoff"

This comment was marked as abuse.

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/client/cache"
Expand All @@ -16,7 +18,6 @@ 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"
)

// Client keeps track of running kubernetes pods and services
Expand Down Expand Up @@ -55,15 +56,22 @@ type client struct {
podWatches []func(Event, Pod)
}

// runReflectorUntil is equivalent to cache.Reflector.RunUntil, but it also logs
// errors, which cache.Reflector.RunUntil simply ignores
func runReflectorUntil(r *cache.Reflector, resyncPeriod time.Duration, stopCh <-chan struct{}) {
loggingListAndWatch := func() {
if err := r.ListAndWatch(stopCh); err != nil {
log.Errorf("Kubernetes reflector: %v", err)
// runReflectorUntil runs cache.Reflector#ListAndWatch in an endless loop.
// Errors are logged and retried with exponential backoff.
func runReflectorUntil(r *cache.Reflector, resyncPeriod time.Duration, stopCh <-chan struct{}, msg string) {
listAndWatch := func() (bool, error) {
select {
case <-stopCh:
return true, nil
default:
err := r.ListAndWatch(stopCh)
return false, err
}
}
go wait.Until(loggingListAndWatch, resyncPeriod, stopCh)
bo := backoff.New(listAndWatch, fmt.Sprintf("Kubernetes reflector (%s)", msg))
bo.SetInitialBackoff(resyncPeriod)
bo.SetMaxBackoff(5 * time.Minute)
go bo.Start()
}

// ClientConfig establishes the configuration for the kubernetes client
Expand Down Expand Up @@ -169,7 +177,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)
runReflectorUntil(cache.NewReflector(lw, itemType, store, c.resyncPeriod), c.resyncPeriod, c.quit, resource)
return store
}

Expand Down