@@ -39,6 +39,7 @@ import (
3939 "github.com/apache/apisix-ingress-controller/internal/controller/indexer"
4040 "github.com/apache/apisix-ingress-controller/internal/provider"
4141 "github.com/apache/apisix-ingress-controller/internal/utils"
42+ pkgutils "github.com/apache/apisix-ingress-controller/pkg/utils"
4243)
4344
4445// GatewayProxyController reconciles a GatewayProxy object.
@@ -48,10 +49,15 @@ type GatewayProxyController struct {
4849 Scheme * runtime.Scheme
4950 Log logr.Logger
5051 Provider provider.Provider
52+
53+ disableGatewayAPI bool
5154}
5255
5356func (r * GatewayProxyController ) SetupWithManager (mrg ctrl.Manager ) error {
54- return ctrl .NewControllerManagedBy (mrg ).
57+ if config .ControllerConfig .DisableGatewayAPI || ! pkgutils .HasAPIResource (mrg , & gatewayv1.Gateway {}) {
58+ r .disableGatewayAPI = true
59+ }
60+ builder := ctrl .NewControllerManagedBy (mrg ).
5561 For (& v1alpha1.GatewayProxy {}).
5662 WithEventFilter (
5763 predicate .Or (
@@ -68,13 +74,15 @@ func (r *GatewayProxyController) SetupWithManager(mrg ctrl.Manager) error {
6874 Watches (& corev1.Secret {},
6975 handler .EnqueueRequestsFromMapFunc (r .listGatewayProxiesForSecret ),
7076 ).
71- Watches (& gatewayv1.Gateway {},
72- handler .EnqueueRequestsFromMapFunc (r .listGatewayProxiesByGateway ),
73- ).
7477 Watches (& networkingv1.IngressClass {},
7578 handler .EnqueueRequestsFromMapFunc (r .listGatewayProxiesForIngressClass ),
76- ).
77- Complete (r )
79+ )
80+ if ! r .disableGatewayAPI {
81+ builder .Watches (& gatewayv1.Gateway {},
82+ handler .EnqueueRequestsFromMapFunc (r .listGatewayProxiesByGateway ),
83+ )
84+ }
85+ return builder .Complete (r )
7886}
7987
8088func (r * GatewayProxyController ) Reconcile (ctx context.Context , req ctrl.Request ) (reconcile.Result , error ) {
@@ -131,19 +139,32 @@ func (r *GatewayProxyController) Reconcile(ctx context.Context, req ctrl.Request
131139 ingressClassList networkingv1.IngressClassList
132140 indexKey = indexer .GenIndexKey (gp .GetNamespace (), gp .GetName ())
133141 )
134- if err := r .List (ctx , & gatewayList , client.MatchingFields {indexer .ParametersRef : indexKey }); err != nil {
135- r .Log .Error (err , "failed to list GatewayList" )
136- return ctrl.Result {}, nil
137- }
138142
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
143+ if ! r .disableGatewayAPI {
144+ if err := r .List (ctx , & gatewayList , client.MatchingFields {indexer .ParametersRef : indexKey }); err != nil {
145+ r .Log .Error (err , "failed to list GatewayList" )
146+ return ctrl.Result {}, nil
147+ }
148+ var gatewayclassList gatewayv1.GatewayClassList
149+ if err := r .List (ctx , & gatewayclassList , client.MatchingFields {indexer .ControllerName : config .GetControllerName ()}); err != nil {
150+ r .Log .Error (err , "failed to list GatewayClassList" )
151+ return ctrl.Result {}, nil
152+ }
153+ gcMatched := make (map [string ]* gatewayv1.GatewayClass )
154+ for _ , item := range gatewayclassList .Items {
155+ gcMatched [item .Name ] = & item
156+ }
157+ // append referrers to translate context
158+ for _ , item := range gatewayList .Items {
159+ gcName := string (item .Spec .GatewayClassName )
160+ if gcName == "" {
161+ continue
162+ }
163+ if _ , ok := gcMatched [gcName ]; ok {
164+ tctx .GatewayProxyReferrers [req .NamespacedName ] = append (tctx .GatewayProxyReferrers [req .NamespacedName ], utils .NamespacedNameKind (& item ))
165+ }
166+ }
167+ r .Log .V (1 ).Info ("found Gateways for GatewayProxy" , "gatewayproxy" , req .String (), "gateways" , len (gatewayList .Items ), "gatewayclasses" , len (gatewayclassList .Items ), "ingressclasses" , len (ingressClassList .Items ))
147168 }
148169
149170 // list IngressClasses that reference the GatewayProxy
@@ -152,23 +173,12 @@ func (r *GatewayProxyController) Reconcile(ctx context.Context, req ctrl.Request
152173 return reconcile.Result {}, err
153174 }
154175
155- // append referrers to translate context
156- for _ , item := range gatewayList .Items {
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- }
164- }
165176 for _ , item := range ingressClassList .Items {
166177 if item .Spec .Controller != config .GetControllerName () {
167178 continue
168179 }
169180 tctx .GatewayProxyReferrers [req .NamespacedName ] = append (tctx .GatewayProxyReferrers [req .NamespacedName ], utils .NamespacedNameKind (& item ))
170181 }
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 ))
172182
173183 if len (tctx .GatewayProxyReferrers [req .NamespacedName ]) == 0 {
174184 return ctrl.Result {}, nil
0 commit comments