Skip to content

Conversation

Danil-Grigorev
Copy link
Member

Motivation

In order to simplify usage of CEL in schema generation with x_kube macro, users interested in simple validation should be able to specify simple &str data without additional syntax, like mandatory .into() call for conversion into Rule type required now.

Solution

This change allows to write simple CEL expressions like so:

#[x_kube(validation = "self.nonNullable == oldSelf.nonNullable")]

as opposed to previous:

#[x_kube(validation = "self.nonNullable == oldSelf.nonNullable".into())]

or

#[x_kube(validation = Rule::new("self.metadata.name != 'forbidden'"))]

and combine multiple more complex expressions with different input types, in case a message or a reason context needs to be specified:

#[x_kube(validation = "self != 'not legal'", validation = Rule::new("self != 'not that'"))]
#[x_kube(validation = ("self != 'also not that'", "some pretty good reason"))]

Copy link

codecov bot commented May 1, 2025

Codecov Report

Attention: Patch coverage is 76.92308% with 3 lines in your changes missing coverage. Please review.

Project coverage is 76.2%. Comparing base (0d79006) to head (6c7fcb6).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
kube-core/src/cel.rs 66.7% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1754     +/-   ##
=======================================
- Coverage   76.2%   76.2%   -0.0%     
=======================================
  Files         84      84             
  Lines       7883    7889      +6     
=======================================
+ Hits        6003    6006      +3     
- Misses      1880    1883      +3     
Files with missing lines Coverage Δ
kube-derive/src/cel_schema.rs 93.7% <100.0%> (ø)
kube-derive/src/custom_resource.rs 84.1% <ø> (ø)
kube-derive/src/lib.rs 0.0% <ø> (ø)
kube-derive/tests/crd_schema_test.rs 97.1% <ø> (ø)
kube-core/src/cel.rs 62.5% <66.7%> (-1.2%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

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

nice improvement!

use proc_macro2::TokenStream;
use syn::{parse_quote, Attribute, DeriveInput, Expr, Ident, Path};

#[derive(FromField)]
#[darling(attributes(x_kube))]
struct XKube {
#[darling(multiple, rename = "validation")]
#[darling(multiple, rename = "validation", with = parse_expr::preserve_str_literal)]
Copy link
Member

Choose a reason for hiding this comment

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

very nice

@clux clux added the changelog-add changelog added category for prs label May 6, 2025
@clux clux added this to the 0.100.0 milestone May 6, 2025
@clux clux merged commit 6b59e72 into kube-rs:main May 6, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-add changelog added category for prs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants