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

Enhance v1beta1 validation code for task 🍸 #3185

Merged

Conversation

vdemeester
Copy link
Member

@vdemeester vdemeester commented Sep 9, 2020

Changes

Opening this to gather some feedback.

This make a better use of knative.dev/pkg apis errors package in order
to make the code simpler, and the report better.

  • This allows reporting multiple validation error at once — so, one
    failure message (webhook error) would be more useful to the user.
  • This enhance the report as it displays more information, like
    resources.inputs[0].invalidsource.type instead of
    resources.inputs.source.type (the index appear, it's easier for the
    user to know where the error is).
  • This reduce if err != nil path, simplifying the code.

/hold

  • This needs more work (at least on this package)
  • Tests that depends on some of this may fail, I need to fix it
  • It's gonna be really useful on function like validateVariables — it's now used there 😉
  • This PR would only do the Task part (without moving too much code away), follow-up includes
    • applying this to the rest of the v1beta1 package
    • extract some code into more files (those are big files, lot's of stuff could be extracted) and more functions
    • make a pass on what is exported, and why

/cc @afrittoli @bobcatfish @sbwsg @nikhil-thomas

Signed-off-by: Vincent Demeester [email protected]

Submitter Checklist

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

  • Includes tests (if functionality changed/added)
  • Includes docs (if user facing)
  • Commit messages follow commit message best practices
  • Release notes block has been filled in or deleted (only if no user facing changes)

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.

Release Notes

Task validation now reports all the error at once, and is a bit more detailed.

@tekton-robot tekton-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 9, 2020
@vdemeester
Copy link
Member Author

/hold

@vdemeester
Copy link
Member Author

/kind cleanup

@tekton-robot tekton-robot added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Sep 9, 2020
@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/task_validation.go 84.7% 85.3% 0.6

@vdemeester vdemeester force-pushed the validation-errs-enhancements branch from 1c8ecef to 8cb3adc Compare September 10, 2020 09:56
@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/task_validation.go 84.7% 85.3% 0.6

@vdemeester vdemeester force-pushed the validation-errs-enhancements branch from 8cb3adc to 39a21ec Compare September 10, 2020 10:48
@@ -44,6 +44,22 @@ func ValidateVariable(name, value, prefix, locationName, path string, vars sets.
return nil
}

func ValidateVariableP(value, prefix string, vars sets.String) *apis.FieldError {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a temporary simplified function that would replace ValidateVariable. Didn't change ValidateVariable because it is used elswhere 🙃

@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/task_validation.go 84.7% 89.8% 5.1
pkg/substitution/substitution.go 60.4% 54.2% -6.1

@vdemeester vdemeester force-pushed the validation-errs-enhancements branch from 39a21ec to 772f289 Compare September 10, 2020 11:07
@tekton-robot tekton-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 10, 2020
return errs
}

func (p ParamSpec) ValidateType() *apis.FieldError {
Copy link
Member Author

Choose a reason for hiding this comment

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

This should move on a different _validation.go file 😉

Copy link
Member

Choose a reason for hiding this comment

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

yup would be great to move such functions 🙏

@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/task_validation.go 84.7% 94.2% 9.6
pkg/substitution/substitution.go 60.4% 43.8% -16.5

@vdemeester vdemeester force-pushed the validation-errs-enhancements branch from 772f289 to e75fa6b Compare September 10, 2020 11:24

func (tr TaskResult) Validate(_ context.Context) *apis.FieldError {
Copy link
Member Author

Choose a reason for hiding this comment

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

This might also go in a separate _validation.go file

@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/task_validation.go 84.7% 94.3% 9.7
pkg/substitution/substitution.go 60.4% 43.8% -16.5

@vdemeester vdemeester force-pushed the validation-errs-enhancements branch from e75fa6b to 2fa0253 Compare September 10, 2020 11:29
@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/task_validation.go 84.7% 94.3% 9.7
pkg/substitution/substitution.go 60.4% 43.8% -16.5

@vdemeester vdemeester force-pushed the validation-errs-enhancements branch from 2fa0253 to cfec788 Compare September 10, 2020 11:39
@vdemeester
Copy link
Member Author

Should be good to go for task_validation.go mostly. To see the "end" result, https://github.com/tektoncd/pipeline/blob/cfec788f7f4f562b8906630fe7316ae189b66047/pkg/apis/pipeline/v1beta1/task_validation.go

@vdemeester vdemeester changed the title Enhance v1beta1 validation code 🍸 Enhance v1beta1 validation code for task 🍸 Sep 10, 2020
@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/task_validation.go 84.7% 94.3% 9.7
pkg/substitution/substitution.go 60.4% 43.8% -16.5

@vdemeester vdemeester force-pushed the validation-errs-enhancements branch from cfec788 to 0ae1c0a Compare September 10, 2020 11:46
@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/task_validation.go 84.7% 94.3% 9.7
pkg/substitution/substitution.go 60.4% 43.8% -16.5

@vdemeester vdemeester force-pushed the validation-errs-enhancements branch from 0ae1c0a to 5764051 Compare September 10, 2020 12:43
@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/task_validation.go 84.7% 95.0% 10.3
pkg/substitution/substitution.go 60.4% 43.8% -16.5

@vdemeester vdemeester added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. and removed kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. labels Sep 11, 2020
@vdemeester
Copy link
Member Author

/hold cancel

@tekton-robot tekton-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 14, 2020
}
if err := validateTaskResources(tr.Outputs, "outputs"); err != nil {
return err
func (tr *TaskResources) Validate(ctx context.Context) (errs *apis.FieldError) {
Copy link
Member

Choose a reason for hiding this comment

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

pointer named return value 😞, could this result in reference to nil on line 30 or 31?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes and no, that's the beauty of it 😛 (and this part of knative.dev/pkg)
See https://pkg.go.dev/knative.dev/[email protected]/apis#FieldError.Also (implementation is here).

func (fe *FieldError) isEmpty() bool {
	if fe == nil {
		return true
	}
	return fe.Message == "" && fe.Details == "" && len(fe.errors) == 0 && len(fe.Paths) == 0
}

It won't ever panic, because it knows how to act on a nil struct 😉

Copy link
Member

@pritidesai pritidesai left a comment

Choose a reason for hiding this comment

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

Huge cleanup and improvement, error message are more meaningful with indices 🙏

@tekton-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: pritidesai

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 Sep 18, 2020
errs = errs.Also(validateTaskVariable(step.WorkingDir, prefix, vars).ViaField("workingDir"))
errs = errs.Also(validateTaskVariable(step.Script, prefix, vars).ViaField("script"))
for i, cmd := range step.Command {
errs = errs.Also(validateTaskVariable(cmd, prefix, vars).ViaFieldIndex("command", i))
Copy link
Member

Choose a reason for hiding this comment

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

NIT: could have been into a common function (line 296 - 308) which is also appearing above

Copy link
Contributor

@savitaashture savitaashture left a comment

Choose a reason for hiding this comment

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

/lgtm

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Sep 21, 2020
This make a better use of knative.dev/pkg apis errors package in order
to make the code simpler, *and* the report better.

- This allows reporting multiple validation error at once — so, one
  failure message (webhook error) would be more useful to the user.
- This enhance the report as it displays more information, like
  `resources.inputs[0].invalidsource.type` instead of
  `resources.inputs.source.type` (the index appear, it's easier for the
  user to know where the error is).
- This reduce `if err != nil` path, simplifying the code.

This only tackle this for task_validation, and should be followed-up
with other refactoring on the different `*_validation.go` files.

Signed-off-by: Vincent Demeester <[email protected]>
@vdemeester vdemeester force-pushed the validation-errs-enhancements branch from 5764051 to 7e08dc0 Compare September 21, 2020 08:39
@tekton-robot tekton-robot removed the lgtm Indicates that a PR is ready to be merged. label Sep 21, 2020
@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1beta1/task_validation.go 84.7% 95.0% 10.3
pkg/substitution/substitution.go 60.4% 43.8% -16.5

@savitaashture
Copy link
Contributor

/test pull-tekton-pipeline-integration-tests

1 similar comment
@ghost
Copy link

ghost commented Sep 21, 2020

/test pull-tekton-pipeline-integration-tests

@savitaashture
Copy link
Contributor

/lgtm

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Sep 21, 2020
@tekton-robot tekton-robot merged commit 7b2b258 into tektoncd:master Sep 21, 2020
@vdemeester vdemeester deleted the validation-errs-enhancements branch September 21, 2020 14:16
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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants