@@ -35,6 +35,7 @@ import (
3535 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
3636
3737 "github.com/apache/apisix-ingress-controller/api/v1alpha1"
38+ "github.com/apache/apisix-ingress-controller/internal/controller/config"
3839 "github.com/apache/apisix-ingress-controller/internal/controller/indexer"
3940 "github.com/apache/apisix-ingress-controller/internal/provider"
4041 "github.com/apache/apisix-ingress-controller/internal/utils"
@@ -67,6 +68,12 @@ func (r *GatewayProxyController) SetupWithManager(mrg ctrl.Manager) error {
6768 Watches (& corev1.Secret {},
6869 handler .EnqueueRequestsFromMapFunc (r .listGatewayProxiesForSecret ),
6970 ).
71+ Watches (& gatewayv1.Gateway {},
72+ handler .EnqueueRequestsFromMapFunc (r .listGatewayProxiesByGateway ),
73+ ).
74+ Watches (& networkingv1.IngressClass {},
75+ handler .EnqueueRequestsFromMapFunc (r .listGatewayProxiesForIngressClass ),
76+ ).
7077 Complete (r )
7178}
7279
@@ -129,6 +136,16 @@ func (r *GatewayProxyController) Reconcile(ctx context.Context, req ctrl.Request
129136 return ctrl.Result {}, nil
130137 }
131138
139+ var gatewayclassList gatewayv1.GatewayClassList
140+ if err := r .List (ctx , & gatewayclassList , client.MatchingFields {indexer .ControllerName : config .GetControllerName ()}); err != nil {
141+ r .Log .Error (err , "failed to list GatewayClassList" )
142+ return ctrl.Result {}, nil
143+ }
144+ gcMatched := make (map [string ]* gatewayv1.GatewayClass )
145+ for _ , item := range gatewayclassList .Items {
146+ gcMatched [item .Name ] = & item
147+ }
148+
132149 // list IngressClasses that reference the GatewayProxy
133150 if err := r .List (ctx , & ingressClassList , client.MatchingFields {indexer .IngressClassParametersRef : indexKey }); err != nil {
134151 r .Log .Error (err , "failed to list IngressClassList" )
@@ -137,12 +154,27 @@ func (r *GatewayProxyController) Reconcile(ctx context.Context, req ctrl.Request
137154
138155 // append referrers to translate context
139156 for _ , item := range gatewayList .Items {
140- tctx .GatewayProxyReferrers [req .NamespacedName ] = append (tctx .GatewayProxyReferrers [req .NamespacedName ], utils .NamespacedNameKind (& item ))
157+ gcName := string (item .Spec .GatewayClassName )
158+ if gcName == "" {
159+ continue
160+ }
161+ if _ , ok := gcMatched [gcName ]; ok {
162+ tctx .GatewayProxyReferrers [req .NamespacedName ] = append (tctx .GatewayProxyReferrers [req .NamespacedName ], utils .NamespacedNameKind (& item ))
163+ }
141164 }
142165 for _ , item := range ingressClassList .Items {
166+ if item .Spec .Controller != config .GetControllerName () {
167+ continue
168+ }
143169 tctx .GatewayProxyReferrers [req .NamespacedName ] = append (tctx .GatewayProxyReferrers [req .NamespacedName ], utils .NamespacedNameKind (& item ))
144170 }
171+ r .Log .V (1 ).Info ("found Gateways for GatewayProxy" , "gatewayproxy" , req .String (), "gateways" , len (gatewayList .Items ), "gatewayclasses" , len (gatewayclassList .Items ), "ingressclasses" , len (ingressClassList .Items ))
172+
173+ if len (tctx .GatewayProxyReferrers [req .NamespacedName ]) == 0 {
174+ return ctrl.Result {}, nil
175+ }
145176
177+ r .Log .V (1 ).Info ("references found for GatewayProxy" , "gatewayproxy" , req .String (), "references" , tctx .GatewayProxyReferrers [req .NamespacedName ])
146178 if err := r .Provider .Update (ctx , tctx , & gp ); err != nil {
147179 return reconcile.Result {}, err
148180 }
@@ -184,3 +216,35 @@ func (r *GatewayProxyController) listGatewayProxiesForSecret(ctx context.Context
184216 indexer .SecretIndexRef : indexer .GenIndexKey (secret .GetNamespace (), secret .GetName ()),
185217 })
186218}
219+
220+ func (r * GatewayProxyController ) listGatewayProxiesForIngressClass (ctx context.Context , object client.Object ) []reconcile.Request {
221+ ingressClass , ok := object .(* networkingv1.IngressClass )
222+ if ! ok {
223+ r .Log .Error (errors .New ("unexpected object type" ), "failed to convert object to IngressClass" )
224+ return nil
225+ }
226+ reqs := []reconcile.Request {}
227+ gp , _ := GetGatewayProxyByIngressClass (ctx , r .Client , ingressClass )
228+ if gp != nil {
229+ reqs = append (reqs , reconcile.Request {
230+ NamespacedName : utils .NamespacedName (gp ),
231+ })
232+ }
233+ return reqs
234+ }
235+
236+ func (r * GatewayProxyController ) listGatewayProxiesByGateway (ctx context.Context , object client.Object ) []reconcile.Request {
237+ gateway , ok := object .(* gatewayv1.Gateway )
238+ if ! ok {
239+ r .Log .Error (errors .New ("unexpected object type" ), "failed to convert object to IngressClass" )
240+ return nil
241+ }
242+ reqs := []reconcile.Request {}
243+ gp , _ := GetGatewayProxyByGateway (ctx , r .Client , gateway )
244+ if gp != nil {
245+ reqs = append (reqs , reconcile.Request {
246+ NamespacedName : utils .NamespacedName (gp ),
247+ })
248+ }
249+ return reqs
250+ }
0 commit comments