-
-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathcrd_derive_no_schema.rs
76 lines (67 loc) · 2.33 KB
/
crd_derive_no_schema.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{
CustomResourceDefinition, CustomResourceValidation, JSONSchemaProps,
};
use kube_derive::CustomResource;
use serde::{Deserialize, Serialize};
/// CustomResource with manually implemented schema
///
/// NB: Everything here is gated on the example's `schema` feature not being set
///
/// Normally you would do this by deriving JsonSchema or manually implementing it / parts of it.
/// But here, we simply drop in a valid schema from a string and avoid schemars from the dependency tree entirely.
#[cfg(not(feature = "schema"))]
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone)]
#[kube(group = "clux.dev", version = "v1", kind = "Bar", namespaced)]
pub struct MyBar {
bars: u32,
}
const MANUAL_SCHEMA: &'static str = r#"
type: object
properties:
spec:
type: object
properties:
bars:
type: int
required:
- bars
"#;
#[cfg(not(feature = "schema"))]
impl Bar {
fn crd_with_manual_schema() -> CustomResourceDefinition {
let schema: JSONSchemaProps = serde_yaml::from_str(MANUAL_SCHEMA).expect("invalid schema");
let mut crd = Self::crd();
crd.spec.versions.iter_mut().for_each(|v| {
v.schema = Some(CustomResourceValidation {
open_api_v3_schema: Some(schema.clone()),
})
});
crd
}
}
#[cfg(not(feature = "schema"))]
fn main() {
let crd = Bar::crd_with_manual_schema();
println!("{}", serde_yaml::to_string(&crd).unwrap());
}
#[cfg(feature = "schema")]
fn main() {
eprintln!("This example it disabled when using the schema feature");
}
// Verify CustomResource derivable still
#[cfg(not(feature = "schema"))]
#[test]
fn verify_bar_is_a_custom_resource() {
use k8s_openapi::Resource;
use schemars::JsonSchema; // only for ensuring it's not implemented
use static_assertions::{assert_impl_all, assert_not_impl_any};
println!("Kind {}", Bar::KIND);
let bar = Bar::new("five", MyBar { bars: 5 });
println!("Spec: {:?}", bar.spec);
assert_impl_all!(Bar: k8s_openapi::Resource, k8s_openapi::Metadata);
assert_not_impl_any!(MyBar: JsonSchema); // but no schemars schema implemented
let crd = Bar::crd_with_manual_schema();
for v in crd.spec.versions {
assert!(v.schema.unwrap().open_api_v3_schema.is_some());
}
}