Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Signed-off-by: Wenqi Qiu <[email protected]>
  • Loading branch information
wenqiq committed Oct 10, 2024
1 parent 1629917 commit c390e0d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
32 changes: 16 additions & 16 deletions pkg/controllers/subnetset/subnetset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pkg/nsx/services/subnet/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/nsx/services/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down

0 comments on commit c390e0d

Please sign in to comment.