Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,37 @@ type ResponseOverride struct {
}

// CustomResponseMatch defines the configuration for matching a user response to return a custom one.
// At least one of statusCodes or requestHeaders must be specified.
// When multiple criteria are specified, all of them must match (logical AND) for the rule to apply.
// +kubebuilder:validation:XValidation:rule="has(self.statusCodes) || has(self.requestHeaders)",message="at least one of statusCodes or requestHeaders must be specified"
type CustomResponseMatch struct {
// Status code to match on. The match evaluates to true if any of the matches are successful.
//
// +optional
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=50
StatusCodes []StatusCodeMatch `json:"statusCodes"`
StatusCodes []StatusCodeMatch `json:"statusCodes,omitempty"`

// RequestHeaders to match on. All of the specified headers must match (logical AND)
// for the rule to apply. Matching is performed against request headers observed on
// the response path of the stream.
//
// +optional
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=16
RequestHeaders []ResponseOverrideHeaderMatch `json:"requestHeaders,omitempty"`
}

// ResponseOverrideHeaderMatch defines the configuration for matching an HTTP header.
type ResponseOverrideHeaderMatch struct {
// Name of the HTTP header to match against. The header name is case-insensitive.
//
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=256
Name string `json:"name"`

// Value specifies how to match against the value of the header.
Value StringMatch `json:"value"`
}

// StatusCodeValueType defines the types of values for the status code match supported by Envoy Gateway.
Expand Down
23 changes: 23 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,51 @@ spec:
match:
description: Match configuration.
properties:
requestHeaders:
description: |-
RequestHeaders to match on. All of the specified headers must match (logical AND)
for the rule to apply. Matching is performed against request headers observed on
the response path of the stream.
items:
description: ResponseOverrideHeaderMatch defines the configuration
for matching an HTTP header.
properties:
name:
description: Name of the HTTP header to match against.
The header name is case-insensitive.
maxLength: 256
minLength: 1
type: string
value:
description: Value specifies how to match against
the value of the header.
properties:
type:
default: Exact
description: Type specifies how to match against
a string.
enum:
- Exact
- Prefix
- Suffix
- RegularExpression
type: string
value:
description: Value specifies the string value
that the match must have.
maxLength: 1024
minLength: 1
type: string
required:
- value
type: object
required:
- name
- value
type: object
maxItems: 16
minItems: 1
type: array
statusCodes:
description: Status code to match on. The match evaluates
to true if any of the matches are successful.
Expand Down Expand Up @@ -2193,9 +2238,11 @@ spec:
maxItems: 50
minItems: 1
type: array
required:
- statusCodes
type: object
x-kubernetes-validations:
- message: at least one of statusCodes or requestHeaders must
be specified
rule: has(self.statusCodes) || has(self.requestHeaders)
redirect:
description: Redirect configuration
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,51 @@ spec:
match:
description: Match configuration.
properties:
requestHeaders:
description: |-
RequestHeaders to match on. All of the specified headers must match (logical AND)
for the rule to apply. Matching is performed against request headers observed on
the response path of the stream.
items:
description: ResponseOverrideHeaderMatch defines the configuration
for matching an HTTP header.
properties:
name:
description: Name of the HTTP header to match against.
The header name is case-insensitive.
maxLength: 256
minLength: 1
type: string
value:
description: Value specifies how to match against
the value of the header.
properties:
type:
default: Exact
description: Type specifies how to match against
a string.
enum:
- Exact
- Prefix
- Suffix
- RegularExpression
type: string
value:
description: Value specifies the string value
that the match must have.
maxLength: 1024
minLength: 1
type: string
required:
- value
type: object
required:
- name
- value
type: object
maxItems: 16
minItems: 1
type: array
statusCodes:
description: Status code to match on. The match evaluates
to true if any of the matches are successful.
Expand Down Expand Up @@ -2192,9 +2237,11 @@ spec:
maxItems: 50
minItems: 1
type: array
required:
- statusCodes
type: object
x-kubernetes-validations:
- message: at least one of statusCodes or requestHeaders must
be specified
rule: has(self.statusCodes) || has(self.requestHeaders)
redirect:
description: Redirect configuration
properties:
Expand Down
8 changes: 8 additions & 0 deletions internal/gatewayapi/backendtrafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,14 @@ func (t *Translator) buildResponseOverride(policy *egv1a1.BackendTrafficPolicy)
}
}

for _, h := range ro.Match.RequestHeaders {
sm := irStringMatch(h.Name, h.Value)
match.RequestHeaders = append(match.RequestHeaders, ir.ResponseOverrideHeaderMatch{
Name: h.Name,
Value: *sm,
})
}

if ro.Redirect != nil {
redirect := &ir.Redirect{
Scheme: ro.Redirect.Scheme,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
gateways:
- apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
namespace: default
name: gateway-1
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
httpRoutes:
- apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: default
name: httproute-1
spec:
hostnames:
- foo.envoyproxy.io
parentRefs:
- namespace: default
name: gateway-1
sectionName: http
rules:
- matches:
- path:
value: "/"
backendRefs:
- name: service-1
port: 8080
backendTrafficPolicies:
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: default
name: policy-for-route-1
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: httproute-1
responseOverride:
- match:
statusCodes:
- value: 404
requestHeaders:
- name: Accept
value:
type: Exact
value: application/json
response:
contentType: application/json
body:
type: Inline
inline: |
{"error":"not found"}
- match:
statusCodes:
- value: 404
response:
contentType: text/html
body:
type: Inline
inline: |
<html><body><h1>Not Found</h1></body></html>
Loading
Loading