Skip to content

Commit e873e6a

Browse files
committed
Add the ability to set watch timeouts.
Allow setting shorter watch timeout for the firt time alone. What this PR does / why we need it: We have seen in some evnironments (where requests are tunnelled) setting up the (first) watch is blocking if there are no resources available to watch. This causes the apply loop to be blocked when setting up watches before apply.
1 parent b758057 commit e873e6a

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

pkg/patterns/declarative/pkg/watch/dynamic.go

+47-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package watch
1818

1919
import (
20+
"atomic"
2021
"context"
2122
"fmt"
2223
"sync"
@@ -35,8 +36,19 @@ import (
3536
"sigs.k8s.io/controller-runtime/pkg/log"
3637
)
3738

38-
// WatchDelay is the time between a Watch being dropped and attempting to resume it
39-
const WatchDelay = 30 * time.Second
39+
var (
40+
// WatchActivityTimeout sets a timeout for a Watch activity under normal operation
41+
WatchActivityTimeout = 300 * time.Second
42+
// WatchActivityFirstTimeout sets a timeout for Watch activity in an Apply path
43+
// We expect the author to set this to a lower value in environments where it makes sense.
44+
// func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { ... watch.WatchActivityFirstTimeout = 10 * time.Second ... }
45+
WatchActivityFirstTimeout = 300 * time.Second
46+
)
47+
48+
const (
49+
// WatchDelay is the time between a Watch being dropped and attempting to resume it
50+
WatchDelay = 30 * time.Second
51+
)
4052

4153
// NewDynamicWatch constructs a watcher for unstructured objects.
4254
// Deprecated: avoid using directly; will move to internal in future.
@@ -138,13 +150,44 @@ type clientObject struct {
138150
//
139151
// [1] https://github.com/kubernetes/kubernetes/issues/54878#issuecomment-357575276
140152
func (w *dynamicKindWatch) watchUntilClosed(ctx context.Context, eventTarget metav1.ObjectMeta, watchStarted *sync.WaitGroup) {
153+
var sawActivity atomic.Bool
154+
141155
log := log.FromContext(ctx)
142156

143157
options := w.FilterOptions
144158
// Though we don't use the resource version, we allow bookmarks to help keep TCP connections healthy.
145159
options.AllowWatchBookmarks = true
146160

147-
events, err := w.resource.Watch(context.TODO(), options)
161+
activityTimeout := WatchActivityTimeout
162+
if watchStarted != nil {
163+
activityTimeout = WatchActivityFirstTimeout
164+
}
165+
ctx, cancel := context.WithCancel(ctx)
166+
defer cancel()
167+
// Check for events periodically
168+
ticker := time.NewTicker(activityTimeout)
169+
defer ticker.Stop()
170+
sawActivity.Store(false)
171+
172+
go func() {
173+
for {
174+
select {
175+
case <-ctx.Done():
176+
return
177+
case <-ticker.C:
178+
if !sawActivity.Load() {
179+
log.WithValues("kind", w.GVK.String()).WithValues("namespace", w.FilterNamespace).WithValues("labels", options.LabelSelector).Info("no activity seen for a while, cancelling watch")
180+
cancel()
181+
return
182+
}
183+
sawActivity.Store(false)
184+
}
185+
}
186+
}()
187+
188+
events, err := w.resource.Watch(ctx, options)
189+
sawActivity.Store(true)
190+
148191
if watchStarted != nil {
149192
watchStarted.Done()
150193
}
@@ -159,6 +202,7 @@ func (w *dynamicKindWatch) watchUntilClosed(ctx context.Context, eventTarget met
159202
defer events.Stop()
160203

161204
for clientEvent := range events.ResultChan() {
205+
sawActivity.Store(true)
162206
switch clientEvent.Type {
163207
case watch.Bookmark:
164208
// not an object change, we ignore it

0 commit comments

Comments
 (0)