Skip to content

Commit

Permalink
wasmplugin controller: custom field indexer called from the wasmplugi…
Browse files Browse the repository at this point in the history
…n controller
  • Loading branch information
eguzki committed Nov 29, 2023
1 parent a471506 commit 0e69ab3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 52 deletions.
55 changes: 54 additions & 1 deletion controllers/rate_limiting_wasmplugin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"),
}
Expand Down
14 changes: 14 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
9 changes: 1 addition & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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{
Expand Down
43 changes: 0 additions & 43 deletions pkg/common/gatewayapi_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ 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"
)

const (
GatewayProgrammedConditionType = "Programmed"
HTTPRouteParents = ".metadata.parent"
)

type HTTPRouteRule struct {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0e69ab3

Please sign in to comment.