-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add cancel the pipelinerun feature
- Loading branch information
1 parent
86c950c
commit c621535
Showing
13 changed files
with
254 additions
and
16 deletions.
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
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,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 | ||
|
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
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,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 |
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
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,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 | ||
} |
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,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) | ||
} |
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