diff --git a/pkg/proxy/config/config.go b/pkg/proxy/config/config.go index 1f24196e90c..ca213329354 100644 --- a/pkg/proxy/config/config.go +++ b/pkg/proxy/config/config.go @@ -260,12 +260,9 @@ func (c *ServiceConfig) handleDeleteService(obj interface{}) { // NodeHandler is an abstract interface of objects which receive // notifications about node object changes. type NodeHandler interface { - // OnNodeAdd is called whenever creation of new node object - // is observed. - OnNodeAdd(node *v1.Node) - // OnNodeUpdate is called whenever modification of an existing - // node object is observed. - OnNodeUpdate(oldNode, node *v1.Node) + // OnNodeChange is called whenever creation or modification + // of node object is observed. + OnNodeChange(node *v1.Node) // OnNodeDelete is called whenever deletion of an existing node // object is observed. OnNodeDelete(node *v1.Node) @@ -290,8 +287,8 @@ func NewNodeConfig(ctx context.Context, nodeInformer v1informers.NodeInformer, r handlerRegistration, _ := nodeInformer.Informer().AddEventHandlerWithResyncPeriod( cache.ResourceEventHandlerFuncs{ - AddFunc: result.handleAddNode, - UpdateFunc: result.handleUpdateNode, + AddFunc: func(obj interface{}) { result.handleChangeNode(obj) }, + UpdateFunc: func(_, newObj interface{}) { result.handleChangeNode(newObj) }, DeleteFunc: result.handleDeleteNode, }, resyncPeriod, @@ -321,32 +318,15 @@ func (c *NodeConfig) Run(stopCh <-chan struct{}) { } } -func (c *NodeConfig) handleAddNode(obj interface{}) { +func (c *NodeConfig) handleChangeNode(obj interface{}) { node, ok := obj.(*v1.Node) if !ok { utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", obj)) return } for i := range c.eventHandlers { - c.logger.V(4).Info("Calling handler.OnNodeAdd") - c.eventHandlers[i].OnNodeAdd(node) - } -} - -func (c *NodeConfig) handleUpdateNode(oldObj, newObj interface{}) { - oldNode, ok := oldObj.(*v1.Node) - if !ok { - utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", oldObj)) - return - } - node, ok := newObj.(*v1.Node) - if !ok { - utilruntime.HandleError(fmt.Errorf("unexpected object type: %v", newObj)) - return - } - for i := range c.eventHandlers { - c.logger.V(5).Info("Calling handler.OnNodeUpdate") - c.eventHandlers[i].OnNodeUpdate(oldNode, node) + c.logger.V(4).Info("Calling handler.OnNodeChange") + c.eventHandlers[i].OnNodeChange(node) } } diff --git a/pkg/proxy/config/config_test.go b/pkg/proxy/config/config_test.go index 4ab3e4c93b7..e52b2c2773d 100644 --- a/pkg/proxy/config/config_test.go +++ b/pkg/proxy/config/config_test.go @@ -454,14 +454,7 @@ func NewNodeHandlerMock() *NodeHandlerMock { return h } -func (h *NodeHandlerMock) OnNodeAdd(node *v1.Node) { - h.lock.Lock() - defer h.lock.Unlock() - h.state[node.Name] = node - h.sendNodes() -} - -func (h *NodeHandlerMock) OnNodeUpdate(oldNode, node *v1.Node) { +func (h *NodeHandlerMock) OnNodeChange(node *v1.Node) { h.lock.Lock() defer h.lock.Unlock() h.state[node.Name] = node diff --git a/pkg/proxy/healthcheck/healthcheck_test.go b/pkg/proxy/healthcheck/healthcheck_test.go index 5f39563e745..e5924296dcd 100644 --- a/pkg/proxy/healthcheck/healthcheck_test.go +++ b/pkg/proxy/healthcheck/healthcheck_test.go @@ -495,7 +495,7 @@ func TestHealthzServer(t *testing.T) { testProxyHealthUpdater(hs, hsTest, fakeClock, ptr.To(true), t) // Should return 200 "OK" if we've synced a node, tainted in any other way - nodeManager.OnNodeUpdate(nil, makeNode(tweakTainted("other"))) + nodeManager.OnNodeChange(makeNode(tweakTainted("other"))) expectedPayload = ProxyHealth{ CurrentTime: fakeClock.Now(), LastUpdated: fakeClock.Now(), @@ -509,7 +509,7 @@ func TestHealthzServer(t *testing.T) { testHTTPHandler(hsTest, http.StatusOK, expectedPayload, t) // Should return 503 "ServiceUnavailable" if we've synced a ToBeDeletedTaint node - nodeManager.OnNodeUpdate(nil, makeNode(tweakTainted(ToBeDeletedTaint))) + nodeManager.OnNodeChange(makeNode(tweakTainted(ToBeDeletedTaint))) expectedPayload = ProxyHealth{ CurrentTime: fakeClock.Now(), LastUpdated: fakeClock.Now(), @@ -523,7 +523,7 @@ func TestHealthzServer(t *testing.T) { testHTTPHandler(hsTest, http.StatusServiceUnavailable, expectedPayload, t) // Should return 200 "OK" if we've synced a node, tainted in any other way - nodeManager.OnNodeUpdate(nil, makeNode(tweakTainted("other"))) + nodeManager.OnNodeChange(makeNode(tweakTainted("other"))) expectedPayload = ProxyHealth{ CurrentTime: fakeClock.Now(), LastUpdated: fakeClock.Now(), @@ -537,7 +537,7 @@ func TestHealthzServer(t *testing.T) { testHTTPHandler(hsTest, http.StatusOK, expectedPayload, t) // Should return 503 "ServiceUnavailable" if we've synced a deleted node - nodeManager.OnNodeUpdate(nil, makeNode(tweakDeleted())) + nodeManager.OnNodeChange(makeNode(tweakDeleted())) expectedPayload = ProxyHealth{ CurrentTime: fakeClock.Now(), LastUpdated: fakeClock.Now(), @@ -575,7 +575,7 @@ func TestLivezServer(t *testing.T) { testProxyHealthUpdater(hs, hsTest, fakeClock, nil, t) // Should return 200 "OK" irrespective of node syncs - nodeManager.OnNodeUpdate(nil, makeNode(tweakTainted("other"))) + nodeManager.OnNodeChange(makeNode(tweakTainted("other"))) expectedPayload = ProxyHealth{ CurrentTime: fakeClock.Now(), LastUpdated: fakeClock.Now(), @@ -588,7 +588,7 @@ func TestLivezServer(t *testing.T) { testHTTPHandler(hsTest, http.StatusOK, expectedPayload, t) // Should return 200 "OK" irrespective of node syncs - nodeManager.OnNodeUpdate(nil, makeNode(tweakTainted(ToBeDeletedTaint))) + nodeManager.OnNodeChange(makeNode(tweakTainted(ToBeDeletedTaint))) expectedPayload = ProxyHealth{ CurrentTime: fakeClock.Now(), LastUpdated: fakeClock.Now(), @@ -601,7 +601,7 @@ func TestLivezServer(t *testing.T) { testHTTPHandler(hsTest, http.StatusOK, expectedPayload, t) // Should return 200 "OK" irrespective of node syncs - nodeManager.OnNodeUpdate(nil, makeNode(tweakTainted("other"))) + nodeManager.OnNodeChange(makeNode(tweakTainted("other"))) expectedPayload = ProxyHealth{ CurrentTime: fakeClock.Now(), LastUpdated: fakeClock.Now(), @@ -614,7 +614,7 @@ func TestLivezServer(t *testing.T) { testHTTPHandler(hsTest, http.StatusOK, expectedPayload, t) // Should return 200 "OK" irrespective of node syncs - nodeManager.OnNodeUpdate(nil, makeNode(tweakDeleted())) + nodeManager.OnNodeChange(makeNode(tweakDeleted())) expectedPayload = ProxyHealth{ CurrentTime: fakeClock.Now(), LastUpdated: fakeClock.Now(), diff --git a/pkg/proxy/node.go b/pkg/proxy/node.go index a3b54d9f4e2..3bd7a47f8b9 100644 --- a/pkg/proxy/node.go +++ b/pkg/proxy/node.go @@ -171,18 +171,8 @@ func (n *NodeManager) NodeInformer() v1informers.NodeInformer { return n.nodeInformer } -// OnNodeAdd is a handler for Node creates. -func (n *NodeManager) OnNodeAdd(node *v1.Node) { - n.onNodeChange(node) -} - -// OnNodeUpdate is a handler for Node updates. -func (n *NodeManager) OnNodeUpdate(_, node *v1.Node) { - n.onNodeChange(node) -} - -// onNodeChange functions helps to implement OnNodeAdd and OnNodeUpdate. -func (n *NodeManager) onNodeChange(node *v1.Node) { +// OnNodeChange is a handler for Node creation and update. +func (n *NodeManager) OnNodeChange(node *v1.Node) { // update the node object n.mu.Lock() n.node = node diff --git a/pkg/proxy/node_test.go b/pkg/proxy/node_test.go index 9d0120c8294..cbb2205d362 100644 --- a/pkg/proxy/node_test.go +++ b/pkg/proxy/node_test.go @@ -294,7 +294,7 @@ func TestNodeManagerOnNodeChange(t *testing.T) { nodeManager, err := newNodeManager(ctx, client, 30*time.Second, testNodeName, tc.watchPodCIDRs, exitFunc, 10*time.Millisecond, time.Second, time.Second) require.NoError(t, err) - nodeManager.onNodeChange(makeNode(tweakNodeIPs(tc.updatedNodeIPs...), tweakPodCIDRs(tc.updatedPodCIDRs...))) + nodeManager.OnNodeChange(makeNode(tweakNodeIPs(tc.updatedNodeIPs...), tweakPodCIDRs(tc.updatedPodCIDRs...))) require.Equal(t, tc.expectedExitCode, exitCode) }) } @@ -328,7 +328,7 @@ func TestNodeManagerNode(t *testing.T) { require.NoError(t, err) require.Equal(t, "1", nodeManager.Node().ResourceVersion) - nodeManager.OnNodeUpdate(nil, makeNode(tweakResourceVersion("2"))) + nodeManager.OnNodeChange(makeNode(tweakResourceVersion("2"))) require.NoError(t, err) require.Equal(t, "2", nodeManager.Node().ResourceVersion) }