-
Notifications
You must be signed in to change notification settings - Fork 39
/
log.go
87 lines (75 loc) · 2.24 KB
/
log.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
package workflow
import (
"context"
"errors"
"fmt"
"io"
"github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflow"
"github.com/argoproj/argo-workflows/v3/workflow/common"
corev1 "k8s.io/api/core/v1"
"github.com/seal-io/walrus/pkg/dao/types"
"github.com/seal-io/walrus/utils/strs"
)
// GetWorkflowStepExecutionLogs gets workflow step execution logs.
func (s *ArgoWorkflowClient) GetLogs(ctx context.Context, opts LogsOptions) ([]byte, error) {
logsClient, err := s.apiClient.NewWorkflowServiceClient().
WorkflowLogs(s.apiClient.Ctx, &workflow.WorkflowLogRequest{
Name: strs.Join("-", opts.WorkflowExecution.Name, opts.WorkflowExecution.ID.String()),
Namespace: types.WalrusSystemNamespace,
LogOptions: &corev1.PodLogOptions{
Container: common.MainContainerName,
Previous: opts.Previous,
SinceSeconds: opts.SinceSeconds,
Timestamps: opts.Timestamps,
TailLines: opts.TailLines,
},
Selector: fmt.Sprintf("step-execution-id=%s", opts.StepExecution.ID),
})
if err != nil {
return nil, err
}
logs := []byte{}
for {
event, err := logsClient.Recv()
if errors.Is(err, io.EOF) {
return logs, nil
}
if err != nil {
return nil, err
}
logs = append(logs, []byte(event.Content+"\n")...)
}
}
// StreamWorkflowStepExecutionLogs streams workflow step execution logs.
func (s *ArgoWorkflowClient) StreamLogs(ctx context.Context, opts StreamLogsOptions) error {
logsClient, err := s.apiClient.NewWorkflowServiceClient().
WorkflowLogs(s.apiClient.Ctx, &workflow.WorkflowLogRequest{
Name: strs.Join("-", opts.WorkflowExecution.Name, opts.WorkflowExecution.ID.String()),
Namespace: types.WalrusSystemNamespace,
LogOptions: &corev1.PodLogOptions{
Container: common.MainContainerName,
Follow: true,
Previous: opts.Previous,
SinceSeconds: opts.SinceSeconds,
Timestamps: opts.Timestamps,
TailLines: opts.TailLines,
},
Selector: fmt.Sprintf("step-execution-id=%s", opts.StepExecution.ID),
})
if err != nil {
return err
}
for {
event, err := logsClient.Recv()
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return err
}
_, err = opts.Out.Write([]byte(event.Content + "\n"))
if err != nil {
return err
}
}
}