Skip to content

Commit 3249d4b

Browse files
committed
+ Added grpc multiple mirror support
+ Added tests
1 parent e9f451d commit 3249d4b

File tree

4 files changed

+199
-2
lines changed

4 files changed

+199
-2
lines changed

internal/dag/accessors_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,36 @@ func TestGetServiceClusters(t *testing.T) {
291291
},
292292
},
293293
}
294+
295+
dagWithMirror := &DAG{
296+
Listeners: map[string]*Listener{
297+
"http-1": {
298+
VirtualHosts: []*VirtualHost{
299+
{
300+
Routes: map[string]*Route{
301+
"foo": {
302+
Clusters: []*Cluster{
303+
{Upstream: &Service{ExternalName: "bar.com"}},
304+
{Upstream: &Service{}},
305+
},
306+
MirrorPolicies: []*MirrorPolicy{
307+
{
308+
Cluster: &Cluster{
309+
Upstream: &Service{},
310+
},
311+
},
312+
},
313+
},
314+
},
315+
},
316+
},
317+
},
318+
},
319+
}
294320
// We should only get one cluster since the other is for an ExternalName
295321
// service.
296322
assert.Len(t, d.GetServiceClusters(), 1)
323+
324+
// We should get one two clusters since we have mirrorPolicies.
325+
assert.Len(t, dagWithMirror.GetServiceClusters(), 2)
297326
}

internal/dag/builder_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3844,6 +3844,54 @@ func TestDAGInsertGatewayAPI(t *testing.T) {
38443844
},
38453845
),
38463846
},
3847+
"HTTPRoute rule with two request mirror filters": {
3848+
gatewayclass: validClass,
3849+
gateway: gatewayHTTPAllNamespaces,
3850+
objs: []any{
3851+
kuardService,
3852+
kuardService2,
3853+
kuardService3,
3854+
&gatewayapi_v1beta1.HTTPRoute{
3855+
ObjectMeta: metav1.ObjectMeta{
3856+
Name: "basic",
3857+
Namespace: "projectcontour",
3858+
},
3859+
Spec: gatewayapi_v1beta1.HTTPRouteSpec{
3860+
CommonRouteSpec: gatewayapi_v1beta1.CommonRouteSpec{
3861+
ParentRefs: []gatewayapi_v1beta1.ParentReference{gatewayapi.GatewayParentRef("projectcontour", "contour")},
3862+
},
3863+
Hostnames: []gatewayapi_v1beta1.Hostname{
3864+
"test.projectcontour.io",
3865+
},
3866+
Rules: []gatewayapi_v1beta1.HTTPRouteRule{{
3867+
Matches: gatewayapi.HTTPRouteMatch(gatewayapi_v1beta1.PathMatchPathPrefix, "/"),
3868+
BackendRefs: gatewayapi.HTTPBackendRef("kuard", 8080, 1),
3869+
Filters: []gatewayapi_v1beta1.HTTPRouteFilter{
3870+
{
3871+
Type: gatewayapi_v1beta1.HTTPRouteFilterRequestMirror,
3872+
RequestMirror: &gatewayapi_v1beta1.HTTPRequestMirrorFilter{
3873+
BackendRef: gatewayapi.ServiceBackendObjectRef("kuard2", 8080),
3874+
},
3875+
},
3876+
{
3877+
Type: gatewayapi_v1beta1.HTTPRouteFilterRequestMirror,
3878+
RequestMirror: &gatewayapi_v1beta1.HTTPRequestMirrorFilter{
3879+
BackendRef: gatewayapi.ServiceBackendObjectRef("kuard3", 8080),
3880+
},
3881+
},
3882+
},
3883+
}},
3884+
},
3885+
},
3886+
},
3887+
want: listeners(
3888+
&Listener{
3889+
Name: "http-80",
3890+
VirtualHosts: virtualhosts(virtualhost("test.projectcontour.io",
3891+
withMirror(prefixrouteHTTPRoute("/", service(kuardService)), []*Service{service(kuardService2), service(kuardService3)}, 100))),
3892+
},
3893+
),
3894+
},
38473895
"HTTPRoute rule with request mirror filter with multiple matches": {
38483896
gatewayclass: validClass,
38493897
gateway: gatewayHTTPAllNamespaces,
@@ -5706,6 +5754,56 @@ func TestDAGInsertGatewayAPI(t *testing.T) {
57065754
},
57075755
),
57085756
},
5757+
"GRPCRoute: rule with two request mirror filters": {
5758+
gatewayclass: validClass,
5759+
gateway: gatewayHTTPAllNamespaces,
5760+
objs: []any{
5761+
kuardService,
5762+
kuardService2,
5763+
kuardService3,
5764+
&gatewayapi_v1alpha2.GRPCRoute{
5765+
ObjectMeta: metav1.ObjectMeta{
5766+
Name: "basic",
5767+
Namespace: "projectcontour",
5768+
},
5769+
Spec: gatewayapi_v1alpha2.GRPCRouteSpec{
5770+
CommonRouteSpec: gatewayapi_v1alpha2.CommonRouteSpec{
5771+
ParentRefs: []gatewayapi_v1alpha2.ParentReference{gatewayapi.GatewayParentRef("projectcontour", "contour")},
5772+
},
5773+
Hostnames: []gatewayapi_v1alpha2.Hostname{
5774+
"test.projectcontour.io",
5775+
},
5776+
Rules: []gatewayapi_v1alpha2.GRPCRouteRule{{
5777+
Matches: []gatewayapi_v1alpha2.GRPCRouteMatch{{
5778+
Method: gatewayapi.GRPCMethodMatch(gatewayapi_v1alpha2.GRPCMethodMatchExact, "io.projectcontour", "Login"),
5779+
}},
5780+
BackendRefs: gatewayapi.GRPCRouteBackendRef("kuard", 8080, 1),
5781+
Filters: []gatewayapi_v1alpha2.GRPCRouteFilter{
5782+
{
5783+
Type: gatewayapi_v1alpha2.GRPCRouteFilterRequestMirror,
5784+
RequestMirror: &gatewayapi_v1alpha2.HTTPRequestMirrorFilter{
5785+
BackendRef: gatewayapi.ServiceBackendObjectRef("kuard2", 8080),
5786+
},
5787+
},
5788+
{
5789+
Type: gatewayapi_v1alpha2.GRPCRouteFilterRequestMirror,
5790+
RequestMirror: &gatewayapi_v1alpha2.HTTPRequestMirrorFilter{
5791+
BackendRef: gatewayapi.ServiceBackendObjectRef("kuard3", 8080),
5792+
},
5793+
},
5794+
},
5795+
}},
5796+
},
5797+
},
5798+
},
5799+
want: listeners(
5800+
&Listener{
5801+
Name: "http-80",
5802+
VirtualHosts: virtualhosts(virtualhost("test.projectcontour.io",
5803+
withMirror(exactrouteGRPCRoute("/io.projectcontour/Login", grpcService(kuardService, "h2c")), []*Service{grpcService(kuardService2, "h2c"), grpcService(kuardService3, "h2c")}, 100))),
5804+
},
5805+
),
5806+
},
57095807

