Commit Graph

31 Commits

Author SHA1 Message Date
Nghia Tran 728dfd8e28
Add FieldError#WrappedErrors to allow dependent code to have access (readonly) (#2618)
* Add FieldError#GetErrors to allow downstream reads

* Rename based on PR feedback

* Rename correctly for real

* Also expose `Normalized`

* WrappedErrors = merge(normalized)
2022-10-27 14:30:07 +00:00
Matt Moore 245166458e
Switch to iota/fmt.Stringer-based diagnostic levels. (#2511)
See discussion in: https://github.com/knative/pkg/pull/2498
2022-05-03 22:38:58 +00:00
Matt Moore 16b36b7fca
Add support for admission webhook warnings. (#2498)
* Add support for admission webhook warnings.

This extends `apis.FieldError` to support designating certain FieldErrors as "warnings" (or explicitly as "errors", however, this is the default for back-compat).

You can turn an `apis.FieldError` into a warning using: `fe.At(apis.WarningLevel)` or force it into an error using: `fe.At(apis.ErrorLevel)`.

You can get the errors at a particular diagnostic level using: `fe.Filter(apis.WarningLevel)`.

This change also hooks this into the admission webhook infrastructure to support surfacing the "warning" level `apis.FieldError`s via the `Warnings` section of the `AdmissionResponse`.

Fixes: #2497

* Add a comment about the use of defer.
2022-05-02 21:56:57 +00:00
Senthilnathan M 9a4b612820
Added invalid value field error with details (#2246)
* Added invalid value field with details

* gofmt

* Addressed review comments
2021-09-08 13:28:58 -07:00
Josh Soref b39d5da935
Spelling (#1797)
* spelling: adopted

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: aliased

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: apierrs

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: assignment

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: available

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: coexistence

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: commit

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: conversions

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: creates

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: custom

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: determine

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: different

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: distribution

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: duplicate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: editing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: endpoint

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: environment

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: generate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: implementation

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: identified

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: ignore

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: indicates

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: interface

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: interleaved

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: labels

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: label

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: mimic

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: namespaced

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: necessary

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: organization

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: populatable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: prometheus

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: refer

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: reference

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: repetitive

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: response

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: something

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: specable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: spoofing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: synchronized

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: this

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: trailing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unsupported

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: validation

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* chore: reviewdog go header boilerplate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-18 14:22:57 -07:00
Victor Agababov 247841408e
Apply the cleanup to the pkg (#1776)
* Apply the cleanup to the pkg

This is probably the final iteration of my cleanup application.

* codegen

* one more try
2020-10-05 21:41:20 -07:00
Victor Agababov ddf2cbe3c1
Various nits around the code (#1719)
* Various nits around the code

* stagger
2020-09-18 16:22:45 -07:00
Victor Agababov eb05e8dd5b
Various cleanups (#1446)
Thanks @markusthoemmes for the tool :)
2020-06-24 14:04:28 -07:00
Markus Thömmes 09d5e09da8
Assorted linting fixes. (#1443) 2020-06-24 12:11:27 -07:00
Victor Agababov 23f2fed590
Skip doing any work if `Also` receives no errors (#1256)
* Skip doing any work if Also receives no errors

1. if no errors are passed skip doing stuff (checks, allocations, etc)
2. if there's one error passed and it's empty (the same)
  - this is a quite popular pattern where in API validation we do
    `x.Also(validateY())` and if that succeeded then it'll be an empty
    error. Which is in reality most of the time.
3. Improve tests by better printing
4. better test coverage shows commutativity of Also(a, b) and
   Also(a).Also(b)

* fix flow
2020-04-28 12:03:51 -07:00
Kenjiro Nakayama 3415797394 Introduce error util ErrInvalidCombination for invalid combination (#638)
* Introduce error util ErrInvalidCombination for invalid combination

Sometimes valid value becomes invalid value by combination.

example 1. https://github.com/knative/serving/issues/5382
example 2. following combination in `spec.traffic`.

```
  traffic:
  - latestRevision: true
    revisionName: hello-example-dk7nd
    percent: 100
```

But there are no error util for them, so we need to create
custom error like c1583f3045
or `ErrInvalidValue`.

The custom error will make code complicated and `ErrInvalidValue` is
not debug friendly.

To solve it, this patch introduces an util func `ErrInvalidCombination`.

* Introduce ErrGeneric instead of ErrInvalidCombination
2019-09-12 16:44:32 -07:00
Matt Moore 222dd25986 Migrate pkg to use the knative.dev/pkg import path (#489)
* Manual changes.

* scripted changes.
2019-06-26 13:02:06 -07:00
Scott Nichols 70ab9cc77d Adding Strict Validation based on struct.*Deprecated*Foo (#339)
* have simple tests. working on impl.

* strict setting, reflection based.

* ran codegen.

* adding license.

* update based on feedback and merge better.

* getting closer to something  simpler assuming shallow reflect.

* adding validation test.

* use the json tag.

* Golang things nil typed pointers are not nil.

* Use real value of reflect invalid.

* add a missing test.

* two methods, one for update, one for single check.

* checkdep is now in apis.

* fix pkg.

* Update apis/deprecated_test.go

Co-Authored-By: n3wscott <32305648+n3wscott@users.noreply.github.com>

* add code clarity.

* include inlined struct objects recursively.

* Update commnets and add a flatten error test for inlined.
2019-04-19 09:19:27 -07:00
Dan Gerdesmeier 6fa5cc22e3 Add CheckDisallowedFields (#389)
Adds a new function in FieldErrors for validating against fieldmasks.

See https://github.com/knative/serving/pull/3779 for usage.
2019-04-17 17:51:26 -07:00
Matt Moore f55c11c3ce ErrInvalidValue support for interface{} (#372) 2019-04-08 18:26:58 -07:00
Matt Moore e0fcca3e1d Enable our OOB error to take raw types. (#371) 2019-04-08 17:49:57 -07:00
Victor Agababov b032768e6d Fix v1beta1 deployment to v1 in test (#346) 2019-03-27 13:48:51 -07:00
Victor Agababov 819ebda80c Add `AtIndex` method to deal to attach index at the current error. (#257)
* Add `AtIndex` method to deal to attach index at the current error.

This is useful when we are dealing with primite leaf list (e.g.
a list of strings).
The alternative are
a) creating dummy error and then executing ViaFieldIndex(), which is
plain ugly
b) implementing Validate() on the primitive type via type aliasing and
then collecting errors, which seems an overkill.

In addition it seems ViaFieldIndex was not covered by unit tests,
so I've added one to take care of that.

* implement suggestions from the review

* cleanup

* cosmetic

* cosmetic
2019-01-31 12:47:38 -08:00
Victor Agababov 585076ba67 Fix some comments about code comments left from the previous PR. (#196) 2018-12-09 06:42:27 -08:00
Victor Agababov 1f624b77dd Some clean ups in the FieldError code and its tests. (#194)
* Some clean ups in the FieldError code and its tests.

- rename FieldError.getNormalizedError to be normalized:
  - Go kind of discourages GetA() vs A()
  - it's an error, so normalizing itself is also an error, so
    normalizedError() is a tautology at this point
- flatten was doing a[x]=fmt.Sprintf("%s%s", a[x],y), which is a very
  expensive substitution for a[x]+=y
- some minor test cleanups (fix strings, streamline `got, want:=`
  pattern, etc.

* Also make normalize() and merge() operate on pointers.

As the code currently is structured we are doing lots of duplication
and copies of the objects, since we're doing []FieldError throughout
normalize() and merge() even though normalize already returns a copy of
the initial error tree, rather than a list of pointers to existing
errors.

Hence list returned by normalize() and hence merge() can actually be pointer
lists which would save us some CPU and memory.
2018-12-06 17:06:26 -08:00
Kenny Leung 3e52d67e3d Add ErrOutOfBoundsValue for value boundary validation (#167) 2018-11-13 17:36:45 -08:00
Scott Nichols 5a67e38d13 Simplify FieldError merging. (#99)
* checkpoint.

* Tried a new way.

* Some more slimplifying based on feedback.

* use bang vs == false.

* not sure why I did not use go style on this forloop, fixed.
2018-10-30 01:20:33 -07:00
Scott Nichols 7aae4072d7 Sort keys and paths for FieldError. (#90) 2018-09-24 15:48:20 -07:00
Scott Nichols a133825579 Adding also always to return nil for nil errors (#83) 2018-09-20 15:00:19 -07:00
Scott Nichols 70762ec49b Reintroduce FieldError (#77)
* Testing an idea for field errors.

* wrap other tests for webhook.

* move FieldError to Error, FieldErrors to FieldError

* Rename files to reflect their type.

* Reworked this to require no changes externally.

* test also.

* gotta cover those tests.

* nil check

* Adding Also.

* Clean up comments.

* Don't delete the tests.

* Null check test the clear.

* Fix nits.

* FieldError is now non-mutating.

* More errorf formatting fixes.

* I can make this even simpiler

* I can make this even MORE simpiler

* Add comment to make flow of also clear.

* fix comment, optimize make errors
2018-09-20 09:24:19 -07:00
Scott Nichols f0ec8c5ac7 Adding ViaIndex, ViaKey, ViaFieldIndex, ViaFieldKey to FieldErrors (#55)
* Adding .ViaIndex(1). to make spec.param[1] easier to do.

* Adding ViaKey

* fix order of bag example.

* Add code coverage to missed line.

* Adding ViaFieldIndex helper

* cleaning up.

* compress more.

* more cleanup

* test name
2018-09-14 21:07:17 -07:00
Ville Aikas 760afb6c09 cleanup (#57) 2018-08-30 09:36:12 -07:00
Scott Nichols 5182837041 flipped the error text around. fixed. (#36) 2018-08-02 13:13:38 -07:00
Scott Nichols c1e9219cee Adding more field errors. (#34)
* Adding mutually exclusive fields error.

* Adding ErrMissingOneOf, ErrMultipleOneOf, ErrInvalidParameterName

* Adding gitignore.

* Change to ErrInvalidKeyName.

* Use ... semantic.

* those pesky newlines.
2018-08-02 10:05:37 -07:00
Matt Moore 84f0a719d7 Add comments to exported error helpers. (#20)
Noticed via the go report!
2018-07-26 16:40:35 -07:00
Matt Moore 945c05cc41
Bring in the API utilities from `pkg/apis/serving/v1alpha1` used for validation and defaulting in the webhook. (#13)
Fixes: https://github.com/knative/pkg/issues/12
2018-07-26 12:14:57 -07:00