diff --git a/controllers/rate_limiting_wasmplugin_controller.go b/controllers/rate_limiting_wasmplugin_controller.go index 49c39ecd4..22221b0d4 100644 --- a/controllers/rate_limiting_wasmplugin_controller.go +++ b/controllers/rate_limiting_wasmplugin_controller.go @@ -26,6 +26,7 @@ import ( istioclientgoextensionv1alpha1 "istio.io/client-go/pkg/apis/extensions/v1alpha1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" @@ -38,6 +39,10 @@ import ( "github.com/kuadrant/kuadrant-operator/pkg/rlptools/wasm" ) +const ( + HTTPRouteParents = ".metadata.parent" +) + // RateLimitingWASMPluginReconciler reconciles a WASMPlugin object for rate limiting type RateLimitingWASMPluginReconciler struct { reconcilers.TargetRefReconciler @@ -179,7 +184,7 @@ func (r *RateLimitingWASMPluginReconciler) gatewayAPITopologyFromGateway(ctx con routeList := &gatewayapiv1.HTTPRouteList{} // Get all the routes having the gateway as parent - err = r.Client().List(ctx, routeList, client.MatchingFields{common.HTTPRouteParents: client.ObjectKeyFromObject(gw).String()}) + err = r.Client().List(ctx, routeList, client.MatchingFields{HTTPRouteParents: client.ObjectKeyFromObject(gw).String()}) logger.V(1).Info("gatewayAPITopologyFromGateway: list httproutes from gateway", "err", err) if err != nil { return nil, err @@ -261,8 +266,56 @@ func (r *RateLimitingWASMPluginReconciler) RouteFromRLP(t *common.KuadrantTopolo return route } +// addHTTPRouteByGatewayIndexer declares an index key that we can later use with the client as a pseudo-field name, +// allowing to query all the routes parenting a given gateway +// to prevent creating the same index field multiple times, the function is declared private to be +// called ontly by this controller +func addHTTPRouteByGatewayIndexer(mgr ctrl.Manager, logger logr.Logger) error { + if err := mgr.GetFieldIndexer().IndexField(context.Background(), &gatewayapiv1.HTTPRoute{}, HTTPRouteParents, func(rawObj client.Object) []string { + // grab the route object, extract the parents + route, assertionOk := rawObj.(*gatewayapiv1.HTTPRoute) + logger.Info("assertionOK", "ok", assertionOk) + if !assertionOk { + return nil + } + + logger.Info("route", "name", client.ObjectKeyFromObject(route).String()) + + keys := make([]string, 0) + + for _, parentRef := range route.Spec.CommonRouteSpec.ParentRefs { + if !common.IsParentGateway(parentRef) { + logger.Info("parent not gateway", "ParentRefs", parentRef) + continue + } + + key := client.ObjectKey{ + Name: string(parentRef.Name), + Namespace: string(ptr.Deref(parentRef.Namespace, gatewayapiv1.Namespace(route.Namespace))), + } + + logger.Info("new key", "key", key.String()) + + keys = append(keys, key.String()) + } + + // ...and if so, return it + return keys + }); err != nil { + return err + } + + return nil +} + // SetupWithManager sets up the controller with the Manager. func (r *RateLimitingWASMPluginReconciler) SetupWithManager(mgr ctrl.Manager) error { + // Add custom indexer + err := addHTTPRouteByGatewayIndexer(mgr, r.Logger().WithName("routeByGatewayIndexer")) + if err != nil { + return err + } + httpRouteToParentGatewaysEventMapper := &common.HTTPRouteToParentGatewaysEventMapper{ Logger: r.Logger().WithName("httpRouteToParentGatewaysEventMapper"), } diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 40ffd4d76..d31be88bd 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -181,6 +181,20 @@ var _ = BeforeSuite(func() { Expect(err).NotTo(HaveOccurred()) + rateLimitingWASMPluginBaseReconciler := reconcilers.NewBaseReconciler( + mgr.GetClient(), mgr.GetScheme(), mgr.GetAPIReader(), + log.Log.WithName("ratelimitpolicy").WithName("wasmplugin"), + mgr.GetEventRecorderFor("RateLimitingWASMPlugin"), + ) + + err = (&RateLimitingWASMPluginReconciler{ + TargetRefReconciler: reconcilers.TargetRefReconciler{ + BaseReconciler: rateLimitingWASMPluginBaseReconciler, + }, + }).SetupWithManager(mgr) + + Expect(err).NotTo(HaveOccurred()) + go func() { defer GinkgoRecover() err = mgr.Start(ctrl.SetupSignalHandler()) diff --git a/main.go b/main.go index 9c9c1b564..c7b466320 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,6 @@ import ( kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" "github.com/kuadrant/kuadrant-operator/controllers" - "github.com/kuadrant/kuadrant-operator/pkg/common" "github.com/kuadrant/kuadrant-operator/pkg/log" "github.com/kuadrant/kuadrant-operator/pkg/reconcilers" //+kubebuilder:scaffold:imports @@ -131,12 +130,6 @@ func main() { os.Exit(1) } - err = common.AddHTTPRouteByGatewayIndexer(mgr, log.Log.WithName("routeByGatewayIndexer")) - if err != nil { - setupLog.Error(err, "unable to add indexer to the manager") - os.Exit(1) - } - kuadrantBaseReconciler := reconcilers.NewBaseReconciler( mgr.GetClient(), mgr.GetScheme(), mgr.GetAPIReader(), log.Log.WithName("kuadrant"), @@ -210,7 +203,7 @@ func main() { rateLimitingWASMPluginBaseReconciler := reconcilers.NewBaseReconciler( mgr.GetClient(), mgr.GetScheme(), mgr.GetAPIReader(), log.Log.WithName("ratelimitpolicy").WithName("wasmplugin"), - mgr.GetEventRecorderFor("GatewayKuadrant"), + mgr.GetEventRecorderFor("RateLimitingWASMPlugin"), ) if err = (&controllers.RateLimitingWASMPluginReconciler{ diff --git a/pkg/common/gatewayapi_utils.go b/pkg/common/gatewayapi_utils.go index c507c9626..9933e057b 100644 --- a/pkg/common/gatewayapi_utils.go +++ b/pkg/common/gatewayapi_utils.go @@ -7,13 +7,11 @@ import ( "reflect" "strings" - "github.com/go-logr/logr" "golang.org/x/exp/slices" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/types" "k8s.io/utils/ptr" - ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" @@ -21,7 +19,6 @@ import ( const ( GatewayProgrammedConditionType = "Programmed" - HTTPRouteParents = ".metadata.parent" ) type HTTPRouteRule struct { @@ -645,46 +642,6 @@ func IsHTTPRouteAccepted(httpRoute *gatewayapiv1.HTTPRoute) bool { return true } -// AddHTTPRouteByGatewayIndexer declares an index key that we can later use with the client as a pseudo-field name, -// allowing to query all the routes parenting a given gateway -func AddHTTPRouteByGatewayIndexer(mgr ctrl.Manager, logger logr.Logger) error { - if err := mgr.GetFieldIndexer().IndexField(context.Background(), &gatewayapiv1.HTTPRoute{}, HTTPRouteParents, func(rawObj client.Object) []string { - // grab the route object, extract the parents - route, assertionOk := rawObj.(*gatewayapiv1.HTTPRoute) - logger.Info("assertionOK", "ok", assertionOk) - if !assertionOk { - return nil - } - - logger.Info("route", "name", client.ObjectKeyFromObject(route).String()) - - keys := make([]string, 0) - - for _, parentRef := range route.Spec.CommonRouteSpec.ParentRefs { - if !IsParentGateway(parentRef) { - logger.Info("parent not gateway", "ParentRefs", parentRef) - continue - } - - key := client.ObjectKey{ - Name: string(parentRef.Name), - Namespace: string(ptr.Deref(parentRef.Namespace, gatewayapiv1.Namespace(route.Namespace))), - } - - logger.Info("new key", "key", key.String()) - - keys = append(keys, key.String()) - } - - // ...and if so, return it - return keys - }); err != nil { - return err - } - - return nil -} - func GetRouteAcceptedGatewayParentKeys(route *gatewayapiv1.HTTPRoute) []client.ObjectKey { if route == nil { return nil