Skip to content

Commit

Permalink
generate wasm rules only for routeSelector-filtered hostnames that be…
Browse files Browse the repository at this point in the history
…long to the targeted route
  • Loading branch information
guicassolato committed Jun 26, 2023
1 parent 117312b commit 62a4a43
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
16 changes: 16 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
62 changes: 62 additions & 0 deletions pkg/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 4 additions & 2 deletions pkg/rlptools/wasm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
Expand Down
8 changes: 8 additions & 0 deletions pkg/rlptools/wasm_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
},
},
},
},
Expand Down

0 comments on commit 62a4a43

Please sign in to comment.