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

Making task logs interactive. #508

Merged
Merged
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_task.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ Manage tasks
* [tkn task delete](tkn_task_delete.md) - Delete a task resource in a namespace
* [tkn task describe](tkn_task_describe.md) - Describes a task in a namespace
* [tkn task list](tkn_task_list.md) - Lists tasks in a namespace
* [tkn task logs](tkn_task_logs.md) - Show task logs
* [tkn task start](tkn_task_start.md) - Start tasks

54 changes: 54 additions & 0 deletions docs/cmd/tkn_task_logs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## tkn task logs

Show task logs

### Usage

```
tkn task logs
```

### Synopsis

Show task logs

### Examples


# interactive mode: shows logs of the selected taskrun
tkn task logs -n namespace

# interactive mode: shows logs of the selected taskrun of the given task
tkn task logs task -n namespace

# show logs of given task for last taskrun
tkn task logs task -n namespace --last

# show logs for given task and taskrun
tkn task logs task taskrun -n namespace



### Options

```
-a, --all show all logs including init steps injected by tekton
-f, --follow stream live logs
-h, --help help for logs
-L, --last show logs for last taskrun
--limit int lists number of taskruns (default 5)
```

### Options inherited from parent commands

```
-c, --context string name of the kubeconfig context to use (default: kubectl config current-context)
-k, --kubeconfig string kubectl config file (default: $HOME/.kube/config)
-n, --namespace string namespace to use (default: from $KUBECONFIG)
-C, --nocolour disable colouring (default: false)
```

### SEE ALSO

* [tkn task](tkn_task.md) - Manage tasks

81 changes: 81 additions & 0 deletions docs/man/man1/tkn-task-logs.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
.TH "TKN\-TASK\-LOGS" "1" "" "Auto generated by spf13/cobra" ""
.nh
.ad l


.SH NAME
.PP
tkn\-task\-logs \- Show task logs


.SH SYNOPSIS
.PP
\fBtkn task logs\fP


.SH DESCRIPTION
.PP
Show task logs


.SH OPTIONS
.PP
\fB\-a\fP, \fB\-\-all\fP[=false]
show all logs including init steps injected by tekton

.PP
\fB\-f\fP, \fB\-\-follow\fP[=false]
stream live logs

.PP
\fB\-h\fP, \fB\-\-help\fP[=false]
help for logs

.PP
\fB\-L\fP, \fB\-\-last\fP[=false]
show logs for last taskrun

.PP
\fB\-\-limit\fP=5
lists number of taskruns


.SH OPTIONS INHERITED FROM PARENT COMMANDS
.PP
\fB\-c\fP, \fB\-\-context\fP=""
name of the kubeconfig context to use (default: kubectl config current\-context)

.PP
\fB\-k\fP, \fB\-\-kubeconfig\fP=""
kubectl config file (default: $HOME/.kube/config)

.PP
\fB\-n\fP, \fB\-\-namespace\fP=""
namespace to use (default: from $KUBECONFIG)

.PP
\fB\-C\fP, \fB\-\-nocolour\fP[=false]
disable colouring (default: false)


.SH EXAMPLE
.PP
# interactive mode: shows logs of the selected taskrun
tkn task logs \-n namespace

.PP
# interactive mode: shows logs of the selected taskrun of the given task
tkn task logs task \-n namespace

.PP
# show logs of given task for last taskrun
tkn task logs task \-n namespace \-\-last

.PP
# show logs for given task and taskrun
tkn task logs task taskrun \-n namespace


.SH SEE ALSO
.PP
\fBtkn\-task(1)\fP
2 changes: 1 addition & 1 deletion docs/man/man1/tkn-task.1
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ Manage tasks

