diff --git a/internal/envoy/v3/listener.go b/internal/envoy/v3/listener.go index 73403867ff9..c5a80505f3d 100644 --- a/internal/envoy/v3/listener.go +++ b/internal/envoy/v3/listener.go @@ -418,7 +418,7 @@ func (b *httpConnectionManagerBuilder) AddFilter(f *http.HttpFilter) *httpConnec // If this happens, it has to be programmer error, so we panic to tell them // it needs to be fixed. Note that in hitting this case, it doesn't matter we added // the second one earlier, because we're panicking anyway. - if f.GetTypedConfig().MessageIs(&envoy_router_v3.Router{}) { + if f.GetTypedConfig().MessageIs(&envoy_router_v3.Router{}) && routerIndex != lastIndex { panic("Can't add more than one router to a filter chain") } if routerIndex != lastIndex { diff --git a/internal/envoy/v3/listener_test.go b/internal/envoy/v3/listener_test.go index 12bccde3262..aa2164284a2 100644 --- a/internal/envoy/v3/listener_test.go +++ b/internal/envoy/v3/listener_test.go @@ -1785,6 +1785,35 @@ func TestAddFilter(t *testing.T) { }, }, }, + "Add a single router filter to non-empty builder": { + builder: HTTPConnectionManagerBuilder().AddFilter(&http.HttpFilter{ + Name: "grpcweb", + ConfigType: &http.HttpFilter_TypedConfig{ + TypedConfig: protobuf.MustMarshalAny(&envoy_grpc_web_v3.GrpcWeb{}), + }, + }), + add: &http.HttpFilter{ + Name: "router", + ConfigType: &http.HttpFilter_TypedConfig{ + TypedConfig: protobuf.MustMarshalAny(&envoy_router_v3.Router{}), + }, + }, + want: []*http.HttpFilter{ + { + Name: "grpcweb", + ConfigType: &http.HttpFilter_TypedConfig{ + TypedConfig: protobuf.MustMarshalAny(&envoy_grpc_web_v3.GrpcWeb{}), + }, + }, + { + Name: "router", + ConfigType: &http.HttpFilter_TypedConfig{ + TypedConfig: protobuf.MustMarshalAny(&envoy_router_v3.Router{}), + }, + }, + }, + }, + "Add a filter to a builder with a router": { builder: HTTPConnectionManagerBuilder().AddFilter(&http.HttpFilter{ Name: "router",