-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[pkg/stanza]: fix operator field config validation #40728
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
[pkg/stanza]: fix operator field config validation #40728
Conversation
A few operators with `entry.Field` in their config were validating their configuration in their `Build` function via a comparison to `entry.NewNilField`. This was an inaccuracy; when a field is not provided during the unmarshal step, nothing ever gets run to actually construct the underlying struct field, meaning that the `entry.Field` was functionally set to `entry.Field{}`. This is not the same as `entry.NewNilField` which actually implements and supplies an interface value under the hood. This caused bad configs to pass validation and led to collector panic instead of nice failure messages. This PR changed `entry.NewNilField` to actually produce the correct pattern for a "nil field", while leaving the original implementation intact in `entry.NewNoopField` in case it is useful elsewhere (though at the moment I only saw it used for nil comparisons, which as far as I can tell were either never useful or something got refactored without being caught). I also added functionality to `operatortest.ConfigUnmarshalTest` to check for validation failures by expecting a particular error when calling the operator's `Build` method.
Turns out the construction of the `field` config is different in certain scenarios where all resource or all attributes are intended to be removed, meaning the original comparison didn't cover the case properly. This is now added. Also fixed copy operator test configs to use a prefix that is actually recognized so the field is parsed.
Out of time to fix the new lint failures, will work on it tomorrow. |
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.
Thank you very much for this, this fix is much needed. Please let me know what you think about the proposed implementation without the Nil/NoopField
type.
This commit changes to a `Field.IsEmpty` to allow the deletion of the `NoopField` and `NilField` types. It also adds multiple error validation to allow for friendlier validation error messages for the `copy` and `move` operators. Finally, it fixes some tests that were revealed to be broken by relying on the previously faulty `copy` operator config validation.
@andrzej-stencel Implemented your suggestions. While doing so I found some tests that relied on the old broken validation so I've now fixed those as well. Should be ready for re-review. |
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.
Awesome! Please let me know what you think about the breaking change concern
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.
Thank you 🚀 👏 🙏
Description
A few operators with
entry.Field
in their config were validating their configuration in theirBuild
function via a comparison toentry.NewNilField
. This was an inaccuracy; when a field is not provided during the unmarshal step, nothing ever gets run to actually construct the underlying struct field, meaning that theentry.Field
was functionally set toentry.Field{}
. This is not the same asentry.NewNilField
which actually implements and supplies an interface value under the hood. This caused bad configs to pass validation and led to collector panic instead of nice failure messages.This PR changed
entry.NewNilField
to actually produce the correct pattern for a "nil field", while leaving the original implementation intact inentry.NewNoopField
in case it is useful elsewhere (though at the moment I only saw it used for nil comparisons, which as far as I can tell were either never useful or something got refactored without being caught). I also added functionality tooperatortest.ConfigUnmarshalTest
to check for validation failures by expecting a particular error when calling the operator'sBuild
method.Link to tracking issue
Addresses a panic discovered by #40398.
Testing
Tests were added to each operator's config validation. I tested manually via a built collector by running the config from the linked issue and confirming that instead of
panic
I get an error message.