55 "fmt"
66 "reflect"
77
8+ "github.com/api7/api7-ingress-controller/api/v1alpha1"
89 "github.com/api7/api7-ingress-controller/internal/controller/config"
10+ "github.com/api7/api7-ingress-controller/internal/controller/indexer"
911 "github.com/api7/api7-ingress-controller/internal/provider"
1012 "github.com/api7/gopkg/pkg/log"
1113 "github.com/go-logr/logr"
@@ -33,8 +35,23 @@ type GatewayReconciler struct { //nolint:revive
3335 Provider provider.Provider
3436}
3537
38+ func (r * GatewayReconciler ) setupIndexer (mgr ctrl.Manager ) error {
39+ if err := mgr .GetFieldIndexer ().IndexField (
40+ context .TODO (),
41+ & gatewayv1.Gateway {},
42+ indexer .ParametersRef ,
43+ indexer .GatewayParametersRefIndexFunc ,
44+ ); err != nil {
45+ return err
46+ }
47+ return nil
48+ }
49+
3650// SetupWithManager sets up the controller with the Manager.
3751func (r * GatewayReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
52+ if err := r .setupIndexer (mgr ); err != nil {
53+ return err
54+ }
3855 return ctrl .NewControllerManagedBy (mgr ).
3956 For (& gatewayv1.Gateway {}).
4057 Watches (
@@ -46,6 +63,10 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
4663 & gatewayv1.HTTPRoute {},
4764 handler .EnqueueRequestsFromMapFunc (r .listGatewaysForHTTPRoute ),
4865 ).
66+ Watches (
67+ & v1alpha1.GatewayProxy {},
68+ handler .EnqueueRequestsFromMapFunc (r .listGatewaysForGatewayProxy ),
69+ ).
4970 Complete (r )
5071}
5172
@@ -181,6 +202,35 @@ func (r *GatewayReconciler) checkGatewayClass(gateway *gatewayv1.Gateway) bool {
181202 return matchesController (string (gatewayClass .Spec .ControllerName ))
182203}
183204
205+ func (r * GatewayReconciler ) listGatewaysForGatewayProxy (ctx context.Context , obj client.Object ) []reconcile.Request {
206+ gatewayProxy , ok := obj .(* v1alpha1.GatewayProxy )
207+ if ! ok {
208+ r .Log .Error (fmt .Errorf ("unexpected object type" ), "failed to convert object to GatewayProxy" )
209+ return nil
210+ }
211+ namespace := gatewayProxy .GetNamespace ()
212+ name := gatewayProxy .GetName ()
213+
214+ gatewayList := & gatewayv1.GatewayList {}
215+ if err := r .List (ctx , gatewayList , client.MatchingFields {
216+ indexer .ParametersRef : indexer .GenIndexKey (namespace , name ),
217+ }); err != nil {
218+ r .Log .Error (err , "failed to list gateways for gateway proxy" , "gatewayproxy" , gatewayProxy .GetName ())
219+ return nil
220+ }
221+
222+ recs := make ([]reconcile.Request , 0 , len (gatewayList .Items ))
223+ for _ , gateway := range gatewayList .Items {
224+ recs = append (recs , reconcile.Request {
225+ NamespacedName : client.ObjectKey {
226+ Namespace : gateway .GetNamespace (),
227+ Name : gateway .GetName (),
228+ },
229+ })
230+ }
231+ return recs
232+ }
233+
184234func (r * GatewayReconciler ) listGatewaysForHTTPRoute (_ context.Context , obj client.Object ) []reconcile.Request {
185235 httpRoute , ok := obj .(* gatewayv1.HTTPRoute )
186236 if ! ok {
0 commit comments