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

Clarify error message for PipelineRun alpha fields #5045

Merged
merged 1 commit into from
Jul 1, 2022
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
37 changes: 14 additions & 23 deletions pkg/apis/pipeline/v1beta1/pipelinerun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"time"

"github.com/tektoncd/pipeline/pkg/apis/config"
apisconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/validate"
"github.com/tektoncd/pipeline/pkg/apis/version"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)
Expand Down Expand Up @@ -72,8 +72,6 @@ func (ps *PipelineRunSpec) Validate(ctx context.Context) (errs *apis.FieldError)
}
}

// This is an alpha feature and will fail validation if it's used in a pipelinerun spec
// when the enable-api-fields feature gate is anything but "alpha".
if ps.Timeouts != nil {
if ps.Timeout != nil {
// can't have both at the same time
Expand Down Expand Up @@ -153,7 +151,7 @@ func (ps *PipelineRunSpec) validatePipelineTimeout(timeout time.Duration, errorM
if ps.Timeouts.Tasks.Duration > timeout {
tasksTimeoutErr = true
}
if ps.Timeouts.Tasks.Duration == apisconfig.NoTimeoutDuration && timeout != apisconfig.NoTimeoutDuration {
if ps.Timeouts.Tasks.Duration == config.NoTimeoutDuration && timeout != config.NoTimeoutDuration {
tasksTimeoutErr = true
tasksTimeoutStr += " (no timeout)"
}
Expand All @@ -168,7 +166,7 @@ func (ps *PipelineRunSpec) validatePipelineTimeout(timeout time.Duration, errorM
if ps.Timeouts.Finally.Duration > timeout {
finallyTimeoutErr = true
}
if ps.Timeouts.Finally.Duration == apisconfig.NoTimeoutDuration && timeout != apisconfig.NoTimeoutDuration {
if ps.Timeouts.Finally.Duration == config.NoTimeoutDuration && timeout != config.NoTimeoutDuration {
finallyTimeoutErr = true
finallyTimeoutStr += " (no timeout)"
}
Expand All @@ -187,24 +185,17 @@ func (ps *PipelineRunSpec) validatePipelineTimeout(timeout time.Duration, errorM
}

func validateTaskRunSpec(ctx context.Context, trs PipelineTaskRunSpec) (errs *apis.FieldError) {
cfg := config.FromContextOrDefaults(ctx)
if cfg.FeatureFlags.EnableAPIFields == config.AlphaAPIFields {
if trs.StepOverrides != nil {
errs = errs.Also(validateStepOverrides(trs.StepOverrides).ViaField("stepOverrides"))
}
if trs.SidecarOverrides != nil {
errs = errs.Also(validateSidecarOverrides(trs.SidecarOverrides).ViaField("sidecarOverrides"))
}
if trs.ComputeResources != nil {
errs = errs.Also(validateTaskRunComputeResources(trs.ComputeResources, trs.StepOverrides))
}
} else {
if trs.StepOverrides != nil {
errs = errs.Also(apis.ErrDisallowedFields("stepOverrides"))
}
if trs.SidecarOverrides != nil {
errs = errs.Also(apis.ErrDisallowedFields("sidecarOverrides"))
}
if trs.StepOverrides != nil {
errs = errs.Also(version.ValidateEnabledAPIFields(ctx, "stepOverrides", config.AlphaAPIFields).ViaField("stepOverrides"))
errs = errs.Also(validateStepOverrides(trs.StepOverrides).ViaField("stepOverrides"))
}
if trs.SidecarOverrides != nil {
errs = errs.Also(version.ValidateEnabledAPIFields(ctx, "sidecarOverrides", config.AlphaAPIFields).ViaField("sidecarOverrides"))
errs = errs.Also(validateSidecarOverrides(trs.SidecarOverrides).ViaField("sidecarOverrides"))
}
if trs.ComputeResources != nil {
errs = errs.Also(version.ValidateEnabledAPIFields(ctx, "computeResources", config.AlphaAPIFields).ViaField("computeResources"))
errs = errs.Also(validateTaskRunComputeResources(trs.ComputeResources, trs.StepOverrides))
}
return errs
}
18 changes: 16 additions & 2 deletions pkg/apis/pipeline/v1beta1/pipelinerun_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func TestPipelineRunSpec_Invalidate(t *testing.T) {
},
},
},
wantErr: apis.ErrDisallowedFields("stepOverrides").ViaIndex(0).ViaField("taskRunSpecs"),
wantErr: apis.ErrGeneric("stepOverrides requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\"").ViaIndex(0).ViaField("taskRunSpecs"),
}, {
name: "sidecarOverride disallowed without alpha feature gate",
spec: v1beta1.PipelineRunSpec{
Expand All @@ -560,7 +560,7 @@ func TestPipelineRunSpec_Invalidate(t *testing.T) {
},
},
},
wantErr: apis.ErrDisallowedFields("sidecarOverrides").ViaIndex(0).ViaField("taskRunSpecs"),
wantErr: apis.ErrGeneric("sidecarOverrides requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\"").ViaIndex(0).ViaField("taskRunSpecs"),
}, {
name: "missing stepOverride name",
spec: v1beta1.PipelineRunSpec{
Expand Down Expand Up @@ -634,6 +634,20 @@ func TestPipelineRunSpec_Invalidate(t *testing.T) {
"taskRunSpecs[0].computeResources",
),
withContext: enableAlphaAPIFields,
}, {
name: "computeResources disallowed without alpha feature gate",
spec: v1beta1.PipelineRunSpec{
PipelineRef: &v1beta1.PipelineRef{Name: "foo"},
TaskRunSpecs: []v1beta1.PipelineTaskRunSpec{
{
PipelineTaskName: "bar",
ComputeResources: &corev1.ResourceRequirements{
Requests: corev1.ResourceList{corev1.ResourceMemory: corev1resources.MustParse("2Gi")},
},
},
},
},
wantErr: apis.ErrGeneric("computeResources requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\"").ViaIndex(0).ViaField("taskRunSpecs"),
}}

for _, ps := range tests {
Expand Down
5 changes: 4 additions & 1 deletion pkg/apis/pipeline/v1beta1/taskrun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ func (ts *TaskRunSpec) Validate(ctx context.Context) (errs *apis.FieldError) {
if ts.StepOverrides != nil {
errs = errs.Also(version.ValidateEnabledAPIFields(ctx, "stepOverrides", config.AlphaAPIFields).ViaField("stepOverrides"))
errs = errs.Also(validateStepOverrides(ts.StepOverrides).ViaField("stepOverrides"))
errs = errs.Also(validateTaskRunComputeResources(ts.ComputeResources, ts.StepOverrides))
}
if ts.SidecarOverrides != nil {
errs = errs.Also(version.ValidateEnabledAPIFields(ctx, "sidecarOverrides", config.AlphaAPIFields).ViaField("sidecarOverrides"))
errs = errs.Also(validateSidecarOverrides(ts.SidecarOverrides).ViaField("sidecarOverrides"))
}
if ts.ComputeResources != nil {
errs = errs.Also(version.ValidateEnabledAPIFields(ctx, "computeResources", config.AlphaAPIFields).ViaField("computeResources"))
errs = errs.Also(validateTaskRunComputeResources(ts.ComputeResources, ts.StepOverrides))
}

if ts.Status != "" {
if ts.Status != TaskRunSpecStatusCancelled {
Expand Down
13 changes: 13 additions & 0 deletions pkg/apis/pipeline/v1beta1/taskrun_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,19 @@ func TestTaskRunSpec_Invalidate(t *testing.T) {
"computeResources",
),
wc: enableAlphaAPIFields,
}, {
name: "computeResources disallowed without alpha feature gate",
spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{
Name: "foo",
},
ComputeResources: &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: corev1resources.MustParse("2Gi"),
},
},
},
wantErr: apis.ErrGeneric("computeResources requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\""),
}}

for _, ts := range tests {
Expand Down