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

✨ allow setting watchTimeoutPeriod when creating informers #2738

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ type Options struct {
// instead of `reconcile.Result{}`.
SyncPeriod *time.Duration

// WatchTimeoutPeriod is the timeout for the watch request. If the watch request
// times out, the cache will close the watch and reconnect.
//
// Defaults to a random duration between 5 and 10 minutes if unset.
WatchTimeoutPeriod *time.Duration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG. Will it be easier to understand if name it like RewatchPeriod?

A WatchTimeoutPeriod field in cache.Options seems to be the timeout that waits for the cache starting to watch?

WDYT @xiang90 @sbueringer @vincepri


// ReaderFailOnMissingInformer configures the cache to return a ErrResourceNotCached error when a user
// requests, using Get() and List(), a resource the cache does not already have an informer for.
//
Expand Down Expand Up @@ -383,6 +389,7 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
WatchErrorHandler: opts.DefaultWatchErrorHandler,
UnsafeDisableDeepCopy: ptr.Deref(config.UnsafeDisableDeepCopy, false),
NewInformer: opts.newInformer,
WatchTimeoutPeriod: opts.WatchTimeoutPeriod,
}),
readerFailOnMissingInformer: opts.ReaderFailOnMissingInformer,
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/cache/internal/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/client-go/metadata"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/internal/syncs"
)
Expand All @@ -51,6 +52,7 @@ type InformersOpts struct {
Transform cache.TransformFunc
UnsafeDisableDeepCopy bool
WatchErrorHandler cache.WatchErrorHandler
WatchTimeoutPeriod *time.Duration
}

// NewInformers creates a new InformersMap that can create informers under the hood.
Expand Down Expand Up @@ -79,6 +81,7 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
unsafeDisableDeepCopy: options.UnsafeDisableDeepCopy,
newInformer: newInformer,
watchErrorHandler: options.WatchErrorHandler,
watchTimeoutPeriod: options.WatchTimeoutPeriod,
}
}

Expand Down Expand Up @@ -147,6 +150,9 @@ type Informers struct {
// so that all informers will not send list requests simultaneously.
resync time.Duration

// watchTimeoutPeriod is the timeout period for watch requests
watchTimeoutPeriod *time.Duration

// mu guards access to the map
mu sync.RWMutex

Expand Down Expand Up @@ -354,6 +360,9 @@ func (ip *Informers) addInformerToMap(gvk schema.GroupVersionKind, obj runtime.O
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
ip.selector.ApplyToList(&opts)
opts.Watch = true // Watch needs to be set to true separately
if ip.watchTimeoutPeriod != nil {
opts.TimeoutSeconds = ptr.To(int64(ip.watchTimeoutPeriod.Seconds()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a certain time without any random, it might bring a large number of watch requests to apiserver when all the old watches closed and try to reconnect, if your operator watch a lot of resources as you described in this pr.

This is why the default timeout is a random number between 5 and 10 minutes. So how about add a little random time base on the given period?

Copy link
Author

@ccding ccding Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
return listWatcher.WatchFunc(opts)
},
}, obj, calculateResyncPeriod(ip.resync), cache.Indexers{
Expand Down