forked from kubernetes-sigs/gateway-api-inference-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
119 lines (105 loc) · 2.75 KB
/
plugin.go
File metadata and controls
119 lines (105 loc) · 2.75 KB
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
package dynamo_inject_workerid
import (
"context"
"encoding/json"
"strconv"
"strings"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
rc "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/requestcontrol"
schedtypes "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
)
const (
typeString = "dynamo-inject-workerid"
pluginName = "dynamo-inject-workerid"
WorkerIDHeader = "x-worker-instance-id"
tokenDataAnnotationKey = "dynamo/token-data"
)
var _ plugins.Plugin = (*InjectWorkerIDPreRequest)(nil)
var _ rc.PreRequest = (*InjectWorkerIDPreRequest)(nil)
var _ rc.RequestBodyMutator = (*InjectWorkerIDPreRequest)(nil)
type InjectWorkerIDPreRequest struct {
typedName plugins.TypedName
}
func NewInjectWorkerIDPreRequest() *InjectWorkerIDPreRequest {
return &InjectWorkerIDPreRequest{
typedName: plugins.TypedName{Type: typeString, Name: pluginName},
}
}
func (p *InjectWorkerIDPreRequest) WithName(name string) *InjectWorkerIDPreRequest {
p.typedName.Name = name
return p
}
func InjectWorkerIDPreRequestFactory(name string, _ json.RawMessage, _ plugins.Handle) (plugins.Plugin, error) {
return NewInjectWorkerIDPreRequest().WithName(name), nil
}
func (p *InjectWorkerIDPreRequest) TypedName() plugins.TypedName { return p.typedName }
func (p *InjectWorkerIDPreRequest) PreRequest(
_ context.Context,
req *schedtypes.LLMRequest,
_ *schedtypes.SchedulingResult,
_ int,
) {
if req == nil {
return
}
if req.Headers == nil {
req.Headers = map[string]string{}
}
wid := strings.TrimSpace(req.Headers[WorkerIDHeader])
if wid == "" {
return
}
req.Headers[WorkerIDHeader] = wid
}
func (p *InjectWorkerIDPreRequest) MutateRequestBody(
_ context.Context,
req *schedtypes.LLMRequest,
_ *schedtypes.SchedulingResult,
_ int,
body map[string]any,
) {
if req == nil || body == nil {
return
}
if req.Headers == nil {
return
}
wid := strings.TrimSpace(req.Headers[WorkerIDHeader])
if wid == "" {
return
}
nvext, _ := body["nvext"].(map[string]any)
if nvext == nil {
nvext = map[string]any{}
body["nvext"] = nvext
}
if widUint, err := strconv.ParseUint(wid, 10, 64); err == nil {
nvext["backend_instance_id"] = widUint
}
if tokens, ok := req.Annotations[tokenDataAnnotationKey]; ok {
switch v := tokens.(type) {
case []int64:
if len(v) > 0 {
nvext["token_data"] = v
}
case []any:
var out []int64
for _, elem := range v {
switch t := elem.(type) {
case int64:
out = append(out, t)
case float64:
out = append(out, int64(t))
}
}
if len(out) > 0 {
nvext["token_data"] = out
}
case json.RawMessage:
var out []int64
if err := json.Unmarshal(v, &out); err == nil && len(out) > 0 {
nvext["token_data"] = out
}
}
}
}