Skip to content

Commit 4c7ed4a

Browse files
authored
Small refactor to capture request data for route. (#765)
1 parent 69af61e commit 4c7ed4a

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

pkg/epp/handlers/request.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ import (
3535
func (s *StreamingServer) HandleRequestBody(
3636
ctx context.Context,
3737
reqCtx *RequestContext,
38-
req *extProcPb.ProcessingRequest,
39-
requestBodyMap map[string]interface{},
4038
) (*RequestContext, error) {
4139
var requestBodyBytes []byte
4240
logger := log.FromContext(ctx)
41+
requestBodyMap := reqCtx.Request.Body
4342

4443
// Resolve target models.
4544
model, ok := requestBodyMap["model"].(string)
@@ -152,6 +151,15 @@ func (s *StreamingServer) HandleRequestHeaders(ctx context.Context, reqCtx *Requ
152151
}
153152
endpoint := pod.Address + ":" + strconv.Itoa(int(pool.Spec.TargetPortNumber))
154153
s.populateRequestHeaderResponse(reqCtx, endpoint, 0)
154+
return nil
155+
}
156+
157+
for _, header := range req.RequestHeaders.Headers.Headers {
158+
if header.RawValue != nil {
159+
reqCtx.Request.Headers[header.Key] = string(header.RawValue)
160+
} else {
161+
reqCtx.Request.Headers[header.Key] = header.Value
162+
}
155163
}
156164
return nil
157165
}

pkg/epp/handlers/server.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type RequestContext struct {
8282
ResponseComplete bool
8383
ResponseStatusCode string
8484
RequestRunning bool
85+
Request *Request
8586

8687
RequestState StreamRequestState
8788
modelServerStreaming bool
@@ -95,6 +96,10 @@ type RequestContext struct {
9596
respTrailerResp *extProcPb.ProcessingResponse
9697
}
9798

99+
type Request struct {
100+
Headers map[string]string
101+
Body map[string]interface{}
102+
}
98103
type StreamRequestState int
99104

100105
const (
@@ -118,10 +123,14 @@ func (s *StreamingServer) Process(srv extProcPb.ExternalProcessor_ProcessServer)
118123
// See https://github.com/envoyproxy/envoy/issues/17540.
119124
reqCtx := &RequestContext{
120125
RequestState: RequestReceived,
126+
Request: &Request{
127+
Headers: make(map[string]string),
128+
Body: make(map[string]interface{}),
129+
},
121130
}
122131

123132
var body []byte
124-
var requestBody, responseBody map[string]interface{}
133+
var responseBody map[string]interface{}
125134

126135
// Create error handling var as each request should only report once for
127136
// error metrics. This doesn't cover the error "Cannot receive stream request" because
@@ -167,15 +176,17 @@ func (s *StreamingServer) Process(srv extProcPb.ExternalProcessor_ProcessServer)
167176
// Message is buffered, we can read and decode.
168177
if v.RequestBody.EndOfStream {
169178
loggerTrace.Info("decoding")
170-
err = json.Unmarshal(body, &requestBody)
179+
err = json.Unmarshal(body, &reqCtx.Request.Body)
171180
if err != nil {
172181
logger.V(logutil.DEFAULT).Error(err, "Error unmarshaling request body")
182+
// TODO: short circuit and send the body back as is (this could be an envoy error), currently we drop
183+
// whatever the body request would have been and send our immediate response instead.
173184
}
174185

175186
// Body stream complete. Allocate empty slice for response to use.
176187
body = []byte{}
177188

178-
reqCtx, err = s.HandleRequestBody(ctx, reqCtx, req, requestBody)
189+
reqCtx, err = s.HandleRequestBody(ctx, reqCtx)
179190
if err != nil {
180191
logger.V(logutil.DEFAULT).Error(err, "Error handling body")
181192
} else {
@@ -256,7 +267,7 @@ func (s *StreamingServer) Process(srv extProcPb.ExternalProcessor_ProcessServer)
256267
loggerTrace.Info("stream completed")
257268
// Don't send a 500 on a response error. Just let the message passthrough and log our error for debugging purposes.
258269
// We assume the body is valid JSON, err messages are not guaranteed to be json, and so capturing and sending a 500 obfuscates the response message.
259-
// using the standard 'err' var will send an immediate error response back to the caller.
270+
// Using the standard 'err' var will send an immediate error response back to the caller.
260271
var responseErr error
261272
responseErr = json.Unmarshal(body, &responseBody)
262273
if responseErr != nil {

0 commit comments

Comments
 (0)