From 69c86931673e405bee35240d07f39b65d00dc714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=82=9F=E7=A9=BA?= Date: Wed, 30 Apr 2025 16:29:55 +0800 Subject: [PATCH 1/2] chore: add indexer for listing gateways by classname --- internal/controller/indexer/indexer.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/controller/indexer/indexer.go b/internal/controller/indexer/indexer.go index 42ccddc92..47c75a44d 100644 --- a/internal/controller/indexer/indexer.go +++ b/internal/controller/indexer/indexer.go @@ -24,6 +24,7 @@ const ( IngressClassParametersRef = "ingressClassParametersRef" ConsumerGatewayRef = "consumerGatewayRef" PolicyTargetRefs = "targetRefs" + GatewayClassIndexRef = "gatewayClassRef" ) func SetupIndexer(mgr ctrl.Manager) error { @@ -191,6 +192,17 @@ func setupGatewayProxyIndexer(mgr ctrl.Manager) error { return nil } +func setupGatewayClassIndexer(mgr ctrl.Manager) error { + return mgr.GetFieldIndexer().IndexField( + context.Background(), + &gatewayv1.Gateway{}, + GatewayClassIndexRef, + func(obj client.Object) (requests []string) { + return []string{string(obj.(*gatewayv1.Gateway).Spec.GatewayClassName)} + }, + ) +} + func GatewayProxySecretIndexFunc(rawObj client.Object) []string { gatewayProxy := rawObj.(*v1alpha1.GatewayProxy) secretKeys := make([]string, 0) From bd81b5ace46c25264aaa367f4a5c4a20022c8f29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=82=9F=E7=A9=BA?= Date: Wed, 30 Apr 2025 16:47:41 +0800 Subject: [PATCH 2/2] Refactor GatewayClass protection logic to use indexer Simplify the GatewayClass deletion protection by leveraging an indexer to filter Gateways. This improves efficiency and readability by reducing unnecessary iterations and directly querying relevant resources. --- internal/controller/gatewayclass_congroller.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/controller/gatewayclass_congroller.go b/internal/controller/gatewayclass_congroller.go index 26578cf64..939a3c25d 100644 --- a/internal/controller/gatewayclass_congroller.go +++ b/internal/controller/gatewayclass_congroller.go @@ -17,6 +17,7 @@ import ( gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" "github.com/api7/api7-ingress-controller/internal/controller/config" + "github.com/api7/api7-ingress-controller/internal/controller/indexer" ) const ( @@ -61,20 +62,18 @@ func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request } else { if controllerutil.ContainsFinalizer(gc, FinalizerGatewayClassProtection) { var gatewayList gatewayv1.GatewayList - if err := r.List(ctx, &gatewayList); err != nil { + if err := r.List(ctx, &gatewayList, client.MatchingFields{indexer.GatewayClassIndexRef: gc.Name}); err != nil { r.Log.Error(err, "failed to list gateways") return ctrl.Result{}, err } - var gateways []types.NamespacedName - for _, gateway := range gatewayList.Items { - if string(gateway.Spec.GatewayClassName) == gc.GetName() { + if len(gatewayList.Items) > 0 { + var gateways []types.NamespacedName + for _, item := range gatewayList.Items { gateways = append(gateways, types.NamespacedName{ - Namespace: gateway.GetNamespace(), - Name: gateway.GetName(), + Namespace: item.GetNamespace(), + Name: item.GetName(), }) } - } - if len(gateways) > 0 { r.Eventf(gc, "Warning", "DeletionBlocked", "the GatewayClass is still used by Gateways: %v", gateways) return ctrl.Result{RequeueAfter: 5 * time.Second}, nil } else {