Skip to content

Commit

Permalink
feat: Add cancel the pipelinerun feature
Browse files Browse the repository at this point in the history
  • Loading branch information
khrm authored and tekton-robot committed Aug 19, 2019
1 parent 86c950c commit c621535
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 16 deletions.
10 changes: 5 additions & 5 deletions docs/cmd/tkn_pipeline_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ Show pipeline logs
### Examples


# show logs interactively for no inputs
# interactive mode: shows logs of the selected pipeline run
tkn pipeline logs -n namespace

# show logs interactively for given pipeline
tkn pipeline logs pipeline_name -n namespace
# interactive mode: shows logs of the selected pipelinerun of the given pipeline
tkn pipeline logs pipeline -n namespace

# show logs of given pipeline for last run
tkn pipeline logs pipeline_name -n namespace --last
tkn pipeline logs pipeline -n namespace --last

# show logs for given pipeline and pipelinerun
tkn pipeline logs pipeline_name pipelinerun_name -n namespace
tkn pipeline logs pipeline run -n namespace



Expand Down
1 change: 1 addition & 0 deletions docs/cmd/tkn_pipelinerun.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Manage pipelineruns
### SEE ALSO

* [tkn](tkn.md) - CLI for tekton pipelines
* [tkn pipelinerun cancel](tkn_pipelinerun_cancel.md) - Cancel the PipelineRun
* [tkn pipelinerun describe](tkn_pipelinerun_describe.md) - Describe a pipelinerun in a namespace
* [tkn pipelinerun list](tkn_pipelinerun_list.md) - Lists pipelineruns in a namespace
* [tkn pipelinerun logs](tkn_pipelinerun_logs.md) - Show the logs of PipelineRun
Expand Down
38 changes: 38 additions & 0 deletions docs/cmd/tkn_pipelinerun_cancel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## tkn pipelinerun cancel

Cancel the PipelineRun

### Usage

```
tkn pipelinerun cancel pipelinerunName
```

### Synopsis

Cancel the PipelineRun

### Examples


# cancel the PipelineRun named "foo" from the namespace "bar"
tkn pipelinerun cancel foo -n bar


### Options

```
-h, --help help for cancel
```

### Options inherited from parent commands

```
-k, --kubeconfig string kubectl config file (default: $HOME/.kube/config)
-n, --namespace string namespace to use (default: from $KUBECONFIG)
```

### SEE ALSO

* [tkn pipelinerun](tkn_pipelinerun.md) - Manage pipelineruns

1 change: 0 additions & 1 deletion docs/cmd/tkn_task_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Lists tasks in a namespace
```
--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
-l, --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
1 change: 1 addition & 0 deletions docs/cmd/tkn_taskrun_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tkn tr list -n foo \n",
```
--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
-l, --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
10 changes: 5 additions & 5 deletions docs/man/man1/tkn-pipeline-logs.1
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ Show pipeline logs

.SH EXAMPLE
.PP
# show logs interactively for no inputs
# interactive mode: shows logs of the selected pipeline run
tkn pipeline logs \-n namespace

.PP
# show logs interactively for given pipeline
tkn pipeline logs pipeline\_name \-n namespace
# interactive mode: shows logs of the selected pipelinerun of the given pipeline
tkn pipeline logs pipeline \-n namespace

.PP
# show logs of given pipeline for last run
tkn pipeline logs pipeline\_name \-n namespace \-\-last
tkn pipeline logs pipeline \-n namespace \-\-last

.PP
# show logs for given pipeline and pipelinerun
tkn pipeline logs pipeline\_name pipelinerun\_name \-n namespace
tkn pipeline logs pipeline run \-n namespace


.SH SEE ALSO
Expand Down
45 changes: 45 additions & 0 deletions docs/man/man1/tkn-pipelinerun-cancel.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
.TH "TKN\-PIPELINERUN\-CANCEL" "1" "Aug 2019" "Auto generated by spf13/cobra" ""
.nh
.ad l


.SH NAME
.PP
tkn\-pipelinerun\-cancel \- Cancel the PipelineRun


.SH SYNOPSIS
.PP
\fBtkn pipelinerun cancel pipelinerunName\fP


.SH DESCRIPTION
.PP
Cancel the PipelineRun


.SH OPTIONS
.PP
\fB\-h\fP, \fB\-\-help\fP[=false]
help for cancel


.SH OPTIONS INHERITED FROM PARENT COMMANDS
.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)


.SH EXAMPLE
.PP
# cancel the PipelineRun named "foo" from the namespace "bar"
tkn pipelinerun cancel foo \-n bar


.SH SEE ALSO
.PP
\fBtkn\-pipelinerun(1)\fP
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-pipelinerun-list.1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Lists pipelineruns in a namespace
\fB\-h\fP, \fB\-\-help\fP[=false]
help for list

.PP
\fB\-l\fP, \fB\-\-limit\fP=0
limit pipelineruns listed (default: return all pipelineruns)

.PP
\fB\-o\fP, \fB\-\-output\fP=""
Output format. One of: json|yaml|name|go\-template|go\-template\-file|template|templatefile|jsonpath|jsonpath\-file.
Expand Down
2 changes: 1 addition & 1 deletion docs/man/man1/tkn-pipelinerun.1
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ Manage pipelineruns

.SH SEE ALSO
.PP
\fBtkn(1)\fP, \fBtkn\-pipelinerun\-describe(1)\fP, \fBtkn\-pipelinerun\-list(1)\fP, \fBtkn\-pipelinerun\-logs(1)\fP
\fBtkn(1)\fP, \fBtkn\-pipelinerun\-cancel(1)\fP, \fBtkn\-pipelinerun\-describe(1)\fP, \fBtkn\-pipelinerun\-list(1)\fP, \fBtkn\-pipelinerun\-logs(1)\fP
6 changes: 5 additions & 1 deletion 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\-l\fP, \fB\-\-limit\fP=0
limit taskruns listed (default: return all taskruns)

