@@ -12,6 +12,7 @@ import (
1212 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313 "k8s.io/apimachinery/pkg/labels"
1414 "k8s.io/apimachinery/pkg/runtime"
15+ kutilerrors "k8s.io/apimachinery/pkg/util/errors"
1516 kubeinformers "k8s.io/client-go/informers"
1617 corelistersv1 "k8s.io/client-go/listers/core/v1"
1718 "k8s.io/client-go/util/workqueue"
@@ -88,7 +89,14 @@ func newNodeInformerController(
8889 return controller
8990}
9091
92+ var once sync.Once
93+
9194func (c * nodeInformerController ) sync (ctx context.Context , syncCtx factory.SyncContext ) error {
95+ // Warm up controller's caches.
96+ // This has to be called after informers caches have been synced and before the first event comes in.
97+ // The existing openshift-library does not provide such a hook.
98+ once .Do (c .initializeCachesNoErrors )
99+
92100 queueKey := syncCtx .QueueKey ()
93101 klog .V (4 ).Infof ("NI :: Syncing with key %s" , queueKey )
94102
@@ -194,6 +202,37 @@ func (c *nodeInformerController) sync(ctx context.Context, syncCtx factory.SyncC
194202 return nil
195203}
196204
205+ func (c * nodeInformerController ) initializeCachesNoErrors () {
206+ if err := c .initializeCaches (); err != nil {
207+ klog .Errorf ("Failed to initialize caches: %v" , err )
208+ }
209+ }
210+
211+ func (c * nodeInformerController ) initializeCaches () error {
212+ var errs []error
213+
214+ if pools , err := c .machineConfigPools .List (labels .Everything ()); err != nil {
215+ errs = append (errs , err )
216+ } else {
217+ for _ , pool := range pools {
218+ c .machineConfigPoolSelectorCache .ingest (pool )
219+ }
220+ }
221+ klog .V (2 ).Infof ("Stored %d machineConfigPools in the cache" , c .machineConfigPoolSelectorCache .len ())
222+
223+ machineConfigs , err := c .machineConfigs .List (labels .Everything ())
224+ if err != nil {
225+ errs = append (errs , err )
226+ } else {
227+ for _ , mc := range machineConfigs {
228+ c .machineConfigVersionCache .ingest (mc )
229+ }
230+ }
231+ klog .V (2 ).Infof ("Stored %d machineConfig versions in the cache" , c .machineConfigVersionCache .len ())
232+
233+ return kutilerrors .NewAggregate (errs )
234+ }
235+
197236type machineConfigVersionCache struct {
198237 cache map [string ]string
199238 lock sync.Mutex
@@ -240,6 +279,12 @@ func (c *machineConfigVersionCache) match(config string) (string, bool) {
240279 return v , ok
241280}
242281
282+ func (c * machineConfigVersionCache ) len () int {
283+ c .lock .Lock ()
284+ defer c .lock .Unlock ()
285+ return len (c .cache )
286+ }
287+
243288type machineConfigPoolSelectorCache struct {
244289 cache map [string ]labels.Selector
245290 lock sync.Mutex
0 commit comments