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) diff --git a/pkg/common/kuadrant_topology_test.go b/pkg/common/kuadrant_topology_test.go index f9194bb40..7e19e524a 100644 --- a/pkg/common/kuadrant_topology_test.go +++ b/pkg/common/kuadrant_topology_test.go @@ -189,7 +189,7 @@ func TestGetPolicyHTTPRoute(t *testing.T) { }) } -func TestGetFreeRoutes(t *testing.T) { +func TestGetUntargetedRoutes(t *testing.T) { t.Run("gateway without routes", func(subT *testing.T) { // gw1 // policy1 -> gw1 @@ -201,8 +201,8 @@ func TestGetFreeRoutes(t *testing.T) { topology := NewKuadrantTopology(gateways, nil, policies) - freeRoutes := topology.GetFreeRoutes(gw1) - assert.Equal(subT, len(freeRoutes), 0) + untargetedRoutes := topology.GetUntargetedRoutes(gw1) + assert.Equal(subT, len(untargetedRoutes), 0) }) t.Run("all routes have policies", func(subT *testing.T) { @@ -224,11 +224,11 @@ func TestGetFreeRoutes(t *testing.T) { topology := NewKuadrantTopology(gateways, routes, policies) - freeRoutes := topology.GetFreeRoutes(gw1) - assert.Equal(subT, len(freeRoutes), 0) + untargetedRoutes := topology.GetUntargetedRoutes(gw1) + assert.Equal(subT, len(untargetedRoutes), 0) }) - t.Run("only one route is free", func(subT *testing.T) { + t.Run("only one route is untargeted", func(subT *testing.T) { // gw1 // route 1 -> gw1 // route 2 -> gw1 @@ -245,15 +245,15 @@ func TestGetFreeRoutes(t *testing.T) { topology := NewKuadrantTopology(gateways, routes, policies) - freeRoutes := topology.GetFreeRoutes(gw1) - assert.Equal(subT, len(freeRoutes), 1) + untargetedRoutes := topology.GetUntargetedRoutes(gw1) + assert.Equal(subT, len(untargetedRoutes), 1) assert.Equal(subT, - client.ObjectKeyFromObject(freeRoutes[0]), + client.ObjectKeyFromObject(untargetedRoutes[0]), client.ObjectKeyFromObject(route2), ) }) - t.Run("all routes are free", func(subT *testing.T) { + t.Run("all routes are untargeted", func(subT *testing.T) { // gw1 // route 1 -> gw1 // route 2 -> gw1 @@ -266,8 +266,8 @@ func TestGetFreeRoutes(t *testing.T) { topology := NewKuadrantTopology(gateways, routes, nil) - freeRoutes := topology.GetFreeRoutes(gw1) - assert.Equal(subT, len(freeRoutes), 2) + untargetedRoutes := topology.GetUntargetedRoutes(gw1) + assert.Equal(subT, len(untargetedRoutes), 2) }) } @@ -281,7 +281,7 @@ func TestKuadrantTopologyString(t *testing.T) { assert.Assert(subT, strings.Contains(topologyStr, `"policies": null`)) assert.Assert(subT, strings.Contains(topologyStr, `"policiesPerGateway": null`)) assert.Assert(subT, strings.Contains(topologyStr, `"policiesTargetingRoutes": null`)) - assert.Assert(subT, strings.Contains(topologyStr, `"freeRoutesPerGateway": null`)) + assert.Assert(subT, strings.Contains(topologyStr, `"untargetedRoutesPerGateway": null`)) }) t.Run("1 gateway 1 route 1 policy for route", func(subT *testing.T) { @@ -333,7 +333,7 @@ func TestKuadrantTopologyString(t *testing.T) { assert.Assert(subT, strings.Contains(topologyStr, `"policiesTargetingRoutes": { "nsA/policy1": "nsA/route1" }`)) - assert.Assert(subT, strings.Contains(topologyStr, `"freeRoutesPerGateway": { + assert.Assert(subT, strings.Contains(topologyStr, `"untargetedRoutesPerGateway": { "nsA/gw1": [] }`)) })