-
-
Notifications
You must be signed in to change notification settings - Fork 323
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
Better support for x-kubernetes
properties in schemas
#1367
Comments
for #1367 and kube-rs/website#53 Signed-off-by: clux <[email protected]>
* Naive CEL validation example for #1367 and kube-rs/website#53 Signed-off-by: clux <[email protected]> * fmt Signed-off-by: clux <[email protected]> --------- Signed-off-by: clux <[email protected]>
This would be a great feature. Looking for how to enforce immutability |
FWIW here is a better example, that allows to preserve existing schema - https://github.com/s3rius/kuo/blob/4c83b4033093e84035cefb0f5a22c46c7b9d471c/src/crds/managed_user.rs#L70-L84 |
That example is really useful @azat, thank you. We could maybe create a higher level version of that that allows generating schema modifying functions like that, if we can curry the "rule" and "message" as initial arguments, and return the mutating fn from this. |
I was looking into this recently, but I think right now I need help to decide if any of these approaches are correct
#[derive(Validated, JsonSchema)]
struct FooSpec {
#[validated(
method = cel_validated,
rule = Rule{rule: "self != 'illegal'".into(), message: Some(Message::Expression("'string cannot be illegal'".into())), reason: Some(Reason::FieldValueForbidden), ..Default::default()},
rule = Rule{rule: "self != 'not legal'".into(), reason: Some(Reason::FieldValueInvalid), ..Default::default()}
)]
#[schemars(schema_with = "cel_validated")]
field: …
#[derive(Validated, JsonSchema)]
struct FooSpec {
#[cel_validation(
rule = "self.minReplicas <= self.replicas",
message = "replicas should be greater than or equal to minReplicas.".into() // Is actually an enum Message
reason = FieldValueInvalid // a Reason::FieldValueInvalid
)]
#[cel_validation(rule = "self.minReplicas <= self.replicas")] // Multiple
#[schemars(schema_with = "FooSpecValidation::cel_validated")]
field: …
#[derive(Validated)] // No jsonSchema - Validated implements it
struct FooSpec {
#[validated(
rule = Rule{rule: "self != 'illegal'".into(), message: Some(Message::Expression("'string cannot be illegal'".into())), reason: Some(Reason::FieldValueForbidden), ..Default::default()},
rule = Rule{rule: "self != 'not legal'".into(), message: Some(“a message”.into()), reason: Some(Reason::FieldValueInvalid), ..Default::default()}
)]
// No need for schemars attribute - added automatically
field: … Personally I like 3 most, but I noticed some issues with the lint, like complains to Other things tried:
|
i do like the idea to inject it automatically (3) the most. i put some questions on some specifics in the pr.
yeah, i'd probably want to wait until there's a new release here before forcing out anything new. what new does schemars alpha extend/transform you reference add to the table? |
Once 1.0 is released, generation of the method will no longer be required, as However there are a couple of downfalls with just purely sticking with upstream
#[schemars(extend(“x-kubernetes-validations”: vec![Rule{…}, Rule{…}]))]
Once the 1.0 is out, it will be easy enough to change the behavior of |
Closing via #1649 which includes a Let's see what we can do with schemars 1.0 in the next year. |
There is a slew of
x-kubernetes
extension properties that is usable inside CRD schemas to improve the validation and merge behaviour of structures of members.From Kubernetes 1.25 this is being leaned into even more heavily with x-kubernetes-validations rules on CRD schemas:
This type of validation makes it possible to write controllers with much richer type of native validation without having to lean on the clunkier admission controllers / admission controller frameworks to manage extraneous policy rules - and has a lot of potential use. There's a KubeConNA'23 talk called Declarative Everything that explores this and the direction of the feature from sig apimachinery.
Current Usage
Such properties can be specified via
schemars
attributes to override the schema generation for a type as perkube/examples/crd_derive_schema.rs
Lines 84 to 102 in 3a4f724
This is not ideal because you then have to manage all the other schema properties for that type yourself.
Ideas
Schemars Extensions
To properly support adding properties like this, it would be interesting to see if
schemas
which has support for a large amount of attributes already, could be enhanced with akubernetes
attribute feature set to allow stuff like e.g.:Have asked upstream about what they think about this in GREsau/schemars#258
CEL Crate?
Maybe it's better to start some of this from a CEL POV because maybe we want to be able to run validations we put into CEL rules locally. In this case a crate that allows us to do something like
in either case we probably still need something in
schemars
so that the rules are forwarded into the schema, but it means we will have our own properties (like serde/validator) that schemars can pickup on later.And we likely would need a new crate or majorly extend the cel-parser / cel-interpreter crate (both of which live in cel-rust).
The text was updated successfully, but these errors were encountered: