-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
xds/internal/xdsclient: Add support for String Matcher Header Matcher in RDS #6313
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -467,3 +467,83 @@ func TestHeaderSuffixMatcherMatch(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestHeaderStringMatch(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
key string | ||
sm StringMatcher | ||
invert bool | ||
md metadata.MD | ||
want bool | ||
}{ | ||
{ | ||
name: "should-match", | ||
key: "th", | ||
sm: StringMatcher{ | ||
exactMatch: newStringP("tv"), | ||
}, | ||
invert: false, | ||
md: metadata.Pairs("th", "tv"), | ||
want: true, | ||
}, | ||
{ | ||
name: "not match", | ||
key: "th", | ||
sm: StringMatcher{ | ||
containsMatch: newStringP("tv"), | ||
}, | ||
invert: false, | ||
md: metadata.Pairs("th", "not-match"), | ||
want: false, | ||
}, | ||
{ | ||
name: "invert string match", | ||
key: "th", | ||
sm: StringMatcher{ | ||
containsMatch: newStringP("tv"), | ||
}, | ||
invert: true, | ||
md: metadata.Pairs("th", "not-match"), | ||
want: true, | ||
}, | ||
{ | ||
name: "header missing", | ||
key: "th", | ||
sm: StringMatcher{ | ||
containsMatch: newStringP("tv"), | ||
}, | ||
invert: false, | ||
md: metadata.Pairs("not-specified-key", "not-match"), | ||
want: false, | ||
}, | ||
{ | ||
name: "header missing invert true", | ||
key: "th", | ||
sm: StringMatcher{ | ||
containsMatch: newStringP("tv"), | ||
}, | ||
invert: true, | ||
md: metadata.Pairs("not-specified-key", "not-match"), | ||
want: false, | ||
}, | ||
{ | ||
name: "header empty string invert", | ||
key: "th", | ||
sm: StringMatcher{ | ||
containsMatch: newStringP("tv"), | ||
}, | ||
invert: true, | ||
md: metadata.Pairs("th", ""), | ||
want: true, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add test cases that cover the unusual corner cases mentioned above once we determine the correct behavior. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Added 3 test cases, invert and not invert for missing, and invert for empty string wanting true. |
||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
hsm := NewHeaderStringMatcher(test.key, test.sm, test.invert) | ||
if got := hsm.Match(test.md); got != test.want { | ||
t.Errorf("match() = %v, want %v", got, test.want) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,13 +24,15 @@ import ( | |
"strings" | ||
"time" | ||
|
||
v3routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3" | ||
v3typepb "github.com/envoyproxy/go-control-plane/envoy/type/v3" | ||
"github.com/golang/protobuf/proto" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/internal/envconfig" | ||
"google.golang.org/grpc/internal/xds/matcher" | ||
"google.golang.org/grpc/xds/internal/clusterspecifier" | ||
"google.golang.org/protobuf/types/known/anypb" | ||
|
||
v3routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3" | ||
v3typepb "github.com/envoyproxy/go-control-plane/envoy/type/v3" | ||
) | ||
|
||
func unmarshalRouteConfigResource(r *anypb.Any) (string, RouteConfigUpdate, error) { | ||
|
@@ -273,6 +275,12 @@ func routesProtoToSlice(routes []*v3routepb.Route, csps map[string]clusterspecif | |
header.PrefixMatch = &ht.PrefixMatch | ||
case *v3routepb.HeaderMatcher_SuffixMatch: | ||
header.SuffixMatch = &ht.SuffixMatch | ||
case *v3routepb.HeaderMatcher_StringMatch: | ||
sm, err := matcher.StringMatcherFromProto(ht.StringMatch) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("route %+v has an invalid string matcher: %v", err, ht.StringMatch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't correct formatting, I need to fix. |
||
} | ||
header.StringMatch = &sm | ||
default: | ||
return nil, nil, fmt.Errorf("route %+v has an unrecognized header matcher: %+v", r, ht) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior here is unclear in some cases:
false
? Or should it reporttrue
ifinvert=true
?""
, is that distinct from the header being missing, in any way?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my chat thread. I think this is WAI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.