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 validation for condition check name #2528

Merged
merged 1 commit into from
May 5, 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
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