.PP
\fB\-o\fP, \fB\-\-output\fP=""
Output format. One of: json|yaml|name|go\-template|go\-template\-file|template|templatefile|jsonpath|jsonpath\-file.
Expand Down Expand Up @@ -56,7 +60,7 @@ tkn taskrun list foo \-n bar

.SH List all taskruns in a namespaces 'foo'
.PP
tkn pr list \-n foo \\n",
tkn tr list \-n foo \\n",


.SH SEE ALSO
Expand Down
56 changes: 56 additions & 0 deletions pkg/cmd/pipelinerun/cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package pipelinerun

import (
"fmt"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func cancelCommand(p cli.Params) *cobra.Command {
eg := `
# cancel the PipelineRun named "foo" from the namespace "bar"
tkn pipelinerun cancel foo -n bar
`

c := &cobra.Command{
Use: "cancel pipelinerunName",
Short: "Cancel the PipelineRun",
Example: eg,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pr := args[0]
s := &cli.Stream{
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}
// opts.Streamer = pods.NewStream
return cancelPipelineRun(p, s, pr)
},
}
return c
}

func cancelPipelineRun(p cli.Params, s *cli.Stream, prName string) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("Failed to create tekton client")
}

pr, err := cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Get(prName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("Failed to find pipelinerun: %s", prName)
}

pr.Spec.Status = v1alpha1.PipelineRunSpecStatusCancelled
_, err = cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Update(pr)
if err != nil {
return fmt.Errorf("Failed to cancel pipelinerun: %s, err: %s", prName, err.Error())
}

fmt.Fprintf(s.Out, "Pipelinerun cancelled: %s\n", pr.Name)
return nil
}
87 changes: 87 additions & 0 deletions pkg/cmd/pipelinerun/cancel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package pipelinerun

import (
"errors"
"testing"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
"k8s.io/apimachinery/pkg/runtime"
k8stest "k8s.io/client-go/testing"

tu "github.com/tektoncd/cli/pkg/test"
)

func Test_cancel_pipelinerun(t *testing.T) {

prName := "test-pipeline-run-123"

prs := []*v1alpha1.PipelineRun{
tb.PipelineRun(prName, "ns",
tb.PipelineRunLabel("tekton.dev/pipeline", "pipelineName"),
tb.PipelineRunSpec("pipelineName",
tb.PipelineRunServiceAccount("test-sa"),
tb.PipelineRunResourceBinding("git-repo", tb.PipelineResourceBindingRef("some-repo")),
tb.PipelineRunResourceBinding("build-image", tb.PipelineResourceBindingRef("some-image")),
tb.PipelineRunParam("pipeline-param-1", "somethingmorefun"),
tb.PipelineRunParam("rev-param", "revision1"),
),
),
}

cs, _ := test.SeedTestData(t, test.Data{PipelineRuns: prs})
p := &tu.Params{Tekton: cs.Pipeline, Kube: cs.Kube}

pRun := Command(p)
got, _ := tu.ExecuteCommand(pRun, "cancel", prName, "-n", "ns")

expected := "Pipelinerun cancelled: " + prName + "\n"
tu.AssertOutput(t, expected, got)
}

func Test_cancel_pipelinerun_not_found(t *testing.T) {

prName := "test-pipeline-run-123"

cs, _ := test.SeedTestData(t, test.Data{})
p := &tu.Params{Tekton: cs.Pipeline, Kube: cs.Kube}

pRun := Command(p)
got, _ := tu.ExecuteCommand(pRun, "cancel", prName, "-n", "ns")

expected := "Error: Failed to find pipelinerun: " + prName + "\n"
tu.AssertOutput(t, expected, got)
}

func Test_cancel_pipelinerun_client_err(t *testing.T) {

prName := "test-pipeline-run-123"
errStr := "test generated error"

prs := []*v1alpha1.PipelineRun{
tb.PipelineRun(prName, "ns",
tb.PipelineRunLabel("tekton.dev/pipeline", "pipelineName"),
tb.PipelineRunSpec("pipelineName",
tb.PipelineRunServiceAccount("test-sa"),
tb.PipelineRunResourceBinding("git-repo", tb.PipelineResourceBindingRef("some-repo")),
tb.PipelineRunResourceBinding("build-image", tb.PipelineResourceBindingRef("some-image")),
tb.PipelineRunParam("pipeline-param-1", "somethingmorefun"),
tb.PipelineRunParam("rev-param", "revision1"),
),
),
}

cs, _ := test.SeedTestData(t, test.Data{PipelineRuns: prs})
p := &tu.Params{Tekton: cs.Pipeline, Kube: cs.Kube}

cs.Pipeline.PrependReactor("update", "pipelineruns", func(action k8stest.Action) (bool, runtime.Object, error) {
return true, nil, errors.New(errStr)
})

pRun := Command(p)
got, _ := tu.ExecuteCommand(pRun, "cancel", prName, "-n", "ns")

expected := "Error: Failed to cancel pipelinerun: " + prName + ", err: " + errStr + "\n"
tu.AssertOutput(t, expected, got)
}
9 changes: 6 additions & 3 deletions pkg/cmd/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ func Command(p cli.Params) *cobra.Command {
}

flags.AddTektonOptions(c)
c.AddCommand(listCommand(p))
c.AddCommand(logCommand(p))
c.AddCommand(describeCommand(p))
c.AddCommand(
describeCommand(p),
listCommand(p),
logCommand(p),
cancelCommand(p),
)

return c
}

0 comments on commit c621535

Please sign in to comment.