-
Notifications
You must be signed in to change notification settings - Fork 250
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
tekton-robot
merged 1 commit into
tektoncd:master
from
16yuki0702:making_task_logs_interactive
Dec 16, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
hasSilenceUsage: True
, but no need to dig further in this pr. It might be something we should look into.