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,10 +35,26 @@ 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 {}).
57+ WithEventFilter (predicate.GenerationChangedPredicate {}).
4058 Watches (
4159 & gatewayv1.GatewayClass {},
4260 handler .EnqueueRequestsFromMapFunc (r .listGatewayForGatewayClass ),
@@ -46,6 +64,10 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
4664 & gatewayv1.HTTPRoute {},
4765 handler .EnqueueRequestsFromMapFunc (r .listGatewaysForHTTPRoute ),
4866 ).
67+ Watches (
68+ & v1alpha1.GatewayProxy {},
69+ handler .EnqueueRequestsFromMapFunc (r .listGatewaysForGatewayProxy ),
70+ ).
4971 Complete (r )
5072}
5173
@@ -99,6 +121,13 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
99121 Secrets : make (map [types.NamespacedName ]* corev1.Secret ),
100122 }
101123 r .processListenerConfig (tctx , gateway , ns )
124+ if err := r .processInfrastructure (tctx , gateway , ns ); err != nil {
125+ acceptStatus = status {
126+ status : false ,
127+ msg : err .Error (),
128+ }
129+ }
130+
102131 if err := r .Provider .Update (ctx , tctx , gateway ); err != nil {
103132 acceptStatus = status {
104133 status : false ,
@@ -108,6 +137,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
108137
109138 ListenerStatuses , err := getListenerStatus (ctx , r .Client , gateway )
110139 if err != nil {
140+ log .Error (err , "failed to get listener status" , "gateway" , gateway .GetName ())
111141 return ctrl.Result {}, err
112142 }
113143
@@ -123,6 +153,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
123153
124154 return ctrl.Result {}, r .Status ().Update (ctx , gateway )
125155 }
156+
126157 return ctrl.Result {}, nil
127158}
128159
@@ -181,6 +212,35 @@ func (r *GatewayReconciler) checkGatewayClass(gateway *gatewayv1.Gateway) bool {
181212 return matchesController (string (gatewayClass .Spec .ControllerName ))
182213}
183214
215+ func (r * GatewayReconciler ) listGatewaysForGatewayProxy (ctx context.Context , obj client.Object ) []reconcile.Request {
216+ gatewayProxy , ok := obj .(* v1alpha1.GatewayProxy )
217+ if ! ok {
218+ r .Log .Error (fmt .Errorf ("unexpected object type" ), "failed to convert object to GatewayProxy" )
219+ return nil
220+ }
221+ namespace := gatewayProxy .GetNamespace ()
222+ name := gatewayProxy .GetName ()
223+
224+ gatewayList := & gatewayv1.GatewayList {}
225+ if err := r .List (ctx , gatewayList , client.MatchingFields {
226+ indexer .ParametersRef : indexer .GenIndexKey (namespace , name ),
227+ }); err != nil {
228+ r .Log .Error (err , "failed to list gateways for gateway proxy" , "gatewayproxy" , gatewayProxy .GetName ())
229+ return nil
230+ }
231+
232+ recs := make ([]reconcile.Request , 0 , len (gatewayList .Items ))
233+ for _ , gateway := range gatewayList .Items {
234+ recs = append (recs , reconcile.Request {
235+ NamespacedName : client.ObjectKey {
236+ Namespace : gateway .GetNamespace (),
237+ Name : gateway .GetName (),
238+ },
239+ })
240+ }
241+ return recs
242+ }
243+
184244func (r * GatewayReconciler ) listGatewaysForHTTPRoute (_ context.Context , obj client.Object ) []reconcile.Request {
185245 httpRoute , ok := obj .(* gatewayv1.HTTPRoute )
186246 if ! ok {
@@ -215,6 +275,30 @@ func (r *GatewayReconciler) listGatewaysForHTTPRoute(_ context.Context, obj clie
215275 return recs
216276}
217277
278+ func (r * GatewayReconciler ) processInfrastructure (tctx * provider.TranslateContext , gateway * gatewayv1.Gateway , ns string ) error {
279+ infra := gateway .Spec .Infrastructure
280+ if infra == nil || infra .ParametersRef == nil {
281+ return nil
282+ }
283+
284+ paramRef := infra .ParametersRef
285+ if string (paramRef .Group ) == v1alpha1 .GroupVersion .Group && string (paramRef .Kind ) == "GatewayProxy" {
286+ gatewayProxy := & v1alpha1.GatewayProxy {}
287+ if err := r .Get (context .Background (), client.ObjectKey {
288+ Namespace : ns ,
289+ Name : paramRef .Name ,
290+ }, gatewayProxy ); err != nil {
291+ log .Error (err , "failed to get GatewayProxy" , "namespace" , ns , "name" , paramRef .Name )
292+ return err
293+ } else {
294+ log .Info ("found GatewayProxy for Gateway" , "gateway" , gateway .Name , "gatewayproxy" , gatewayProxy .Name )
295+ tctx .GatewayProxy = gatewayProxy
296+ }
297+ }
298+
299+ return nil
300+ }
301+
218302func (r * GatewayReconciler ) processListenerConfig (tctx * provider.TranslateContext , gateway * gatewayv1.Gateway , ns string ) {
219303 listeners := gateway .Spec .Listeners
220304 for _ , listener := range listeners {
0 commit comments