57105808
"GRPCRoute: references a backend in a different namespace, with valid ReferenceGrant": {
57115809
gatewayclass: validClass,

internal/dag/gatewayapi_processor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,8 +1433,7 @@ func (p *GatewayAPIProcessor) computeGRPCRouteForListener(route *gatewayapi_v1al
14331433
routeAccessor.AddCondition(gatewayapi_v1beta1.RouteConditionResolvedRefs, metav1.ConditionFalse, status.ReasonDegraded, fmt.Sprintf("%s on response headers", err))
14341434
}
14351435
case gatewayapi_v1alpha2.GRPCRouteFilterRequestMirror:
1436-
// If more than one, we only take the first RequestMirror filter.
1437-
if filter.RequestMirror == nil || len(mirrorPolicies) > 0 {
1436+
if filter.RequestMirror == nil {
14381437
continue
14391438
}
14401439

internal/envoy/v3/route_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import (
4343
func TestRouteRoute(t *testing.T) {
4444
s1 := fixture.NewService("kuard").
4545
WithPorts(v1.ServicePort{Name: "http", Port: 8080, TargetPort: intstr.FromInt(8080)})
46+
s2 := fixture.NewService("kuard2").
47+
WithPorts(v1.ServicePort{Name: "http", Port: 8080, TargetPort: intstr.FromInt(8080)})
4648
c1 := &dag.Cluster{
4749
Upstream: &dag.Service{
4850
Weighted: dag.WeightedService{
@@ -722,6 +724,75 @@ func TestRouteRoute(t *testing.T) {
722724
},
723725
},
724726
},
727+
"two mirrors": {
728+
route: &dag.Route{
729+
Clusters: []*dag.Cluster{{
730+
Upstream: &dag.Service{
731+
Weighted: dag.WeightedService{
732+
Weight: 1,
733+
ServiceName: s1.Name,
734+
ServiceNamespace: s1.Namespace,
735+
ServicePort: s1.Spec.Ports[0],
736+
},
737+
},
738+
Weight: 90,
739+
}},
740+
MirrorPolicies: []*dag.MirrorPolicy{
741+
{
742+
Cluster: &dag.Cluster{
743+
Upstream: &dag.Service{
744+
Weighted: dag.WeightedService{
745+
Weight: 1,
746+
ServiceName: s1.Name,
747+
ServiceNamespace: s1.Namespace,
748+
ServicePort: s1.Spec.Ports[0],
749+
},
750+
},
751+
},
752+
Weight: 100,
753+
},
754+
{
755+
Cluster: &dag.Cluster{
756+
Upstream: &dag.Service{
757+
Weighted: dag.WeightedService{
758+
Weight: 1,
759+
ServiceName: s2.Name,
760+
ServiceNamespace: s2.Namespace,
761+
ServicePort: s2.Spec.Ports[0],
762+
},
763+
},
764+
},
765+
Weight: 100,
766+
},
767+
}},
768+
want: &envoy_route_v3.Route_Route{
769+
Route: &envoy_route_v3.RouteAction{
770+
ClusterSpecifier: &envoy_route_v3.RouteAction_Cluster{
771+
Cluster: "default/kuard/8080/da39a3ee5e",
772+
},
773+
RequestMirrorPolicies: []*envoy_route_v3.RouteAction_RequestMirrorPolicy{
774+
{
775+
Cluster: "default/kuard/8080/da39a3ee5e",
776+
RuntimeFraction: &envoy_core_v3.RuntimeFractionalPercent{
777+
DefaultValue: &envoy_type_v3.FractionalPercent{
778+
Numerator: 100,
779+
Denominator: envoy_type_v3.FractionalPercent_HUNDRED,
780+
},
781+
},
782+
},
783+
{
784+
Cluster: "default/kuard2/8080/da39a3ee5e",
785+
RuntimeFraction: &envoy_core_v3.RuntimeFractionalPercent{
786+
DefaultValue: &envoy_type_v3.FractionalPercent{
787+
Numerator: 100,
788+
Denominator: envoy_type_v3.FractionalPercent_HUNDRED,
789+
},
790+
},
791+
},
792+
},
793+
},
794+
},
795+
},
725796
"prefix rewrite": {
726797
route: &dag.Route{
727798
Clusters: []*dag.Cluster{c1},

0 commit comments

Comments
 (0)