forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
137 lines (112 loc) · 4.08 KB
/
logs.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
package commands
import (
"context"
"fmt"
"io"
"log"
"os"
"time"
"github.com/argoproj/pkg/errors"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
workflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflow"
)
func NewLogsCommand() *cobra.Command {
var (
since time.Duration
sinceTime string
tailLines int64
grep string
selector string
)
logOptions := &corev1.PodLogOptions{}
command := &cobra.Command{
Use: "logs WORKFLOW [POD]",
Short: "view logs of a pod or workflow",
Example: `# Print the logs of a workflow:
argo logs my-wf
# Follow the logs of a workflows:
argo logs my-wf --follow
# Print the logs of a workflows with a selector:
argo logs my-wf -l app=sth
# Print the logs of single container in a pod
argo logs my-wf my-pod -c my-container
# Print the logs of a workflow's pods:
argo logs my-wf my-pod
# Print the logs of a pods:
argo logs --since=1h my-pod
# Print the logs of the latest workflow:
argo logs @latest
`,
Run: func(cmd *cobra.Command, args []string) {
// parse all the args
workflow := ""
podName := ""
switch len(args) {
case 1:
workflow = args[0]
case 2:
workflow = args[0]
podName = args[1]
default:
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
if since > 0 && sinceTime != "" {
log.Fatal("--since-time and --since cannot be used together")
}
if since > 0 {
logOptions.SinceSeconds = pointer.Int64Ptr(int64(since.Seconds()))
}
if sinceTime != "" {
parsedTime, err := time.Parse(time.RFC3339, sinceTime)
errors.CheckError(err)
sinceTime := metav1.NewTime(parsedTime)
logOptions.SinceTime = &sinceTime
}
if tailLines >= 0 {
logOptions.TailLines = pointer.Int64Ptr(tailLines)
}
// set-up
ctx, apiClient := client.NewAPIClient(cmd.Context())
serviceClient := apiClient.NewWorkflowServiceClient()
namespace := client.Namespace()
logWorkflow(ctx, serviceClient, namespace, workflow, podName, grep, selector, logOptions)
},
}
command.Flags().StringVarP(&logOptions.Container, "container", "c", "main", "Print the logs of this container")
command.Flags().BoolVarP(&logOptions.Follow, "follow", "f", false, "Specify if the logs should be streamed.")
command.Flags().BoolVarP(&logOptions.Previous, "previous", "p", false, "Specify if the previously terminated container logs should be returned.")
command.Flags().DurationVar(&since, "since", 0, "Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to all logs. Only one of since-time / since may be used.")
command.Flags().StringVar(&sinceTime, "since-time", "", "Only return logs after a specific date (RFC3339). Defaults to all logs. Only one of since-time / since may be used.")
command.Flags().Int64Var(&tailLines, "tail", -1, "If set, the number of lines from the end of the logs to show. If not specified, logs are shown from the creation of the container or sinceSeconds or sinceTime")
command.Flags().StringVar(&grep, "grep", "", "grep for lines")
command.Flags().StringVarP(&selector, "selector", "l", "", "log selector for some pod")
command.Flags().BoolVar(&logOptions.Timestamps, "timestamps", false, "Include timestamps on each line in the log output")
command.Flags().BoolVar(&noColor, "no-color", false, "Disable colorized output")
return command
}
func logWorkflow(ctx context.Context, serviceClient workflowpkg.WorkflowServiceClient, namespace, workflow, podName, grep, selector string, logOptions *corev1.PodLogOptions) {
// logs
stream, err := serviceClient.WorkflowLogs(ctx, &workflowpkg.WorkflowLogRequest{
Name: workflow,
Namespace: namespace,
PodName: podName,
LogOptions: logOptions,
Selector: selector,
Grep: grep,
})
errors.CheckError(err)
// loop on log lines
for {
event, err := stream.Recv()
if err == io.EOF {
return
}
errors.CheckError(err)
fmt.Println(ansiFormat(fmt.Sprintf("%s: %s", event.PodName, event.Content), ansiColorCode(event.PodName)))
}
}