Skip to content

Commit 9b92896

Browse files
committed
Address comments from review
1 parent 998d96c commit 9b92896

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

pkg/updatestatus/nodeinformer.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ func newNodeInformerController(
9292
return controller
9393
}
9494

95+
// sync is called for any controller event. There are two kinds of them:
96+
// - standard Kubernetes informer events on watched resources such as nodes, machineConfigs, and machineConfigPools
97+
// - synthetic events such as eventNameReconcileAllNodes. They are generated for reconciling when handling events
98+
// of the first kind.
99+
//
100+
// A changed node resource produces insights that are sent to the update status controller.
101+
// A changed mc/mcp may produce the synthetic event eventNameReconcileAllNodes which triggers the reconciliation of all nodes.
95102
func (c *nodeInformerController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
96103
// Warm up controller's caches.
97104
// This has to be called after informers caches have been synced and before the first event comes in.
@@ -117,11 +124,8 @@ func (c *nodeInformerController) sync(ctx context.Context, syncCtx factory.SyncC
117124

118125
var msg informerMsg
119126
switch t {
120-
case eventKindName:
121-
if name != eventNameReconcileAllNodes {
122-
return fmt.Errorf("invalid name in queue key %s with type %s", queueKey, t)
123-
}
124-
return c.reconcileAllNodes(syncCtx.Queue())
127+
case syntheticKeyName:
128+
return c.syncSyntheticKey(name, syncCtx.Queue())
125129
case nodeKindName:
126130
msgP, err := c.syncNode(ctx, name)
127131
if err != nil {
@@ -260,7 +264,7 @@ func (c *machineConfigPoolSelectorCache) forget(mcpName string) bool {
260264
}
261265

262266
func queueKeyFoReconcileAllNodes(queue workqueue.TypedRateLimitingInterface[any]) {
263-
queue.Add(queueKeyFor(eventKindName, eventNameReconcileAllNodes))
267+
queue.Add(queueKeyFor(syntheticKeyName, eventNameReconcileAllNodes))
264268
}
265269

266270
func (c *nodeInformerController) reconcileAllNodes(queue workqueue.TypedRateLimitingInterface[any]) error {
@@ -364,6 +368,14 @@ func (c *nodeInformerController) syncNode(ctx context.Context, name string) (*in
364368
return &msg, nil
365369
}
366370

371+
func (c *nodeInformerController) syncSyntheticKey(name string, q workqueue.TypedRateLimitingInterface[any]) error {
372+
if name == eventNameReconcileAllNodes {
373+
return c.reconcileAllNodes(q)
374+
}
375+
klog.Errorf("Got an invalid synthetic key %s", name)
376+
return nil
377+
}
378+
367379
func makeInsightMsgForNode(nodeInsight *updatestatus.NodeStatusInsight, acquiredAt metav1.Time) (informerMsg, error) {
368380
insight := updatestatus.WorkerPoolInsight{
369381
UID: fmt.Sprintf("node-%s", nodeInsight.Resource.Name),
@@ -576,10 +588,13 @@ func assessNode(node *corev1.Node, mcp *machineconfigv1.MachineConfigPool, machi
576588
}
577589

578590
const (
579-
nodeKindName = "Node"
580-
machineConfigKindName = "MachineConfig"
581-
machineConfigPoolKindName = "MachineConfigPool"
582-
eventKindName = "Event"
591+
nodeKindName = "Node"
592+
machineConfigKindName = "MachineConfig"
593+
machineConfigPoolKindName = "MachineConfigPool"
594+
595+
syntheticKeyName = "synthetic"
596+
// eventNameReconcileAllNodes presents a synthetic event that is used when nodeInformerController should reconcile
597+
// all nodes.
583598
eventNameReconcileAllNodes = "reconcileAllNodes"
584599

585600
nodesInformerName = "ni"
@@ -598,11 +613,6 @@ func nodeInformerControllerQueueKeys(object runtime.Object) []string {
598613
return nil
599614
}
600615
switch o := object.(type) {
601-
case *corev1.Event:
602-
if o.Name != eventNameReconcileAllNodes {
603-
panic(fmt.Sprintf("USC :: Unknown object %s with type: %T", o.Name, object))
604-
}
605-
return []string{queueKeyFor(eventKindName, o.Name)}
606616
case *corev1.Node:
607617
return []string{queueKeyFor(nodeKindName, o.Name)}
608618
case *machineconfigv1.MachineConfig:

pkg/updatestatus/nodeinformer_test.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,33 +1140,22 @@ func Test_sync_with_event(t *testing.T) {
11401140
testCases := []struct {
11411141
name string
11421142

1143-
object runtime.Object
1144-
1145-
expectedPanic bool
1143+
key string
11461144
expectedErr error
11471145
expectedQueueLen int
11481146
}{
11491147
{
11501148
name: "reconcile for all nodes",
1151-
object: &corev1.Event{ObjectMeta: metav1.ObjectMeta{Name: "reconcileAllNodes"}},
1149+
key: "synthetic/reconcileAllNodes",
11521150
expectedQueueLen: 2,
11531151
},
11541152
{
1155-
name: "panic with aaa",
1156-
object: &corev1.Event{ObjectMeta: metav1.ObjectMeta{Name: "a"}},
1157-
expectedPanic: true,
1153+
name: "unknown key",
1154+
key: "synthetic/unknown",
11581155
},
11591156
}
11601157
for _, tc := range testCases {
11611158
t.Run(tc.name, func(t *testing.T) {
1162-
defer func() {
1163-
if tc.expectedPanic {
1164-
if r := recover(); r == nil {
1165-
t.Errorf("The expected panic did not happen")
1166-
}
1167-
}
1168-
}()
1169-
11701159
var actualMsgs []informerMsg
11711160
var sendInsight sendInsightFn = func(insight informerMsg) {
11721161
actualMsgs = append(actualMsgs, insight)
@@ -1178,9 +1167,8 @@ func Test_sync_with_event(t *testing.T) {
11781167
machineConfigPools: mcpLister,
11791168
sendInsight: sendInsight,
11801169
}
1181-
queueKey := nodeInformerControllerQueueKeys(tc.object)[0]
11821170

1183-
syncContext := newTestSyncContext(queueKey)
1171+
syncContext := newTestSyncContext(tc.key)
11841172
actualErr := controller.sync(context.TODO(), syncContext)
11851173

11861174
if diff := cmp.Diff(tc.expectedErr, actualErr, cmp.Comparer(func(x, y error) bool {

0 commit comments

Comments
 (0)