Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Nov 18, 2023
1 parent fae9e61 commit e688387
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 55 deletions.
1 change: 1 addition & 0 deletions api/external/maistra/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions api/external/maistra/v2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions controllers/rate_limiting_wasmplugin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1beta1"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"

kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2"
"github.com/kuadrant/kuadrant-operator/pkg/common"
Expand All @@ -56,7 +56,7 @@ func (r *RateLimitingWASMPluginReconciler) Reconcile(eventCtx context.Context, r
logger.Info("Reconciling rate limiting WASMPlugin")
ctx := logr.NewContext(eventCtx, logger)

gw := &gatewayapiv1beta1.Gateway{}
gw := &gatewayapiv1.Gateway{}
if err := r.Client().Get(ctx, req.NamespacedName, gw); err != nil {
if apierrors.IsNotFound(err) {
logger.Info("no gateway found")
Expand Down Expand Up @@ -84,7 +84,7 @@ func (r *RateLimitingWASMPluginReconciler) Reconcile(eventCtx context.Context, r
return ctrl.Result{}, nil
}

func (r *RateLimitingWASMPluginReconciler) reconcileRateLimitingWASMPlugin(ctx context.Context, gw *gatewayapiv1beta1.Gateway) error {
func (r *RateLimitingWASMPluginReconciler) reconcileRateLimitingWASMPlugin(ctx context.Context, gw *gatewayapiv1.Gateway) error {
desired, err := r.desiredRateLimitingWASMPlugin(ctx, gw)
if err != nil {
return err
Expand All @@ -98,13 +98,13 @@ func (r *RateLimitingWASMPluginReconciler) reconcileRateLimitingWASMPlugin(ctx c
return nil
}

func (r *RateLimitingWASMPluginReconciler) gatewayAPITopologyFromGateway(ctx context.Context, gw *gatewayapiv1beta1.Gateway) (*common.KuadrantTopology, error) {
func (r *RateLimitingWASMPluginReconciler) gatewayAPITopologyFromGateway(ctx context.Context, gw *gatewayapiv1.Gateway) (*common.KuadrantTopology, error) {
logger, err := logr.FromContext(ctx)
if err != nil {
return nil, err
}

routeList := &gatewayapiv1beta1.HTTPRouteList{}
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()})
logger.V(1).Info("gatewayAPITopologyFromGateway: list httproutes from gateway", "err", err)
Expand All @@ -122,13 +122,13 @@ func (r *RateLimitingWASMPluginReconciler) gatewayAPITopologyFromGateway(ctx con
}

return common.NewKuadrantTopology(
[]*gatewayapiv1beta1.Gateway{gw},
common.Map(routeList.Items, func(r gatewayapiv1beta1.HTTPRoute) *gatewayapiv1beta1.HTTPRoute { return &r }),
[]*gatewayapiv1.Gateway{gw},
common.Map(routeList.Items, func(r gatewayapiv1.HTTPRoute) *gatewayapiv1.HTTPRoute { return &r }),
common.Map(rlpList.Items, func(p kuadrantv1beta2.RateLimitPolicy) common.KuadrantPolicy { return &p }),
), nil
}

func (r *RateLimitingWASMPluginReconciler) desiredRateLimitingWASMPlugin(ctx context.Context, gw *gatewayapiv1beta1.Gateway) (*istioclientgoextensionv1alpha1.WasmPlugin, error) {
func (r *RateLimitingWASMPluginReconciler) desiredRateLimitingWASMPlugin(ctx context.Context, gw *gatewayapiv1.Gateway) (*istioclientgoextensionv1alpha1.WasmPlugin, error) {
wasmPlugin := &istioclientgoextensionv1alpha1.WasmPlugin{
TypeMeta: metav1.TypeMeta{
Kind: "WasmPlugin",
Expand Down Expand Up @@ -172,7 +172,7 @@ func (r *RateLimitingWASMPluginReconciler) desiredRateLimitingWASMPlugin(ctx con
return wasmPlugin, nil
}

func (r *RateLimitingWASMPluginReconciler) wasmPluginConfig(ctx context.Context, gw *gatewayapiv1beta1.Gateway) (*wasm.Plugin, error) {
func (r *RateLimitingWASMPluginReconciler) wasmPluginConfig(ctx context.Context, gw *gatewayapiv1.Gateway) (*wasm.Plugin, error) {
logger, err := logr.FromContext(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -212,9 +212,9 @@ func (r *RateLimitingWASMPluginReconciler) wasmPluginConfig(ctx context.Context,
return wasmPlugin, nil
}

func (r *RateLimitingWASMPluginReconciler) WASMRateLimitPolicy(t *common.KuadrantTopology, rlp *kuadrantv1beta2.RateLimitPolicy, gw *gatewayapiv1beta1.Gateway) *wasm.RateLimitPolicy {
func (r *RateLimitingWASMPluginReconciler) WASMRateLimitPolicy(t *common.KuadrantTopology, rlp *kuadrantv1beta2.RateLimitPolicy, gw *gatewayapiv1.Gateway) *wasm.RateLimitPolicy {
gwHostnamesTmp := common.TargetHostnames(gw)
gwHostnames := common.Map(gwHostnamesTmp, func(str string) gatewayapiv1beta1.Hostname { return gatewayapiv1beta1.Hostname(str) })
gwHostnames := common.Map(gwHostnamesTmp, func(str string) gatewayapiv1.Hostname { return gatewayapiv1.Hostname(str) })

route := r.RouteFromRLP(t, rlp, gw)

Expand All @@ -240,7 +240,7 @@ func (r *RateLimitingWASMPluginReconciler) WASMRateLimitPolicy(t *common.Kuadran
}
}

func (r *RateLimitingWASMPluginReconciler) RouteFromRLP(t *common.KuadrantTopology, rlp *kuadrantv1beta2.RateLimitPolicy, gw *gatewayapiv1beta1.Gateway) *gatewayapiv1beta1.HTTPRoute {
func (r *RateLimitingWASMPluginReconciler) RouteFromRLP(t *common.KuadrantTopology, rlp *kuadrantv1beta2.RateLimitPolicy, gw *gatewayapiv1.Gateway) *gatewayapiv1.HTTPRoute {
route := t.GetPolicyHTTPRoute(rlp)

if route == nil {
Expand All @@ -253,16 +253,16 @@ func (r *RateLimitingWASMPluginReconciler) RouteFromRLP(t *common.KuadrantTopolo
// For policies targeting a gateway, when no httproutes is attached to the gateway, skip wasm config
// test wasm config when no http routes attached to the gateway
//logger.V(1).Info("no httproutes attached to the targeted gateway, skipping wasm config for the gateway rlp", "ratelimitpolicy", gwRLPKey)
freeRules := make([]gatewayapiv1beta1.HTTPRouteRule, 0)
freeRules := make([]gatewayapiv1.HTTPRouteRule, 0)
for idx := range freeRoutes {
freeroute := freeRoutes[idx]
freeRules = append(freeRules, freeroute.Spec.Rules...)
}

gwHostnamesTmp := common.TargetHostnames(gw)
gwHostnames := common.Map(gwHostnamesTmp, func(str string) gatewayapiv1beta1.Hostname { return gatewayapiv1beta1.Hostname(str) })
route = &gatewayapiv1beta1.HTTPRoute{
Spec: gatewayapiv1beta1.HTTPRouteSpec{
gwHostnames := common.Map(gwHostnamesTmp, func(str string) gatewayapiv1.Hostname { return gatewayapiv1.Hostname(str) })
route = &gatewayapiv1.HTTPRoute{
Spec: gatewayapiv1.HTTPRouteSpec{
Hostnames: gwHostnames,
Rules: freeRules,
},
Expand All @@ -278,7 +278,7 @@ func (r *RateLimitingWASMPluginReconciler) SetupWithManager(mgr ctrl.Manager) er
// Rate limiting WASMPlugin controller only cares about
// the annotation having references to RLP's
// kuadrant.io/ratelimitpolicies
For(&gatewayapiv1beta1.Gateway{}, builder.WithPredicates(predicate.AnnotationChangedPredicate{})).
For(&gatewayapiv1.Gateway{}, builder.WithPredicates(predicate.AnnotationChangedPredicate{})).
Owns(&istioclientgoextensionv1alpha1.WasmPlugin{}).
Complete(r)
}
14 changes: 7 additions & 7 deletions pkg/common/gatewayapi_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,9 @@ func IsHTTPRouteAccepted(httpRoute *gatewayapiv1.HTTPRoute) bool {
// 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(), &gatewayapiv1beta1.HTTPRoute{}, HTTPRouteParents, func(rawObj client.Object) []string {
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.(*gatewayapiv1beta1.HTTPRoute)
route, assertionOk := rawObj.(*gatewayapiv1.HTTPRoute)
logger.Info("assertionOK", "ok", assertionOk)
if !assertionOk {
return nil
Expand All @@ -668,7 +668,7 @@ func AddHTTPRouteByGatewayIndexer(mgr ctrl.Manager, logger logr.Logger) error {

key := client.ObjectKey{
Name: string(parentRef.Name),
Namespace: string(ptr.Deref(parentRef.Namespace, gatewayapiv1beta1.Namespace(route.Namespace))),
Namespace: string(ptr.Deref(parentRef.Namespace, gatewayapiv1.Namespace(route.Namespace))),
}

logger.Info("new key", "key", key.String())
Expand All @@ -685,12 +685,12 @@ func AddHTTPRouteByGatewayIndexer(mgr ctrl.Manager, logger logr.Logger) error {
return nil
}

func GetRouteAcceptedGatewayParentKeys(route *gatewayapiv1beta1.HTTPRoute) []client.ObjectKey {
func GetRouteAcceptedGatewayParentKeys(route *gatewayapiv1.HTTPRoute) []client.ObjectKey {
if route == nil {
return nil
}

acceptedRouteParentStatus := Filter(route.Status.RouteStatus.Parents, func(p gatewayapiv1beta1.RouteParentStatus) bool {
acceptedRouteParentStatus := Filter(route.Status.RouteStatus.Parents, func(p gatewayapiv1.RouteParentStatus) bool {
// Only gateway parents
if !IsParentGateway(p.ParentRef) {
return false
Expand All @@ -704,10 +704,10 @@ func GetRouteAcceptedGatewayParentKeys(route *gatewayapiv1beta1.HTTPRoute) []cli
return true
})

return Map(acceptedRouteParentStatus, func(p gatewayapiv1beta1.RouteParentStatus) client.ObjectKey {
return Map(acceptedRouteParentStatus, func(p gatewayapiv1.RouteParentStatus) client.ObjectKey {
return client.ObjectKey{
Name: string(p.ParentRef.Name),
Namespace: string(ptr.Deref(p.ParentRef.Namespace, gatewayapiv1beta1.Namespace(route.Namespace))),
Namespace: string(ptr.Deref(p.ParentRef.Namespace, gatewayapiv1.Namespace(route.Namespace))),
}
})
}
38 changes: 19 additions & 19 deletions pkg/common/kuadrant_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

type KuadrantTopology struct {
// Gateway -> []Policy
gatewayPolicies map[client.ObjectKey][]KuadrantPolicy

// Policy -> HTTPRoute
policyRoute map[client.ObjectKey]*gatewayapiv1beta1.HTTPRoute
policyRoute map[client.ObjectKey]*gatewayapiv1.HTTPRoute

// Gateway -> []HTTPRoute (routes not targeted directly by any policy)
freeRoutes map[client.ObjectKey][]*gatewayapiv1beta1.HTTPRoute
freeRoutes map[client.ObjectKey][]*gatewayapiv1.HTTPRoute

// Raw topology with gateways, routes and policies
// Currently only used for logging
internalTopology *gatewayAPITopology
}

func NewKuadrantTopology(gateways []*gatewayapiv1beta1.Gateway, routes []*gatewayapiv1beta1.HTTPRoute, policies []KuadrantPolicy) *KuadrantTopology {
func NewKuadrantTopology(gateways []*gatewayapiv1.Gateway, routes []*gatewayapiv1.HTTPRoute, policies []KuadrantPolicy) *KuadrantTopology {
t := newGatewayAPITopology(gateways, routes, policies)

return &KuadrantTopology{
Expand All @@ -35,15 +35,15 @@ func NewKuadrantTopology(gateways []*gatewayapiv1beta1.Gateway, routes []*gatewa
}
}

func (k *KuadrantTopology) PoliciesFromGateway(gateway *gatewayapiv1beta1.Gateway) []KuadrantPolicy {
func (k *KuadrantTopology) PoliciesFromGateway(gateway *gatewayapiv1.Gateway) []KuadrantPolicy {
return k.gatewayPolicies[client.ObjectKeyFromObject(gateway)]
}

func (k *KuadrantTopology) GetPolicyHTTPRoute(policy KuadrantPolicy) *gatewayapiv1beta1.HTTPRoute {
func (k *KuadrantTopology) GetPolicyHTTPRoute(policy KuadrantPolicy) *gatewayapiv1.HTTPRoute {
return k.policyRoute[client.ObjectKeyFromObject(policy)]
}

func (k *KuadrantTopology) GetFreeRoutes(gateway *gatewayapiv1beta1.Gateway) []*gatewayapiv1beta1.HTTPRoute {
func (k *KuadrantTopology) GetFreeRoutes(gateway *gatewayapiv1.Gateway) []*gatewayapiv1.HTTPRoute {
return k.freeRoutes[client.ObjectKeyFromObject(gateway)]
}

Expand Down Expand Up @@ -150,7 +150,7 @@ func (k *KuadrantTopology) String() string {
freeRoutesPerGateway := func() map[string][]string {
index := make(map[string][]string, 0)
for gatewayKey, routeList := range k.freeRoutes {
index[gatewayKey.String()] = Map(routeList, func(route *gatewayapiv1beta1.HTTPRoute) string {
index[gatewayKey.String()] = Map(routeList, func(route *gatewayapiv1.HTTPRoute) string {
return client.ObjectKeyFromObject(route).String()
})
}
Expand Down Expand Up @@ -209,9 +209,9 @@ func buildGatewayPoliciesIndex(t *gatewayAPITopology) map[client.ObjectKey][]Kua
return index
}

func buildPolicyRouteIndex(t *gatewayAPITopology) map[client.ObjectKey]*gatewayapiv1beta1.HTTPRoute {
func buildPolicyRouteIndex(t *gatewayAPITopology) map[client.ObjectKey]*gatewayapiv1.HTTPRoute {
// Build Policy -> HTTPRoute index with the route targeted by the indexed policy
index := make(map[client.ObjectKey]*gatewayapiv1beta1.HTTPRoute, 0)
index := make(map[client.ObjectKey]*gatewayapiv1.HTTPRoute, 0)
for _, policyNode := range t.PoliciesIndex {
if policyNode.TargetedRoute != nil {
index[policyNode.ObjectKey()] = policyNode.TargetedRoute.Route
Expand All @@ -221,12 +221,12 @@ func buildPolicyRouteIndex(t *gatewayAPITopology) map[client.ObjectKey]*gatewaya
return index
}

func buildFreeRoutesIndex(t *gatewayAPITopology) map[client.ObjectKey][]*gatewayapiv1beta1.HTTPRoute {
func buildFreeRoutesIndex(t *gatewayAPITopology) map[client.ObjectKey][]*gatewayapiv1.HTTPRoute {
// Build Gateway -> []HTTPRoute index with all the routes not targeted by a policy
index := make(map[client.ObjectKey][]*gatewayapiv1beta1.HTTPRoute, 0)
index := make(map[client.ObjectKey][]*gatewayapiv1.HTTPRoute, 0)

for _, gatewayNode := range t.GatewaysIndex {
routes := make([]*gatewayapiv1beta1.HTTPRoute, 0)
routes := make([]*gatewayapiv1.HTTPRoute, 0)

for _, routeNode := range gatewayNode.Routes {
if routeNode.DirectPolicy == nil {
Expand All @@ -243,7 +243,7 @@ func buildFreeRoutesIndex(t *gatewayAPITopology) map[client.ObjectKey][]*gateway
type gatewayNode struct {
DirectPolicy *policyNode
Routes map[client.ObjectKey]*routeNode
Gateway *gatewayapiv1beta1.Gateway
Gateway *gatewayapiv1.Gateway
}

func (g *gatewayNode) ObjectKey() client.ObjectKey {
Expand All @@ -257,7 +257,7 @@ func (g *gatewayNode) ObjectKey() client.ObjectKey {
type routeNode struct {
Parents map[client.ObjectKey]*gatewayNode
DirectPolicy *policyNode
Route *gatewayapiv1beta1.HTTPRoute
Route *gatewayapiv1.HTTPRoute
}

func (r *routeNode) ObjectKey() client.ObjectKey {
Expand Down Expand Up @@ -288,7 +288,7 @@ type gatewayAPITopology struct {
PoliciesIndex map[client.ObjectKey]*policyNode
}

func newGatewayAPITopology(gateways []*gatewayapiv1beta1.Gateway, routes []*gatewayapiv1beta1.HTTPRoute, policies []KuadrantPolicy) *gatewayAPITopology {
func newGatewayAPITopology(gateways []*gatewayapiv1.Gateway, routes []*gatewayapiv1.HTTPRoute, policies []KuadrantPolicy) *gatewayAPITopology {
gatewaysIndex := initializeGateways(gateways)
routesIndex := initializeRoutes(routes)
policiesIndex := initializePolicies(policies)
Expand Down Expand Up @@ -343,10 +343,10 @@ func newGatewayAPITopology(gateways []*gatewayapiv1beta1.Gateway, routes []*gate
}
}

func initializeGateways(gateways []*gatewayapiv1beta1.Gateway) map[client.ObjectKey]*gatewayNode {
func initializeGateways(gateways []*gatewayapiv1.Gateway) map[client.ObjectKey]*gatewayNode {
gatewaysIndex := make(map[client.ObjectKey]*gatewayNode, 0)

validGateways := Filter(gateways, func(g *gatewayapiv1beta1.Gateway) bool {
validGateways := Filter(gateways, func(g *gatewayapiv1.Gateway) bool {
return meta.IsStatusConditionTrue(g.Status.Conditions, GatewayProgrammedConditionType)
})

Expand All @@ -359,7 +359,7 @@ func initializeGateways(gateways []*gatewayapiv1beta1.Gateway) map[client.Object
return gatewaysIndex
}

func initializeRoutes(routes []*gatewayapiv1beta1.HTTPRoute) map[client.ObjectKey]*routeNode {
func initializeRoutes(routes []*gatewayapiv1.HTTPRoute) map[client.ObjectKey]*routeNode {
routesIndex := make(map[client.ObjectKey]*routeNode, 0)
for _, route := range routes {
routesIndex[client.ObjectKeyFromObject(route)] = &routeNode{
Expand Down

0 comments on commit e688387

Please sign in to comment.