Skip to content

Commit e73b994

Browse files
committed
Add validation for condition check name
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
1 parent 7bbc7eb commit e73b994

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pkg/apis/pipeline/v1alpha1/condition_validation.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package v1alpha1
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"github.com/tektoncd/pipeline/pkg/apis/validate"
2324
"k8s.io/apimachinery/pkg/api/equality"
25+
"k8s.io/apimachinery/pkg/util/validation"
2426
"knative.dev/pkg/apis"
2527
)
2628

@@ -38,6 +40,15 @@ func (cs *ConditionSpec) Validate(ctx context.Context) *apis.FieldError {
3840
return apis.ErrMissingField(apis.CurrentField)
3941
}
4042

43+
// Validate condition check name
44+
if errs := validation.IsDNS1123Label(cs.Check.Name); cs.Check.Name != "" && len(errs) > 0 {
45+
return &apis.FieldError{
46+
Message: fmt.Sprintf("invalid value %q", cs.Check.Name),
47+
Paths: []string{"Check.name"},
48+
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",
49+
}
50+
}
51+
4152
if err := validateSteps([]Step{cs.Check}).ViaField("Check"); err != nil {
4253
return err
4354
}

pkg/apis/pipeline/v1alpha1/condition_validation_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/google/go-cmp/cmp"
2424
"github.com/google/go-cmp/cmp/cmpopts"
2525
"knative.dev/pkg/apis"
26-
2726
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
2827
tb "github.com/tektoncd/pipeline/test/builder"
2928
)
@@ -72,6 +71,18 @@ func TestCondition_Invalidate(t *testing.T) {
7271
Message: "step 0 script cannot be used with command",
7372
Paths: []string{"Spec.Check.script"},
7473
},
74+
}, {
75+
name: "condition with invalid check name",
76+
cond: tb.Condition("cond",
77+
tb.ConditionSpec(
78+
tb.ConditionSpecCheck("Cname", "image", tb.Command("exit 0")),
79+
tb.ConditionSpecCheckScript("echo foo"),
80+
)),
81+
expectedError: apis.FieldError{
82+
Message: "invalid value \"Cname\"",
83+
Paths: []string{"Spec.Check.name"},
84+
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",
85+
},
7586
}}
7687

7788
for _, tc := range tcs {

0 commit comments

Comments
 (0)