@@ -83,6 +83,9 @@ func (r *IngressReconciler) SetupWithManager(mgr ctrl.Manager) error {
8383 handler .EnqueueRequestsFromMapFunc (r .listIngressesByHTTPRoutePolicy ),
8484 builder .WithPredicates (httpRoutePolicyPredicateFuncs (r .genericEvent )),
8585 ).
86+ Watches (& v1alpha1.GatewayProxy {},
87+ handler .EnqueueRequestsFromMapFunc (r .listIngressesForGatewayProxy ),
88+ ).
8689 WatchesRawSource (
8790 source .Channel (
8891 r .genericEvent ,
@@ -219,38 +222,8 @@ func (r *IngressReconciler) getIngressClass(obj client.Object) (*networkingv1.In
219222
220223// checkIngressClass check if the ingress uses the ingress class that we control
221224func (r * IngressReconciler ) checkIngressClass (obj client.Object ) bool {
222- ingress := obj .(* networkingv1.Ingress )
223-
224- if ingress .Spec .IngressClassName == nil {
225- // handle the case where IngressClassName is not specified
226- // find all ingress classes and check if any of them is marked as default
227- ingressClassList := & networkingv1.IngressClassList {}
228- if err := r .List (context .Background (), ingressClassList , client.MatchingFields {
229- indexer .IngressClass : config .GetControllerName (),
230- }); err != nil {
231- r .Log .Error (err , "failed to list ingress classes" )
232- return false
233- }
234-
235- // find the ingress class that is marked as default
236- for _ , ic := range ingressClassList .Items {
237- if IsDefaultIngressClass (& ic ) && matchesController (ic .Spec .Controller ) {
238- log .Debugw ("match the default ingress class" )
239- return true
240- }
241- }
242-
243- log .Debugw ("no default ingress class found" )
244- return false
245- }
246-
247- // check if the ingress class is controlled by us
248- ingressClass := networkingv1.IngressClass {}
249- if err := r .Client .Get (context .Background (), client.ObjectKey {Name : * ingress .Spec .IngressClassName }, & ingressClass ); err != nil {
250- return false
251- }
252-
253- return matchesController (ingressClass .Spec .Controller )
225+ _ , err := r .getIngressClass (obj )
226+ return err == nil
254227}
255228
256229// matchesIngressController check if the ingress class is controlled by us
@@ -735,3 +708,46 @@ func (r *IngressReconciler) processIngressClassParameters(ctx context.Context, t
735708
736709 return nil
737710}
711+
712+ // listIngressesForGatewayProxy list all ingresses that use a specific gateway proxy
713+ func (r * IngressReconciler ) listIngressesForGatewayProxy (ctx context.Context , obj client.Object ) []reconcile.Request {
714+ gatewayProxy , ok := obj .(* v1alpha1.GatewayProxy )
715+ if ! ok {
716+ r .Log .Error (fmt .Errorf ("unexpected object type" ), "failed to convert object to GatewayProxy" )
717+ return nil
718+ }
719+
720+ // find all ingress classes that reference this gateway proxy
721+ ingressClassList := & networkingv1.IngressClassList {}
722+ if err := r .List (ctx , ingressClassList , client.MatchingFields {
723+ indexer .IngressClassParametersRef : indexer .GenIndexKey (gatewayProxy .GetNamespace (), gatewayProxy .GetName ()),
724+ }); err != nil {
725+ r .Log .Error (err , "failed to list ingress classes for gateway proxy" , "gatewayproxy" , gatewayProxy .GetName ())
726+ return nil
727+ }
728+
729+ var requests []reconcile.Request
730+
731+ for _ , ingressClass := range ingressClassList .Items {
732+ requests = append (requests , r .listIngressForIngressClass (ctx , & ingressClass )... )
733+ }
734+
735+ // the requests may contain duplicates, distinct the requests
736+ requests = distinctRequests (requests )
737+
738+ return requests
739+ }
740+
741+ // distinctRequests distinct the requests
742+ func distinctRequests (requests []reconcile.Request ) []reconcile.Request {
743+ uniqueRequests := make (map [string ]reconcile.Request )
744+ for _ , request := range requests {
745+ uniqueRequests [request .String ()] = request
746+ }
747+
748+ distinctRequests := make ([]reconcile.Request , 0 , len (uniqueRequests ))
749+ for _ , request := range uniqueRequests {
750+ distinctRequests = append (distinctRequests , request )
751+ }
752+ return distinctRequests
753+ }
0 commit comments