kube_derive/lib.rs
1//! A crate for kube's derive macros.
2#![recursion_limit = "1024"]
3extern crate proc_macro;
4#[macro_use] extern crate quote;
5
6mod cel_schema;
7mod custom_resource;
8mod resource;
9
10/// A custom derive for kubernetes custom resource definitions.
11///
12/// This will generate a **root object** containing your spec and metadata.
13/// This root object will implement the [`kube::Resource`] trait
14/// so it can be used with [`kube::Api`].
15///
16/// The generated type will also implement kube's [`kube::CustomResourceExt`] trait to generate the crd
17/// and generate [`kube::core::ApiResource`] information for use with the dynamic api.
18///
19/// # Example
20///
21/// ```rust
22/// use serde::{Serialize, Deserialize};
23/// use kube::core::{Resource, CustomResourceExt};
24/// use kube_derive::CustomResource;
25/// use schemars::JsonSchema;
26///
27/// #[derive(CustomResource, Clone, Debug, Deserialize, Serialize, JsonSchema)]
28/// #[kube(group = "clux.dev", version = "v1", kind = "Foo", namespaced)]
29/// struct FooSpec {
30/// info: String,
31/// }
32///
33/// println!("kind = {}", Foo::kind(&())); // impl kube::Resource
34/// let f = Foo::new("foo-1", FooSpec {
35/// info: "informative info".into(),
36/// });
37/// println!("foo: {:?}", f); // debug print on root type
38/// println!("crd: {}", serde_yaml::to_string(&Foo::crd()).unwrap()); // crd yaml
39/// ```
40///
41/// This example generates a `struct Foo` containing metadata, the spec,
42/// and optionally status. The **root** struct `Foo` can be used with the [`kube`] crate
43/// as an `Api<Foo>` object (`FooSpec` can not be used with [`Api`][`kube::Api`]).
44///
45/// ```no_run
46/// # use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition;
47/// # use kube_derive::CustomResource;
48/// # use kube::{api::{Api, Patch, PatchParams}, Client, CustomResourceExt};
49/// # use serde::{Deserialize, Serialize};
50/// # async fn wrapper() -> Result<(), Box<dyn std::error::Error>> {
51/// # #[derive(CustomResource, Clone, Debug, Deserialize, Serialize, schemars::JsonSchema)]
52/// # #[kube(group = "clux.dev", version = "v1", kind = "Foo", namespaced)]
53/// # struct FooSpec {}
54/// # let client: Client = todo!();
55/// let foos: Api<Foo> = Api::default_namespaced(client.clone());
56/// let crds: Api<CustomResourceDefinition> = Api::all(client.clone());
57/// crds.patch("foos.clux.dev", &PatchParams::apply("myapp"), &Patch::Apply(Foo::crd())).await;
58/// # Ok(())
59/// # }
60/// ```
61///
62/// This example posts the generated `::crd` to the `CustomResourceDefinition` API.
63/// After this has been accepted (few secs max), you can start using `foos` as a normal
64/// kube `Api` object. See the `crd_` prefixed [examples](https://github.com/kube-rs/kube/blob/main/examples/)
65/// for details on this.
66///
67/// # Required properties
68///
69/// ## `#[kube(group = "mygroup.tld")]`
70/// Your cr api group. The part before the slash in the top level `apiVersion` key.
71///
72/// ## `#[kube(version = "v1")]`
73/// Your cr api version. The part after the slash in the top level `apiVersion` key.
74///
75/// ## `#[kube(kind = "Kind")]`
76/// Name of your kind, and implied default for your generated root type.
77///
78/// # Optional `#[kube]` attributes
79///
80/// ## `#[kube(singular = "nonstandard-singular")]`
81/// To specify the singular name. Defaults to lowercased `.kind` value.
82///
83/// ## `#[kube(plural = "nonstandard-plural")]`
84/// To specify the plural name. Defaults to inferring from singular.
85///
86/// ## `#[kube(namespaced)]`
87/// To specify that this is a namespaced resource rather than cluster level.
88///
89/// ## `#[kube(root = "StructName")]`
90/// Customize the name of the generated root struct (defaults to `.kind` value).
91///
92/// ## `#[kube(crates(kube_core = "::kube::core"))]`
93/// Customize the crate name the generated code will reach into (defaults to `::kube::core`).
94/// Should be one of `kube::core`, `kube_client::core` or `kube_core`.
95///
96/// ## `#[kube(crates(k8s_openapi = "::k8s_openapi"))]`
97/// Customize the crate name the generated code will use for [`k8s_openapi`](https://docs.rs/k8s-openapi/) (defaults to `::k8s_openapi`).
98///
99/// ## `#[kube(crates(schemars = "::schemars"))]`
100/// Customize the crate name the generated code will use for [`schemars`](https://docs.rs/schemars/) (defaults to `::schemars`).
101///
102/// ## `#[kube(crates(serde = "::serde"))]`
103/// Customize the crate name the generated code will use for [`serde`](https://docs.rs/serde/) (defaults to `::serde`).
104///
105/// ## `#[kube(crates(serde_json = "::serde_json"))]`
106/// Customize the crate name the generated code will use for [`serde_json`](https://docs.rs/serde_json/) (defaults to `::serde_json`).
107///
108/// ## `#[kube(status = "StatusStructName")]`
109/// Adds a status struct to the top level generated type and enables the status
110/// subresource in your crd.
111///
112/// ## `#[kube(derive = "Trait")]`
113/// Adding `#[kube(derive = "PartialEq")]` is required if you want your generated
114/// top level type to be able to `#[derive(PartialEq)]`
115///
116/// ## `#[kube(schema = "mode")]`
117/// Defines whether the `JsonSchema` of the top level generated type should be used when generating a `CustomResourceDefinition`.
118///
119/// Legal values:
120/// - `"derived"`: A `JsonSchema` implementation is automatically derived
121/// - `"manual"`: `JsonSchema` is not derived, but used when creating the `CustomResourceDefinition` object
122/// - `"disabled"`: No `JsonSchema` is used
123///
124/// This can be used to provide a completely custom schema, or to interact with third-party custom resources
125/// where you are not responsible for installing the `CustomResourceDefinition`.
126///
127/// Defaults to `"derived"`.
128///
129/// NOTE: `CustomResourceDefinition`s require a schema. If `schema = "disabled"` then
130/// `Self::crd()` will not be installable into the cluster as-is.
131///
132/// ## `#[kube(scale(...))]`
133///
134/// Allow customizing the scale struct for the [scale subresource](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#subresources).
135/// It should be noted, that the status subresource must also be enabled to use the scale subresource. This is because
136/// the `statusReplicasPath` only accepts JSONPaths under `.status`.
137///
138/// ```ignore
139/// #[kube(scale(
140/// specReplicasPath = ".spec.replicas",
141/// statusReplicaPath = ".status.replicas",
142/// labelSelectorPath = ".spec.labelSelector"
143/// ))]
144/// ```
145///
146/// The deprecated way of customizing the scale subresource using a raw JSON string is still
147/// support for backwards-compatibility.
148///
149/// ## `#[kube(printcolumn = r#"json"#)]`
150/// Allows adding straight json to [printcolumns](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#additional-printer-columns).
151///
152/// ## `#[kube(shortname = "sn")]`
153/// Add a single shortname to the generated crd.
154///
155/// ## `#[kube(category = "apps")]`
156/// Add a single category to `crd.spec.names.categories`.
157///
158/// ## `#[kube(selectable = "fieldSelectorPath")]`
159/// Adds a Kubernetes >=1.30 `selectableFields` property ([KEP-4358](https://github.com/kubernetes/enhancements/blob/master/keps/sig-api-machinery/4358-custom-resource-field-selectors/README.md)) to the schema.
160/// Unlocks `kubectl get kind --field-selector fieldSelectorPath`.
161///
162/// ## `#[kube(doc = "description")]`
163/// Sets the description of the schema in the generated CRD. If not specified
164/// `Auto-generated derived type for {customResourceName} via CustomResource` will be used instead.
165///
166/// ## `#[kube(annotation("ANNOTATION_KEY", "ANNOTATION_VALUE"))]`
167/// Add a single annotation to the generated CRD.
168///
169/// ## `#[kube(label("LABEL_KEY", "LABEL_VALUE"))]`
170/// Add a single label to the generated CRD.
171///
172/// ## `#[kube(storage = true)]`
173/// Sets the `storage` property to `true` or `false`.
174///
175/// ## `#[kube(served = true)]`
176/// Sets the `served` property to `true` or `false`.
177///
178/// ## `#[kube(deprecated [= "warning"])]`
179/// Sets the `deprecated` property to `true`.
180///
181/// ```ignore
182/// #[kube(deprecated)]
183/// ```
184///
185/// Aditionally, you can provide a `deprecationWarning` using the following example.
186///
187/// ```ignore
188/// #[kube(deprecated = "Replaced by other CRD")]
189/// ```
190///
191/// ## `#[kube(rule = Rule::new("self == oldSelf").message("field is immutable"))]`
192/// Inject a top level CEL validation rule for the top level generated struct.
193/// This attribute is for resources deriving [`CELSchema`] instead of [`schemars::JsonSchema`].
194///
195/// ## Example with all properties
196///
197/// ```rust
198/// use serde::{Serialize, Deserialize};
199/// use kube_derive::CustomResource;
200/// use schemars::JsonSchema;
201///
202/// #[derive(CustomResource, Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
203/// #[kube(
204/// group = "clux.dev",
205/// version = "v1",
206/// kind = "Foo",
207/// root = "FooCrd",
208/// namespaced,
209/// doc = "Custom resource representing a Foo",
210/// status = "FooStatus",
211/// derive = "PartialEq",
212/// singular = "foot",
213/// plural = "feetz",
214/// shortname = "f",
215/// scale = r#"{"specReplicasPath":".spec.replicas", "statusReplicasPath":".status.replicas"}"#,
216/// printcolumn = r#"{"name":"Spec", "type":"string", "description":"name of foo", "jsonPath":".spec.name"}"#,
217/// selectable = "spec.replicasCount"
218/// )]
219/// #[serde(rename_all = "camelCase")]
220/// struct FooSpec {
221/// #[schemars(length(min = 3))]
222/// data: String,
223/// replicas_count: i32
224/// }
225///
226/// #[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
227/// struct FooStatus {
228/// replicas: i32
229/// }
230/// ```
231///
232/// # Enums
233///
234/// Kubernetes requires that the generated [schema is "structural"](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema).
235/// This means that the structure of the schema must not depend on the particular values. For enums this imposes a few limitations:
236///
237/// - Only [externally tagged enums](https://serde.rs/enum-representations.html#externally-tagged) are supported
238/// - Unit variants may not be mixed with struct or tuple variants (`enum Foo { Bar, Baz {}, Qux() }` is invalid, for example)
239///
240/// If these restrictions are not followed then `YourCrd::crd()` may panic, or the Kubernetes API may reject the CRD definition.
241///
242/// # Generated code
243///
244/// The example above will **roughly** generate:
245/// ```compile_fail
246/// #[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
247/// #[serde(rename_all = "camelCase")]
248/// pub struct FooCrd {
249/// api_version: String,
250/// kind: String,
251/// metadata: ObjectMeta,
252/// spec: FooSpec,
253/// status: Option<FooStatus>,
254/// }
255/// impl kube::Resource for FooCrd { .. }
256///
257/// impl FooCrd {
258/// pub fn new(name: &str, spec: FooSpec) -> Self { .. }
259/// pub fn crd() -> CustomResourceDefinition { .. }
260/// }
261/// ```
262///
263/// # Customizing Schemas
264/// Should you need to customize the schemas, you can use:
265/// - [Serde/Schemars Attributes](https://graham.cool/schemars/examples/3-schemars_attrs/) (no need to duplicate serde renames)
266/// - [`#[schemars(schema_with = "func")]`](https://graham.cool/schemars/examples/7-custom_serialization/) (e.g. like in the [`crd_derive` example](https://github.com/kube-rs/kube/blob/main/examples/crd_derive.rs))
267/// - `impl JsonSchema` on a type / newtype around external type. See [#129](https://github.com/kube-rs/kube/issues/129#issuecomment-750852916)
268/// - [`#[garde(...)]` field attributes for client-side validation](https://github.com/jprochazk/garde) (see [`crd_api` example](https://github.com/kube-rs/kube/blob/main/examples/crd_api.rs))
269///
270/// You might need to override parts of the schemas (for fields in question) when you are:
271/// - **using complex enums**: enums do not currently generate [structural schemas](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema), so kubernetes won't support them by default
272/// - **customizing [merge-strategies](https://kubernetes.io/docs/reference/using-api/server-side-apply/#merge-strategy)** (e.g. like in the [`crd_derive_schema` example](https://github.com/kube-rs/kube/blob/main/examples/crd_derive_schema.rs))
273///
274/// See [kubernetes openapi validation](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation) for the format of the OpenAPI v3 schemas.
275///
276/// If you have to override a lot, [you can opt-out of schema-generation entirely](#kubeschema--mode)
277///
278/// # Advanced Features
279///
280/// - **embedding k8s-openapi types** can be done by enabling the `schemars` feature of `k8s-openapi` from [`0.13.0`](https://github.com/Arnavion/k8s-openapi/blob/master/CHANGELOG.md#v0130-2021-08-09)
281/// - **adding validation** via [validator crate](https://github.com/Keats/validator) is supported from `schemars` >= [`0.8.5`](https://github.com/GREsau/schemars/blob/master/CHANGELOG.md#085---2021-09-20)
282/// - **generating rust code from schemas** can be done via [kopium](https://github.com/kube-rs/kopium) and is supported on stable crds (> 1.16 kubernetes)
283///
284/// ## Schema Validation
285/// There are two main ways of doing validation; **server-side** (embedding validation attributes into the schema for the apiserver to respect), and **client-side** (provides `validate()` methods in your code).
286///
287/// Client side validation of structs can be achieved by hooking up `#[garde]` attributes in your struct and is a replacement of the now unmaintained [`validator`](https://github.com/Keats/validator/issues/201) crate.
288/// Server-side validation require mutation of your generated schema, and can in the basic cases be achieved through the use of `schemars`'s [validation attributes](https://graham.cool/schemars/deriving/attributes/#supported-validator-attributes).
289/// For complete control, [parts of the schema can be overridden](https://github.com/kube-rs/kube/blob/e01187e13ba364ccecec452e023316a62fb13e04/examples/crd_derive.rs#L37-L38) to support more advanced [Kubernetes specific validation rules](https://kubernetes.io/blog/2022/09/23/crd-validation-rules-beta/).
290///
291/// When using `garde` directly, you must add it to your dependencies (with the `derive` feature).
292///
293/// ### Validation Caveats
294/// Make sure your validation rules are static and handled by `schemars`:
295/// - validations from `#[garde(custom(my_func))]` will not show up in the schema.
296/// - similarly; [nested / must_match / credit_card were unhandled by schemars at time of writing](https://github.com/GREsau/schemars/pull/78)
297/// - encoding validations specified through garde (i.e. #[garde(ascii)]), are currently not supported by schemars
298/// - to validate required attributes client-side, garde requires a custom validation function (`#[garde(custom(my_required_check))]`)
299/// - when using garde, fields that should not be validated need to be explictly skipped through the `#[garde(skip)]` attr
300///
301/// For sanity, you should review the generated schema before sending it to kubernetes.
302///
303/// ## Versioning
304/// Note that any changes to your struct / validation rules / serialization attributes will require you to re-apply the
305/// generated schema to kubernetes, so that the apiserver can validate against the right version of your structs.
306///
307/// **Backwards compatibility** between schema versions is **recommended** unless you are in a controlled environment
308/// where you can migrate manually. I.e. if you add new properties behind options, and simply mark old fields as deprecated,
309/// then you can safely roll schema out changes **without bumping** the version.
310///
311/// If you need **multiple versions**, then you need:
312///
313/// - one **module** for **each version** of your types (e.g. `v1::MyCrd` and `v2::MyCrd`)
314/// - use the [`merge_crds`](https://docs.rs/kube/latest/kube/core/crd/fn.merge_crds.html) fn to combine crds
315/// - roll out new schemas utilizing conversion webhooks / manual conversions / or allow kubectl to do its best
316///
317/// See the [crd_derive_multi](https://github.com/kube-rs/kube/blob/main/examples/crd_derive_multi.rs) example to see
318/// how this upgrade flow works without special logic.
319///
320/// The **upgrade flow** with **breaking changes** involves:
321///
322/// 1. upgrade version marked as `storage` (from v1 to v2)
323/// 2. read instances from the older `Api<v1::MyCrd>`
324/// 3. perform conversion in memory and write them to the new `Api<v2::MyCrd>`.
325/// 4. remove support for old version
326///
327/// If you need to maintain support for the old version for some time, then you have to repeat or continuously
328/// run steps 2 and 3. I.e. you probably need a **conversion webhook**.
329///
330/// **NB**: kube does currently [not implement conversion webhooks yet](https://github.com/kube-rs/kube/issues/865).
331///
332/// ## Debugging
333/// Try `cargo-expand` to see your own macro expansion.
334///
335/// # Installation
336/// Enable the `derive` feature on the `kube` crate:
337///
338/// ```toml
339/// kube = { version = "...", features = ["derive"] }
340/// ```
341///
342/// ## Runtime dependencies
343/// Due to [rust-lang/rust#54363](https://github.com/rust-lang/rust/issues/54363), we cannot be resilient against crate renames within our generated code.
344/// It's therefore **required** that you have the following crates in scope, not renamed:
345///
346/// - `serde_json`
347/// - `k8s_openapi`
348/// - `schemars` (by default, unless `schema` feature disabled)
349///
350/// You are ultimately responsible for maintaining the versions and feature flags of these libraries.
351///
352/// [`kube`]: https://docs.rs/kube
353/// [`kube::Api`]: https://docs.rs/kube/*/kube/struct.Api.html
354/// [`kube::Resource`]: https://docs.rs/kube/*/kube/trait.Resource.html
355/// [`kube::core::ApiResource`]: https://docs.rs/kube/*/kube/core/struct.ApiResource.html
356/// [`kube::CustomResourceExt`]: https://docs.rs/kube/*/kube/trait.CustomResourceExt.html
357#[proc_macro_derive(CustomResource, attributes(kube))]
358pub fn derive_custom_resource(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
359 custom_resource::derive(proc_macro2::TokenStream::from(input)).into()
360}
361
362/// Generates a JsonSchema implementation a set of CEL validation rules applied on the CRD.
363///
364/// ```rust
365/// use kube::CELSchema;
366/// use kube::CustomResource;
367/// use serde::Deserialize;
368/// use serde::Serialize;
369/// use kube::core::crd::CustomResourceExt;
370///
371/// #[derive(CustomResource, CELSchema, Serialize, Deserialize, Clone, Debug)]
372/// #[kube(
373/// group = "kube.rs",
374/// version = "v1",
375/// kind = "Struct",
376/// rule = Rule::new("self.matadata.name == 'singleton'"),
377/// )]
378/// #[cel_validate(rule = Rule::new("self == oldSelf"))]
379/// struct MyStruct {
380/// #[serde(default = "default")]
381/// #[cel_validate(rule = Rule::new("self != ''").message("failure message"))]
382/// field: String,
383/// }
384///
385/// fn default() -> String {
386/// "value".into()
387/// }
388///
389/// assert!(serde_json::to_string(&Struct::crd()).unwrap().contains("x-kubernetes-validations"));
390/// assert!(serde_json::to_string(&Struct::crd()).unwrap().contains(r#""rule":"self == oldSelf""#));
391/// assert!(serde_json::to_string(&Struct::crd()).unwrap().contains(r#""rule":"self != ''""#));
392/// assert!(serde_json::to_string(&Struct::crd()).unwrap().contains(r#""message":"failure message""#));
393/// assert!(serde_json::to_string(&Struct::crd()).unwrap().contains(r#""default":"value""#));
394/// assert!(serde_json::to_string(&Struct::crd()).unwrap().contains(r#""rule":"self.matadata.name == 'singleton'""#));
395/// ```
396#[proc_macro_derive(CELSchema, attributes(cel_validate, schemars))]
397pub fn derive_schema_validation(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
398 cel_schema::derive_validated_schema(input.into()).into()
399}
400
401/// A custom derive for inheriting Resource impl for the type.
402///
403/// This will generate a [`kube::Resource`] trait implementation,
404/// inheriting from a specified resource trait implementation.
405///
406/// This allows strict typing to some typical resources like `Secret` or `ConfigMap`,
407/// in cases when implementing CRD is not desirable or it does not fit the use-case.
408///
409/// Once derived, the type can be used with [`kube::Api`].
410///
411/// # Example
412///
413/// ```rust,no_run
414/// use kube::api::ObjectMeta;
415/// use k8s_openapi::api::core::v1::ConfigMap;
416/// use kube_derive::Resource;
417/// use kube::Client;
418/// use kube::Api;
419/// use serde::Deserialize;
420///
421/// #[derive(Resource, Clone, Debug, Deserialize)]
422/// #[resource(inherit = "ConfigMap")]
423/// struct FooMap {
424/// metadata: ObjectMeta,
425/// data: Option<FooMapSpec>,
426/// }
427///
428/// #[derive(Clone, Debug, Deserialize)]
429/// struct FooMapSpec {
430/// field: String,
431/// }
432///
433/// let client: Client = todo!();
434/// let api: Api<FooMap> = Api::default_namespaced(client);
435/// let config_map = api.get("with-field");
436/// ```
437///
438/// The example above will generate:
439/// ```
440/// // impl kube::Resource for FooMap { .. }
441/// ```
442/// [`kube`]: https://docs.rs/kube
443/// [`kube::Api`]: https://docs.rs/kube/*/kube/struct.Api.html
444/// [`kube::Resource`]: https://docs.rs/kube/*/kube/trait.Resource.html
445/// [`kube::core::ApiResource`]: https://docs.rs/kube/*/kube/core/struct.ApiResource.html
446/// [`kube::CustomResourceExt`]: https://docs.rs/kube/*/kube/trait.CustomResourceExt.html
447#[proc_macro_derive(Resource, attributes(resource))]
448pub fn derive_resource(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
449 resource::derive(proc_macro2::TokenStream::from(input)).into()
450}