Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add check for existence of HTTPRoute in KongUpstreamPolicy controller #5780

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ Adding a new version? You'll need three changes:
[#5737](https://github.com/Kong/kubernetes-ingress-controller/issues/5737)
- Set proper User-Agent for request made to Kong and Konnect.
[#5753](https://github.com/Kong/kubernetes-ingress-controller/pull/5753)
- `KongUpstreamPolicy` controller no longer requires existence of `HTTPRoute` CRD
to start.
[#5780](https://github.com/Kong/kubernetes-ingress-controller/pull/5780)

## [3.1.2]

Expand Down
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,
randmonkey marked this conversation as resolved.
Show resolved Hide resolved
Controller: &crds.DynamicCRDController{
Manager: mgr,
Log: ctrl.LoggerFrom(ctx).WithName("controllers").WithName("Dynamic/KongUpstreamPolicy"),
Log: ctrl.LoggerFrom(ctx).WithName("controllers").WithName("Dynamic/Gateway"),
randmonkey marked this conversation as resolved.
Show resolved Hide resolved
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