Skip to content

Commit

Permalink
feat(translator): ext-proc attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Guy Daich <guy.daich@sap.com>
  • Loading branch information
guydc committed Nov 28, 2024
1 parent 98457b5 commit 5ac1412
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 6 deletions.
33 changes: 33 additions & 0 deletions examples/grpc-ext-proc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ func (s *extProcServer) Process(srv envoy_service_proc_v3.ExternalProcessor_Proc
resp := &envoy_service_proc_v3.ProcessingResponse{}
switch v := req.Request.(type) {
case *envoy_service_proc_v3.ProcessingRequest_RequestHeaders:
xdsRouteName := ""

if req.Attributes != nil {
if epa, ok := req.Attributes["envoy.filters.http.ext_proc"]; ok {
if rqa, ok := epa.Fields["xds.route_name"]; ok {
xdsRouteName = rqa.GetStringValue()
}
}
}

xrch := ""
if v.RequestHeaders != nil {
hdrs := v.RequestHeaders.Headers.GetHeaders()
Expand All @@ -230,6 +240,12 @@ func (s *extProcServer) Process(srv envoy_service_proc_v3.ExternalProcessor_Proc
RawValue: []byte("true"),
},
},
{
Header: &envoy_api_v3_core.HeaderValue{
Key: "x-request-xds-route-name",
RawValue: []byte(xdsRouteName),
},
},
},
},
},
Expand Down Expand Up @@ -257,8 +273,19 @@ func (s *extProcServer) Process(srv envoy_service_proc_v3.ExternalProcessor_Proc
RequestHeaders: rhq,
},
}

break
case *envoy_service_proc_v3.ProcessingRequest_ResponseHeaders:

respXDSRouteName := ""

if req.Attributes != nil {
if epa, ok := req.Attributes["envoy.filters.http.ext_proc"]; ok {
if rsa, ok := epa.Fields["xds.route_name"]; ok {
respXDSRouteName = rsa.GetStringValue()
}
}
}
rhq := &envoy_service_proc_v3.HeadersResponse{
Response: &envoy_service_proc_v3.CommonResponse{
HeaderMutation: &envoy_service_proc_v3.HeaderMutation{
Expand All @@ -269,6 +296,12 @@ func (s *extProcServer) Process(srv envoy_service_proc_v3.ExternalProcessor_Proc
RawValue: []byte("true"),
},
},
{
Header: &envoy_api_v3_core.HeaderValue{
Key: "x-response-xds-route-name",
RawValue: []byte(respXDSRouteName),
},
},
},
},
},
Expand Down
8 changes: 8 additions & 0 deletions internal/gatewayapi/envoyextensionpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,21 @@ func (t *Translator) buildExtProc(
if extProc.ProcessingMode.Request.Body != nil {
extProcIR.RequestBodyProcessingMode = ptr.To(ir.ExtProcBodyProcessingMode(*extProc.ProcessingMode.Request.Body))
}

if extProc.ProcessingMode.Request.Attributes != nil {
extProcIR.RequestAttributes = append(extProcIR.RequestAttributes, extProc.ProcessingMode.Request.Attributes...)
}
}

