Skip to content

Commit

Permalink
Add long flag --display-name to display the log of pipelinerun
Browse files Browse the repository at this point in the history
  • Loading branch information
icloudnote committed Dec 21, 2024
1 parent de8e888 commit 3bd40a3
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 39 deletions.
1 change: 0 additions & 1 deletion docs/cmd/tkn_pipelinerun_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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: 0 additions & 4 deletions docs/man/man1/tkn-pipelinerun-logs.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/pipelinerun/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ 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 @@ -111,7 +110,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).Write(opts.Stream, logC, errC)

// get pipelinerun status
if opts.ExitWithPrError {
Expand Down
9 changes: 4 additions & 5 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ var pipelineGroupResource = schema.GroupVersionResource{Group: "tekton.dev", Res

// Log represents data to write on log channel
type Log struct {
Pipeline string
Task string
TaskDisplayName string
Step string
Log string
Pipeline string
Task string
Step string
Log string
}
3 changes: 1 addition & 2 deletions 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, TaskDisplayName: l.TaskDisplayName, Step: l.Step, Log: l.Log}
logC <- Log{Task: l.Task, Step: l.Step, Log: l.Log}

case e, ok := <-terrC:
if !ok {
Expand All @@ -208,7 +208,6 @@ 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: 0 additions & 5 deletions pkg/log/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type Reader struct {
steps []string
logType string
task string
displayName string
number int
activityTimeout time.Duration
retries int
Expand Down Expand Up @@ -109,10 +108,6 @@ 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, TaskDisplayName: r.displayName, Step: step.name, Log: "EOFLOG"}
logC <- Log{Task: r.task, Step: step.name, Log: "EOFLOG"}
continue
}
logC <- Log{Task: r.task, TaskDisplayName: r.displayName, Step: step.name, Log: l.Log}
logC <- Log{Task: r.task, Step: step.name, Log: l.Log}

case e, ok := <-containerLogErrC:
if !ok {
Expand Down
17 changes: 3 additions & 14 deletions pkg/log/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import (

// Writer helps logging pod"s log
type Writer struct {
fmt *formatted.Color
logType string
prefixing bool
displayName bool
fmt *formatted.Color
logType string
prefixing bool
}

// NewWriter returns the new instance of LogWriter
Expand All @@ -38,12 +37,6 @@ 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 @@ -59,10 +52,6 @@ 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: 0 additions & 1 deletion pkg/options/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ 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
11 changes: 8 additions & 3 deletions pkg/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

type Run struct {
Name string
DisplayName string
Task string
Retries int
StartTime *metav1.Time
Expand Down Expand Up @@ -91,9 +90,8 @@ func SortTasksBySpecOrder(pipelineTasks []v1.PipelineTask, pipelinesTaskRuns map
if n, ok := trNames[ts.Name]; ok {
trStatusFields := pipelinesTaskRuns[n].Status.TaskRunStatusFields
trs = append(trs, Run{
Task: ts.Name,
Task: taskName(ts),
Name: n,
DisplayName: ts.DisplayName,
Retries: ts.Retries,
StartTime: trStatusFields.StartTime,
CompletionTime: trStatusFields.CompletionTime,
Expand All @@ -103,3 +101,10 @@ func SortTasksBySpecOrder(pipelineTasks []v1.PipelineTask, pipelinesTaskRuns map
sort.Sort(trs)
return trs
}

func taskName(task v1.PipelineTask) string {
if task.DisplayName != "" {
return task.DisplayName
}
return task.Name
}

0 comments on commit 3bd40a3

Please sign in to comment.