Skip to content
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

Add Check for Timeout Status for PipelineRun and TaskRun Cancel #1124

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/cmd/pipelinerun/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import (
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/pipelinerun"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
succeeded = formatted.ColorStatus("Succeeded")
failed = formatted.ColorStatus("Failed")
prCancelled = formatted.ColorStatus("Cancelled") + "(PipelineRunCancelled)"
prTimeout = formatted.ColorStatus("Failed") + "(" + v1beta1.PipelineRunReasonTimedOut.String() + ")"
)

func cancelCommand(p cli.Params) *cobra.Command {
Expand Down Expand Up @@ -73,7 +75,7 @@ func cancelPipelineRun(p cli.Params, s *cli.Stream, prName string) error {
}

prCond := formatted.Condition(pr.Status.Conditions)
if prCond == succeeded || prCond == failed || prCond == prCancelled {
if prCond == succeeded || prCond == failed || prCond == prCancelled || prCond == prTimeout {
return fmt.Errorf("failed to cancel PipelineRun %s: PipelineRun has already finished execution", prName)
}

Expand Down
85 changes: 85 additions & 0 deletions pkg/cmd/pipelinerun/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,3 +727,88 @@ func Test_finished_pipelinerun_cancel_v1beta1(t *testing.T) {
expected := "Error: failed to cancel PipelineRun " + prName + ": PipelineRun has already finished execution\n"
tu.AssertOutput(t, expected, got)
}

func Test_finished_pipelinerun_timeout_v1beta1(t *testing.T) {
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

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

prs := []*v1beta1.PipelineRun{
{
ObjectMeta: metav1.ObjectMeta{
Name: prName,
Namespace: "ns",
Labels: map[string]string{"tekton.dev/pipeline": "pipelineName"},
},
Spec: v1beta1.PipelineRunSpec{
PipelineRef: &v1beta1.PipelineRef{
Name: "pipelineName",
},
ServiceAccountName: "test-sa",
Resources: []v1beta1.PipelineResourceBinding{
{
Name: "git-repo",
ResourceRef: &v1beta1.PipelineResourceRef{
Name: "some-repo",
},
},
{
Name: "build-image",
ResourceRef: &v1beta1.PipelineResourceRef{
Name: "some-image",
},
},
},
Params: []v1beta1.Param{
{
Name: "pipeline-param-1",
Value: v1beta1.ArrayOrString{
Type: v1beta1.ParamTypeString,
StringVal: "somethingmorefun",
},
},
{
Name: "rev-param",
Value: v1beta1.ArrayOrString{
Type: v1beta1.ParamTypeString,
StringVal: "revision1",
},
},
},
},
Status: v1beta1.PipelineRunStatus{
Status: duckv1beta1.Status{
Conditions: duckv1beta1.Conditions{
{
Status: corev1.ConditionFalse,
Reason: v1beta1.PipelineRunReasonTimedOut.String(),
},
},
},
},
},
}

cs, _ := test.SeedV1beta1TestData(t, pipelinev1beta1test.Data{PipelineRuns: prs, Namespaces: ns})
cs.Pipeline.Resources = cb.APIResourceList(versionB1, []string{"pipeline", "pipelinerun"})
tdc := testDynamic.Options{}
dc, err := tdc.Client(
cb.UnstructuredV1beta1PR(prs[0], versionB1),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
}
p := &tu.Params{Tekton: cs.Pipeline, Kube: cs.Kube, Dynamic: dc}

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

expected := "Error: failed to cancel PipelineRun " + prName + ": PipelineRun has already finished execution\n"
tu.AssertOutput(t, expected, got)
}
4 changes: 3 additions & 1 deletion pkg/cmd/taskrun/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import (
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/taskrun"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
succeeded = formatted.ColorStatus("Succeeded")
failed = formatted.ColorStatus("Failed")
taskrunCancelled = formatted.ColorStatus("Cancelled") + "(TaskRunCancelled)"
taskrunTimeout = formatted.ColorStatus("Failed") + "(" + v1beta1.TaskRunReasonTimedOut.String() + ")"
)

func cancelCommand(p cli.Params) *cobra.Command {
Expand Down Expand Up @@ -71,7 +73,7 @@ func cancelTaskRun(p cli.Params, s *cli.Stream, trName string) error {
}

taskrunCond := formatted.Condition(tr.Status.Conditions)
if taskrunCond == succeeded || taskrunCond == failed || taskrunCond == taskrunCancelled {
if taskrunCond == succeeded || taskrunCond == failed || taskrunCond == taskrunCancelled || taskrunCond == taskrunTimeout {
return fmt.Errorf("failed to cancel TaskRun %s: TaskRun has already finished execution", trName)
}

Expand Down
35 changes: 33 additions & 2 deletions pkg/cmd/taskrun/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ func TestTaskRunCancel_v1beta1(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "taskrun-2",
Labels: map[string]string{"tekton.dev/task": "failure-task"},
Labels: map[string]string{"tekton.dev/task": "success-task"},
},
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{
Name: "failure-task",
Name: "success-task",
},
},
Status: v1beta1.TaskRunStatus{
Expand All @@ -271,6 +271,28 @@ func TestTaskRunCancel_v1beta1(t *testing.T) {
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "taskrun-3",
Labels: map[string]string{"tekton.dev/task": "timeout-task"},
},
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{
Name: "timeout-task",
},
},
Status: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: duckv1beta1.Conditions{
{
Status: corev1.ConditionFalse,
Reason: v1beta1.TaskRunReasonTimedOut.String(),
},
},
},
},
},
}

trs2 := []*v1beta1.TaskRun{
Expand Down Expand Up @@ -345,6 +367,7 @@ func TestTaskRunCancel_v1beta1(t *testing.T) {
dc, err := tdc.Client(
cb.UnstructuredV1beta1TR(trs[0], versionB1),
cb.UnstructuredV1beta1TR(trs[1], versionB1),
cb.UnstructuredV1beta1TR(trs[2], versionB1),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
Expand Down Expand Up @@ -435,6 +458,14 @@ func TestTaskRunCancel_v1beta1(t *testing.T) {
wantError: true,
want: "failed to cancel TaskRun cancel-taskrun-1: TaskRun has already finished execution",
},
{
name: "Failed canceling taskrun that timed out",
command: []string{"cancel", "taskrun-3", "-n", "ns"},
dynamic: seeds[0].dynamicClient,
input: seeds[0].pipelineClient,
wantError: true,
want: "failed to cancel TaskRun taskrun-3: TaskRun has already finished execution",
},
}

for _, tp := range testParams {
Expand Down