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

Correct missing field(s) error #2295

Merged
merged 5 commits into from
Mar 27, 2020
Merged

Correct missing field(s) error #2295

merged 5 commits into from
Mar 27, 2020

Conversation

GregDritschler
Copy link
Contributor

Fixes #2235

Fixes #1844

Changes

As I mentioned in the issue, it is debatable whether a completely empty pipeline spec is an error since all fields are optional. On the other hand there is value in trying to catch mistakes. It's unlikely anyone wants to define a pipeline with nothing in it. So I changed the code to return the following error.

	if equality.Semantic.DeepEqual(ps, &PipelineSpec{}) {
		return apis.ErrGeneric("expected at least one, got none", "spec.description", "spec.params", "spec.resources", "spec.tasks", "spec.workspaces")
	}

field_error.go doesn't have any methods with canned text for this situation. I found uses of ErrGeneric with the text "expected at least one, got none" in some places under vendor/knative.dev and decided that was good text for this case. It produces the following error:

Error from server (BadRequest): error when creating "pipelinemissingspec1.yaml": admission webhook "validation.webhook.pipeline.tekton.dev" denied the request: validation failed: expected at least one, got none: spec.description, spec.params, spec.resources, spec.tasks, spec.workspaces

I looked for other resource validation methods that use apis.ErrMissingField(apis.CurrentField).

  • task_validation.go:
    Immediately after checking for an empty spec, the validation method checks if any steps are defined. Therefore the empty spec check is superfluous and I removed it.
  • pipelineresource_validation.go:
    I changed this one to specify the path that's required ("spec.type").
  • condition_validation.go:
    I could not reproduce the ErrMissingField error with condition. I left the code alone to be safe.
  • workspace_validation.go:
    I could not reproduce the ErrMissingField error with workspace. I left the code alone to be safe.

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

See the contribution guide for more details.

Double check this list of stuff that's easy to miss:

Reviewer Notes

If API changes are included, additive changes must be approved by at least two OWNERS and backwards incompatible changes must be approved by more than 50% of the OWNERS, and they must first be added in a backwards compatible way.

@googlebot googlebot added the cla: yes Trying to make the CLA bot happy with ppl from different companies work on one commit label Mar 26, 2020
@tekton-robot tekton-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 26, 2020
@tekton-robot
Copy link
Collaborator

Hi @GregDritschler. Thanks for your PR.

I'm waiting for a tektoncd member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@tekton-robot tekton-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Mar 26, 2020
@vdemeester
Copy link
Member

/ok-to-test

@tekton-robot tekton-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 26, 2020
@tekton-robot
Copy link
Collaborator

The following is the coverage report on pkg/.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1alpha1/pipeline_validation.go 96.3% 97.1% 0.7
pkg/apis/pipeline/v1alpha1/task_validation.go 83.9% 83.7% -0.2
pkg/apis/pipeline/v1beta1/pipeline_validation.go 93.8% 94.5% 0.8
pkg/apis/pipeline/v1beta1/task_validation.go 83.6% 83.3% -0.2
pkg/apis/resource/v1alpha1/pipelineresource_validation.go 86.8% 88.7% 1.9

@GregDritschler
Copy link
Contributor Author

/retest

@tekton-robot
Copy link
Collaborator

The following is the coverage report on pkg/.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1alpha1/pipeline_validation.go 96.3% 97.1% 0.7
pkg/apis/pipeline/v1alpha1/task_validation.go 83.9% 83.7% -0.2
pkg/apis/pipeline/v1beta1/pipeline_validation.go 93.8% 94.5% 0.8
pkg/apis/pipeline/v1beta1/task_validation.go 83.6% 83.3% -0.2
pkg/apis/resource/v1alpha1/pipelineresource_validation.go 86.8% 88.7% 1.9

@GregDritschler
Copy link
Contributor Author

/retest

@tekton-robot
Copy link
Collaborator

The following is the coverage report on pkg/.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1alpha1/pipeline_validation.go 96.3% 97.1% 0.7
pkg/apis/pipeline/v1alpha1/task_validation.go 83.9% 83.7% -0.2
pkg/apis/pipeline/v1beta1/pipeline_validation.go 93.8% 94.5% 0.8
pkg/apis/pipeline/v1beta1/task_validation.go 83.6% 83.3% -0.2
pkg/apis/resource/v1alpha1/pipelineresource_validation.go 86.8% 88.7% 1.9

@GregDritschler
Copy link
Contributor Author

/retest

@tekton-robot
Copy link
Collaborator

