Skip to content

Commit 0f34b93

Browse files
authored
chore: index for listing gateways (#120)
1 parent d07a2ec commit 0f34b93

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

internal/controller/gatewayclass_congroller.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
1818

1919
"github.com/api7/api7-ingress-controller/internal/controller/config"
20+
"github.com/api7/api7-ingress-controller/internal/controller/indexer"
2021
)
2122

2223
const (
@@ -61,20 +62,18 @@ func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request
6162
} else {
6263
if controllerutil.ContainsFinalizer(gc, FinalizerGatewayClassProtection) {
6364
var gatewayList gatewayv1.GatewayList
64-
if err := r.List(ctx, &gatewayList); err != nil {
65+
if err := r.List(ctx, &gatewayList, client.MatchingFields{indexer.GatewayClassIndexRef: gc.Name}); err != nil {
6566
r.Log.Error(err, "failed to list gateways")
6667
return ctrl.Result{}, err
6768
}
68-
var gateways []types.NamespacedName
69-
for _, gateway := range gatewayList.Items {
70-
if string(gateway.Spec.GatewayClassName) == gc.GetName() {
69+
if len(gatewayList.Items) > 0 {
70+
var gateways []types.NamespacedName
71+
for _, item := range gatewayList.Items {
7172
gateways = append(gateways, types.NamespacedName{
72-
Namespace: gateway.GetNamespace(),
73-
Name: gateway.GetName(),
73+
Namespace: item.GetNamespace(),
74+
Name: item.GetName(),
7475
})
7576
}
76-
}
77-
if len(gateways) > 0 {
7877
r.Eventf(gc, "Warning", "DeletionBlocked", "the GatewayClass is still used by Gateways: %v", gateways)
7978
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
8079
} else {

internal/controller/indexer/indexer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
IngressClassParametersRef = "ingressClassParametersRef"
2525
ConsumerGatewayRef = "consumerGatewayRef"
2626
PolicyTargetRefs = "targetRefs"
27+
GatewayClassIndexRef = "gatewayClassRef"
2728
)
2829

2930
func SetupIndexer(mgr ctrl.Manager) error {
@@ -36,6 +37,7 @@ func SetupIndexer(mgr ctrl.Manager) error {
3637
setupIngressClassIndexer,
3738
setupGatewayProxyIndexer,
3839
setupGatewaySecretIndex,
40+
setupGatewayClassIndexer,
3941
} {
4042
if err := setup(mgr); err != nil {
4143
return err
@@ -193,6 +195,17 @@ func setupGatewaySecretIndex(mgr ctrl.Manager) error {
193195
)
194196
}
195197

198+
func setupGatewayClassIndexer(mgr ctrl.Manager) error {
199+
return mgr.GetFieldIndexer().IndexField(
200+
context.Background(),
201+
&gatewayv1.Gateway{},
202+
GatewayClassIndexRef,
203+
func(obj client.Object) (requests []string) {
204+
return []string{string(obj.(*gatewayv1.Gateway).Spec.GatewayClassName)}
205+
},
206+
)
207+
}
208+
196209
func GatewayProxySecretIndexFunc(rawObj client.Object) []string {
197210
gatewayProxy := rawObj.(*v1alpha1.GatewayProxy)
198211
secretKeys := make([]string, 0)

0 commit comments

Comments
 (0)