Skip to content

Commit cdc2e0e

Browse files
✨ Allow configuring cache sync timeouts
This PR allows users to configure timeout for cache syncs while starting the controller.
1 parent 5564be7 commit cdc2e0e

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

pkg/controller/controller.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controller
1919
import (
2020
"context"
2121
"fmt"
22+
"time"
2223

2324
"github.com/go-logr/logr"
2425
"k8s.io/client-go/util/workqueue"
@@ -47,6 +48,10 @@ type Options struct {
4748
// Log is the logger used for this controller and passed to each reconciliation
4849
// request via the context field.
4950
Log logr.Logger
51+
52+
// CacheSyncTimeout refers to the time limit set to wait for syncing caches.
53+
// Defaults to 2 minutes if not set.
54+
CacheSyncTimeout time.Duration
5055
}
5156

5257
// Controller implements a Kubernetes API. A Controller manages a work queue fed reconcile.Requests
@@ -104,6 +109,10 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
104109
options.MaxConcurrentReconciles = 1
105110
}
106111

112+
if options.CacheSyncTimeout == 0 {
113+
options.CacheSyncTimeout = 2 * time.Minute
114+
}
115+
107116
if options.RateLimiter == nil {
108117
options.RateLimiter = workqueue.DefaultControllerRateLimiter()
109118
}
@@ -120,6 +129,7 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
120129
return workqueue.NewNamedRateLimitingQueue(options.RateLimiter, name)
121130
},
122131
MaxConcurrentReconciles: options.MaxConcurrentReconciles,
132+
CacheSyncTimeout: options.CacheSyncTimeout,
123133
SetFields: mgr.SetFields,
124134
Name: name,
125135
Log: options.Log.WithName("controller").WithName(name),

pkg/internal/controller/controller.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ type Controller struct {
7979
// undergo a major refactoring and redesign to allow for context to not be stored in a struct.
8080
ctx context.Context
8181

82+
// CacheSyncTimeout refers to the time limit set on waiting cache to sync
83+
// Defaults to 2 minutes if not set.
84+
CacheSyncTimeout time.Duration
85+
8286
// startWatches maintains a list of sources, handlers, and predicates to start when the controller is started.
8387
startWatches []watchDescription
8488

@@ -142,6 +146,10 @@ func (c *Controller) Start(ctx context.Context) error {
142146
// Set the internal context.
143147
c.ctx = ctx
144148

149+
// use a context with timeout for launching sources and syncing caches.
150+
sourceStartCtx, cancel := context.WithTimeout(ctx, c.CacheSyncTimeout)
151+
defer cancel()
152+
145153
c.Queue = c.MakeQueue()
146154
defer c.Queue.ShutDown() // needs to be outside the iife so that we shutdown after the stop channel is closed
147155

@@ -156,7 +164,7 @@ func (c *Controller) Start(ctx context.Context) error {
156164
// caches.
157165
for _, watch := range c.startWatches {
158166
c.Log.Info("Starting EventSource", "source", watch.src)
159-
if err := watch.src.Start(ctx, watch.handler, c.Queue, watch.predicates...); err != nil {
167+
if err := watch.src.Start(sourceStartCtx, watch.handler, c.Queue, watch.predicates...); err != nil {
160168
return err
161169
}
162170
}
@@ -169,7 +177,7 @@ func (c *Controller) Start(ctx context.Context) error {
169177
if !ok {
170178
continue
171179
}
172-
if err := syncingSource.WaitForSync(ctx); err != nil {
180+
if err := syncingSource.WaitForSync(sourceStartCtx); err != nil {
173181
// This code is unreachable in case of kube watches since WaitForCacheSync will never return an error
174182
// Leaving it here because that could happen in the future
175183
err := fmt.Errorf("failed to wait for %s caches to sync: %w", c.Name, err)

pkg/internal/controller/controller_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,60 @@ var _ = Describe("controller", func() {
122122
close(done)
123123
})
124124

125+
It("should error when cache sync timeout occurs", func(done Done) {
126+
ctrl.CacheSyncTimeout = 10 * time.Nanosecond
127+
c, err := cache.New(cfg, cache.Options{})
128+
Expect(err).NotTo(HaveOccurred())
129+
_, err = c.GetInformer(context.TODO(), &appsv1.Deployment{})
130+
Expect(err).NotTo(HaveOccurred())
131+
sync := false
132+
ctrl.startWatches = []watchDescription{{
133+
src: source.NewKindWithCache(&appsv1.Deployment{}, &informertest.FakeInformers{
134+
Synced: &sync,
135+
}),
136+
}}
137+
138+
err = ctrl.Start(context.TODO())
139+
Expect(err).To(HaveOccurred())
140+
Expect(err.Error()).To(ContainSubstring("cache did not sync"))
141+
142+
close(done)
143+
})
144+
145+
It("should not error when cache sync time out is of reasonable value", func(done Done) {
146+
ctrl.CacheSyncTimeout = 1 * time.Second
147+
c, err := cache.New(cfg, cache.Options{})
148+
Expect(err).NotTo(HaveOccurred())
149+
ctrl.startWatches = []watchDescription{{
150+
src: source.NewKindWithCache(&appsv1.Deployment{}, c),
151+
}}
152+
153+
By("running the cache and waiting for it to sync")
154+
go func() {
155+
defer GinkgoRecover()
156+
Expect(c.Start(context.TODO())).To(Succeed())
157+
}()
158+
close(done)
159+
})
160+
161+
It("should error when timeout is set to a very low value such that cache cannot sync", func(done Done) {
162+
ctrl.CacheSyncTimeout = 1 * time.Nanosecond
163+
c, err := cache.New(cfg, cache.Options{})
164+
Expect(err).NotTo(HaveOccurred())
165+
ctrl.startWatches = []watchDescription{{
166+
src: source.NewKindWithCache(&appsv1.Deployment{}, c),
167+
}}
168+
169+
By("running the cache and waiting for it to sync")
170+
go func() {
171+
defer GinkgoRecover()
172+
err = ctrl.Start(context.TODO())
173+
Expect(err).To(HaveOccurred())
174+
Expect(err.Error()).To(ContainSubstring("cache did not sync"))
175+
}()
176+
close(done)
177+
})
178+
125179
It("should call Start on sources with the appropriate EventHandler, Queue, and Predicates", func() {
126180
pr1 := &predicate.Funcs{}
127181
pr2 := &predicate.Funcs{}

0 commit comments

Comments
 (0)