From 728d7b507cfebae1ace3e819e06f66b41ef4aa9c Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Mon, 26 Feb 2024 23:35:43 +0100 Subject: [PATCH] wasmplugin controller: refactor freeRoute -> untargetedRoute --- .../rate_limiting_wasmplugin_controller.go | 15 ++++--- pkg/common/kuadrant_topology.go | 39 ++++++++++++------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/controllers/rate_limiting_wasmplugin_controller.go b/controllers/rate_limiting_wasmplugin_controller.go index 33ed94616..f80c1e4cc 100644 --- a/controllers/rate_limiting_wasmplugin_controller.go +++ b/controllers/rate_limiting_wasmplugin_controller.go @@ -279,19 +279,18 @@ func (r *RateLimitingWASMPluginReconciler) RouteFromRLP(ctx context.Context, t * // This gateway policy will be enforced into all HTTPRoutes that do not have a policy attached to it // Build imaginary route with all the routes not having a RLP targeting it - freeRoutes := t.GetFreeRoutes(gw) + untargetedRoutes := t.GetUntargetedRoutes(gw) - if len(freeRoutes) == 0 { + if len(untargetedRoutes) == 0 { // 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 free httproutes attached to the targeted gateway, skipping wasm config for the gateway rlp", "ratelimitpolicy", client.ObjectKeyFromObject(rlp)) + logger.V(1).Info("no untargeted httproutes attached to the targeted gateway, skipping wasm config for the gateway rlp", "ratelimitpolicy", client.ObjectKeyFromObject(rlp)) return nil, nil } - freeRules := make([]gatewayapiv1.HTTPRouteRule, 0) - for idx := range freeRoutes { - freeroute := freeRoutes[idx] - freeRules = append(freeRules, freeroute.Spec.Rules...) + untargetedRules := make([]gatewayapiv1.HTTPRouteRule, 0) + for idx := range untargetedRoutes { + untargetedRules = append(untargetedRules, untargetedRoutes[idx].Spec.Rules...) } gwHostnamesTmp := common.TargetHostnames(gw) @@ -299,7 +298,7 @@ func (r *RateLimitingWASMPluginReconciler) RouteFromRLP(ctx context.Context, t * route = &gatewayapiv1.HTTPRoute{ Spec: gatewayapiv1.HTTPRouteSpec{ Hostnames: gwHostnames, - Rules: freeRules, + Rules: untargetedRules, }, } } diff --git a/pkg/common/kuadrant_topology.go b/pkg/common/kuadrant_topology.go index 0698f389c..4a2e0332d 100644 --- a/pkg/common/kuadrant_topology.go +++ b/pkg/common/kuadrant_topology.go @@ -22,9 +22,9 @@ type KuadrantTopology struct { // Type: Policy -> HTTPRoute policyRoute map[client.ObjectKey]*gatewayapiv1.HTTPRoute - // freeRoutes is an index of gateways mapping to HTTPRoutes not targeted by a kuadrant policy + // untargetedRoutes is an index of gateways mapping to HTTPRoutes not targeted by a kuadrant policy // Gateway -> []HTTPRoute - freeRoutes map[client.ObjectKey][]*gatewayapiv1.HTTPRoute + untargetedRoutes map[client.ObjectKey][]*gatewayapiv1.HTTPRoute // Raw topology with gateways, routes and policies // Currently only used for logging @@ -37,21 +37,30 @@ func NewKuadrantTopology(gateways []*gatewayapiv1.Gateway, routes []*gatewayapiv return &KuadrantTopology{ gatewayPolicies: buildGatewayPoliciesIndex(t), policyRoute: buildPolicyRouteIndex(t), - freeRoutes: buildFreeRoutesIndex(t), + untargetedRoutes: buildUntargetedRoutesIndex(t), internalTopology: t, } } +// PoliciesFromGateway returns Kuadrant Policies which +// directly or indirectly are targeting the gateway given as input. +// Type: Gateway -> []Policy func (k *KuadrantTopology) PoliciesFromGateway(gateway *gatewayapiv1.Gateway) []KuadrantPolicy { return k.gatewayPolicies[client.ObjectKeyFromObject(gateway)] } +// GetPolicyHTTPRoute returns the HTTPRoute being targetd by the policy. +// The method only returns existing and accepted (by parent gateways) HTTPRoutes +// Type: Policy -> HTTPRoute func (k *KuadrantTopology) GetPolicyHTTPRoute(policy KuadrantPolicy) *gatewayapiv1.HTTPRoute { return k.policyRoute[client.ObjectKeyFromObject(policy)] } -func (k *KuadrantTopology) GetFreeRoutes(gateway *gatewayapiv1.Gateway) []*gatewayapiv1.HTTPRoute { - return k.freeRoutes[client.ObjectKeyFromObject(gateway)] +// GetUntargetedRoutes returns the HTTPRoutes not targeted by any kuadrant policy +// having the gateway given as input as parent. +// Gateway -> []HTTPRoute +func (k *KuadrantTopology) GetUntargetedRoutes(gateway *gatewayapiv1.Gateway) []*gatewayapiv1.HTTPRoute { + return k.untargetedRoutes[client.ObjectKeyFromObject(gateway)] } // String representation of the topology @@ -154,9 +163,9 @@ func (k *KuadrantTopology) String() string { return index }() - freeRoutesPerGateway := func() map[string][]string { + untargetedRoutesPerGateway := func() map[string][]string { index := make(map[string][]string, 0) - for gatewayKey, routeList := range k.freeRoutes { + for gatewayKey, routeList := range k.untargetedRoutes { index[gatewayKey.String()] = Map(routeList, func(route *gatewayapiv1.HTTPRoute) string { return client.ObjectKeyFromObject(route).String() }) @@ -168,19 +177,19 @@ func (k *KuadrantTopology) String() string { }() topologyRepr := struct { - Gateways []Gateway `json:"gateways"` - Routes []Route `json:"routes"` - Policies []Policy `json:"policies"` - GatewayPolicies map[string][]string `json:"policiesPerGateway"` - PolicyRoute map[string]string `json:"policiesTargetingRoutes"` - FreeRoutes map[string][]string `json:"freeRoutesPerGateway"` + Gateways []Gateway `json:"gateways"` + Routes []Route `json:"routes"` + Policies []Policy `json:"policies"` + GatewayPolicies map[string][]string `json:"policiesPerGateway"` + PolicyRoute map[string]string `json:"policiesTargetingRoutes"` + UntargetedRoutes map[string][]string `json:"untargetedRoutesPerGateway"` }{ gateways, routes, policies, policiesPerGateway, policiesTargetingRoutes, - freeRoutesPerGateway, + untargetedRoutesPerGateway, } jsonData, err := json.MarshalIndent(topologyRepr, "", " ") @@ -227,7 +236,7 @@ func buildPolicyRouteIndex(t *gatewayAPITopology) map[client.ObjectKey]*gatewaya return index } -func buildFreeRoutesIndex(t *gatewayAPITopology) map[client.ObjectKey][]*gatewayapiv1.HTTPRoute { +func buildUntargetedRoutesIndex(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][]*gatewayapiv1.HTTPRoute, 0)