Skip to content

Commit

Permalink
Add validation for condition check name
Browse files Browse the repository at this point in the history
This will add validation for condition check name like the
one we are doing for task step name

Right now the condition is getting created with
invalid name format but when we use it in pipeline
its not working
  • Loading branch information
piyush-garg authored and tekton-robot committed May 5, 2020
1 parent acc5c22 commit 24b8ce7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 11 additions & 0 deletions pkg/apis/pipeline/v1alpha1/condition_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/apis/pipeline/v1alpha1/condition_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 24b8ce7

Please sign in to comment.