From 6c4f6b0f649aae168e86ca907b487ffd4243a061 Mon Sep 17 00:00:00 2001 From: pengli Date: Tue, 14 Apr 2020 10:14:35 +0800 Subject: [PATCH] Add validation for "cencel" of "taskrun" and "pipelinerun" --- .../pipeline/v1beta1/pipelinerun_validation.go | 6 ++++++ .../v1beta1/pipelinerun_validation_test.go | 14 ++++++++++++++ pkg/apis/pipeline/v1beta1/taskrun_validation.go | 6 ++++++ .../pipeline/v1beta1/taskrun_validation_test.go | 9 +++++++++ 4 files changed, 35 insertions(+) diff --git a/pkg/apis/pipeline/v1beta1/pipelinerun_validation.go b/pkg/apis/pipeline/v1beta1/pipelinerun_validation.go index 2d1a8ae53d5..7e75a294470 100644 --- a/pkg/apis/pipeline/v1beta1/pipelinerun_validation.go +++ b/pkg/apis/pipeline/v1beta1/pipelinerun_validation.go @@ -65,6 +65,12 @@ func (ps *PipelineRunSpec) Validate(ctx context.Context) *apis.FieldError { } } + if ps.Status != "" { + if ps.Status != PipelineRunSpecStatusCancelled { + return apis.ErrInvalidValue(fmt.Sprintf("%s should be %s", ps.Status, PipelineRunSpecStatusCancelled), "spec.status") + } + } + if ps.Workspaces != nil { wsNames := make(map[string]int) for idx, ws := range ps.Workspaces { diff --git a/pkg/apis/pipeline/v1beta1/pipelinerun_validation_test.go b/pkg/apis/pipeline/v1beta1/pipelinerun_validation_test.go index aa697fcd1ac..3d0927077f9 100644 --- a/pkg/apis/pipeline/v1beta1/pipelinerun_validation_test.go +++ b/pkg/apis/pipeline/v1beta1/pipelinerun_validation_test.go @@ -79,6 +79,20 @@ func TestPipelineRun_Invalidate(t *testing.T) { }, }, want: apis.ErrInvalidValue("-48h0m0s should be >= 0", "spec.timeout"), + }, { + name: "wrong pipelinerun cancel", + pr: v1beta1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pipelinelineName", + }, + Spec: v1beta1.PipelineRunSpec{ + PipelineRef: &v1beta1.PipelineRef{ + Name: "prname", + }, + Status: "PipelineRunCancell", + }, + }, + want: apis.ErrInvalidValue("PipelineRunCancell should be PipelineRunCancelled", "spec.status"), }, } diff --git a/pkg/apis/pipeline/v1beta1/taskrun_validation.go b/pkg/apis/pipeline/v1beta1/taskrun_validation.go index dc8fa70eff5..40be7f88a1e 100644 --- a/pkg/apis/pipeline/v1beta1/taskrun_validation.go +++ b/pkg/apis/pipeline/v1beta1/taskrun_validation.go @@ -72,6 +72,12 @@ func (ts *TaskRunSpec) Validate(ctx context.Context) *apis.FieldError { return err } + if ts.Status != "" { + if ts.Status != TaskRunSpecStatusCancelled { + return apis.ErrInvalidValue(fmt.Sprintf("%s should be %s", ts.Status, TaskRunSpecStatusCancelled), "spec.status") + } + } + if ts.Timeout != nil { // timeout should be a valid duration of at least 0. if ts.Timeout.Duration < 0 { diff --git a/pkg/apis/pipeline/v1beta1/taskrun_validation_test.go b/pkg/apis/pipeline/v1beta1/taskrun_validation_test.go index e2565887e8a..b3db60133e2 100644 --- a/pkg/apis/pipeline/v1beta1/taskrun_validation_test.go +++ b/pkg/apis/pipeline/v1beta1/taskrun_validation_test.go @@ -162,6 +162,15 @@ func TestTaskRunSpec_Invalidate(t *testing.T) { Timeout: &metav1.Duration{Duration: -48 * time.Hour}, }, wantErr: apis.ErrInvalidValue("-48h0m0s should be >= 0", "spec.timeout"), + }, { + name: "wrong taskrun cancel", + spec: v1beta1.TaskRunSpec{ + TaskRef: &v1beta1.TaskRef{ + Name: "taskrefname", + }, + Status: "TaskRunCancell", + }, + wantErr: apis.ErrInvalidValue("TaskRunCancell should be TaskRunCancelled", "spec.status"), }, { name: "invalid taskspec", spec: v1beta1.TaskRunSpec{