Skip to content

Commit

Permalink
add HTTPRoute controller for watching HTTPRoute to enqueue KongUpstre…
Browse files Browse the repository at this point in the history
…amPolicy needing to update ancestor status
  • Loading branch information
randmonkey committed Mar 29, 2024
1 parent eee3ed3 commit 0b4e91a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
59 changes: 41 additions & 18 deletions internal/controllers/configuration/kongupstreampolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
}
30 changes: 20 additions & 10 deletions internal/manager/controllerdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
{
Expand All @@ -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,
},
},
},
Expand Down

0 comments on commit 0b4e91a

Please sign in to comment.