Skip to content

Commit

Permalink
Support RegularExpression for path matching
Browse files Browse the repository at this point in the history
  • Loading branch information
dmavrommatis committed May 23, 2024
1 parent 0e215f9 commit 6e61fe0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/content/routing/providers/kubernetes-gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Kubernetes cluster before creating `HTTPRoute` objects.
| [6] | `rules` | A list of HTTP matchers, filters and actions. |
| [7] | `matches` | Conditions used for matching the rule against incoming HTTP requests. Each match is independent, i.e. this rule will be matched if **any** one of the matches is satisfied. |
| [8] | `path` | An HTTP request path matcher. If this field is not specified, a default prefix match on the "/" path is provided. |
| [9] | `type` | Type of match against the path Value (supported types: `Exact`, `PathPrefix`). |
| [9] | `type` | Type of match against the path Value (supported types: `Exact`, `PathPrefix`, and `RegularExpression`). |
| [10] | `value` | The value of the HTTP path to match against. |
| [11] | `headers` | Conditions to select a HTTP route by matching HTTP request headers. |
| [12] | `name` | Name of the HTTP header to be matched. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ spec:
weight: 1
kind: Service
group: ""

- matches:
- path:
type: RegularExpression
value: "^/buzz/[0-9]+$"
backendRefs:
- name: whoami
port: 80
weight: 1
kind: Service
group: ""
2 changes: 2 additions & 0 deletions pkg/provider/kubernetes/gateway/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,8 @@ func extractRule(routeRule gatev1.HTTPRouteRule, hostRule string) (string, error
matchRules = append(matchRules, fmt.Sprintf("Path(`%s`)", *match.Path.Value))
case gatev1.PathMatchPathPrefix:
matchRules = append(matchRules, buildPathMatchPathPrefixRule(*match.Path.Value))
case gatev1.PathMatchRegularExpression:
matchRules = append(matchRules, fmt.Sprintf("PathRegexp(`%s`)", *match.Path.Value))
default:
return "", fmt.Errorf("unsupported path match type %s", *match.Path.Type)
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/provider/kubernetes/gateway/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,12 @@ func TestLoadHTTPRoutes(t *testing.T) {
Rule: "Host(`foo.com`) && Path(`/bar`) && Header(`my-header`,`bar`)",
RuleSyntax: "v3",
},
"default-http-app-1-my-gateway-web-d23f7039bc8036fb918c": {
EntryPoints: []string{"web"},
Service: "default-http-app-1-my-gateway-web-d23f7039bc8036fb918c-wrr",
Rule: "Host(`foo.com`) && PathRegexp(`^/buzz/[0-9]+$`)",
RuleSyntax: "v3",
},
},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{
Expand All @@ -1323,6 +1329,16 @@ func TestLoadHTTPRoutes(t *testing.T) {
},
},
},
"default-http-app-1-my-gateway-web-d23f7039bc8036fb918c-wrr": {
Weighted: &dynamic.WeightedRoundRobin{
Services: []dynamic.WRRService{
{
Name: "default-whoami-80",
Weight: func(i int) *int { return &i }(1),
},
},
},
},
"default-whoami-80": {
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
Expand Down

0 comments on commit 6e61fe0

Please sign in to comment.