-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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. | ||
|
@@ -79,6 +81,7 @@ func NewInformers(config *rest.Config, options *InformersOpts) *Informers { | |
unsafeDisableDeepCopy: options.UnsafeDisableDeepCopy, | ||
newInformer: newInformer, | ||
watchErrorHandler: options.WatchErrorHandler, | ||
watchTimeoutPeriod: options.WatchTimeoutPeriod, | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -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())) | ||
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. 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? 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. Thanks. That makes sense. Fixed it following the style of https://github.com/kubernetes/client-go/blob/8c4efe8d079e405329f314fb789a41ac6af101dc/tools/cache/reflector.go#L418 |
||
} | ||
return listWatcher.WatchFunc(opts) | ||
}, | ||
}, obj, calculateResyncPeriod(ip.resync), cache.Indexers{ | ||
|
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.
LG. Will it be easier to understand if name it like
RewatchPeriod
?A
WatchTimeoutPeriod
field incache.Options
seems to be the timeout that waits for the cache starting to watch?WDYT @xiang90 @sbueringer @vincepri