diff --git a/internal/controllers/configuration/kongupstreampolicy_controller.go b/internal/controllers/configuration/kongupstreampolicy_controller.go index 0c4b49b740..d6c943eb1c 100644 --- a/internal/controllers/configuration/kongupstreampolicy_controller.go +++ b/internal/controllers/configuration/kongupstreampolicy_controller.go @@ -71,15 +71,6 @@ func (r *KongUpstreamPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error return err } - // Watch for HTTPRoute changes to trigger reconciliation for the KongUpstreamPolicies referenced by the Services - // of the HTTPRoute. - if err := c.Watch( - source.Kind(mgr.GetCache(), &gatewayapi.HTTPRoute{}), - handler.EnqueueRequestsFromMapFunc(r.getUpstreamPoliciesForHTTPRouteServices), - ); err != nil { - return err - } - if r.KongServiceFacadeEnabled { if err := c.Watch( source.Kind(mgr.GetCache(), &incubatorv1alpha1.KongServiceFacade{}), @@ -136,15 +127,6 @@ func (r *KongUpstreamPolicyReconciler) setupIndices(mgr ctrl.Manager) error { return fmt.Errorf("failed to index services on annotation %s: %w", kongv1beta1.KongUpstreamPolicyAnnotationKey, err) } - if err := mgr.GetCache().IndexField( - context.Background(), - &gatewayapi.HTTPRoute{}, - routeBackendRefServiceNameIndexKey, - indexRoutesOnBackendRefServiceName, - ); err != nil { - return fmt.Errorf("failed to index HTTPRoutes on backendReferences: %w", err) - } - if r.KongServiceFacadeEnabled { if err := mgr.GetCache().IndexField( context.Background(), @@ -381,3 +363,44 @@ func (r *KongUpstreamPolicyReconciler) Reconcile(ctx context.Context, req ctrl.R func (r *KongUpstreamPolicyReconciler) SetLogger(l logr.Logger) { r.Log = l } + +type HTTPRouteReconcilerForKongUpstreamPolicy struct { + Reconciler *KongUpstreamPolicyReconciler +} + +func (r *HTTPRouteReconcilerForKongUpstreamPolicy) SetupWithManager(mgr ctrl.Manager) error { + c, err := controller.New("HTTPRouteForKongUpstreamPolicy", mgr, controller.Options{ + Reconciler: r.Reconciler, + LogConstructor: func(_ *reconcile.Request) logr.Logger { + return r.Reconciler.Log + }, + CacheSyncTimeout: r.Reconciler.CacheSyncTimeout, + }) + if err != nil { + return err + } + + if err := mgr.GetCache().IndexField( + context.Background(), + &gatewayapi.HTTPRoute{}, + routeBackendRefServiceNameIndexKey, + indexRoutesOnBackendRefServiceName, + ); err != nil { + return fmt.Errorf("failed to index HTTPRoutes on backendReferences: %w", err) + } + + // Watch for HTTPRoute changes to trigger reconciliation for the KongUpstreamPolicies referenced by the Services + // of the HTTPRoute. + if err := c.Watch( + source.Kind(mgr.GetCache(), &gatewayapi.HTTPRoute{}), + handler.EnqueueRequestsFromMapFunc(r.Reconciler.getUpstreamPoliciesForHTTPRouteServices), + ); err != nil { + return err + } + + return nil +} + +func (r *HTTPRouteReconcilerForKongUpstreamPolicy) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + return r.Reconciler.Reconcile(ctx, req) +} diff --git a/internal/manager/controllerdef.go b/internal/manager/controllerdef.go index ce8a344eb9..8e3113d583 100644 --- a/internal/manager/controllerdef.go +++ b/internal/manager/controllerdef.go @@ -67,6 +67,16 @@ func setupControllers( kongAdminAPIEndpointsNotifier configuration.EndpointsNotifier, adminAPIsDiscoverer configuration.AdminAPIsDiscoverer, ) []ControllerDef { + kongUpstreamPolicyReconciler := &configuration.KongUpstreamPolicyReconciler{ + Client: mgr.GetClient(), + Log: ctrl.LoggerFrom(ctx).WithName("controllers").WithName("KongUpstreamPolicy"), + Scheme: mgr.GetScheme(), + DataplaneClient: dataplaneClient, + CacheSyncTimeout: c.CacheSyncTimeout, + KongServiceFacadeEnabled: featureGates.Enabled(featuregates.KongServiceFacade) && c.KongServiceFacadeEnabled, + StatusQueue: kubernetesStatusQueue, + } + controllers := []ControllerDef{ // --------------------------------------------------------------------------- // Kong Gateway Admin API Service discovery @@ -250,11 +260,17 @@ func setupControllers( // StatusQueue: kubernetesStatusQueue, }, }, + // KongUpstreamPolicy controller without HTTPRoute. { - Enabled: c.KongUpstreamPolicyEnabled, + Enabled: c.KongUpstreamPolicyEnabled, + Controller: kongUpstreamPolicyReconciler, + }, + // HTTPRoute controller for updating KongUpstreamPolicy's ancestor status with services used as backends of HTTPRoutes. + { + Enabled: c.KongUpstreamPolicyEnabled && c.GatewayAPIHTTPRouteController, Controller: &crds.DynamicCRDController{ Manager: mgr, - Log: ctrl.LoggerFrom(ctx).WithName("controllers").WithName("Dynamic/KongUpstreamPolicy"), + Log: ctrl.LoggerFrom(ctx).WithName("controllers").WithName("Dynamic/Gateway"), CacheSyncTimeout: c.CacheSyncTimeout, RequiredCRDs: []schema.GroupVersionResource{ { @@ -263,14 +279,8 @@ func setupControllers( Resource: "httproutes", }, }, - Controller: &configuration.KongUpstreamPolicyReconciler{ - Client: mgr.GetClient(), - Log: ctrl.LoggerFrom(ctx).WithName("controllers").WithName("KongUpstreamPolicy"), - Scheme: mgr.GetScheme(), - DataplaneClient: dataplaneClient, - CacheSyncTimeout: c.CacheSyncTimeout, - KongServiceFacadeEnabled: featureGates.Enabled(featuregates.KongServiceFacade) && c.KongServiceFacadeEnabled, - StatusQueue: kubernetesStatusQueue, + Controller: &configuration.HTTPRouteReconcilerForKongUpstreamPolicy{ + Reconciler: kongUpstreamPolicyReconciler, }, }, },