@@ -490,7 +490,7 @@ func (c *clusterCache) startMissingWatches() error {
490
490
c .apisMeta [api .GroupKind ] = & apiMeta {namespaced : api .Meta .Namespaced , watchCancel : cancel }
491
491
492
492
err := c .processApi (client , api , func (resClient dynamic.ResourceInterface , ns string ) error {
493
- resourceVersion , err := c .loadInitialState (ctx , api , resClient , ns )
493
+ resourceVersion , err := c .loadInitialState (ctx , api , resClient , ns , false ) // don't lock here, we are already in a lock before startMissingWatches is called inside watchEvents
494
494
if err != nil && c .isRestrictedResource (err ) {
495
495
keep := false
496
496
if c .respectRBAC == RespectRbacStrict {
@@ -566,7 +566,7 @@ func (c *clusterCache) listResources(ctx context.Context, resClient dynamic.Reso
566
566
return resourceVersion , callback (listPager )
567
567
}
568
568
569
- func (c * clusterCache ) loadInitialState (ctx context.Context , api kube.APIResourceInfo , resClient dynamic.ResourceInterface , ns string ) (string , error ) {
569
+ func (c * clusterCache ) loadInitialState (ctx context.Context , api kube.APIResourceInfo , resClient dynamic.ResourceInterface , ns string , lock bool ) (string , error ) {
570
570
return c .listResources (ctx , resClient , func (listPager * pager.ListPager ) error {
571
571
var items []* Resource
572
572
err := listPager .EachListItem (ctx , metav1.ListOptions {}, func (obj runtime.Object ) error {
@@ -581,11 +581,15 @@ func (c *clusterCache) loadInitialState(ctx context.Context, api kube.APIResourc
581
581
if err != nil {
582
582
return fmt .Errorf ("failed to load initial state of resource %s: %w" , api .GroupKind .String (), err )
583
583
}
584
-
585
- return runSynced (& c .lock , func () error {
584
+ if lock {
585
+ return runSynced (& c .lock , func () error {
586
+ c .replaceResourceCache (api .GroupKind , items , ns )
587
+ return nil
588
+ })
589
+ } else {
586
590
c .replaceResourceCache (api .GroupKind , items , ns )
587
591
return nil
588
- })
592
+ }
589
593
})
590
594
}
591
595
@@ -599,7 +603,7 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo
599
603
600
604
// load API initial state if no resource version provided
601
605
if resourceVersion == "" {
602
- resourceVersion , err = c .loadInitialState (ctx , api , resClient , ns )
606
+ resourceVersion , err = c .loadInitialState (ctx , api , resClient , ns , true )
603
607
if err != nil {
604
608
return err
605
609
}
0 commit comments