Skip to content

Commit

Permalink
feat(MeshHTTPRoute): add hostToBackendHostname rewrite with MeshGatew…
Browse files Browse the repository at this point in the history
…ay (#8772)

Signed-off-by: Mike Beaumont <[email protected]>
  • Loading branch information
michaelbeaumont authored Jan 9, 2024
1 parent 4d008ff commit 7397880
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,12 @@ spec:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: HostToBackendHostname rewrites
the hostname to the hostname of the upstream
host. This option is only available when
targeting MeshGateways.
type: boolean
hostname:
description: Hostname is the value to be
used to replace the host header value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,12 @@ spec:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: HostToBackendHostname rewrites
the hostname to the hostname of the upstream
host. This option is only available when
targeting MeshGateways.
type: boolean
hostname:
description: Hostname is the value to be
used to replace the host header value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,12 @@ spec:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: HostToBackendHostname rewrites
the hostname to the hostname of the upstream
host. This option is only available when
targeting MeshGateways.
type: boolean
hostname:
description: Hostname is the value to be
used to replace the host header value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,12 @@ spec:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: HostToBackendHostname rewrites
the hostname to the hostname of the upstream
host. This option is only available when
targeting MeshGateways.
type: boolean
hostname:
description: Hostname is the value to be
used to replace the host header value
Expand Down
6 changes: 6 additions & 0 deletions app/kumactl/cmd/install/testdata/install-crds.all.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2956,6 +2956,12 @@ spec:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: HostToBackendHostname rewrites
the hostname to the hostname of the upstream
host. This option is only available when
targeting MeshGateways.
type: boolean
hostname:
description: Hostname is the value to be
used to replace the host header value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3160,6 +3160,12 @@ spec:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: HostToBackendHostname rewrites
the hostname to the hostname of the upstream
host. This option is only available when
targeting MeshGateways.
type: boolean
hostname:
description: Hostname is the value to be
used to replace the host header value
Expand Down
6 changes: 6 additions & 0 deletions deployments/charts/kuma/crds/kuma.io_meshhttproutes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ spec:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: HostToBackendHostname rewrites
the hostname to the hostname of the upstream
host. This option is only available when
targeting MeshGateways.
type: boolean
hostname:
description: Hostname is the value to be
used to replace the host header value
Expand Down
7 changes: 7 additions & 0 deletions docs/generated/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3985,6 +3985,13 @@ components:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: >-
HostToBackendHostname rewrites the
hostname to the hostname of the upstream
host. This option is only available when
targeting MeshGateways.
type: boolean
hostname:
description: >-
Hostname is the value to be used to
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/raw/crds/kuma.io_meshhttproutes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ spec:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: HostToBackendHostname rewrites
the hostname to the hostname of the upstream
host. This option is only available when
targeting MeshGateways.
type: boolean
hostname:
description: Hostname is the value to be
used to replace the host header value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ type URLRewrite struct {
Hostname *PreciseHostname `json:"hostname,omitempty"`
// Path defines a path rewrite.
Path *PathRewrite `json:"path,omitempty"`
// HostToBackendHostname rewrites the hostname to the hostname of the
// upstream host. This option is only available when targeting MeshGateways.
HostToBackendHostname bool `json:"hostToBackendHostname,omitempty"`
}

type RequestMirror struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/plugins/policies/meshhttproute/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ properties:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: HostToBackendHostname rewrites the hostname to the hostname of the upstream host. This option is only available when targeting MeshGateways.
type: boolean
hostname:
description: Hostname is the value to be used to replace the host header value during forwarding.
maxLength: 253
Expand Down
13 changes: 11 additions & 2 deletions pkg/plugins/policies/meshhttproute/api/v1alpha1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func validateRules(topTargetRef common_api.TargetRef, rules []Rule) validators.V
for i, rule := range rules {
path := validators.Root().Index(i)
errs.AddErrorAt(path.Field("matches"), validateMatches(rule.Matches))
errs.AddErrorAt(path.Field("default").Field("filters"), validateFilters(rule.Default.Filters, rule.Matches))
errs.AddErrorAt(path.Field("default").Field("filters"), validateFilters(topTargetRef, rule.Default.Filters, rule.Matches))
errs.AddErrorAt(path.Field("default").Field("backendRefs"), validateBackendRefs(topTargetRef, rule.Default.BackendRefs))
}

Expand Down Expand Up @@ -185,7 +185,7 @@ func hasAnyMatchesWithoutPrefix(matches []Match) bool {
return false
}

func validateFilters(filters *[]Filter, matches []Match) validators.ValidationError {
func validateFilters(topTargetRef common_api.TargetRef, filters *[]Filter, matches []Match) validators.ValidationError {
var errs validators.ValidationError

if filters == nil {
Expand Down Expand Up @@ -231,6 +231,15 @@ func validateFilters(filters *[]Filter, matches []Match) validators.ValidationEr
errs.AddViolationAt(path.Field("urlRewrite").Field("path").Field("replacePrefixMatch"), "can only appear if all matches match a path prefix")
}
errs.AddErrorAt(path.Field("urlRewrite").Field("hostname"), validatePreciseHostname(filter.URLRewrite.Hostname))
if filter.URLRewrite.HostToBackendHostname {
if topTargetRef.Kind != common_api.MeshGateway {
errs.AddViolationAt(path.Field("urlRewrite").Field("hostToBackendHostname"), "can only be set with MeshGateway")
}

if filter.URLRewrite.Hostname != nil {
errs.AddViolationAt(path.Field("urlRewrite").Field("hostToBackendHostname"), "cannot be set together with hostname")
}
}
case RequestMirrorType:
if filter.RequestMirror == nil {
errs.AddViolationAt(path.Field("requestMirror"), validators.MustBeDefined)
Expand Down
17 changes: 14 additions & 3 deletions pkg/plugins/policies/meshhttproute/api/v1alpha1/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,14 @@ to:
version: v1

`),
ErrorCase("hostnames not allowed with services",
validators.Violation{
ErrorCases("hostnames and hostname to backend rewrite not allowed with services",
[]validators.Violation{{
Field: `spec.to[0].hostnames`,
Message: `must not be defined`,
}, `
}, {
Field: "spec.to[0].rules[0].default.filters[0].urlRewrite.hostToBackendHostname",
Message: "can only be set with MeshGateway",
}}, `
type: MeshHTTPRoute
mesh: mesh-1
name: route-1
Expand All @@ -328,6 +331,10 @@ to:
type: PathPrefix
value: /
default:
filters:
- type: URLRewrite
urlRewrite:
hostToBackendHostname: true
backendRefs:
- kind: MeshService
name: backend
Expand Down Expand Up @@ -600,6 +607,10 @@ to:
value: /
type: PathPrefix
default:
filters:
- type: URLRewrite
urlRewrite:
hostToBackendHostname: true
backendRefs:
- kind: MeshService
name: backend
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ spec:
type: string
urlRewrite:
properties:
hostToBackendHostname:
description: HostToBackendHostname rewrites
the hostname to the hostname of the upstream
host. This option is only available when
targeting MeshGateways.
type: boolean
hostname:
description: Hostname is the value to be
used to replace the host header value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func makeHttpRouteEntry(name string, rule api.Rule) route.Entry {
rewrite.ReplaceHostname = pointer.To(string(*r.Hostname))
}

if r.HostToBackendHostname {
rewrite.HostToBackendHostname = true
}

entry.Rewrite = &rewrite
}
}
Expand Down

0 comments on commit 7397880

Please sign in to comment.