From 62a4a431036f7352b739ff2e820288669bcd16ec Mon Sep 17 00:00:00 2001 From: Guilherme Cassolato Date: Sun, 25 Jun 2023 10:22:14 +0200 Subject: [PATCH] generate wasm rules only for routeSelector-filtered hostnames that belong to the targeted route --- pkg/common/common.go | 16 +++++++++ pkg/common/common_test.go | 62 +++++++++++++++++++++++++++++++++ pkg/rlptools/wasm_utils.go | 6 ++-- pkg/rlptools/wasm_utils_test.go | 8 +++++ 4 files changed, 90 insertions(+), 2 deletions(-) diff --git a/pkg/common/common.go b/pkg/common/common.go index f8ac965fa..4330caeef 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -122,6 +122,22 @@ func Intersect[T comparable](slice1, slice2 []T) bool { return false } +func Intersection[T comparable](slice1, slice2 []T) []T { + smallerSlice := slice1 + largerSlice := slice2 + if len(slice1) > len(slice2) { + smallerSlice = slice2 + largerSlice = slice1 + } + var result []T + for _, item := range smallerSlice { + if Contains(largerSlice, item) { + result = append(result, item) + } + } + return result +} + func Find[T any](slice []T, match func(T) bool) (*T, bool) { for _, item := range slice { if match(item) { diff --git a/pkg/common/common_test.go b/pkg/common/common_test.go index ebea4a5d2..d6752e4dc 100644 --- a/pkg/common/common_test.go +++ b/pkg/common/common_test.go @@ -456,6 +456,68 @@ func TestIntersectWithInts(t *testing.T) { } } +func TestIntersection(t *testing.T) { + testCases := []struct { + name string + slice1 []string + slice2 []string + expected []string + }{ + { + name: "when slice1 and slice2 have one common item then return true", + slice1: []string{"test-gw1", "test-gw2"}, + slice2: []string{"test-gw1", "test-gw3", "test-gw4"}, + expected: []string{"test-gw1"}, + }, + { + name: "when slice1 and slice2 have no common item then return false", + slice1: []string{"test-gw1", "test-gw2"}, + slice2: []string{"test-gw3", "test-gw4"}, + expected: nil, + }, + { + name: "when slice1 is empty then return false", + slice1: []string{}, + slice2: []string{"test-gw3", "test-gw4"}, + expected: nil, + }, + { + name: "when slice2 is empty then return false", + slice1: []string{"test-gw1", "test-gw2"}, + slice2: []string{}, + expected: nil, + }, + { + name: "when both slices are empty then return false", + slice1: []string{}, + slice2: []string{}, + expected: nil, + }, + { + name: "when slice1 is nil then return false", + slice2: []string{"test-gw3", "test-gw4"}, + expected: nil, + }, + { + name: "when slice2 is nil then return false", + slice1: []string{"test-gw1", "test-gw2"}, + expected: nil, + }, + { + name: "when both slices are nil then return false", + expected: nil, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + if r := Intersection(tc.slice1, tc.slice2); !reflect.DeepEqual(r, tc.expected) { + t.Errorf("expected=%v; got=%v", tc.expected, r) + } + }) + } +} + func TestMap(t *testing.T) { slice1 := []int{1, 2, 3, 4} f1 := func(x int) int { return x + 1 } diff --git a/pkg/rlptools/wasm_utils.go b/pkg/rlptools/wasm_utils.go index bb4630a3f..34c8a48c1 100644 --- a/pkg/rlptools/wasm_utils.go +++ b/pkg/rlptools/wasm_utils.go @@ -65,8 +65,10 @@ func conditionsFromLimit(limit *kuadrantv1beta2.Limit, route *gatewayapiv1beta1. // build conditions from the rules selected by the route selectors for _, routeSelector := range limit.RouteSelectors { for _, rule := range HTTPRouteRulesFromRouteSelector(routeSelector, route) { - hostnames := routeSelector.Hostnames // FIXME(guicassolato): it should be the intersection between routeSelector.Hostnames and route.Spec.Hostnames - if len(hostnames) == 0 { + var hostnames []gatewayapiv1beta1.Hostname + if len(routeSelector.Hostnames) > 0 { + hostnames = common.Intersection(routeSelector.Hostnames, route.Spec.Hostnames) + } else { hostnames = route.Spec.Hostnames } routeConditions = append(routeConditions, conditionsFromRule(rule, hostnames)...) diff --git a/pkg/rlptools/wasm_utils_test.go b/pkg/rlptools/wasm_utils_test.go index 5717c3a42..f1c5206a1 100644 --- a/pkg/rlptools/wasm_utils_test.go +++ b/pkg/rlptools/wasm_utils_test.go @@ -58,6 +58,14 @@ func TestWasmRules(t *testing.T) { Unit: kuadrantv1beta2.TimeUnit("minute"), }, }, + RouteSelectors: []kuadrantv1beta2.RouteSelector{ + { + Hostnames: []gatewayapiv1beta1.Hostname{ + "*.example.com", + "myapp.apps.example.com", // ignored + }, + }, + }, }, }, },