From ba4f45a9106aa77622eb0808a2f7be04dfd26f6f Mon Sep 17 00:00:00 2001 From: Wenqi Qiu Date: Sat, 12 Oct 2024 20:56:24 +0800 Subject: [PATCH] Address comments Signed-off-by: Wenqi Qiu --- pkg/controllers/subnetset/subnetset_controller.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/controllers/subnetset/subnetset_controller.go b/pkg/controllers/subnetset/subnetset_controller.go index 6a4aefc50..5c06f48e3 100644 --- a/pkg/controllers/subnetset/subnetset_controller.go +++ b/pkg/controllers/subnetset/subnetset_controller.go @@ -262,7 +262,7 @@ func (r *SubnetSetReconciler) CollectGarbage(ctx context.Context) { log.Info("SubnetSet garbage collection completed", "duration(ms)", time.Since(startTime).Milliseconds()) }() - crdSubnetSetIDs, err := r.listSubnetSetIDsFromCRs(ctx) + crdSubnetSetIDs, err := r.listSubnetSetIDsFromCRs(ctx, true) if err != nil { log.Error(err, "Failed to list Subnet CRs") return @@ -306,7 +306,7 @@ func (r *SubnetSetReconciler) deleteSubnetForSubnetSet(obj v1alpha1.SubnetSet, u return nil } -func (r *SubnetSetReconciler) listSubnetSetIDsFromCRs(ctx context.Context) ([]string, error) { +func (r *SubnetSetReconciler) listSubnetSetIDsFromCRs(ctx context.Context, deleteSubnetForSubnetSet bool) ([]string, error) { crdSubnetSetList := &v1alpha1.SubnetSetList{} err := r.Client.List(ctx, crdSubnetSetList) if err != nil { @@ -317,6 +317,15 @@ func (r *SubnetSetReconciler) listSubnetSetIDsFromCRs(ctx context.Context) ([]st for _, sr := range crdSubnetSetList.Items { crdSubnetSetIDs = append(crdSubnetSetIDs, string(sr.UID)) } + if deleteSubnetForSubnetSet { + for _, subnetSet := range crdSubnetSetList.Items { + if err := r.deleteSubnetForSubnetSet(subnetSet, true); err != nil { + metrics.CounterInc(r.SubnetService.NSXConfig, metrics.ControllerDeleteFailTotal, MetricResTypeSubnetSet) + } else { + metrics.CounterInc(r.SubnetService.NSXConfig, metrics.ControllerDeleteSuccessTotal, MetricResTypeSubnetSet) + } + } + } return crdSubnetSetIDs, nil } @@ -339,7 +348,7 @@ func (r *SubnetSetReconciler) deleteSubnets(nsxSubnets []*model.VpcSubnet) error } func (r *SubnetSetReconciler) deleteStaleSubnets(ctx context.Context, nsxSubnets []*model.VpcSubnet) error { - crdSubnetSetIDs, err := r.listSubnetSetIDsFromCRs(ctx) + crdSubnetSetIDs, err := r.listSubnetSetIDsFromCRs(ctx, false) if err != nil { log.Error(err, "Failed to list Subnet CRs") return err