-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathlogs.go
174 lines (145 loc) · 4.86 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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 pipelinerun
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/log"
"github.com/tektoncd/cli/pkg/options"
pipelinerunpkg "github.com/tektoncd/cli/pkg/pipelinerun"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
defaultLimit = 5
)
func logCommand(p cli.Params) *cobra.Command {
opts := &options.LogOptions{Params: p}
eg := `Show the logs of PipelineRun named 'foo' from namespace 'bar':
tkn pipelinerun logs foo -n bar
Show the logs of PipelineRun named 'microservice-1' for Task 'build' only from namespace 'bar':
tkn pr logs microservice-1 -t build -n bar
Show the logs of PipelineRun named 'microservice-1' for all Tasks and steps (including init steps) from namespace 'foo':
tkn pr logs microservice-1 -a -n foo
`
c := &cobra.Command{
Use: "logs",
DisableFlagsInUseLine: true,
Short: "Show the logs of a PipelineRun",
Annotations: map[string]string{
"commandType": "main",
},
Example: eg,
ValidArgsFunction: formatted.ParentCompletion,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
opts.PipelineRunName = args[0]
}
if !opts.Fzf {
if _, ok := os.LookupEnv("TKN_USE_FZF"); ok {
opts.Fzf = true
}
}
opts.Stream = &cli.Stream{
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}
return Run(opts)
},
}
c.Flags().BoolVarP(&opts.AllSteps, "all", "a", false, "show all logs including init steps injected by tekton")
c.Flags().BoolVarP(&opts.Last, "last", "L", false, "show logs for last PipelineRun")
c.Flags().BoolVarP(&opts.Fzf, "fzf", "F", false, "use fzf to select a PipelineRun")
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.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")
return c
}
func Run(opts *options.LogOptions) error {
if opts.PipelineRunName == "" {
if err := opts.ValidateOpts(); err != nil {
return err
}
if err := askRunName(opts); err != nil {
return err
}
}
lr, err := log.NewReader(log.LogTypePipeline, opts)
if err != nil {
return err
}
logC, errC, err := lr.Read()
if err != nil {
return err
}
log.NewWriter(log.LogTypePipeline, opts.Prefixing).Write(opts.Stream, logC, errC)
// get pipelinerun status
if opts.ExitWithPrError {
clients, err := opts.Params.Clients()
if err != nil {
return err
}
pr, err := pipelinerunpkg.GetPipelineRun(pipelineRunGroupResource, clients, opts.PipelineRunName, opts.Params.Namespace())
if err != nil {
return err
}
os.Exit(prStatusToUnixStatus(pr))
}
return nil
}
func prStatusToUnixStatus(pr *tektonv1.PipelineRun) int {
if len(pr.Status.Conditions) == 0 {
return 2
}
if pr.Status.Conditions[0].Status == corev1.ConditionFalse {
return 1
}
return 0
}
func askRunName(opts *options.LogOptions) error {
lOpts := metav1.ListOptions{}
// We are able to show much more than the default 5 with fzf, so let
// increase that limit limited to 100
if opts.Fzf && opts.Limit == defaultLimit {
opts.Limit = 100
}
clients, err := opts.Params.Clients()
if err != nil {
return err
}
prs, err := pipelinerunpkg.GetAllPipelineRuns(pipelineRunGroupResource, lOpts, clients, opts.Params.Namespace(), opts.Limit, opts.Params.Time())
if err != nil {
return err
}
if len(prs) == 0 {
fmt.Fprint(os.Stdout, "No PipelineRuns found")
return nil
}
if len(prs) == 1 || opts.Last {
opts.PipelineRunName = strings.Fields(prs[0])[0]
return nil
}
if opts.Fzf {
return opts.FuzzyAsk(options.ResourceNamePipelineRun, prs)
}
return opts.Ask(options.ResourceNamePipelineRun, prs)
}