Skip to content

Commit f610d7e

Browse files
author
zhongzichao
authored
add logs api for pods (#940) (#941)
* add logs api for pods
1 parent d77626e commit f610d7e

File tree

5 files changed

+895
-0
lines changed

5 files changed

+895
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package log
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/PaddlePaddle/PaddleFlow/pkg/apiserver/common"
7+
"github.com/PaddlePaddle/PaddleFlow/pkg/common/logger"
8+
"github.com/PaddlePaddle/PaddleFlow/pkg/common/schema"
9+
runtime "github.com/PaddlePaddle/PaddleFlow/pkg/job/runtime_v2"
10+
"github.com/PaddlePaddle/PaddleFlow/pkg/model"
11+
)
12+
13+
// GetMixedLogRequest can request job log or k8s pod/deploy events and log
14+
type GetMixedLogRequest struct {
15+
Name string
16+
Namespace string
17+
ResourceType string
18+
Framework string
19+
20+
LineLimit string
21+
SizeLimit int64
22+
IsReadFromTail bool
23+
ClusterName string
24+
ClusterInfo model.ClusterInfo
25+
}
26+
27+
// GetMixedLogResponse return mixed logs
28+
type GetMixedLogResponse struct {
29+
ResourceName string `json:"name"`
30+
Resourcetype string `json:"type"`
31+
TaskList []schema.TaskLogInfo `json:"taskList"`
32+
Events []string `json:"eventList"`
33+
}
34+
35+
// GetPFJobLogs todo to be merged with GetLogs
36+
// return mixed log by events and logs
37+
func GetPFJobLogs(ctx *logger.RequestContext, request GetMixedLogRequest) (schema.JobLogInfo, error) {
38+
ctx.Logging().Debugf("Get k8s logs by request: %v", request)
39+
switch schema.Framework(request.Framework) {
40+
case schema.FrameworkStandalone, schema.FrameworkSpark, schema.FrameworkPaddle, schema.FrameworkTF,
41+
schema.FrameworkPytorch, schema.FrameworkMXNet, schema.FrameworkRay:
42+
// todo call runtimeSvc.GetJobLog()
43+
ctx.Logging().Warnf("todo")
44+
default:
45+
err := fmt.Errorf("job %s framework %s unsupport", request.Name, request.Framework)
46+
ctx.ErrorCode = common.InvalidArguments
47+
ctx.Logging().Errorln(err)
48+
return schema.JobLogInfo{}, err
49+
}
50+
return schema.JobLogInfo{}, nil
51+
}
52+
53+
// GetLogs return mixed logs
54+
func GetLogs(ctx *logger.RequestContext, request GetMixedLogRequest) (schema.JobLogInfo, error) {
55+
ctx.Logging().Debugf("Get mixed logs by request: %v", request)
56+
runtimeSvc, err := runtime.GetOrCreateRuntime(request.ClusterInfo)
57+
if err != nil {
58+
err = fmt.Errorf("get cluster client failed. error:%s", err.Error())
59+
ctx.ErrorCode = common.ClusterNotFound
60+
ctx.Logging().Errorln(err)
61+
return schema.JobLogInfo{}, err
62+
}
63+
schemaReq := schema.MixedLogRequest{
64+
Name: request.Name,
65+
Namespace: request.Namespace,
66+
ResourceType: request.ResourceType,
67+
Framework: request.Framework,
68+
LineLimit: request.LineLimit,
69+
SizeLimit: request.SizeLimit,
70+
IsReadFromTail: request.IsReadFromTail,
71+
}
72+
response, err := runtimeSvc.GetLog(schema.JobLogRequest{}, schemaReq)
73+
if err != nil {
74+
err = fmt.Errorf("get mixed logs failed. error: %v", err)
75+
ctx.ErrorCode = common.InternalError
76+
ctx.Logging().Errorln(err)
77+
return schema.JobLogInfo{}, err
78+
}
79+
response.Resourcetype = request.ResourceType
80+
response.ResourceName = fmt.Sprintf("%s/%s", request.Namespace, request.Name)
81+
return response, nil
82+
}

0 commit comments

Comments
 (0)