Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add long flag --display-name to display the log of pipelinerun #2450

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/cmd/tkn_pipelinerun_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-pipelinerun-logs.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/pipelinerun/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion pkg/log/pipeline_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/log/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Reader struct {
steps []string
logType string
task string
displayName string
number int
activityTimeout time.Duration
retries int
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/log/task_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 14 additions & 3 deletions pkg/log/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pkg/options/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions pkg/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

type Run struct {
Name string
DisplayName string
Task string
Retries int
StartTime *metav1.Time
Expand Down Expand Up @@ -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,
Expand Down