-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Conversation
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 Once the patch is verified, the new status will be reflected by the 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. |
/ok-to-test |
The following is the coverage report on pkg/.
|
/retest |
The following is the coverage report on pkg/.
|
/retest |
The following is the coverage report on pkg/.
|
/retest |
The following is the coverage report on pkg/.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/meow
In response to this:
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. |
[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 |
/cc @sbwsg |
sure thing! added cherry pick label. |
@@ -285,7 +285,7 @@ func TestTaskSpecValidateError(t *testing.T) { | |||
name: "nil", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
/retest |
The following is the coverage report on pkg/.
|
@@ -365,10 +365,10 @@ func TestTaskSpecValidateError(t *testing.T) { | |||
fields fields | |||
expectedError apis.FieldError | |||
}{{ | |||
name: "nil", | |||
name: "empty spec", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice 🙏 thankyou!
/lgtm |
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.
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:
I looked for other resource validation methods that use apis.ErrMissingField(apis.CurrentField).
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.
I changed this one to specify the path that's required ("spec.type").
I could not reproduce the ErrMissingField error with condition. I left the code alone to be safe.
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:
cmd
dir, please updatethe release Task to build and release this image.
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.