Skip to content

Commit

Permalink
wasmplugin controller: refactor freeRoute -> untargetedRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Feb 27, 2024
1 parent 88af9f2 commit c58450a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
15 changes: 7 additions & 8 deletions controllers/rate_limiting_wasmplugin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,27 +279,26 @@ 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)
gwHostnames := common.Map(gwHostnamesTmp, func(str string) gatewayapiv1.Hostname { return gatewayapiv1.Hostname(str) })
route = &gatewayapiv1.HTTPRoute{
Spec: gatewayapiv1.HTTPRouteSpec{
Hostnames: gwHostnames,
Rules: freeRules,
Rules: untargetedRules,
},
}
}
Expand Down
39 changes: 24 additions & 15 deletions pkg/common/kuadrant_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Check failure on line 52 in pkg/common/kuadrant_topology.go

View workflow job for this annotation

GitHub Actions / github.com/client9/misspell

[github.com/client9/misspell] pkg/common/kuadrant_topology.go#L52

"targetd" is a misspelling of "targeted"
Raw output
pkg/common/kuadrant_topology.go:52:50: "targetd" is a misspelling of "targeted"
// 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
Expand Down Expand Up @@ -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()
})

Check warning on line 171 in pkg/common/kuadrant_topology.go

View check run for this annotation

Codecov / codecov/patch

pkg/common/kuadrant_topology.go#L170-L171

Added lines #L170 - L171 were not covered by tests
Expand All @@ -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, "", " ")
Expand Down Expand Up @@ -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)

Expand Down
28 changes: 14 additions & 14 deletions pkg/common/kuadrant_topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
})
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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": []
}`))
})
Expand Down

0 comments on commit c58450a

Please sign in to comment.