-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassigner.go
More file actions
367 lines (319 loc) · 10.8 KB
/
Copy pathassigner.go
File metadata and controls
367 lines (319 loc) · 10.8 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/utils/pointer"
"knative.dev/client/pkg/kn/commands"
servinglib "knative.dev/client/pkg/serving"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
)
type Assigner struct{}
// create the service and forward the request
func (a *Assigner) AssignService(spec ServiceSpec, group RequestGroup) {
log.Println("Assigning service based on the ServiceSpec")
p := commands.KnParams{}
p.Initialize()
// Process each request in group to json payloads , store in array
var packedRequests []io.ReadCloser
for _, req := range group.Requests {
packedRequest := a.CreatePayload(req)
packedRequests = append(packedRequests, packedRequest)
log.Printf("Packed new requests for service : %s", spec.Name)
}
// Initialize the Knative serving client
client, err := p.NewServingClient(namespace)
if err != nil {
log.Fatalf("Error creating Knative serving client: %s", err.Error())
return
}
// List all services
serviceList, err := client.ListServices(context.Background())
if err != nil {
log.Fatalf("Error listing Knative services: %s", err.Error())
return
}
// Check if the specified service name from spec exists
serviceExists := false
for _, svc := range serviceList.Items {
log.Printf("Found service: %s", svc.Name)
if svc.Name == spec.Name {
serviceExists = true
break
}
}
// There is a service running , just forward the payload
if serviceExists {
log.Printf("Service %s exists, updating the service", spec.Name)
a.CurrentService(spec, packedRequests)
} else {
// There isn't service running ,create a service and forward payload
log.Printf("Service %s does not exist, creating a new service", spec.Name)
a.CreateNewService(spec, packedRequests)
}
}
// CreatePayload creates a single json payload for a request
func (a *Assigner) CreatePayload(req Request) io.ReadCloser {
log.Println("Creating payload for request")
var payload map[string]interface{}
if req.Payload != nil {
payload = req.Payload
} else {
payload = map[string]interface{}{
"inputs": req.Token,
"parameters": req.Par,
}
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
log.Fatalf("Error packing request: %s", err.Error())
return nil
}
log.Printf("Request Payload: %s", string(jsonPayload))
return ioutil.NopCloser(bytes.NewBuffer(jsonPayload))
}
// CreateNewService creates a new service and forwards the request
func (a *Assigner) CreateNewService(spec ServiceSpec, requestPayloads []io.ReadCloser) {
log.Printf("Creating new Knative service - Name: %s", spec.Name)
p := commands.KnParams{}
p.Initialize()
// Create new knative serving client
client, err := p.NewServingClient(namespace)
if err != nil {
log.Fatalf("Error creating Knative serving client: %s", err.Error())
return
}
//Create a service instance
var svcInstance = &servingv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: spec.Name,
Namespace: namespace,
},
}
// Define resource requirements based on the spec
resourceRequirements := v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse(fmt.Sprintf("%dm", spec.CPU)), // Convert to millicores
v1.ResourceMemory: resource.MustParse(fmt.Sprintf("%dMi", spec.Memory)), // Memory in MiB
},
Limits: v1.ResourceList{
v1.ResourceCPU: resource.MustParse(fmt.Sprintf("%dm", spec.CPU)),
v1.ResourceMemory: resource.MustParse(fmt.Sprintf("%dMi", spec.Memory)),
},
}
// Add GPU resource request if specified on spec
if len(spec.GPU_slices) > 0 {
for sliceType, quantity := range spec.GPU_slices {
resourceRequirements.Requests[v1.ResourceName(sliceType)] = resource.MustParse(fmt.Sprintf("%d", quantity))
resourceRequirements.Limits[v1.ResourceName(sliceType)] = resource.MustParse(fmt.Sprintf("%d", quantity))
}
}
// Convert spec.Env from a map[string]string to []v1.EnvVar
var envVars []v1.EnvVar
for key, value := range spec.Env {
envVars = append(envVars, v1.EnvVar{
Name: key,
Value: value,
})
}
for gpuType := range spec.GPU_slices {
percentage, ok := mpsActiveThreadPercentageMap[gpuType]
if !ok {
log.Printf("No MPS active thread percentage configured for GPU type %s, using default 100%%", gpuType)
percentage = "100"
}
envVars = append(envVars, v1.EnvVar{
Name: "CUDA_MPS_ACTIVE_THREAD_PERCENTAGE", // for setting mps compute resource (setting active gpu thread percentage on one gpu)
Value: percentage,
})
log.Printf("Setting CUDA_MPS_ACTIVE_THREAD_PERCENTAGE to %s", percentage)
break // Assuming we only need to set this for one GPU type
}
// Add all the resource requirements define to service instance
svcInstance.Spec.Template = servingv1.RevisionTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
servinglib.UserImageAnnotationKey: "",
"autoscaling.knative.dev/max-scale": "1", // disable knative autoscaling
"autoscaling.knative.dev/min-scale": "1", // disable knative autoscaling
},
Labels: spec.Label,
},
Spec: servingv1.RevisionSpec{
PodSpec: v1.PodSpec{
Containers: []v1.Container{{
Image: image,
ImagePullPolicy: v1.PullIfNotPresent,
Resources: resourceRequirements,
// VolumeMounts: []v1.VolumeMount{{
// Name: "disk-volume",
// MountPath: "/data",
// }},
Env: envVars,
SecurityContext: &v1.SecurityContext{ // run as user 1000 for mps server connection
RunAsUser: pointer.Int64(1000),
},
}},
RuntimeClassName: &spec.RuntimeClassName,
// Volumes: []v1.Volume{{
// Name: "disk-volume",
// VolumeSource: v1.VolumeSource{
// PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
// ClaimName: "knative-pv-claim", // Use the PVC created in your cluster
// ReadOnly: false, // Set to false if you need write access
// },
// },
// }},
},
},
}
// Use the service instance to create service
ctx := context.Background()
err = client.CreateService(ctx, svcInstance)
if err != nil {
log.Fatalf("Error creating Knative service: %s", err.Error())
}
// wait for service be ready and forward payload
go a.waitForServiceReadyAndForward(spec, requestPayloads)
}
// forwards the requests to existing service
func (a *Assigner) CurrentService(spec ServiceSpec, requestPayloads []io.ReadCloser) {
// Forward each request one by one
for _, requestPayload := range requestPayloads {
go a.forwardRequest(spec, requestPayload)
}
}
// wait for service be ready and forward payload
func (a *Assigner) waitForServiceReadyAndForward(spec ServiceSpec, requestPayloads []io.ReadCloser) {
log.Printf("Waiting for service to be ready - Name: %s", spec.Name)
timeCounter := 0
p := commands.KnParams{}
p.Initialize()
knClient, err := p.NewServingClient(namespace)
if err != nil {
log.Fatalf("Error creating Knative serving client: %s", err.Error())
return
}
ctx := context.Background()
for {
service, err := knClient.GetService(ctx, spec.Name)
if err != nil {
log.Fatalf("Error getting Knative service: %s, service may not exist", err.Error())
return
}
// if timeCounter >= 60 { // wait for 60 seconds
// log.Printf("Service %s is not ready after 300 seconds, request forwarding failed", spec.Name)
// return
// }
// wait for service to be ready
for _, condition := range service.Status.Conditions {
if condition.Type == "Ready" && condition.Status == "True" {
log.Printf("\nKnative Service is ready - Name: %s", spec.Name)
// Forward each request payload of the request group one by one
for _, requestPayload := range requestPayloads {
a.forwardRequest(spec, requestPayload)
}
return
}
}
log.Printf("Waiting service %s to be ready ... (%d seconds waited)", spec.Name, timeCounter)
timeCounter += 1
time.Sleep(1 * time.Second)
}
}
// forward a request to the service using kourier-internal with Host header and print request info
func (a *Assigner) forwardRequest(spec ServiceSpec, requestPayload io.ReadCloser) {
log.Printf("Forwarding request to service: %s", spec.Name)
payload, err := ioutil.ReadAll(requestPayload)
if err != nil {
log.Printf("Error reading request payload: %s", err.Error())
return
}
// Initialize Kubernetes client
config, err := rest.InClusterConfig()
if err != nil {
log.Fatalf("Failed to create in-cluster config: %v", err)
return
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to create Kubernetes client: %v", err)
return
}
// Get the IP address of the kourier service
kourierService, err := kubeClient.CoreV1().Services("kourier-system").Get(context.TODO(), "kourier", metav1.GetOptions{})
if err != nil {
log.Fatalf("Failed to get kourier service: %v", err)
return
}
kourierIP := kourierService.Status.LoadBalancer.Ingress[0].IP
log.Printf("Kourier IP: %s", kourierIP)
// Use Knative client to get the service URL
p := commands.KnParams{}
p.Initialize()
knClient, err := p.NewServingClient(namespace)
if err != nil {
log.Fatalf("Error creating Knative serving client: %s", err.Error())
return
}
service, err := knClient.GetService(context.Background(), spec.Name)
if err != nil {
log.Fatalf("Error getting Knative service: %s", err.Error())
return
}
serviceURL := service.Status.URL.String()
log.Printf("Service URL: %s", serviceURL)
// Remove "https://" prefix if it exists in the service URL
if strings.HasPrefix(serviceURL, "http://") {
serviceURL = strings.TrimPrefix(serviceURL, "http://")
}
host := fmt.Sprintf(serviceURL)
url := "http://" + kourierIP
if spec.Subroute != "" {
url += spec.Subroute
log.Printf("Using custom URL route: %s", url)
}
// Create the HTTP POST request
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
log.Printf("Failed to create request: %v\n", err)
return
}
// Set headers
req.Host = host
req.Header.Set("Content-Type", "application/json")
// Print out the information (Host, URL, Headers, and Payload)
log.Printf("Request Information:")
log.Printf("URL: %s", url)
log.Printf("Host: %s", host)
log.Printf("Content-Type: %s", req.Header.Get("Content-Type"))
log.Printf("Payload: %s", string(payload))
// Send the request
resp, err := client.Do(req)
if err != nil {
log.Printf("Failed to forward request: %v\n", err)
return
}
defer resp.Body.Close()
// Process the response
respPayload, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Failed to read response: %v\n", err)
return
}
log.Printf("Response Payload from service: %s", string(respPayload))
log.Println("Response sent back to original sender")
}