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 .Background (),
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
@@ -99,6 +120,13 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
99120 Secrets : make (map [types.NamespacedName ]* corev1.Secret ),
100121 }
101122 r .processListenerConfig (tctx , gateway , ns )
123+ if err := r .processGatewayProxy (tctx , gateway , ns ); err != nil {
124+ acceptStatus = status {
125+ status : false ,
126+ msg : err .Error (),
127+ }
128+ }
129+
102130 if err := r .Provider .Update (ctx , tctx , gateway ); err != nil {
103131 acceptStatus = status {
104132 status : false ,
@@ -181,6 +209,35 @@ func (r *GatewayReconciler) checkGatewayClass(gateway *gatewayv1.Gateway) bool {
181209 return matchesController (string (gatewayClass .Spec .ControllerName ))
182210}
183211
212+ func (r * GatewayReconciler ) listGatewaysForGatewayProxy (ctx context.Context , obj client.Object ) []reconcile.Request {
213+ gatewayProxy , ok := obj .(* v1alpha1.GatewayProxy )
214+ if ! ok {
215+ r .Log .Error (fmt .Errorf ("unexpected object type" ), "failed to convert object to GatewayProxy" )
216+ return nil
217+ }
218+ namespace := gatewayProxy .GetNamespace ()
219+ name := gatewayProxy .GetName ()
220+
221+ gatewayList := & gatewayv1.GatewayList {}
222+ if err := r .List (ctx , gatewayList , client.MatchingFields {
223+ indexer .ParametersRef : indexer .GenIndexKey (namespace , name ),
224+ }); err != nil {
225+ r .Log .Error (err , "failed to list gateways for gateway proxy" , "gatewayproxy" , gatewayProxy .GetName ())
226+ return nil
227+ }
228+
229+ recs := make ([]reconcile.Request , 0 , len (gatewayList .Items ))
230+ for _ , gateway := range gatewayList .Items {
231+ recs = append (recs , reconcile.Request {
232+ NamespacedName : client.ObjectKey {
233+ Namespace : gateway .GetNamespace (),
234+ Name : gateway .GetName (),
235+ },
236+ })
237+ }
238+ return recs
239+ }
240+
184241func (r * GatewayReconciler ) listGatewaysForHTTPRoute (_ context.Context , obj client.Object ) []reconcile.Request {
185242 httpRoute , ok := obj .(* gatewayv1.HTTPRoute )
186243 if ! ok {
@@ -215,6 +272,28 @@ func (r *GatewayReconciler) listGatewaysForHTTPRoute(_ context.Context, obj clie
215272 return recs
216273}
217274
275+ func (r * GatewayReconciler ) processGatewayProxy (tctx * provider.TranslateContext , gateway * gatewayv1.Gateway , ns string ) error {
276+ infra := gateway .Spec .Infrastructure
277+ if infra != nil && infra .ParametersRef != nil {
278+ paramRef := infra .ParametersRef
279+ if string (paramRef .Group ) == v1alpha1 .GroupVersion .Group && string (paramRef .Kind ) == "GatewayProxy" {
280+ gatewayProxy := & v1alpha1.GatewayProxy {}
281+ if err := r .Get (context .Background (), client.ObjectKey {
282+ Namespace : ns ,
283+ Name : paramRef .Name ,
284+ }, gatewayProxy ); err != nil {
285+ log .Error (err , "failed to get GatewayProxy" , "namespace" , ns , "name" , paramRef .Name )
286+ return err
287+ } else {
288+ log .Info ("found GatewayProxy for Gateway" , "gateway" , gateway .Name , "gatewayproxy" , gatewayProxy .Name )
289+ tctx .GatewayProxy = gatewayProxy
290+ }
291+ }
292+ }
293+
294+ return nil
295+ }
296+
218297func (r * GatewayReconciler ) processListenerConfig (tctx * provider.TranslateContext , gateway * gatewayv1.Gateway , ns string ) {
219298 listeners := gateway .Spec .Listeners
220299 for _ , listener := range listeners {
0 commit comments