From de8e888bace01077eab92b271da5a0b142f5d7de Mon Sep 17 00:00:00 2001 From: changjun Date: Fri, 20 Dec 2024 16:25:12 +0800 Subject: [PATCH 1/2] Add long flag --display-name to display the log of pipelinerun --- docs/cmd/tkn_pipelinerun_logs.md | 1 + docs/man/man1/tkn-pipelinerun-logs.1 | 4 ++++ pkg/cmd/pipelinerun/logs.go | 3 ++- pkg/log/log.go | 9 +++++---- pkg/log/pipeline_reader.go | 3 ++- pkg/log/reader.go | 5 +++++ pkg/log/task_reader.go | 4 ++-- pkg/log/writer.go | 17 ++++++++++++++--- pkg/options/logs.go | 1 + pkg/taskrun/taskrun.go | 2 ++ 10 files changed, 38 insertions(+), 11 deletions(-) diff --git a/docs/cmd/tkn_pipelinerun_logs.md b/docs/cmd/tkn_pipelinerun_logs.md index 1648dc4d0e..681994119c 100644 --- a/docs/cmd/tkn_pipelinerun_logs.md +++ b/docs/cmd/tkn_pipelinerun_logs.md @@ -31,6 +31,7 @@ Show the logs of PipelineRun named 'microservice-1' for all Tasks and steps (inc ``` -a, --all show all logs including init steps injected by tekton + --display-name show logs with task display name (display name and step name) -E, --exit-with-pipelinerun-error exit with pipelinerun to the unix shell, 0 if success, 1 if error, 2 on unknown status -f, --follow stream live logs -F, --fzf use fzf to select a PipelineRun diff --git a/docs/man/man1/tkn-pipelinerun-logs.1 b/docs/man/man1/tkn-pipelinerun-logs.1 index a5879ea6d8..0da46f67bd 100644 --- a/docs/man/man1/tkn-pipelinerun-logs.1 +++ b/docs/man/man1/tkn-pipelinerun-logs.1 @@ -23,6 +23,10 @@ Show the logs of a PipelineRun \fB\-a\fP, \fB\-\-all\fP[=false] show all logs including init steps injected by tekton +.PP +\fB\-\-display\-name\fP[=false] + show logs with task display name (display name and step name) + .PP \fB\-E\fP, \fB\-\-exit\-with\-pipelinerun\-error\fP[=false] exit with pipelinerun to the unix shell, 0 if success, 1 if error, 2 on unknown status diff --git a/pkg/cmd/pipelinerun/logs.go b/pkg/cmd/pipelinerun/logs.go index 235003cfcb..ecfae7efeb 100644 --- a/pkg/cmd/pipelinerun/logs.go +++ b/pkg/cmd/pipelinerun/logs.go @@ -84,6 +84,7 @@ Show the logs of PipelineRun named 'microservice-1' for all Tasks and steps (inc c.Flags().BoolVarP(&opts.Follow, "follow", "f", false, "stream live logs") c.Flags().BoolVarP(&opts.Timestamps, "timestamps", "", false, "show logs with timestamp") c.Flags().BoolVarP(&opts.Prefixing, "prefix", "", true, "prefix each log line with the log source (task name and step name)") + c.Flags().BoolVarP(&opts.DisplayName, "display-name", "", false, "show logs with task display name (display name and step name)") c.Flags().BoolVarP(&opts.ExitWithPrError, "exit-with-pipelinerun-error", "E", false, "exit with pipelinerun to the unix shell, 0 if success, 1 if error, 2 on unknown status") c.Flags().StringSliceVarP(&opts.Tasks, "task", "t", []string{}, "show logs for mentioned Tasks only") c.Flags().IntVarP(&opts.Limit, "limit", "", defaultLimit, "lists number of PipelineRuns") @@ -110,7 +111,7 @@ func Run(opts *options.LogOptions) error { return err } - log.NewWriter(log.LogTypePipeline, opts.Prefixing).Write(opts.Stream, logC, errC) + log.NewWriter(log.LogTypePipeline, opts.Prefixing).WithDisplayName(opts.DisplayName).Write(opts.Stream, logC, errC) // get pipelinerun status if opts.ExitWithPrError { diff --git a/pkg/log/log.go b/pkg/log/log.go index 7e1f606b71..c95a6f93f4 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -27,8 +27,9 @@ var pipelineGroupResource = schema.GroupVersionResource{Group: "tekton.dev", Res // Log represents data to write on log channel type Log struct { - Pipeline string - Task string - Step string - Log string + Pipeline string + Task string + TaskDisplayName string + Step string + Log string } diff --git a/pkg/log/pipeline_reader.go b/pkg/log/pipeline_reader.go index 740d416ce9..19cb59312b 100644 --- a/pkg/log/pipeline_reader.go +++ b/pkg/log/pipeline_reader.go @@ -192,7 +192,7 @@ func (r *Reader) pipeLogs(logC chan<- Log, errC chan<- error) { tlogC = nil continue } - logC <- Log{Task: l.Task, Step: l.Step, Log: l.Log} + logC <- Log{Task: l.Task, TaskDisplayName: l.TaskDisplayName, Step: l.Step, Log: l.Log} case e, ok := <-terrC: if !ok { @@ -208,6 +208,7 @@ func (r *Reader) setUpTask(taskNumber int, tr taskrunpkg.Run) { r.setNumber(taskNumber) r.setRun(tr.Name) r.setTask(tr.Task) + r.setDisplayName(tr.DisplayName) r.setRetries(tr.Retries) } diff --git a/pkg/log/reader.go b/pkg/log/reader.go index e4ded0d591..2ebe019e64 100644 --- a/pkg/log/reader.go +++ b/pkg/log/reader.go @@ -37,6 +37,7 @@ type Reader struct { steps []string logType string task string + displayName string number int activityTimeout time.Duration retries int @@ -108,6 +109,10 @@ func (r *Reader) setTask(task string) { r.task = task } +func (r *Reader) setDisplayName(displayName string) { + r.displayName = displayName +} + func (r *Reader) clone() *Reader { c := *r return &c diff --git a/pkg/log/task_reader.go b/pkg/log/task_reader.go index 7ba470bb2a..ecb80da0a6 100644 --- a/pkg/log/task_reader.go +++ b/pkg/log/task_reader.go @@ -138,10 +138,10 @@ func (r *Reader) readStepsLogs(logC chan<- Log, errC chan<- error, steps []*step case l, ok := <-containerLogC: if !ok { containerLogC = nil - logC <- Log{Task: r.task, Step: step.name, Log: "EOFLOG"} + logC <- Log{Task: r.task, TaskDisplayName: r.displayName, Step: step.name, Log: "EOFLOG"} continue } - logC <- Log{Task: r.task, Step: step.name, Log: l.Log} + logC <- Log{Task: r.task, TaskDisplayName: r.displayName, Step: step.name, Log: l.Log} case e, ok := <-containerLogErrC: if !ok { diff --git a/pkg/log/writer.go b/pkg/log/writer.go index 0e965987ba..074ec33560 100644 --- a/pkg/log/writer.go +++ b/pkg/log/writer.go @@ -23,9 +23,10 @@ import ( // Writer helps logging pod"s log type Writer struct { - fmt *formatted.Color - logType string - prefixing bool + fmt *formatted.Color + logType string + prefixing bool + displayName bool } // NewWriter returns the new instance of LogWriter @@ -37,6 +38,12 @@ func NewWriter(logType string, prefixing bool) *Writer { } } +// WithDisplayName sets the display name +func (lw *Writer) WithDisplayName(displayName bool) *Writer { + lw.displayName = displayName + return lw +} + // Write formatted pod's logs func (lw *Writer) Write(s *cli.Stream, logC <-chan Log, errC <-chan error) { for logC != nil || errC != nil { @@ -52,6 +59,10 @@ func (lw *Writer) Write(s *cli.Stream, logC <-chan Log, errC <-chan error) { continue } + if lw.displayName && l.TaskDisplayName != "" { + l.Task = l.TaskDisplayName + } + if lw.prefixing { switch lw.logType { case LogTypePipeline: diff --git a/pkg/options/logs.go b/pkg/options/logs.go index 8e353570b6..8fe588f55f 100644 --- a/pkg/options/logs.go +++ b/pkg/options/logs.go @@ -51,6 +51,7 @@ type LogOptions struct { Tail int64 Timestamps bool Prefixing bool + DisplayName bool ExitWithPrError bool // ActivityTimeout is the amount of time to wait for some activity // (e.g. Pod ready) before giving up. diff --git a/pkg/taskrun/taskrun.go b/pkg/taskrun/taskrun.go index 18d4ad42dd..64d314c5cf 100644 --- a/pkg/taskrun/taskrun.go +++ b/pkg/taskrun/taskrun.go @@ -23,6 +23,7 @@ import ( type Run struct { Name string + DisplayName string Task string Retries int StartTime *metav1.Time @@ -92,6 +93,7 @@ func SortTasksBySpecOrder(pipelineTasks []v1.PipelineTask, pipelinesTaskRuns map trs = append(trs, Run{ Task: ts.Name, Name: n, + DisplayName: ts.DisplayName, Retries: ts.Retries, StartTime: trStatusFields.StartTime, CompletionTime: trStatusFields.CompletionTime, From b243caf1132cb015173eeb5ebcbe98c1f6432206 Mon Sep 17 00:00:00 2001 From: changjun Date: Mon, 29 Dec 2025 15:38:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor(logs):=20=E5=B0=86display-name?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E9=87=8D=E5=91=BD=E5=90=8D=E4=B8=BAlong?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 参数名称更改为long以更清晰地表示其功能,同时保持原有行为不变 --- docs/cmd/tkn_pipelinerun_logs.md | 2 +- docs/man/man1/tkn-pipelinerun-logs.1 | 8 ++++---- pkg/cmd/pipelinerun/logs.go | 4 ++-- pkg/options/logs.go | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/cmd/tkn_pipelinerun_logs.md b/docs/cmd/tkn_pipelinerun_logs.md index 681994119c..c9264e1899 100644 --- a/docs/cmd/tkn_pipelinerun_logs.md +++ b/docs/cmd/tkn_pipelinerun_logs.md @@ -31,13 +31,13 @@ Show the logs of PipelineRun named 'microservice-1' for all Tasks and steps (inc ``` -a, --all show all logs including init steps injected by tekton - --display-name show logs with task display name (display name and step name) -E, --exit-with-pipelinerun-error exit with pipelinerun to the unix shell, 0 if success, 1 if error, 2 on unknown status -f, --follow stream live logs -F, --fzf use fzf to select a PipelineRun -h, --help help for logs -L, --last show logs for last PipelineRun --limit int lists number of PipelineRuns (default 5) + --long show logs with task display name (display name and step name) --prefix prefix each log line with the log source (task name and step name) (default true) -t, --task strings show logs for mentioned Tasks only --timestamps show logs with timestamp diff --git a/docs/man/man1/tkn-pipelinerun-logs.1 b/docs/man/man1/tkn-pipelinerun-logs.1 index 0da46f67bd..b27e83f617 100644 --- a/docs/man/man1/tkn-pipelinerun-logs.1 +++ b/docs/man/man1/tkn-pipelinerun-logs.1 @@ -23,10 +23,6 @@ Show the logs of a PipelineRun \fB\-a\fP, \fB\-\-all\fP[=false] show all logs including init steps injected by tekton -.PP -\fB\-\-display\-name\fP[=false] - show logs with task display name (display name and step name) - .PP \fB\-E\fP, \fB\-\-exit\-with\-pipelinerun\-error\fP[=false] exit with pipelinerun to the unix shell, 0 if success, 1 if error, 2 on unknown status @@ -51,6 +47,10 @@ Show the logs of a PipelineRun \fB\-\-limit\fP=5 lists number of PipelineRuns +.PP +\fB\-\-long\fP[=false] + show logs with task display name (display name and step name) + .PP \fB\-\-prefix\fP[=true] prefix each log line with the log source (task name and step name) diff --git a/pkg/cmd/pipelinerun/logs.go b/pkg/cmd/pipelinerun/logs.go index ecfae7efeb..1bdd69a3c5 100644 --- a/pkg/cmd/pipelinerun/logs.go +++ b/pkg/cmd/pipelinerun/logs.go @@ -84,7 +84,7 @@ Show the logs of PipelineRun named 'microservice-1' for all Tasks and steps (inc c.Flags().BoolVarP(&opts.Follow, "follow", "f", false, "stream live logs") c.Flags().BoolVarP(&opts.Timestamps, "timestamps", "", false, "show logs with timestamp") c.Flags().BoolVarP(&opts.Prefixing, "prefix", "", true, "prefix each log line with the log source (task name and step name)") - c.Flags().BoolVarP(&opts.DisplayName, "display-name", "", false, "show logs with task display name (display name and step name)") + c.Flags().BoolVarP(&opts.Long, "long", "", false, "show logs with task display name (display name and step name)") c.Flags().BoolVarP(&opts.ExitWithPrError, "exit-with-pipelinerun-error", "E", false, "exit with pipelinerun to the unix shell, 0 if success, 1 if error, 2 on unknown status") c.Flags().StringSliceVarP(&opts.Tasks, "task", "t", []string{}, "show logs for mentioned Tasks only") c.Flags().IntVarP(&opts.Limit, "limit", "", defaultLimit, "lists number of PipelineRuns") @@ -111,7 +111,7 @@ func Run(opts *options.LogOptions) error { return err } - log.NewWriter(log.LogTypePipeline, opts.Prefixing).WithDisplayName(opts.DisplayName).Write(opts.Stream, logC, errC) + log.NewWriter(log.LogTypePipeline, opts.Prefixing).WithDisplayName(opts.Long).Write(opts.Stream, logC, errC) // get pipelinerun status if opts.ExitWithPrError { diff --git a/pkg/options/logs.go b/pkg/options/logs.go index 8fe588f55f..a3c8f3f569 100644 --- a/pkg/options/logs.go +++ b/pkg/options/logs.go @@ -51,7 +51,7 @@ type LogOptions struct { Tail int64 Timestamps bool Prefixing bool - DisplayName bool + Long bool ExitWithPrError bool // ActivityTimeout is the amount of time to wait for some activity // (e.g. Pod ready) before giving up.