From c390e0d422f75671a1e913e3780246861b236e4f Mon Sep 17 00:00:00 2001 From: Wenqi Qiu Date: Thu, 10 Oct 2024 18:24:07 +0800 Subject: [PATCH] Update Signed-off-by: Wenqi Qiu --- .../subnetset/subnetset_controller.go | 32 +++++++++---------- pkg/nsx/services/subnet/store.go | 8 ++--- pkg/nsx/services/subnet/subnet.go | 4 +-- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/pkg/controllers/subnetset/subnetset_controller.go b/pkg/controllers/subnetset/subnetset_controller.go index 48f3ac60d..7c182c2d7 100644 --- a/pkg/controllers/subnetset/subnetset_controller.go +++ b/pkg/controllers/subnetset/subnetset_controller.go @@ -61,7 +61,7 @@ func (r *SubnetSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( if err := r.Client.Get(ctx, req.NamespacedName, subnetsetCR); err != nil { if apierrors.IsNotFound(err) { - if err := r.deleteSubnetBySubnetSetName(req.Name, req.Namespace); err != nil { + if err := r.deleteSubnetBySubnetSetName(ctx, req.Name, req.Namespace); err != nil { log.Error(err, "Failed to delete NSX Subnet", "SubnetSet", req.NamespacedName) return ResultRequeue, err } @@ -296,9 +296,9 @@ func (r *SubnetSetReconciler) CollectGarbage(ctx context.Context) { } } -func (r *SubnetSetReconciler) deleteSubnetBySubnetSetName(subnetSetName, ns string) error { +func (r *SubnetSetReconciler) deleteSubnetBySubnetSetName(ctx context.Context, subnetSetName, ns string) error { nsxSubnets := r.SubnetService.ListSubnetBySubnetSetName(ns, subnetSetName) - return r.deleteStaleSubnets(nsxSubnets) + return r.deleteStaleSubnets(ctx, nsxSubnets) } func (r *SubnetSetReconciler) deleteSubnetForSubnetSet(obj v1alpha1.SubnetSet, updateStatus bool) error { @@ -315,18 +315,18 @@ func (r *SubnetSetReconciler) deleteSubnetForSubnetSet(obj v1alpha1.SubnetSet, u return nil } -func (r *SubnetSetReconciler) listSubnetIDsFromCRs(ctx context.Context) ([]string, error) { - crdSubnetList := &v1alpha1.SubnetList{} - err := r.Client.List(ctx, crdSubnetList) +func (r *SubnetSetReconciler) listSubnetSetIDsFromCRs(ctx context.Context) ([]string, error) { + crdSubnetSetList := &v1alpha1.SubnetSetList{} + err := r.Client.List(ctx, crdSubnetSetList) if err != nil { return nil, err } - crdSubnetIDs := make([]string, 0, len(crdSubnetList.Items)) - for _, sr := range crdSubnetList.Items { - crdSubnetIDs = append(crdSubnetIDs, string(sr.UID)) + crdSubnetSetIDs := make([]string, 0, len(crdSubnetSetList.Items)) + for _, sr := range crdSubnetSetList.Items { + crdSubnetSetIDs = append(crdSubnetSetIDs, string(sr.UID)) } - return crdSubnetIDs, nil + return crdSubnetSetIDs, nil } func (r *SubnetSetReconciler) deleteSubnets(nsxSubnets []*model.VpcSubnet) error { @@ -347,18 +347,18 @@ func (r *SubnetSetReconciler) deleteSubnets(nsxSubnets []*model.VpcSubnet) error return nil } -func (r *SubnetSetReconciler) deleteStaleSubnets(nsxSubnets []*model.VpcSubnet) error { - crdSubnetIDs, err := r.listSubnetIDsFromCRs(context.Background()) +func (r *SubnetSetReconciler) deleteStaleSubnets(ctx context.Context, nsxSubnets []*model.VpcSubnet) error { + crdSubnetSetIDs, err := r.listSubnetSetIDsFromCRs(ctx) if err != nil { log.Error(err, "Failed to list Subnet CRs") return err } - crdSubnetIDsSet := sets.NewString(crdSubnetIDs...) + crdSubnetSetIDsSet := sets.NewString(crdSubnetSetIDs...) nsxSubnetsToDelete := make([]*model.VpcSubnet, 0, len(nsxSubnets)) for _, nsxSubnet := range nsxSubnets { - uid := nsxutil.FindTag(nsxSubnet.Tags, servicecommon.TagScopeSubnetCRUID) - if crdSubnetIDsSet.Has(uid) { - log.Info("Skipping deletion, Subnet CR still exists in K8s", "ID", *nsxSubnet.Id) + uid := nsxutil.FindTag(nsxSubnet.Tags, servicecommon.TagScopeSubnetSetCRUID) + if crdSubnetSetIDsSet.Has(uid) { + log.Info("Skipping deletion, SubnetSet CR still exists in K8s", "ID", *nsxSubnet.Id) continue } nsxSubnetsToDelete = append(nsxSubnetsToDelete, nsxSubnet) diff --git a/pkg/nsx/services/subnet/store.go b/pkg/nsx/services/subnet/store.go index 21607a8e3..6073e3cdc 100644 --- a/pkg/nsx/services/subnet/store.go +++ b/pkg/nsx/services/subnet/store.go @@ -39,21 +39,21 @@ func subnetIndexFunc(obj interface{}) ([]string, error) { } } -func subnetIndexNamespaceFunc(obj interface{}) ([]string, error) { +func subnetIndexVMNamespaceFunc(obj interface{}) ([]string, error) { switch o := obj.(type) { case *model.VpcSubnet: return filterTag(o.Tags, common.TagScopeVMNamespace), nil default: - return nil, errors.New("subnetIndexNamespaceFunc doesn't support unknown type") + return nil, errors.New("subnetIndexVMNamespaceFunc doesn't support unknown type") } } -func subnetIndexScopeNamespaceFunc(obj interface{}) ([]string, error) { +func subnetIndexNamespaceFunc(obj interface{}) ([]string, error) { switch o := obj.(type) { case *model.VpcSubnet: return filterTag(o.Tags, common.TagScopeNamespace), nil default: - return nil, errors.New("subnetIndexScopeNamespaceFunc doesn't support unknown type") + return nil, errors.New("subnetIndexNamespaceFunc doesn't support unknown type") } } diff --git a/pkg/nsx/services/subnet/subnet.go b/pkg/nsx/services/subnet/subnet.go index afde1544c..caad39431 100644 --- a/pkg/nsx/services/subnet/subnet.go +++ b/pkg/nsx/services/subnet/subnet.go @@ -58,8 +58,8 @@ func InitializeSubnetService(service common.Service) (*SubnetService, error) { Indexer: cache.NewIndexer(keyFunc, cache.Indexers{ common.TagScopeSubnetCRUID: subnetIndexFunc, common.TagScopeSubnetSetCRUID: subnetSetIndexFunc, - common.TagScopeVMNamespace: subnetIndexNamespaceFunc, - common.TagScopeNamespace: subnetIndexScopeNamespaceFunc, + common.TagScopeVMNamespace: subnetIndexVMNamespaceFunc, + common.TagScopeNamespace: subnetIndexNamespaceFunc, }), BindingType: model.VpcSubnetBindingType(), },