-
Notifications
You must be signed in to change notification settings - Fork 426
/
dashboard_httpclient.go
451 lines (374 loc) · 13.5 KB
/
dashboard_httpclient.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package utils
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"
"k8s.io/apimachinery/pkg/util/yaml"
fmtErrors "github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/json"
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
)
var (
// Multi-application URL paths
ServeDetailsPath = "/api/serve/applications/"
DeployPathV2 = "/api/serve/applications/"
// Job URL paths
JobPath = "/api/jobs/"
)
type RayDashboardClientInterface interface {
InitClient(url string)
UpdateDeployments(ctx context.Context, configJson []byte) error
// V2/multi-app Rest API
GetServeDetails(ctx context.Context) (*ServeDetails, error)
GetMultiApplicationStatus(context.Context) (map[string]*ServeApplicationStatus, error)
GetJobInfo(ctx context.Context, jobId string) (*RayJobInfo, error)
ListJobs(ctx context.Context) (*[]RayJobInfo, error)
SubmitJob(ctx context.Context, rayJob *rayv1.RayJob) (string, error)
SubmitJobReq(ctx context.Context, request *RayJobRequest, name *string) (string, error)
GetJobLog(ctx context.Context, jobName string) (*string, error)
StopJob(ctx context.Context, jobName string) error
DeleteJob(ctx context.Context, jobName string) error
}
type BaseDashboardClient struct {
client http.Client
dashboardURL string
}
func GetRayDashboardClient() RayDashboardClientInterface {
return &RayDashboardClient{}
}
type RayDashboardClient struct {
BaseDashboardClient
}
// FetchHeadServiceURL fetches the URL that consists of the FQDN for the RayCluster's head service
// and the port with the given port name (defaultPortName).
func FetchHeadServiceURL(ctx context.Context, cli client.Client, rayCluster *rayv1.RayCluster, defaultPortName string) (string, error) {
log := ctrl.LoggerFrom(ctx)
headSvc := &corev1.Service{}
headSvcName, err := GenerateHeadServiceName(RayClusterCRD, rayCluster.Spec, rayCluster.Name)
if err != nil {
log.Error(err, "Failed to generate head service name", "RayCluster name", rayCluster.Name, "RayCluster spec", rayCluster.Spec)
return "", err
}
if err = cli.Get(ctx, client.ObjectKey{Name: headSvcName, Namespace: rayCluster.Namespace}, headSvc); err != nil {
if errors.IsNotFound(err) {
log.Error(err, "Head service is not found", "head service name", headSvcName, "namespace", rayCluster.Namespace)
}
return "", err
}
log.Info("FetchHeadServiceURL", "head service name", headSvc.Name, "namespace", headSvc.Namespace)
servicePorts := headSvc.Spec.Ports
port := int32(-1)
for _, servicePort := range servicePorts {
if servicePort.Name == defaultPortName {
port = servicePort.Port
break
}
}
if port == int32(-1) {
return "", fmtErrors.Errorf("%s port is not found", defaultPortName)
}
domainName := GetClusterDomainName()
headServiceURL := fmt.Sprintf("%s.%s.svc.%s:%v",
headSvc.Name,
headSvc.Namespace,
domainName,
port)
log.Info("FetchHeadServiceURL", "head service URL", headServiceURL, "port", defaultPortName)
return headServiceURL, nil
}
func (r *RayDashboardClient) InitClient(url string) {
r.client = http.Client{
Timeout: 2 * time.Second,
}
r.dashboardURL = "http://" + url
}
// UpdateDeployments update the deployments in the Ray cluster.
func (r *RayDashboardClient) UpdateDeployments(ctx context.Context, configJson []byte) error {
var req *http.Request
var err error
if req, err = http.NewRequestWithContext(ctx, http.MethodPut, r.dashboardURL+DeployPathV2, bytes.NewBuffer(configJson)); err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := r.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("UpdateDeployments fail: %s %s", resp.Status, string(body))
}
return nil
}
func (r *RayDashboardClient) GetMultiApplicationStatus(ctx context.Context) (map[string]*ServeApplicationStatus, error) {
serveDetails, err := r.GetServeDetails(ctx)
if err != nil {
return nil, fmt.Errorf("Failed to get serve details: %v", err)
}
return r.ConvertServeDetailsToApplicationStatuses(serveDetails)
}
// GetServeDetails gets details on all live applications on the Ray cluster.
func (r *RayDashboardClient) GetServeDetails(ctx context.Context) (*ServeDetails, error) {
req, err := http.NewRequestWithContext(ctx, "GET", r.dashboardURL+ServeDetailsPath, nil)
if err != nil {
return nil, err
}
resp, err := r.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("GetServeDetails fail: %s %s", resp.Status, string(body))
}
var serveDetails ServeDetails
if err = json.Unmarshal(body, &serveDetails); err != nil {
return nil, fmt.Errorf("GetServeDetails failed. Failed to unmarshal bytes: %s", string(body))
}
return &serveDetails, nil
}
func (r *RayDashboardClient) ConvertServeDetailsToApplicationStatuses(serveDetails *ServeDetails) (map[string]*ServeApplicationStatus, error) {
detailsJson, err := json.Marshal(serveDetails.Applications)
if err != nil {
return nil, fmt.Errorf("Failed to marshal serve details: %v.", serveDetails.Applications)
}
applicationStatuses := map[string]*ServeApplicationStatus{}
if err = json.Unmarshal(detailsJson, &applicationStatuses); err != nil {
return nil, fmt.Errorf("Failed to unmarshal serve details bytes into map of application statuses: %v. Bytes: %s", err, string(detailsJson))
}
return applicationStatuses, nil
}
type RuntimeEnvType map[string]interface{}
// RayJobInfo is the response of "ray job status" api.
// Reference to https://docs.ray.io/en/latest/cluster/running-applications/job-submission/rest.html#ray-job-rest-api-spec
// Reference to https://github.com/ray-project/ray/blob/cfbf98c315cfb2710c56039a3c96477d196de049/dashboard/modules/job/pydantic_models.py#L38-L107
type RayJobInfo struct {
JobStatus rayv1.JobStatus `json:"status,omitempty"`
Entrypoint string `json:"entrypoint,omitempty"`
JobId string `json:"job_id,omitempty"`
SubmissionId string `json:"submission_id,omitempty"`
Message string `json:"message,omitempty"`
ErrorType *string `json:"error_type,omitempty"`
StartTime uint64 `json:"start_time,omitempty"`
EndTime uint64 `json:"end_time,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
RuntimeEnv RuntimeEnvType `json:"runtime_env,omitempty"`
}
// RayJobRequest is the request body to submit.
// Reference to https://docs.ray.io/en/latest/cluster/running-applications/job-submission/rest.html#ray-job-rest-api-spec
// Reference to https://github.com/ray-project/ray/blob/cfbf98c315cfb2710c56039a3c96477d196de049/dashboard/modules/job/common.py#L325-L353
type RayJobRequest struct {
Entrypoint string `json:"entrypoint"`
SubmissionId string `json:"submission_id,omitempty"`
RuntimeEnv RuntimeEnvType `json:"runtime_env,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
NumCpus float32 `json:"entrypoint_num_cpus,omitempty"`
NumGpus float32 `json:"entrypoint_num_gpus,omitempty"`
Resources map[string]float32 `json:"entrypoint_resources,omitempty"`
}
type RayJobResponse struct {
JobId string `json:"job_id"`
}
type RayJobStopResponse struct {
Stopped bool `json:"stopped"`
}
type RayJobLogsResponse struct {
Logs string `json:"logs,omitempty"`
}
// Note that RayJobInfo and error can't be nil at the same time.
// Please make sure if the Ray job with JobId can't be found. Return a BadRequest error.
func (r *RayDashboardClient) GetJobInfo(ctx context.Context, jobId string) (*RayJobInfo, error) {
req, err := http.NewRequestWithContext(ctx, "GET", r.dashboardURL+JobPath+jobId, nil)
if err != nil {
return nil, err
}
resp, err := r.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, errors.NewBadRequest("Job " + jobId + " does not exist on the cluster")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var jobInfo RayJobInfo
if err = json.Unmarshal(body, &jobInfo); err != nil {
// Maybe body is not valid json, raise an error with the body.
return nil, fmt.Errorf("GetJobInfo fail: %s", string(body))
}
return &jobInfo, nil
}
func (r *RayDashboardClient) ListJobs(ctx context.Context) (*[]RayJobInfo, error) {
req, err := http.NewRequestWithContext(ctx, "GET", r.dashboardURL+JobPath, nil)
if err != nil {
return nil, err
}
resp, err := r.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, nil
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var jobInfo []RayJobInfo
if err = json.Unmarshal(body, &jobInfo); err != nil {
// Maybe body is not valid json, raise an error with the body.
return nil, fmt.Errorf("GetJobInfo fail: %s", string(body))
}
return &jobInfo, nil
}
func (r *RayDashboardClient) SubmitJob(ctx context.Context, rayJob *rayv1.RayJob) (jobId string, err error) {
request, err := ConvertRayJobToReq(rayJob)
if err != nil {
return "", err
}
return r.SubmitJobReq(ctx, request, &rayJob.Name)
}
func (r *RayDashboardClient) SubmitJobReq(ctx context.Context, request *RayJobRequest, name *string) (jobId string, err error) {
log := ctrl.LoggerFrom(ctx)
rayJobJson, err := json.Marshal(request)
if err != nil {
return
}
if name != nil {
log.Info("Submit a ray job", "rayJob", name, "jobInfo", string(rayJobJson))
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, r.dashboardURL+JobPath, bytes.NewBuffer(rayJobJson))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := r.client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var jobResp RayJobResponse
if err = json.Unmarshal(body, &jobResp); err != nil {
// Maybe body is not valid json, raise an error with the body.
return "", fmt.Errorf("SubmitJob fail: %s", string(body))
}
return jobResp.JobId, nil
}
// Get Job Log
func (r *RayDashboardClient) GetJobLog(ctx context.Context, jobName string) (*string, error) {
log := ctrl.LoggerFrom(ctx)
log.Info("Get ray job log", "rayJob", jobName)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.dashboardURL+JobPath+jobName+"/logs", nil)
if err != nil {
return nil, err
}
resp, err := r.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
// This does the right thing, but breaks E2E test
// return nil, errors.NewBadRequest("Job " + jobId + " does not exist on the cluster")
return nil, nil
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var jobLog RayJobLogsResponse
if err = json.Unmarshal(body, &jobLog); err != nil {
// Maybe body is not valid json, raise an error with the body.
return nil, fmt.Errorf("GetJobLog fail: %s", string(body))
}
return &jobLog.Logs, nil
}
func (r *RayDashboardClient) StopJob(ctx context.Context, jobName string) (err error) {
log := ctrl.LoggerFrom(ctx)
log.Info("Stop a ray job", "rayJob", jobName)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, r.dashboardURL+JobPath+jobName+"/stop", nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := r.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var jobStopResp RayJobStopResponse
if err = json.Unmarshal(body, &jobStopResp); err != nil {
return err
}
if !jobStopResp.Stopped {
jobInfo, err := r.GetJobInfo(ctx, jobName)
if err != nil {
return err
}
// StopJob only returns an error when JobStatus is not in terminal states (STOPPED / SUCCEEDED / FAILED)
if !rayv1.IsJobTerminal(jobInfo.JobStatus) {
return fmt.Errorf("Failed to stopped job: %v", jobInfo)
}
}
return nil
}
func (r *RayDashboardClient) DeleteJob(ctx context.Context, jobName string) error {
log := ctrl.LoggerFrom(ctx)
log.Info("Delete a ray job", "rayJob", jobName)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, r.dashboardURL+JobPath+jobName, nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := r.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func ConvertRayJobToReq(rayJob *rayv1.RayJob) (*RayJobRequest, error) {
req := &RayJobRequest{
Entrypoint: rayJob.Spec.Entrypoint,
SubmissionId: rayJob.Status.JobId,
Metadata: rayJob.Spec.Metadata,
}
if len(rayJob.Spec.RuntimeEnvYAML) != 0 {
runtimeEnv, err := UnmarshalRuntimeEnvYAML(rayJob.Spec.RuntimeEnvYAML)
if err != nil {
return nil, err
}
req.RuntimeEnv = runtimeEnv
}
req.NumCpus = rayJob.Spec.EntrypointNumCpus
req.NumGpus = rayJob.Spec.EntrypointNumGpus
if rayJob.Spec.EntrypointResources != "" {
if err := json.Unmarshal([]byte(rayJob.Spec.EntrypointResources), &req.Resources); err != nil {
return nil, err
}
}
return req, nil
}
func UnmarshalRuntimeEnvYAML(runtimeEnvYAML string) (RuntimeEnvType, error) {
var runtimeEnv RuntimeEnvType
err := yaml.Unmarshal([]byte(runtimeEnvYAML), &runtimeEnv)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal RuntimeEnvYAML: %v: %v", runtimeEnvYAML, err)
}
return runtimeEnv, nil
}