-
Notifications
You must be signed in to change notification settings - Fork 795
fix: Add overlap status for duplicate HTTPRoutes #8373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a45a09a
route overlap
jukie 92aa64a
Check all route types vs only HTTPRoute
jukie ed22848
lint
jukie 82f9942
Merge branch 'main' into route-overlap
jukie 1b002aa
feedback
jukie 8e75daa
use listener name map to avoid repeated linear scans in checkRouteOve…
jukie 5acc84b
Merge branch 'main' into route-overlap
jukie 4071e97
Feedback
jukie 65efdc5
Merge branch 'main' into route-overlap
jukie 3709b55
Merge branch 'main' into route-overlap
jukie fe3d5bf
Merge branch 'main' into route-overlap
jukie 0a8c269
Merge branch 'main' into route-overlap
jukie d2b5973
Merge branch 'main' into route-overlap
jukie e5adbc8
feedback and tweaks
jukie 180f735
Merge branch 'main' into route-overlap
jukie 2b8be90
Merge branch 'main' into route-overlap
jukie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,286 @@ | ||
| // Copyright Envoy Gateway Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // The full text of the Apache license is available in the LICENSE file at | ||
| // the root of the repo. | ||
|
|
||
| package gatewayapi | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "k8s.io/utils/ptr" | ||
|
|
||
| "github.com/envoyproxy/gateway/internal/ir" | ||
| ) | ||
|
|
||
| func TestRouteMatchesOverlap(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| a *ir.HTTPRoute | ||
| b *ir.HTTPRoute | ||
| overlap bool | ||
| }{ | ||
| { | ||
| name: "identical exact path and hostname", | ||
| a: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| }, | ||
| overlap: true, | ||
| }, | ||
| { | ||
| name: "different exact paths", | ||
| a: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/bar")}, | ||
| }, | ||
| overlap: false, | ||
| }, | ||
| { | ||
| name: "different hostnames", | ||
| a: &ir.HTTPRoute{ | ||
| Hostname: "a.example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "b.example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| }, | ||
| overlap: false, | ||
| }, | ||
| { | ||
| name: "prefix paths are not detected as overlap", | ||
| a: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Prefix: ptr.To("/api")}, | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Prefix: ptr.To("/api")}, | ||
| }, | ||
| overlap: false, | ||
| }, | ||
| { | ||
| name: "nil path matches are not detected as overlap", | ||
|
jukie marked this conversation as resolved.
Outdated
|
||
| a: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| }, | ||
| overlap: false, | ||
| }, | ||
| { | ||
| name: "exact vs prefix same value not overlap", | ||
| a: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Prefix: ptr.To("/foo")}, | ||
| }, | ||
| overlap: false, | ||
| }, | ||
| { | ||
| name: "identical exact path with identical header matches", | ||
| a: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| HeaderMatches: []*ir.StringMatch{ | ||
| {Name: "X-Custom", Exact: ptr.To("val1")}, | ||
| }, | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| HeaderMatches: []*ir.StringMatch{ | ||
| {Name: "X-Custom", Exact: ptr.To("val1")}, | ||
| }, | ||
| }, | ||
| overlap: true, | ||
| }, | ||
| { | ||
| name: "identical exact path with different header matches", | ||
| a: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| HeaderMatches: []*ir.StringMatch{ | ||
| {Name: "X-Custom", Exact: ptr.To("val1")}, | ||
| }, | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| HeaderMatches: []*ir.StringMatch{ | ||
| {Name: "X-Custom", Exact: ptr.To("val2")}, | ||
| }, | ||
| }, | ||
| overlap: false, | ||
| }, | ||
| { | ||
| name: "identical exact path one has headers other does not", | ||
| a: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| HeaderMatches: []*ir.StringMatch{ | ||
| {Name: "X-Custom", Exact: ptr.To("val1")}, | ||
| }, | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| }, | ||
| overlap: false, | ||
| }, | ||
| { | ||
| name: "identical exact path with identical query param matches", | ||
| a: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| QueryParamMatches: []*ir.StringMatch{ | ||
| {Name: "key", Exact: ptr.To("value")}, | ||
| }, | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{Exact: ptr.To("/foo")}, | ||
| QueryParamMatches: []*ir.StringMatch{ | ||
| {Name: "key", Exact: ptr.To("value")}, | ||
| }, | ||
| }, | ||
| overlap: true, | ||
| }, | ||
| { | ||
| name: "regex path matches are not detected as overlap", | ||
|
jukie marked this conversation as resolved.
Outdated
|
||
| a: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{SafeRegex: ptr.To("/foo.*")}, | ||
| }, | ||
| b: &ir.HTTPRoute{ | ||
| Hostname: "example.com", | ||
| PathMatch: &ir.StringMatch{SafeRegex: ptr.To("/foo.*")}, | ||
| }, | ||
| overlap: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| assert.Equal(t, tt.overlap, routeMatchesOverlap(tt.a, tt.b)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestStringMatchEqual(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| a *ir.StringMatch | ||
| b *ir.StringMatch | ||
| equal bool | ||
| }{ | ||
| { | ||
| name: "both nil", | ||
| a: nil, | ||
| b: nil, | ||
| equal: true, | ||
| }, | ||
| { | ||
| name: "one nil", | ||
| a: &ir.StringMatch{Exact: ptr.To("foo")}, | ||
| b: nil, | ||
| equal: false, | ||
| }, | ||
| { | ||
| name: "identical exact", | ||
| a: &ir.StringMatch{Name: "h", Exact: ptr.To("v")}, | ||
| b: &ir.StringMatch{Name: "h", Exact: ptr.To("v")}, | ||
| equal: true, | ||
| }, | ||
| { | ||
| name: "different name", | ||
| a: &ir.StringMatch{Name: "a", Exact: ptr.To("v")}, | ||
| b: &ir.StringMatch{Name: "b", Exact: ptr.To("v")}, | ||
| equal: false, | ||
| }, | ||
| { | ||
| name: "different value", | ||
| a: &ir.StringMatch{Name: "h", Exact: ptr.To("v1")}, | ||
| b: &ir.StringMatch{Name: "h", Exact: ptr.To("v2")}, | ||
| equal: false, | ||
| }, | ||
| { | ||
| name: "different distinct", | ||
| a: &ir.StringMatch{Name: "h", Distinct: true}, | ||
| b: &ir.StringMatch{Name: "h", Distinct: false}, | ||
| equal: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| assert.Equal(t, tt.equal, stringMatchEqual(tt.a, tt.b)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestStringMatchSliceEqual(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| a []*ir.StringMatch | ||
| b []*ir.StringMatch | ||
| equal bool | ||
| }{ | ||
| { | ||
| name: "both empty", | ||
| a: nil, | ||
| b: nil, | ||
| equal: true, | ||
| }, | ||
| { | ||
| name: "different lengths", | ||
| a: []*ir.StringMatch{{Name: "a", Exact: ptr.To("1")}}, | ||
| b: nil, | ||
| equal: false, | ||
| }, | ||
| { | ||
| name: "identical", | ||
| a: []*ir.StringMatch{ | ||
| {Name: "a", Exact: ptr.To("1")}, | ||
| {Name: "b", Exact: ptr.To("2")}, | ||
| }, | ||
| b: []*ir.StringMatch{ | ||
| {Name: "a", Exact: ptr.To("1")}, | ||
| {Name: "b", Exact: ptr.To("2")}, | ||
| }, | ||
| equal: true, | ||
| }, | ||
| { | ||
| name: "same elements different order", | ||
| a: []*ir.StringMatch{ | ||
| {Name: "a", Exact: ptr.To("1")}, | ||
| {Name: "b", Exact: ptr.To("2")}, | ||
| }, | ||
| b: []*ir.StringMatch{ | ||
| {Name: "b", Exact: ptr.To("2")}, | ||
| {Name: "a", Exact: ptr.To("1")}, | ||
| }, | ||
| equal: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| assert.Equal(t, tt.equal, stringMatchSliceEqual(tt.a, tt.b)) | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.