forked from kubernetes-sigs/gateway-api-inference-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
209 lines (188 loc) · 7.36 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package handlers
import (
"context"
"encoding/json"
"fmt"
"strconv"
configPb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
extProcPb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3"
"google.golang.org/protobuf/types/known/structpb"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling"
errutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/error"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
)
// HandleRequestBody handles body of the request to the backend server, such as parsing the "model"
// parameter.
// Envoy sends the request body to ext proc before sending the request to the backend server.
func (s *Server) HandleRequestBody(
ctx context.Context,
reqCtx *RequestContext,
req *extProcPb.ProcessingRequest,
) (*extProcPb.ProcessingResponse, error) {
logger := log.FromContext(ctx)
loggerVerbose := logger.V(logutil.VERBOSE)
loggerVerbose.Info("Handling request body")
// Unmarshal request body (must be JSON).
v := req.Request.(*extProcPb.ProcessingRequest_RequestBody)
var rb map[string]interface{}
if err := json.Unmarshal(v.RequestBody.Body, &rb); err != nil {
logger.V(logutil.DEFAULT).Error(err, "Error unmarshaling request body")
return nil, errutil.Error{Code: errutil.BadRequest, Msg: fmt.Sprintf("error unmarshaling request body: %v", err)}
}
loggerVerbose.Info("Request body unmarshalled", "body", rb)
// Resolve target models.
model, ok := rb["model"].(string)
if !ok {
return nil, errutil.Error{Code: errutil.BadRequest, Msg: "model not found in request"}
}
loggerVerbose.Info("Model requested", "model", model)
modelName := model
// NOTE: The nil checking for the modelObject means that we DO allow passthrough currently.
// This might be a security risk in the future where adapters not registered in the InferenceModel
// are able to be requested by using their distinct name.
modelObj, exist := s.datastore.ModelGet(model)
if !exist {
return nil, errutil.Error{Code: errutil.BadConfiguration, Msg: fmt.Sprintf("error finding a model object in InferenceModel for input %v", model)}
}
if len(modelObj.Spec.TargetModels) > 0 {
modelName = datastore.RandomWeightedDraw(logger, modelObj, 0)
if modelName == "" {
return nil, errutil.Error{Code: errutil.BadConfiguration, Msg: fmt.Sprintf("error getting target model name for model %v", modelObj.Name)}
}
}
llmReq := &scheduling.LLMRequest{
Model: model,
ResolvedTargetModel: modelName,
Critical: datastore.IsCritical(modelObj),
}
loggerVerbose.Info("LLM request assembled", "request", llmReq)
requestBody := v.RequestBody.Body
var err error
// Update target models in the body.
if llmReq.Model != llmReq.ResolvedTargetModel {
rb["model"] = llmReq.ResolvedTargetModel
requestBody, err = json.Marshal(rb)
if err != nil {
logger.V(logutil.DEFAULT).Error(err, "Error marshaling request body")
return nil, errutil.Error{Code: errutil.Internal, Msg: fmt.Sprintf("error marshaling request body: %v", err)}
}
loggerVerbose.Info("Updated request body marshalled", "body", string(requestBody))
}
targetPod, err := s.scheduler.Schedule(ctx, llmReq)
if err != nil {
return nil, errutil.Error{Code: errutil.InferencePoolResourceExhausted, Msg: fmt.Errorf("failed to find target pod: %w", err).Error()}
}
logger.V(logutil.DEFAULT).Info("Request handled",
"model", llmReq.Model, "targetModel", llmReq.ResolvedTargetModel, "endpoint", targetPod)
// Insert target endpoint to instruct Envoy to route requests to the specified target pod.
// Attach the port number
pool, err := s.datastore.PoolGet()
if err != nil {
return nil, err
}
endpoint := targetPod.Address + ":" + strconv.Itoa(int(pool.Spec.TargetPortNumber))
reqCtx.Model = llmReq.Model
reqCtx.ResolvedTargetModel = llmReq.ResolvedTargetModel
reqCtx.RequestSize = len(v.RequestBody.Body)
reqCtx.TargetPod = targetPod.NamespacedName.String()
reqCtx.TargetEndpoint = endpoint
headers := []*configPb.HeaderValueOption{
{
Header: &configPb.HeaderValue{
Key: s.destinationEndpointHintKey,
RawValue: []byte(endpoint),
},
},
// We need to update the content length header if the body is mutated, see Envoy doc:
// https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/filters/http/ext_proc/v3/processing_mode.proto
{
Header: &configPb.HeaderValue{
Key: "Content-Length",
RawValue: []byte(strconv.Itoa(len(requestBody))),
},
},
}
// Print headers for debugging
for _, header := range headers {
logger.V(logutil.DEBUG).Info("Request body header", "key", header.Header.Key, "value", header.Header.RawValue)
}
targetEndpointValue := &structpb.Struct{
Fields: map[string]*structpb.Value{
s.destinationEndpointHintKey: {
Kind: &structpb.Value_StringValue{
StringValue: endpoint,
},
},
},
}
dynamicMetadata := targetEndpointValue
if s.destinationEndpointHintMetadataNamespace != "" {
// If a namespace is defined, wrap the selected endpoint with that.
dynamicMetadata = &structpb.Struct{
Fields: map[string]*structpb.Value{
s.destinationEndpointHintMetadataNamespace: {
Kind: &structpb.Value_StructValue{
StructValue: targetEndpointValue,
},
},
},
}
}
resp := &extProcPb.ProcessingResponse{
// The Endpoint Picker supports two approaches to communicating the target endpoint, as a request header
// and as an unstructure ext-proc response metadata key/value pair. This enables different integration
// options for gateway providers.
Response: &extProcPb.ProcessingResponse_RequestBody{
RequestBody: &extProcPb.BodyResponse{
Response: &extProcPb.CommonResponse{
HeaderMutation: &extProcPb.HeaderMutation{
SetHeaders: headers,
},
BodyMutation: &extProcPb.BodyMutation{
Mutation: &extProcPb.BodyMutation_Body{
Body: requestBody,
},
},
},
},
},
DynamicMetadata: dynamicMetadata,
}
return resp, nil
}
func HandleRequestHeaders(
ctx context.Context,
reqCtx *RequestContext,
req *extProcPb.ProcessingRequest,
) *extProcPb.ProcessingResponse {
r := req.Request
h := r.(*extProcPb.ProcessingRequest_RequestHeaders)
log.FromContext(ctx).V(logutil.VERBOSE).Info("Handling request headers", "headers", h)
resp := &extProcPb.ProcessingResponse{
Response: &extProcPb.ProcessingResponse_RequestHeaders{
RequestHeaders: &extProcPb.HeadersResponse{
Response: &extProcPb.CommonResponse{
// Set `clear_route_cache = true` to force Envoy to recompute the target cluster
// based on the new "target-pod" header.
// See https://www.envoyproxy.io/docs/envoy/latest/api-v3/service/ext_proc/v3/external_processor.proto#service-ext-proc-v3-commonresponse.
ClearRouteCache: true,
},
},
},
}
return resp
}