Skip to content

Commit

Permalink
Add support for filtering by label on taskruns
Browse files Browse the repository at this point in the history
We are adding a --label option to allow to do some filtering by a label. Note
that it (server side) does not seem to support multiple labels so only allowed
to do one.

Closes tektoncd#685

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
  • Loading branch information
chmouel authored and tekton-robot committed Feb 12, 2020
1 parent 357973b commit 17007d3
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/cmd/tkn_pipelinerun_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ List all PipelineRuns in a namespace 'foo':
```
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
-h, --help help for list
--labels string Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
--label string A selector (label query) to filter on, supports '=', '==', and '!='
--limit int limit pipelineruns listed (default: return all pipelineruns)
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/tkn_taskrun_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ List all TaskRuns of Task 'foo' in namespace 'bar':
```
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
-h, --help help for list
--label string A selector (label query) to filter on, supports '=', '==', and '!='
--limit int limit taskruns listed (default: return all taskruns)
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
Expand Down
4 changes: 2 additions & 2 deletions docs/man/man1/tkn-pipelinerun-list.1
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Lists pipelineruns in a namespace
help for list

.PP
\fB\-\-labels\fP=""
Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. \-l key1=value1,key2=value2)
\fB\-\-label\fP=""
A selector (label query) to filter on, supports '=', '==', and '!='

.PP
\fB\-\-limit\fP=0
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-taskrun-list.1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Lists TaskRuns in a namespace
\fB\-h\fP, \fB\-\-help\fP[=false]
help for list

.PP
\fB\-\-label\fP=""
A selector (label query) to filter on, supports '=', '==', and '!='

.PP
\fB\-\-limit\fP=0
limit taskruns listed (default: return all taskruns)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pipelinerun/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ List all PipelineRuns in a namespace 'foo':

f.AddFlags(c)
c.Flags().IntVarP(&opts.Limit, "limit", "", 0, "limit pipelineruns listed (default: return all pipelineruns)")
c.Flags().StringVarP(&opts.LabelSelector, "labels", "", opts.LabelSelector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
c.Flags().StringVarP(&opts.LabelSelector, "label", "", opts.LabelSelector, "A selector (label query) to filter on, supports '=', '==', and '!='")
return c
}

Expand Down
13 changes: 10 additions & 3 deletions pkg/cmd/pipelinerun/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestListPipelineRuns(t *testing.T) {
),
tb.PipelineRun("pr2-2", "namespace",
tb.PipelineRunLabel("tekton.dev/pipeline", "random"),
tb.PipelineRunLabel("viva", "galapagos"),
tb.PipelineRunStatus(
tb.PipelineRunStatusCondition(apis.Condition{
Status: corev1.ConditionFalse,
Expand Down Expand Up @@ -142,16 +143,22 @@ func TestListPipelineRuns(t *testing.T) {
args: []string{"list", "-n", "namespace", "--limit", fmt.Sprintf("%d", -1)},
wantError: false,
},
{
name: "filter pipelineruns by label with in query",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "-n", "namespace", "--label", "viva in (wakanda,galapagos)"},
wantError: false,
},
{
name: "filter pipelineruns by label",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "-n", "namespace", "--labels", "viva=wakanda"},
args: []string{"list", "-n", "namespace", "--label", "viva=wakanda"},
wantError: false,
},
{
name: "no mixing pipelinename and labels",
name: "no mixing pipelinename and label",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "-n", "namespace", "--labels", "viva=wakanda", "pr3-1"},
args: []string{"list", "-n", "namespace", "--label", "viva=wakanda", "pr3-1"},
wantError: true,
},

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NAME STARTED DURATION STATUS
pr3-1 --- --- ---
pr2-2 2 hours ago 1 minute Failed
26 changes: 20 additions & 6 deletions pkg/cmd/taskrun/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
trhsort "github.com/tektoncd/cli/pkg/helper/taskrun/sort"
validate "github.com/tektoncd/cli/pkg/helper/validate"
"github.com/tektoncd/cli/pkg/helper/validate"
"github.com/tektoncd/cli/pkg/printer"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -37,7 +37,8 @@ const (
)

type ListOptions struct {
Limit int
Limit int
LabelSelector string
}

func listCommand(p cli.Params) *cobra.Command {
Expand Down Expand Up @@ -77,7 +78,7 @@ List all TaskRuns of Task 'foo' in namespace 'bar':
return nil
}

trs, err := list(p, task, opts.Limit)
trs, err := list(p, task, opts.Limit, opts.LabelSelector)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list taskruns from %s namespace \n", p.Namespace())
return err
Expand Down Expand Up @@ -121,20 +122,33 @@ List all TaskRuns of Task 'foo' in namespace 'bar':

f.AddFlags(c)
c.Flags().IntVarP(&opts.Limit, "limit", "", 0, "limit taskruns listed (default: return all taskruns)")
c.Flags().StringVarP(&opts.LabelSelector, "label", "", opts.LabelSelector, "A selector (label query) to filter on, supports '=', '==', and '!='")

return c
}

func list(p cli.Params, task string, limit int) (*v1alpha1.TaskRunList, error) {
func list(p cli.Params, task string, limit int, labelselector string) (*v1alpha1.TaskRunList, error) {
var selector string
var options v1.ListOptions

cs, err := p.Clients()
if err != nil {
return nil, err
}

options := v1.ListOptions{}
if task != "" && labelselector != "" {
return nil, fmt.Errorf("specifying a task and labels are not compatible")
}

if task != "" {
selector = fmt.Sprintf("tekton.dev/task=%s", task)
} else if labelselector != "" {
selector = labelselector
}

if selector != "" {
options = v1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/task=%s", task),
LabelSelector: selector,
}
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/cmd/taskrun/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestListTaskRuns(t *testing.T) {
),
tb.TaskRun("tr2-2", "foo",
tb.TaskRunLabel("tekton.dev/Task", "random"),
tb.TaskRunLabel("pot", "nutella"),
tb.TaskRunSpec(tb.TaskRunTaskRef("random")),
tb.TaskRunStatus(
tb.StatusCondition(apis.Condition{
Expand All @@ -86,6 +87,7 @@ func TestListTaskRuns(t *testing.T) {
),
tb.TaskRun("tr3-1", "foo",
tb.TaskRunLabel("tekton.dev/Task", "random"),
tb.TaskRunLabel("pot", "honey"),
tb.TaskRunSpec(tb.TaskRunTaskRef("random")),
tb.TaskRunStatus(
tb.StatusCondition(apis.Condition{
Expand Down Expand Up @@ -174,6 +176,24 @@ func TestListTaskRuns(t *testing.T) {
args: []string{"list", "-n", "invalid"},
wantError: true,
},
{
name: "filter taskruns by label",
command: command(t, trs, now, ns),
args: []string{"list", "-n", "foo", "--label", "pot=honey"},
wantError: false,
},
{
name: "filter taskruns by label with in query",
command: command(t, trs, now, ns),
args: []string{"list", "-n", "foo", "--label", "pot in (honey,nutella)"},
wantError: false,
},
{
name: "no mixing pipelinename and labels",
command: command(t, trs, now, ns),
args: []string{"list", "-n", "foo", "--label", "honey=nutella", "tr3-1"},
wantError: true,
},
}

for _, td := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME STARTED DURATION STATUS
tr3-1 --- --- Failed
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NAME STARTED DURATION STATUS
tr3-1 --- --- Failed
tr2-2 59 minutes ago 1 minute Failed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: specifying a task and labels are not compatible

0 comments on commit 17007d3

Please sign in to comment.