Skip to content

Commit c3483f4

Browse files
committed
usc: init controller's caches before syncing
1 parent c332cca commit c3483f4

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

pkg/updatestatus/nodeinformer.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
9194
func (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+
197236
type 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+
243288
type machineConfigPoolSelectorCache struct {
244289
cache map[string]labels.Selector
245290
lock sync.Mutex

pkg/updatestatus/nodeinformer_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import (
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414
"k8s.io/apimachinery/pkg/labels"
1515
"k8s.io/apimachinery/pkg/runtime"
16-
kerrors "k8s.io/apimachinery/pkg/util/errors"
1716
corelistersv1 "k8s.io/client-go/listers/core/v1"
1817
"k8s.io/client-go/tools/cache"
19-
"k8s.io/klog/v2"
2018

2119
configv1 "github.com/openshift/api/config/v1"
2220
machineconfigv1 "github.com/openshift/api/machineconfiguration/v1"
@@ -150,32 +148,6 @@ func getMCP(name string) *machineconfigv1.MachineConfigPool {
150148
}
151149
}
152150

153-
func (c *nodeInformerController) initializeCaches() error {
154-
var errs []error
155-
156-
if pools, err := c.machineConfigPools.List(labels.Everything()); err != nil {
157-
errs = append(errs, err)
158-
} else {
159-
for _, pool := range pools {
160-
c.machineConfigPoolSelectorCache.ingest(pool)
161-
}
162-
}
163-
klog.V(2).Infof("Stored %d machineConfigPools in the cache", len(c.machineConfigPoolSelectorCache.cache))
164-
165-
machineConfigs, err := c.machineConfigs.List(labels.Everything())
166-
if err != nil {
167-
errs = append(errs, err)
168-
} else {
169-
for _, mc := range machineConfigs {
170-
c.machineConfigVersionCache.ingest(mc)
171-
}
172-
}
173-
174-
klog.V(2).Infof("Stored %d machineConfig versions in the cache", len(c.machineConfigVersionCache.cache))
175-
176-
return kerrors.NewAggregate(errs)
177-
}
178-
179151
func Test_whichMCP(t *testing.T) {
180152
testCases := []struct {
181153
name string

0 commit comments

Comments
 (0)