if extProc.ProcessingMode.Response != nil {
extProcIR.ResponseHeaderProcessing = true
if extProc.ProcessingMode.Response.Body != nil {
extProcIR.ResponseBodyProcessingMode = ptr.To(ir.ExtProcBodyProcessingMode(*extProc.ProcessingMode.Response.Body))
}

if extProc.ProcessingMode.Response.Attributes != nil {
extProcIR.ResponseAttributes = append(extProcIR.ResponseAttributes, extProc.ProcessingMode.Response.Attributes...)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,13 @@ envoyExtensionPolicies:
processingMode:
request:
body: Buffered
attributes:
- request.path
response:
body: Streamed
attributes:
- xds.route_metadata
- connection.requested_server_name
messageTimeout: 5s
failOpen: true
- apiVersion: gateway.envoyproxy.io/v1alpha1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ envoyExtensionPolicies:
messageTimeout: 5s
processingMode:
request:
attributes:
- request.path
body: Buffered
response:
attributes:
- xds.route_metadata
- connection.requested_server_name
body: Streamed
targetRef:
group: gateway.networking.k8s.io
Expand Down Expand Up @@ -363,8 +368,13 @@ xdsIR:
failOpen: true
messageTimeout: 5s
name: envoyextensionpolicy/default/policy-for-gateway/extproc/0
requestAttributes:
- request.path
requestBodyProcessingMode: Buffered
requestHeaderProcessing: true
responseAttributes:
- xds.route_metadata
- connection.requested_server_name
responseBodyProcessingMode: Streamed
responseHeaderProcessing: true
hostname: www.bar.com
Expand Down
8 changes: 8 additions & 0 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2602,6 +2602,14 @@ type ExtProc struct {

// ResponseBodyProcessingMode Defines response body processing
ResponseBodyProcessingMode *ExtProcBodyProcessingMode `json:"responseBodyProcessingMode,omitempty" yaml:"responseBodyProcessingMode,omitempty"`

// RequestAttributes defines which envoy attributes are provided as context to external processor
// when processing requests
RequestAttributes []string `json:"requestAttributes,omitempty" yaml:"requestAttributes,omitempty"`

// ResponseAttributes defines which envoy attributes are provided as context to external processor
// when processing responses
ResponseAttributes []string `json:"responseAttributes,omitempty" yaml:"responseAttributes,omitempty"`
}

// Wasm holds the information associated with the Wasm extensions.
Expand Down
10 changes: 10 additions & 0 deletions internal/ir/zz_generated.deepcopy.go

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

12 changes: 12 additions & 0 deletions internal/xds/translator/extproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ func extProcConfig(extProc ir.ExtProc) *extprocv3.ExternalProcessor {
config.ProcessingMode.ResponseHeaderMode = extprocv3.ProcessingMode_SEND
}

if extProc.RequestAttributes != nil {
var attrs []string
attrs = append(attrs, extProc.RequestAttributes...)
config.RequestAttributes = attrs
}

if extProc.ResponseAttributes != nil {
var attrs []string
attrs = append(attrs, extProc.ResponseAttributes...)
config.ResponseAttributes = attrs
}

return config
}

Expand Down
10 changes: 10 additions & 0 deletions internal/xds/translator/testdata/in/xds-ir/ext-proc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ http:
- name: envoyextensionpolicy/default/policy-for-route-2/extproc/0
failOpen: true
messageTimeout: 5s
requestAttributes:
- xds.route_metadata
- connection.requested_server_name
requestHeaderProcessing: true
requestBodyProcessingMode: Buffered
responseAttributes:
- request.path
responseBodyProcessingMode: Streamed
authority: grpc-backend-4.default:4000
destination:
Expand Down Expand Up @@ -71,6 +76,11 @@ http:
- name: envoyextensionpolicy/envoy-gateway/policy-for-gateway-1/extproc/0
failOpen: false
messageTimeout: 15s
requestAttributes:
- xds.route_metadata
- connection.requested_server_name
responseAttributes:
- request.path
authority: grpc-backend.envoy-gateway:9000
destination:
name: envoyextensionpolicy/envoy-gateway/policy-for-gateway-1/0/grpc-backend
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
responseBodyMode: STREAMED
responseHeaderMode: SKIP
responseTrailerMode: SKIP
requestAttributes:
- xds.route_metadata
- connection.requested_server_name
responseAttributes:
- request.path
- disabled: true
name: envoy.filters.http.ext_proc/envoyextensionpolicy/default/policy-for-route-1/extproc/0
typedConfig:
Expand Down Expand Up @@ -79,6 +84,11 @@
requestTrailerMode: SKIP
responseHeaderMode: SKIP
responseTrailerMode: SKIP
requestAttributes:
- xds.route_metadata
- connection.requested_server_name
responseAttributes:
- request.path
- name: envoy.filters.http.router
typedConfig:
'@type': type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
Expand Down
8 changes: 6 additions & 2 deletions test/e2e/testdata/ext-proc-envoyextensionpolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ spec:
namespace: gateway-conformance-infra
port: 9002
processingMode:
request: {}
response: {}
request:
attributes:
- xds.route_name
response:
attributes:
- xds.route_name
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: EnvoyExtensionPolicy
Expand Down
16 changes: 12 additions & 4 deletions test/e2e/tests/ext_proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,24 @@ var ExtProcTest = suite.ConformanceTest{
Request: http.Request{
Path: "/processor",
Headers: map[string]string{
"x-request-ext-processed": "true", // header added by ext-processor to backend-bound request
"x-request-client-header-received": "original", // this is the original client header preserved by ext-proc in a new header
"x-request-client-header": "mutated", // this is the mutated value expected to reach upstream
// header added by ext-processor to backend-bound request
"x-request-ext-processed": "true",
// this is the original client header preserved by ext-proc in a new header
"x-request-client-header-received": "original",
// this is the mutated value expected to reach upstream
"x-request-client-header": "mutated",
// header added by ext-processor to request based on the xds.route_name attribute
"x-request-xds-route-name": "httproute/gateway-conformance-infra/http-with-ext-proc/rule/0/match/0/www_example_com",
},
},
},
Response: http.Response{
StatusCode: 200,
Headers: map[string]string{
"x-response-ext-processed": "true", // header added by ext-processor to client-bound response
// header added by ext-processor to client-bound response
"x-response-ext-processed": "true",
// header added by ext-processor to response based on the xds.cluster_name attribute
"x-response-xds-route-name": "httproute/gateway-conformance-infra/http-with-ext-proc/rule/0/match/0/www_example_com",
},
},
Namespace: ns,
Expand Down

0 comments on commit 5ac1412

Please sign in to comment.