The following is the coverage report on pkg/.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1alpha1/pipeline_validation.go 96.3% 97.1% 0.7
pkg/apis/pipeline/v1alpha1/task_validation.go 83.9% 83.7% -0.2
pkg/apis/pipeline/v1beta1/pipeline_validation.go 93.8% 94.5% 0.8
pkg/apis/pipeline/v1beta1/task_validation.go 83.6% 83.3% -0.2
pkg/apis/resource/v1alpha1/pipelineresource_validation.go 86.8% 88.7% 1.9

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/meow

@tekton-robot
Copy link
Collaborator

@vdemeester: cat image

In response to this:

/meow

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@tekton-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: vdemeester

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 27, 2020
@vdemeester
Copy link
Member

/cc @sbwsg
Do we want this in 0.11 ? 👼

@tekton-robot tekton-robot requested a review from a user March 27, 2020 07:55
@ghost ghost added the needs-cherry-pick Indicates a PR needs to be cherry-pick to a release branch label Mar 27, 2020
@ghost
Copy link

ghost commented Mar 27, 2020

sure thing! added cherry pick label.

@@ -285,7 +285,7 @@ func TestTaskSpecValidateError(t *testing.T) {
name: "nil",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know this isn't part of the diff but it's incorrectly named - the Task Spec does not end up being nil in this test. It ends up being a valid pointer to an empty TaskSpec object.

It doesn't look like we exercise the nil case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the name.

@@ -40,9 +39,6 @@ func (t *Task) Validate(ctx context.Context) *apis.FieldError {
}

func (ts *TaskSpec) Validate(ctx context.Context) *apis.FieldError {
if equality.Semantic.DeepEqual(ts, &TaskSpec{}) {
return apis.ErrMissingField(apis.CurrentField)
}

if len(ts.Steps) == 0 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen if ts is a nil pointer? Is there protection against that elsewhere in the codebase that will prevent it from happening?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way the code is now, the callers of the validate methods appear to be responsible for checking for nil. For example in taskrun_validation:

	// Validate TaskSpec if it's present
	if ts.TaskSpec != nil {
		if err := ts.TaskSpec.Validate(ctx); err != nil {
			return err
		}
	}

I haven't seen a nil check inside any of the validation methods I've looked at.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is always the case though - see Task.Validate a few lines above this one:

func (t *Task) Validate(ctx context.Context) *apis.FieldError {
	if err := validate.ObjectMetadata(t.GetObjectMeta()); err != nil {
		return err.ViaField("metadata")
	}
	return t.Spec.Validate(ctx)
}

I'm fine to remove the check in TaskSpec.Validate if we're absolutely sure that there won't be usages. But if we're cherry-picking this into the 0.11 branch just before we cut the beta and we're not 100% sure then I'd prefer we postpone this change to another PR. WDYT?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Also, I believe - though I could be completely wrong! - that equality.Semantic.DeepEqual will actually consider a nil pointer and &TaskSpec{} as semantically equal, so this would return an error if there was a nil pointer. We should have unit testing around this.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the first thing I did was to check if the DeepEqual check was catching a nil. I put the check back and forced ts to be nil. It fell through the DeepEqual check and panicked on the ts.Steps reference. So I don't believe I'm introducing an exposure. It was there before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(fwiw I don't think this is fixing an urgent problem so it's fine to postpone merging it to post 0.11)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 understood, thanks for testing the nil case and correcting my assumption here.

@GregDritschler
Copy link
Contributor Author

/retest

@tekton-robot
Copy link
Collaborator

The following is the coverage report on pkg/.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1alpha1/pipeline_validation.go 96.3% 97.1% 0.7
pkg/apis/pipeline/v1alpha1/task_validation.go 83.9% 83.7% -0.2
pkg/apis/pipeline/v1beta1/pipeline_validation.go 93.8% 94.5% 0.8
pkg/apis/pipeline/v1beta1/task_validation.go 83.6% 83.3% -0.2
pkg/apis/resource/v1alpha1/pipelineresource_validation.go 86.8% 88.7% 1.9

@@ -365,10 +365,10 @@ func TestTaskSpecValidateError(t *testing.T) {
fields fields
expectedError apis.FieldError
}{{
name: "nil",
name: "empty spec",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 🙏 thankyou!

@ghost
Copy link

ghost commented Mar 27, 2020

/lgtm

@tekton-robot tekton-robot assigned ghost Mar 27, 2020
@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Mar 27, 2020
@tekton-robot tekton-robot merged commit 0b3a8b1 into tektoncd:master Mar 27, 2020
@ghost ghost removed the needs-cherry-pick Indicates a PR needs to be cherry-pick to a release branch label Mar 30, 2020
@afrittoli afrittoli added the kind/bug Categorizes issue or PR as related to a bug. label Apr 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Trying to make the CLA bot happy with ppl from different companies work on one commit kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
5 participants