Skip to content

Commit

Permalink
do not use dynamic CRD controller and and envtest cases
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed Apr 3, 2024
1 parent ea72a43 commit 971593a
Show file tree
Hide file tree
Showing 5 changed files with 484 additions and 74 deletions.
2 changes: 1 addition & 1 deletion controllers/license/konglicense_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func WrapKongLicenseReconcilerToDynamicCRDController(
) *crds.DynamicCRDController {
return &crds.DynamicCRDController{
Manager: mgr,
Log: ctrl.LoggerFrom(ctx).WithName("controllers").WithName("Dynamic/KongUpstreamPolicy"),
Log: ctrl.LoggerFrom(ctx).WithName("controllers").WithName("Dynamic/KongLicense"),
CacheSyncTimeout: r.CacheSyncTimeout,
RequiredCRDs: []schema.GroupVersionResource{
{
Expand Down
68 changes: 26 additions & 42 deletions internal/controllers/configuration/kongupstreampolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type KongUpstreamPolicyReconciler struct {
// KongServiceFacadeEnabled determines whether the controller should populate the KongUpstreamPolicy's ancestor
// status for KongServiceFacades.
KongServiceFacadeEnabled bool
// HTTPRouteEnabled determines whether the controller should populate the KongUpstreamPolicy's
// ancestor status for HTTPRoutes.
HTTPRouteEnabled bool
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -71,6 +74,17 @@ func (r *KongUpstreamPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error
return err
}

if r.HTTPRouteEnabled {
// 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 @@ -127,6 +141,17 @@ func (r *KongUpstreamPolicyReconciler) setupIndices(mgr ctrl.Manager) error {
return fmt.Errorf("failed to index services on annotation %s: %w", kongv1beta1.KongUpstreamPolicyAnnotationKey, err)
}

if r.HTTPRouteEnabled {
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 @@ -243,7 +268,7 @@ func (r *KongUpstreamPolicyReconciler) getUpstreamPoliciesForHTTPRouteServices(c
if !ok {
return nil
}

r.Log.Info("Found HTTPRoute", "name", httpRoute.Name, "namespace", httpRoute.Namespace)
var requests []reconcile.Request
for _, rule := range httpRoute.Spec.Rules {
for _, br := range rule.BackendRefs {
Expand Down Expand Up @@ -363,44 +388,3 @@ 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ func (r *KongUpstreamPolicyReconciler) buildAncestorsStatus(

// getConflictedServices returns a set of services that have conflicts.
func (r *KongUpstreamPolicyReconciler) getConflictedServices(ctx context.Context, services []corev1.Service) (servicesSet, error) {
// return directly when HTTPRoute is not enabled, as it only check conflicted services in HTTPRoute backends only.
if !r.HTTPRouteEnabled {
return make(servicesSet), nil
}
// Prepare a mapping for efficient lookups if a Service uses this KongUpstreamPolicy.
upstreamPolicyServices := make(servicesSet)
for _, service := range services {
Expand Down
48 changes: 17 additions & 31 deletions internal/manager/controllerdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/kong/kubernetes-ingress-controller/v3/internal/controllers/crds"
"github.com/kong/kubernetes-ingress-controller/v3/internal/controllers/gateway"
ctrlref "github.com/kong/kubernetes-ingress-controller/v3/internal/controllers/reference"
"github.com/kong/kubernetes-ingress-controller/v3/internal/controllers/utils"
"github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane"
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/featuregates"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util/kubernetes/object/status"
Expand Down Expand Up @@ -67,16 +68,6 @@ 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 @@ -260,28 +251,23 @@ func setupControllers(
// StatusQueue: kubernetesStatusQueue,
},
},
// KongUpstreamPolicy controller without HTTPRoute.
{
Enabled: c.KongUpstreamPolicyEnabled,
Controller: kongUpstreamPolicyReconciler,
},
// HTTPRoute controller for updating KongUpstreamPolicy's ancestor status with services used as backends of HTTPRoutes.
// KongUpstreamPolicy controller.
// When HTTPRoute exists, the controller is enabled to watch HTTPRoutes to set ancestor status of KongUpstreamPolicies.
{
Enabled: c.KongUpstreamPolicyEnabled && c.GatewayAPIHTTPRouteController,
Controller: &crds.DynamicCRDController{
Manager: mgr,
Log: ctrl.LoggerFrom(ctx).WithName("controllers").WithName("Dynamic/Gateway"),
CacheSyncTimeout: c.CacheSyncTimeout,
RequiredCRDs: []schema.GroupVersionResource{
{
Group: gatewayv1.GroupVersion.Group,
Version: gatewayv1.GroupVersion.Version,
Resource: "httproutes",
},
},
Controller: &configuration.HTTPRouteReconcilerForKongUpstreamPolicy{
Reconciler: kongUpstreamPolicyReconciler,
},
Enabled: c.KongUpstreamPolicyEnabled,
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,
HTTPRouteEnabled: utils.CRDExists(mgr.GetRESTMapper(), schema.GroupVersionResource{
Group: gatewayv1.GroupVersion.Group,
Version: gatewayv1.GroupVersion.Version,
Resource: "httproutes",
}),
},
},
{
Expand Down

0 comments on commit 971593a

Please sign in to comment.