Skip to content

Commit 057f003

Browse files
committed
API: host rewrite from path
Signed-off-by: Huabing (Robin) Zhao <zhaohuabing@gmail.com>
1 parent cbb4337 commit 057f003

8 files changed

Lines changed: 234 additions & 1 deletion

File tree

api/v1alpha1/httproutefilter_types.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ const (
116116
// BackendHTTPHostnameModifier indicates that the Host header value would be replaced by the DNS name of the backend if it exists.
117117
// https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-field-config-route-v3-routeaction-auto-host-rewrite
118118
BackendHTTPHostnameModifier HTTPHostnameModifierType = "Backend"
119+
// PathRegexHTTPHostnameModifier indicates that the Host header value would be rewritten by applying a regex
120+
// match and substitution to the request path.
121+
// https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-field-config-route-v3-routeaction-host-rewrite-path-regex
122+
PathRegexHTTPHostnameModifier HTTPHostnameModifierType = "PathRegex"
119123
)
120124

121125
type ReplaceRegexMatch struct {
@@ -128,6 +132,21 @@ type ReplaceRegexMatch struct {
128132
Substitution string `json:"substitution"`
129133
}
130134

135+
// HostnamePathRegexRewrite defines a hostname rewrite computed from the request path using regex.
136+
type HostnamePathRegexRewrite struct {
137+
// Pattern matches a regular expression against the value of the HTTP Path. The regex string must
138+
// adhere to the syntax documented in https://github.com/google/re2/wiki/Syntax.
139+
// +kubebuilder:validation:MinLength=1
140+
Pattern string `json:"pattern"`
141+
// Substitution is an expression that replaces the matched portion. The expression may include numbered
142+
// capture groups that adhere to syntax documented in https://github.com/google/re2/wiki/Syntax.
143+
//
144+
// The resulting value is used as the upstream Host header and should be constrained to a valid
145+
// DNS hostname by using explicit regex capture groups in Pattern.
146+
// +kubebuilder:validation:MinLength=1
147+
Substitution string `json:"substitution"`
148+
}
149+
131150
// +kubebuilder:validation:XValidation:rule="self.type == 'ReplaceRegexMatch' ? has(self.replaceRegexMatch) : !has(self.replaceRegexMatch)",message="If HTTPPathModifier type is ReplaceRegexMatch, replaceRegexMatch field needs to be set."
132151
type HTTPPathModifier struct {
133152
// +kubebuilder:validation:Enum=ReplaceRegexMatch
@@ -158,14 +177,27 @@ type HTTPPathModifier struct {
158177

159178
// +kubebuilder:validation:XValidation:message="header must be nil if the type is not Header",rule="!(has(self.header) && self.type != 'Header')"
160179
// +kubebuilder:validation:XValidation:message="header must be specified for Header type",rule="!(!has(self.header) && self.type == 'Header')"
180+
// +kubebuilder:validation:XValidation:message="pathRegex must be nil if the type is not PathRegex",rule="!(has(self.pathRegex) && self.type != 'PathRegex')"
181+
// +kubebuilder:validation:XValidation:message="pathRegex must be specified for PathRegex type",rule="!(!has(self.pathRegex) && self.type == 'PathRegex')"
161182
type HTTPHostnameModifier struct {
162-
// +kubebuilder:validation:Enum=Header;Backend
183+
// +kubebuilder:validation:Enum=Header;Backend;PathRegex
163184
// +kubebuilder:validation:Required
164185
Type HTTPHostnameModifierType `json:"type"`
165186

166187
// Header is the name of the header whose value would be used to rewrite the Host header
167188
// +optional
168189
Header *string `json:"header,omitempty"`
190+
191+
// PathRegex defines a regex match and substitution applied to the request path to compute
192+
// the rewritten Host header.
193+
// For example:
194+
// pathRegex:
195+
// pattern: "^/tenant/([a-z0-9-]+)/.*"
196+
// substitution: "\\1.example.internal"
197+
//
198+
// +optional
199+
// +notImplementedHide
200+
PathRegex *HostnamePathRegexRewrite `json:"pathRegex,omitempty"`
169201
}
170202

171203
// HTTPCredentialInjectionFilter defines the configuration to inject credentials into the request.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/gateway-crds-helm/templates/generated/gateway.envoyproxy.io_httproutefilters.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,41 @@ spec:
415415
description: Header is the name of the header whose value
416416
would be used to rewrite the Host header
417417
type: string
418+
pathRegex:
419+
description: |-
420+
PathRegex defines a regex match and substitution applied to the request path to compute
421+
the rewritten Host header.
422+
For example:
423+
pathRegex:
424+
pattern: "^/tenant/([a-z0-9-]+)/.*"
425+
substitution: "\\1.example.internal"
426+
properties:
427+
pattern:
428+
description: |-
429+
Pattern matches a regular expression against the value of the HTTP Path. The regex string must
430+
adhere to the syntax documented in https://github.com/google/re2/wiki/Syntax.
431+
minLength: 1
432+
type: string
433+
substitution:
434+
description: |-
435+
Substitution is an expression that replaces the matched portion. The expression may include numbered
436+
capture groups that adhere to syntax documented in https://github.com/google/re2/wiki/Syntax.
437+
438+
The resulting value is used as the upstream Host header and should be constrained to a valid
439+
DNS hostname by using explicit regex capture groups in Pattern.
440+
minLength: 1
441+
type: string
442+
required:
443+
- pattern
444+
- substitution
445+
type: object
418446
type:
419447
description: HTTPPathModifierType defines the type of Hostname
420448
rewrite.
421449
enum:
422450
- Header
423451
- Backend
452+
- PathRegex
424453
type: string
425454
required:
426455
- type
@@ -430,6 +459,10 @@ spec:
430459
rule: '!(has(self.header) && self.type != ''Header'')'
431460
- message: header must be specified for Header type
432461
rule: '!(!has(self.header) && self.type == ''Header'')'
462+
- message: pathRegex must be nil if the type is not PathRegex
463+
rule: '!(has(self.pathRegex) && self.type != ''PathRegex'')'
464+
- message: pathRegex must be specified for PathRegex type
465+
rule: '!(!has(self.pathRegex) && self.type == ''PathRegex'')'
433466
path:
434467
description: Path defines a path rewrite.
435468
properties:

charts/gateway-helm/charts/crds/crds/generated/gateway.envoyproxy.io_httproutefilters.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,41 @@ spec:
414414
description: Header is the name of the header whose value
415415
would be used to rewrite the Host header
416416
type: string
417+
pathRegex:
418+
description: |-
419+
PathRegex defines a regex match and substitution applied to the request path to compute
420+
the rewritten Host header.
421+
For example:
422+
pathRegex:
423+
pattern: "^/tenant/([a-z0-9-]+)/.*"
424+
substitution: "\\1.example.internal"
425+
properties:
426+
pattern:
427+
description: |-
428+
Pattern matches a regular expression against the value of the HTTP Path. The regex string must
429+
adhere to the syntax documented in https://github.com/google/re2/wiki/Syntax.
430+
minLength: 1
431+
type: string
432+
substitution:
433+
description: |-
434+
Substitution is an expression that replaces the matched portion. The expression may include numbered
435+
capture groups that adhere to syntax documented in https://github.com/google/re2/wiki/Syntax.
436+
437+
The resulting value is used as the upstream Host header and should be constrained to a valid
438+
DNS hostname by using explicit regex capture groups in Pattern.
439+
minLength: 1
440+
type: string
441+
required:
442+
- pattern
443+
- substitution
444+
type: object
417445
type:
418446
description: HTTPPathModifierType defines the type of Hostname
419447
rewrite.
420448
enum:
421449
- Header
422450
- Backend
451+
- PathRegex
423452
type: string
424453
required:
425454
- type
@@ -429,6 +458,10 @@ spec:
429458
rule: '!(has(self.header) && self.type != ''Header'')'
430459
- message: header must be specified for Header type
431460
rule: '!(!has(self.header) && self.type == ''Header'')'
461+
- message: pathRegex must be nil if the type is not PathRegex
462+
rule: '!(has(self.pathRegex) && self.type != ''PathRegex'')'
463+
- message: pathRegex must be specified for PathRegex type
464+
rule: '!(!has(self.pathRegex) && self.type == ''PathRegex'')'
432465
path:
433466
description: Path defines a path rewrite.
434467
properties:

site/content/en/latest/api/extension_types.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,7 @@ _Appears in:_
30883088
| ----- | ----------- |
30893089
| `Header` | HeaderHTTPHostnameModifier indicates that the Host header value would be replaced with the value of the header specified in header.<br />https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-field-config-route-v3-routeaction-host-rewrite-header<br /> |
30903090
| `Backend` | BackendHTTPHostnameModifier indicates that the Host header value would be replaced by the DNS name of the backend if it exists.<br />https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-field-config-route-v3-routeaction-auto-host-rewrite<br /> |
3091+
| `PathRegex` | PathRegexHTTPHostnameModifier indicates that the Host header value would be rewritten by applying a regex<br />match and substitution to the request path.<br />https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-field-config-route-v3-routeaction-host-rewrite-path-regex<br /> |
30913092

30923093

30933094
#### HTTPPathModifier
@@ -3361,6 +3362,21 @@ _Appears in:_
33613362
| `path` | _string_ | true | | Path specifies the HTTP path to match on for health check requests. |
33623363

33633364

3365+
#### HostnamePathRegexRewrite
3366+
3367+
3368+
3369+
HostnamePathRegexRewrite defines a hostname rewrite computed from the request path using regex.
3370+
3371+
_Appears in:_
3372+
- [HTTPHostnameModifier](#httphostnamemodifier)
3373+
3374+
| Field | Type | Required | Default | Description |
3375+
| --- | --- | --- | --- | --- |
3376+
| `pattern` | _string_ | true | | Pattern matches a regular expression against the value of the HTTP Path. The regex string must<br />adhere to the syntax documented in https://github.com/google/re2/wiki/Syntax. |
3377+
| `substitution` | _string_ | true | | Substitution is an expression that replaces the matched portion. The expression may include numbered<br />capture groups that adhere to syntax documented in https://github.com/google/re2/wiki/Syntax.<br />The resulting value is used as the upstream Host header and should be constrained to a valid<br />DNS hostname by using explicit regex capture groups in Pattern. |
3378+
3379+
33643380
#### IPEndpoint
33653381

33663382

test/helm/gateway-crds-helm/all.out.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49833,12 +49833,41 @@ spec:
4983349833
description: Header is the name of the header whose value
4983449834
would be used to rewrite the Host header
4983549835
type: string
49836+
pathRegex:
49837+
description: |-
49838+
PathRegex defines a regex match and substitution applied to the request path to compute
49839+
the rewritten Host header.
49840+
For example:
49841+
pathRegex:
49842+
pattern: "^/tenant/([a-z0-9-]+)/.*"
49843+
substitution: "\\1.example.internal"
49844+
properties:
49845+
pattern:
49846+
description: |-
49847+
Pattern matches a regular expression against the value of the HTTP Path. The regex string must
49848+
adhere to the syntax documented in https://github.com/google/re2/wiki/Syntax.
49849+
minLength: 1
49850+
type: string
49851+
substitution:
49852+
description: |-
49853+
Substitution is an expression that replaces the matched portion. The expression may include numbered
49854+
capture groups that adhere to syntax documented in https://github.com/google/re2/wiki/Syntax.
49855+
49856+
The resulting value is used as the upstream Host header and should be constrained to a valid
49857+
DNS hostname by using explicit regex capture groups in Pattern.
49858+
minLength: 1
49859+
type: string
49860+
required:
49861+
- pattern
49862+
- substitution
49863+
type: object
4983649864
type:
4983749865
description: HTTPPathModifierType defines the type of Hostname
4983849866
rewrite.
4983949867
enum:
4984049868
- Header
4984149869
- Backend
49870+
- PathRegex
4984249871
type: string
4984349872
required:
4984449873
- type
@@ -49848,6 +49877,10 @@ spec:
4984849877
rule: '!(has(self.header) && self.type != ''Header'')'
4984949878
- message: header must be specified for Header type
4985049879
rule: '!(!has(self.header) && self.type == ''Header'')'
49880+
- message: pathRegex must be nil if the type is not PathRegex
49881+
rule: '!(has(self.pathRegex) && self.type != ''PathRegex'')'
49882+
- message: pathRegex must be specified for PathRegex type
49883+
rule: '!(!has(self.pathRegex) && self.type == ''PathRegex'')'
4985149884
path:
4985249885
description: Path defines a path rewrite.
4985349886
properties:

test/helm/gateway-crds-helm/e2e.out.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27806,12 +27806,41 @@ spec:
2780627806
description: Header is the name of the header whose value
2780727807
would be used to rewrite the Host header
2780827808
type: string
27809+
pathRegex:
27810+
description: |-
27811+
PathRegex defines a regex match and substitution applied to the request path to compute
27812+
the rewritten Host header.
27813+
For example:
27814+
pathRegex:
27815+
pattern: "^/tenant/([a-z0-9-]+)/.*"
27816+
substitution: "\\1.example.internal"
27817+
properties:
27818+
pattern:
27819+
description: |-
27820+
Pattern matches a regular expression against the value of the HTTP Path. The regex string must
27821+
adhere to the syntax documented in https://github.com/google/re2/wiki/Syntax.
27822+
minLength: 1
27823+
type: string
27824+
substitution:
27825+
description: |-
27826+
Substitution is an expression that replaces the matched portion. The expression may include numbered
27827+
capture groups that adhere to syntax documented in https://github.com/google/re2/wiki/Syntax.
27828+
27829+
The resulting value is used as the upstream Host header and should be constrained to a valid
27830+
DNS hostname by using explicit regex capture groups in Pattern.
27831+
minLength: 1
27832+
type: string
27833+
required:
27834+
- pattern
27835+
- substitution
27836+
type: object
2780927837
type:
2781027838
description: HTTPPathModifierType defines the type of Hostname
2781127839
rewrite.
2781227840
enum:
2781327841
- Header
2781427842
- Backend
27843+
- PathRegex
2781527844
type: string
2781627845
required:
2781727846
- type
@@ -27821,6 +27850,10 @@ spec:
2782127850
rule: '!(has(self.header) && self.type != ''Header'')'
2782227851
- message: header must be specified for Header type
2782327852
rule: '!(!has(self.header) && self.type == ''Header'')'
27853+
- message: pathRegex must be nil if the type is not PathRegex
27854+
rule: '!(has(self.pathRegex) && self.type != ''PathRegex'')'
27855+
- message: pathRegex must be specified for PathRegex type
27856+
rule: '!(!has(self.pathRegex) && self.type == ''PathRegex'')'
2782427857
path:
2782527858
description: Path defines a path rewrite.
2782627859
properties:

0 commit comments

Comments
 (0)