Skip to content

Commit

Permalink
Add --step flag for taskrun logs
Browse files Browse the repository at this point in the history
This will add --step flag for taskrun logs to
show logs of respective tasks only

Rename --only-tasks to --task in pipelinerun
logs

Add tests and docs

Fix #578
  • Loading branch information
piyush-garg authored and tekton-robot committed Feb 10, 2020
1 parent f3bda4d commit d33279c
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 27 deletions.
10 changes: 5 additions & 5 deletions docs/cmd/tkn_pipelinerun_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ Show the logs of PipelineRun named 'microservice-1' for all tasks and steps (inc
### Options

```
-a, --all show all logs including init steps injected by tekton
-f, --follow stream live logs
-h, --help help for logs
--limit int lists number of pipelineruns (default 5)
-t, --only-tasks strings show logs for mentioned tasks only
-a, --all show all logs including init steps injected by tekton
-f, --follow stream live logs
-h, --help help for logs
--limit int lists number of pipelineruns (default 5)
-t, --task strings show logs for mentioned tasks only
```

### Options inherited from parent commands
Expand Down
13 changes: 9 additions & 4 deletions docs/cmd/tkn_taskrun_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ Show the live logs of TaskRun named 'foo' from namespace 'bar':

tkn taskrun logs -f foo -n bar

Show the logs of TaskRun named 'microservice-1' for step 'build' only from namespace 'bar':

tkn tr logs microservice-1 -s build -n bar


### Options

```
-a, --all show all logs including init steps injected by tekton
-f, --follow stream live logs
-h, --help help for logs
--limit int lists number of taskruns (default 5)
-a, --all show all logs including init steps injected by tekton
-f, --follow stream live logs
-h, --help help for logs
--limit int lists number of taskruns (default 5)
-s, --step strings show logs for mentioned steps only
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion docs/man/man1/tkn-pipelinerun-logs.1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Show the logs of PipelineRun
lists number of pipelineruns

.PP
\fB\-t\fP, \fB\-\-only\-tasks\fP=[]
\fB\-t\fP, \fB\-\-task\fP=[]
show logs for mentioned tasks only


Expand Down
16 changes: 16 additions & 0 deletions docs/man/man1/tkn-taskrun-logs.1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Show taskruns logs
\fB\-\-limit\fP=5
lists number of taskruns

.PP
\fB\-s\fP, \fB\-\-step\fP=[]
show logs for mentioned steps only


.SH OPTIONS INHERITED FROM PARENT COMMANDS
.PP
Expand Down Expand Up @@ -79,6 +83,18 @@ tkn taskrun logs \-f foo \-n bar
.fi
.RE

.PP
Show the logs of TaskRun named 'microservice\-1' for step 'build' only from namespace 'bar':

.PP
.RS

.nf
tkn tr logs microservice\-1 \-s build \-n bar

.fi
.RE


.SH SEE ALSO
.PP
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/pipelinerun/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ func updatePR(finalRuns []*v1alpha1.PipelineRun, watcher *watch.FakeWatcher) {
}()
}

func logOpts(name string, ns string, cs pipelinetest.Clients, streamer stream.NewStreamerFunc, allSteps bool, follow bool, onlyTasks ...string) *options.LogOptions {
func logOpts(name string, ns string, cs pipelinetest.Clients, streamer stream.NewStreamerFunc, allSteps bool, follow bool, tasks ...string) *options.LogOptions {
p := test.Params{
Kube: cs.Kube,
Tekton: cs.Pipeline,
Expand All @@ -889,7 +889,7 @@ func logOpts(name string, ns string, cs pipelinetest.Clients, streamer stream.Ne

logOptions := options.LogOptions{
PipelineRunName: name,
Tasks: onlyTasks,
Tasks: tasks,
AllSteps: allSteps,
Follow: follow,
Params: &p,
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pipelinerun/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Show the logs of PipelineRun named 'microservice-1' for all tasks and steps (inc

c.Flags().BoolVarP(&opts.AllSteps, "all", "a", false, "show all logs including init steps injected by tekton")
c.Flags().BoolVarP(&opts.Follow, "follow", "f", false, "stream live logs")
c.Flags().StringSliceVarP(&opts.Tasks, "only-tasks", "t", []string{}, "show logs for mentioned tasks only")
c.Flags().StringSliceVarP(&opts.Tasks, "task", "t", []string{}, "show logs for mentioned tasks only")
c.Flags().IntVarP(&opts.Limit, "limit", "", 5, "lists number of pipelineruns")

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_pipelinerun")
Expand Down
24 changes: 20 additions & 4 deletions pkg/cmd/taskrun/log_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type LogReader struct {
Follow bool
AllSteps bool
Stream *cli.Stream
Steps []string
}

func (lr *LogReader) Read() (<-chan Log, <-chan error, error) {
Expand Down Expand Up @@ -113,7 +114,7 @@ func (lr *LogReader) readLiveLogs() (<-chan Log, <-chan error, error) {
return nil, nil, errors.New(fmt.Sprintf("task %s failed: %s. Run tkn tr desc %s for more details.", lr.Task, strings.TrimSpace(err.Error()), tr.Name))
}

steps := filterSteps(pod, lr.AllSteps)
steps := filterSteps(pod, lr.AllSteps, lr.Steps)
logC, errC := lr.readStepsLogs(steps, p, lr.Follow)
return logC, errC, err
}
Expand Down Expand Up @@ -143,7 +144,7 @@ func (lr *LogReader) readAvailableLogs(tr *v1alpha1.TaskRun) (<-chan Log, <-chan
return nil, nil, errors.New(fmt.Sprintf("task %s failed: %s. Run tkn tr desc %s for more details.", lr.Task, strings.TrimSpace(err.Error()), tr.Name))
}

steps := filterSteps(pod, lr.AllSteps)
steps := filterSteps(pod, lr.AllSteps, lr.Steps)
logC, errC := lr.readStepsLogs(steps, p, lr.Follow)
return logC, errC, nil
}
Expand Down Expand Up @@ -198,14 +199,29 @@ func (lr *LogReader) readStepsLogs(steps []*step, pod *pods.Pod, follow bool) (<
return logC, errC
}

func filterSteps(pod *corev1.Pod, allSteps bool) []*step {
func filterSteps(pod *corev1.Pod, allSteps bool, stepsGiven []string) []*step {
steps := []*step{}
stepsInPod := getSteps(pod)

if allSteps {
steps = append(steps, getInitSteps(pod)...)
}

steps = append(steps, getSteps(pod)...)
if len(stepsGiven) == 0 {
steps = append(steps, stepsInPod...)
return steps
}

stepsToAdd := map[string]bool{}
for _, s := range stepsGiven {
stepsToAdd[s] = true
}

for _, sp := range stepsInPod {
if stepsToAdd[sp.name] {
steps = append(steps, sp)
}
}

return steps
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/cmd/taskrun/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func logCommand(p cli.Params) *cobra.Command {
Show the live logs of TaskRun named 'foo' from namespace 'bar':
tkn taskrun logs -f foo -n bar
Show the logs of TaskRun named 'microservice-1' for step 'build' only from namespace 'bar':
tkn tr logs microservice-1 -s build -n bar
`
c := &cobra.Command{
Use: "logs",
Expand All @@ -63,13 +67,18 @@ Show the live logs of TaskRun named 'foo' from namespace 'bar':
return err
}

if len(opts.Steps) > 0 && opts.AllSteps {
return fmt.Errorf("option --all and option --step are not compatible")
}

return Run(opts)
},
}

c.Flags().BoolVarP(&opts.AllSteps, "all", "a", false, "show all logs including init steps injected by tekton")
c.Flags().BoolVarP(&opts.Follow, "follow", "f", false, "stream live logs")
c.Flags().IntVarP(&opts.Limit, "limit", "", 5, "lists number of taskruns")
c.Flags().StringSliceVarP(&opts.Steps, "step", "s", []string{}, "show logs for mentioned steps only")

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_taskrun")
return c
Expand Down Expand Up @@ -100,6 +109,7 @@ func Run(opts *options.LogOptions) error {
Stream: opts.Stream,
Follow: opts.Follow,
AllSteps: opts.AllSteps,
Steps: opts.Steps,
}

logC, errC, err := lr.Read()
Expand Down
Loading

0 comments on commit d33279c

Please sign in to comment.