Skip to content

Commit

Permalink
Fix Gateway API HttpRoute cannot strip path prefix.
Browse files Browse the repository at this point in the history
According to Kubernetes guidlines empty prefix should be allowed.

Fixes: #27994

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
  • Loading branch information
chaunceyjiang authored and julianwiedmann committed Sep 14, 2023
1 parent 76c86df commit 567bc43
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions operator/pkg/model/translation/envoy_virtual_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,16 @@ func hostRewriteMutation(rewrite *model.HTTPURLRewriteFilter) routeActionMutatio

func pathPrefixMutation(rewrite *model.HTTPURLRewriteFilter) routeActionMutation {
return func(route *envoy_config_route_v3.Route_Route) *envoy_config_route_v3.Route_Route {
if rewrite == nil || rewrite.Path == nil || len(rewrite.Path.Prefix) == 0 {
if rewrite == nil || rewrite.Path == nil || len(rewrite.Path.Exact) != 0 || len(rewrite.Path.Regex) != 0 {
return route
}
route.Route.PrefixRewrite = rewrite.Path.Prefix
if len(rewrite.Path.Prefix) == 0 {
// Refer to: https://gateway-api.sigs.k8s.io/references/spec/#gateway.networking.k8s.io%2fv1beta1.HTTPPathModifier
// ReplacePrefix is allowed to be empty.
route.Route.PrefixRewrite = "/"
} else {
route.Route.PrefixRewrite = rewrite.Path.Prefix
}
return route
}
}
Expand Down
13 changes: 13 additions & 0 deletions operator/pkg/model/translation/envoy_virtual_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ func Test_pathPrefixMutation(t *testing.T) {
res := pathPrefixMutation(rewrite)(route)
require.Equal(t, res.Route.PrefixRewrite, "/prefix")
})
t.Run("with empty prefix rewrite", func(t *testing.T) {
route := &envoy_config_route_v3.Route_Route{
Route: &envoy_config_route_v3.RouteAction{},
}
rewrite := &model.HTTPURLRewriteFilter{
Path: &model.StringMatch{
Prefix: "",
},
}

res := pathPrefixMutation(rewrite)(route)
require.Equal(t, res.Route.PrefixRewrite, "/")
})
}

func Test_requestMirrorMutation(t *testing.T) {
Expand Down

0 comments on commit 567bc43

Please sign in to comment.