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 committed May 4, 2020
1 parent 7bbc7eb commit 5fcf3e6
Show file tree
Hide file tree
Showing 2 changed files with 25 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 names
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
16 changes: 14 additions & 2 deletions pkg/apis/pipeline/v1alpha1/condition_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ 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"
)
Expand All @@ -41,7 +40,8 @@ func TestCondition_Validate(t *testing.T) {
}

func TestCondition_Invalidate(t *testing.T) {
tcs := []struct {
tDetails :=
cs := []struct {
name string
cond *v1alpha1.Condition
expectedError apis.FieldError
Expand Down Expand Up @@ -72,6 +72,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 5fcf3e6

Please sign in to comment.