Skip to content

Commit

Permalink
Fix bug which should delete node from store when there is node not fo…
Browse files Browse the repository at this point in the history
…und error

When node is deleted, the phase node.ObjectMeta.DeletionTimestamp being not zero is intermittent,
and it is more possible the node is not found, we should delete the node from store.
  • Loading branch information
zhengxiexie committed Dec 31, 2024
1 parent c729a64 commit 28cc7ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pkg/controllers/node/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
if errors.IsNotFound(err) {
log.Info("node not found", "req", req.NamespacedName)
deleted = true
if err := r.Service.SyncNodeStore(req.NamespacedName.Name, deleted); err != nil {
log.Error(err, "failed to sync node store", "req", req.NamespacedName)
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
} else {
log.Error(err, "unable to fetch node", "req", req.NamespacedName)
return common.ResultNormal, client.IgnoreNotFound(err)
}
return common.ResultNormal, client.IgnoreNotFound(err)
}
if common.NodeIsMaster(node) {
// For WCP supervisor cluster, the master node isn't a transport node.
Expand Down
21 changes: 18 additions & 3 deletions pkg/controllers/node/node_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ import (

"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/metrics"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
servicecommon "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/node"
)

func createMockNodeService() *node.NodeService {
return &node.NodeService{
Service: servicecommon.Service{
NSXConfig: &config.NSXOperatorConfig{
NsxConfig: &config.NsxConfig{},
NSXClient: &nsx.Client{
NsxConfig: &config.NSXOperatorConfig{
CoeConfig: &config.CoeConfig{
Cluster: "k8scl-one:test",
},
},
},
},
}
Expand Down Expand Up @@ -90,18 +95,28 @@ func TestNodeReconciler_Reconcile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
nodeName := "test-node"

if tt.existingNode != nil {
nodeName = tt.existingNode.Name
err := reconciler.Client.Create(ctx, tt.existingNode)
assert.NoError(t, err)
}

req := ctrl.Request{
NamespacedName: types.NamespacedName{
Name: "test-node",
Name: nodeName,
},
}

if tt.name != "Master Node" {
patchesSyncNodeStore := gomonkey.ApplyFunc((*node.NodeService).SyncNodeStore,
func(n *node.NodeService, nodeName string, deleted bool) error {
return nil
})
defer patchesSyncNodeStore.Reset()
}

result, err := reconciler.Reconcile(ctx, req)

if tt.expectedErr {
Expand Down

0 comments on commit 28cc7ca

Please sign in to comment.