@@ -38,6 +38,10 @@ const (
3838 // deployment record API. Once an artifact is known to be missing,
3939 // we suppress further API calls for this duration.
4040 unknownArtifactTTL = 1 * time .Hour
41+
42+ // informerSyncTimeoutDuration is the maximum duration of time allowed
43+ // for the informers to sync to prevent the controller from hanging indefinitely.
44+ informerSyncTimeoutDuration = 60 * time .Second
4145)
4246
4347type ttlCache interface {
@@ -92,6 +96,9 @@ type Controller struct {
9296 // best effort cache to suppress API calls for artifacts that
9397 // returned a 404 (no artifact found). Keyed by image digest.
9498 unknownArtifacts ttlCache
99+ // informerSyncTimeout is the maximum time allowed for all informers to sync
100+ // and prevents sync from hanging indefinitely.
101+ informerSyncTimeout time.Duration
95102}
96103
97104// New creates a new deployment tracker controller.
@@ -160,6 +167,7 @@ func New(clientset kubernetes.Interface, metadataAggregator podMetadataAggregato
160167 cfg : cfg ,
161168 observedDeployments : amcache .NewExpiring (),
162169 unknownArtifacts : amcache .NewExpiring (),
170+ informerSyncTimeout : informerSyncTimeoutDuration ,
163171 }
164172
165173 // Add event handlers to the informer
@@ -320,16 +328,20 @@ func (c *Controller) Run(ctx context.Context, workers int) error {
320328
321329 // Wait for the caches to be synced
322330 slog .Info ("Waiting for informer caches to sync" )
323- if ! cache .WaitForCacheSync (ctx .Done (),
331+ informerSyncCxt , cancel := context .WithTimeout (ctx , c .informerSyncTimeout )
332+
333+ if ! cache .WaitForCacheSync (informerSyncCxt .Done (),
324334 c .podInformer .HasSynced ,
325335 c .deploymentInformer .HasSynced ,
326336 c .daemonSetInformer .HasSynced ,
327337 c .statefulSetInformer .HasSynced ,
328338 c .jobInformer .HasSynced ,
329339 c .cronJobInformer .HasSynced ,
330340 ) {
331- return errors .New ("timed out waiting for caches to sync" )
341+ cancel ()
342+ return errors .New ("timed out waiting for caches to sync - please ensure deployment tracker has the correct kubernetes permissions" )
332343 }
344+ cancel ()
333345
334346 slog .Info ("Starting workers" ,
335347 "count" , workers ,
0 commit comments