kube_derive

Derive Macro CELSchema

Source
#[derive(CELSchema)]
{
    // Attributes available to this derive:
    #[cel_validate]
    #[schemars]
}
Expand description

Generates a JsonSchema implementation a set of CEL validation rules applied on the CRD.

use kube::CELSchema;
use kube::CustomResource;
use serde::Deserialize;
use serde::Serialize;
use kube::core::crd::CustomResourceExt;

#[derive(CustomResource, CELSchema, Serialize, Deserialize, Clone, Debug)]
#[kube(
    group = "kube.rs",
    version = "v1",
    kind = "Struct",
    rule = Rule::new("self.matadata.name == 'singleton'"),
)]
#[cel_validate(rule = Rule::new("self == oldSelf"))]
struct MyStruct {
    #[serde(default = "default")]
    #[cel_validate(rule = Rule::new("self != ''").message("failure message"))]
    field: String,
}

fn default() -> String {
    "value".into()
}

assert!(serde_json::to_string(&Struct::crd()).unwrap().contains("x-kubernetes-validations"));
assert!(serde_json::to_string(&Struct::crd()).unwrap().contains(r#""rule":"self == oldSelf""#));
assert!(serde_json::to_string(&Struct::crd()).unwrap().contains(r#""rule":"self != ''""#));
assert!(serde_json::to_string(&Struct::crd()).unwrap().contains(r#""message":"failure message""#));
assert!(serde_json::to_string(&Struct::crd()).unwrap().contains(r#""default":"value""#));
assert!(serde_json::to_string(&Struct::crd()).unwrap().contains(r#""rule":"self.matadata.name == 'singleton'""#));