.SH SEE ALSO
.PP
\fBtkn(1)\fP, \fBtkn\-task\-create(1)\fP, \fBtkn\-task\-delete(1)\fP, \fBtkn\-task\-describe(1)\fP, \fBtkn\-task\-list(1)\fP, \fBtkn\-task\-start(1)\fP
\fBtkn(1)\fP, \fBtkn\-task\-create(1)\fP, \fBtkn\-task\-delete(1)\fP, \fBtkn\-task\-describe(1)\fP, \fBtkn\-task\-list(1)\fP, \fBtkn\-task\-logs(1)\fP, \fBtkn\-task\-start(1)\fP
196 changes: 196 additions & 0 deletions pkg/cmd/task/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// Copyright © 2019 The Tekton Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package task

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/cmd/taskrun"
"github.com/tektoncd/cli/pkg/flags"
"github.com/tektoncd/cli/pkg/helper/options"
thelper "github.com/tektoncd/cli/pkg/helper/task"
trlist "github.com/tektoncd/cli/pkg/helper/taskrun/list"
validate "github.com/tektoncd/cli/pkg/helper/validate"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func nameArg(args []string, p cli.Params) error {
if len(args) == 1 {
c, err := p.Clients()
if err != nil {
return err
}
name, ns := args[0], p.Namespace()
if _, err = c.Tekton.TektonV1alpha1().Tasks(ns).Get(name, metav1.GetOptions{}); err != nil {
return err
}
}
return nil
}

func logCommand(p cli.Params) *cobra.Command {
opts := options.NewLogOptions(p)

eg := `
# interactive mode: shows logs of the selected taskrun
tkn task logs -n namespace

# interactive mode: shows logs of the selected taskrun of the given task
tkn task logs task -n namespace

# show logs of given task for last taskrun
tkn task logs task -n namespace --last

# show logs for given task and taskrun
tkn task logs task taskrun -n namespace

`
c := &cobra.Command{
Use: "logs",
DisableFlagsInUseLine: true,
Short: "Show task logs",
Example: eg,
SilenceUsage: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is needed anymore since root.go has SilenceUsage: True, but no need to dig further in this pr. It might be something we should look into.

Annotations: map[string]string{
"commandType": "main",
},
Args: func(cmd *cobra.Command, args []string) error {
if err := flags.InitParams(p, cmd); err != nil {
return err
}
return nameArg(args, p)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.Stream = &cli.Stream{
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}

if err := validate.NamespaceExists(p); err != nil {
return err
}

return run(opts, args)
},
}
c.Flags().BoolVarP(&opts.Last, "last", "L", false, "show logs for last taskrun")
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.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_task")
return c
}

func run(opts *options.LogOptions, args []string) error {
if err := initOpts(opts, args); err != nil {
return err
}

if opts.TaskName == "" || opts.TaskrunName == "" {
return nil
}

return taskrun.Run(opts)
}

func initOpts(opts *options.LogOptions, args []string) error {
// ensure the client is properly initialized
if _, err := opts.Params.Clients(); err != nil {
return err
}

if err := opts.ValidateOpts(); err != nil {
return err
}

switch len(args) {
case 0: // no inputs
return getAllInputs(opts)

case 1: // task name provided
opts.TaskName = args[0]
return askRunName(opts)

case 2: // both task and run provided
opts.TaskName = args[0]
opts.TaskrunName = args[1]

default:
return fmt.Errorf("too many arguments")
}
return nil
}

func getAllInputs(opts *options.LogOptions) error {
ts, err := thelper.GetAllTaskNames(opts.Params)
if err != nil {
return err
}

if len(ts) == 0 {
fmt.Fprintln(opts.Stream.Err, "No tasks found in namespace:", opts.Params.Namespace())
return nil
}

if err := opts.Ask(options.ResourceNameTask, ts); err != nil {
return nil
}

return askRunName(opts)
}

func askRunName(opts *options.LogOptions) error {
if opts.Last {
return initLastRunName(opts)
}

lOpts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/task=%s", opts.TaskName),
}

trs, err := trlist.GetAllTaskRuns(opts.Params, lOpts, opts.Limit)
if err != nil {
return err
}

if len(trs) == 0 {
fmt.Fprintln(opts.Stream.Err, "No taskruns found for task:", opts.TaskName)
return nil
}

if len(trs) == 1 {
opts.TaskrunName = strings.Fields(trs[0])[0]
return nil
}

return opts.Ask(options.ResourceNameTaskRun, trs)
}

func initLastRunName(opts *options.LogOptions) error {
cs, err := opts.Params.Clients()
if err != nil {
return err
}
lastrun, err := thelper.LastRun(cs.Tekton, opts.TaskName, opts.Params.Namespace())
if err != nil {
return err
}
opts.TaskrunName = lastrun.Name
return nil
}
Loading