diff --git a/pkg/apis/pipeline/v1alpha1/condition_validation.go b/pkg/apis/pipeline/v1alpha1/condition_validation.go index 008d99e783d..dbafbba3a61 100644 --- a/pkg/apis/pipeline/v1alpha1/condition_validation.go +++ b/pkg/apis/pipeline/v1alpha1/condition_validation.go @@ -18,9 +18,11 @@ package v1alpha1 import ( "context" + "fmt" "github.com/tektoncd/pipeline/pkg/apis/validate" "k8s.io/apimachinery/pkg/api/equality" + "k8s.io/apimachinery/pkg/util/validation" "knative.dev/pkg/apis" ) @@ -38,6 +40,15 @@ func (cs *ConditionSpec) Validate(ctx context.Context) *apis.FieldError { return apis.ErrMissingField(apis.CurrentField) } + // Validate condition check name + if errs := validation.IsDNS1123Label(cs.Check.Name); cs.Check.Name != "" && len(errs) > 0 { + return &apis.FieldError{ + Message: fmt.Sprintf("invalid value %q", cs.Check.Name), + Paths: []string{"Check.name"}, + Details: "Condition check name must be a valid DNS Label, For more info refer to https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names", + } + } + if err := validateSteps([]Step{cs.Check}).ViaField("Check"); err != nil { return err } diff --git a/pkg/apis/pipeline/v1alpha1/condition_validation_test.go b/pkg/apis/pipeline/v1alpha1/condition_validation_test.go index 16ff633bed9..b875b5fab2e 100644 --- a/pkg/apis/pipeline/v1alpha1/condition_validation_test.go +++ b/pkg/apis/pipeline/v1alpha1/condition_validation_test.go @@ -22,10 +22,9 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "knative.dev/pkg/apis" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" tb "github.com/tektoncd/pipeline/test/builder" + "knative.dev/pkg/apis" ) func TestCondition_Validate(t *testing.T) { @@ -72,6 +71,18 @@ func TestCondition_Invalidate(t *testing.T) { Message: "step 0 script cannot be used with command", Paths: []string{"Spec.Check.script"}, }, + }, { + name: "condition with invalid check name", + cond: tb.Condition("cond", + tb.ConditionSpec( + tb.ConditionSpecCheck("Cname", "image", tb.Command("exit 0")), + tb.ConditionSpecCheckScript("echo foo"), + )), + expectedError: apis.FieldError{ + Message: "invalid value \"Cname\"", + Paths: []string{"Spec.Check.name"}, + Details: "Condition check name must be a valid DNS Label, For more info refer to https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names", + }, }} for _, tc := range tcs {