stackable_versioned_macros/lib.rs
1use darling::{FromMeta, ast::NestedMeta};
2#[cfg(doc)]
3use kube::core::conversion::ConversionReview;
4use proc_macro::TokenStream;
5use syn::{Error, Item, spanned::Spanned};
6
7use crate::{attrs::module::ModuleAttributes, codegen::module::Module};
8
9#[cfg(test)]
10mod test_utils;
11
12mod attrs;
13mod codegen;
14mod utils;
15
16/// This macro enables generating versioned structs and enums.
17///
18/// In this guide, code blocks usually come in pairs. The first code block
19/// describes how the macro is used. The second expandable block displays the
20/// generated piece of code for explanation purposes. It should be noted, that
21/// the exact code can diverge from what is being depicted in this guide. Most
22/// code is heavily simplified. For example, `#[automatically_derived]` and
23/// `#[allow(deprecated)]` are removed in most examples to reduce visual clutter.
24///
25/// <div class="warning">
26///
27/// It is **important** to note that this macro must be placed before any other
28/// (derive) macros and attributes. Macros supplied before the versioned macro
29/// will be erased, because the original struct, enum or module (container) is
30/// erased, and new containers are generated. This ensures that the macros and
31/// attributes are applied to the generated versioned instances of the
32/// container.
33///
34/// </div>
35///
36/// # Version Declarations
37///
38/// Before any of the fields or variants can be versioned, versions need to be
39/// declared at the module level. Each version currently supports two
40/// parameters: `name` and the `deprecated` flag. The `name` must be a valid
41/// (and supported) format.
42///
43/// <div class="warning">
44///
45/// Currently, only [Kubernetes API versions][k8s-version-format] are supported.
46/// The macro checks each declared version and reports any error encountered
47/// during parsing.
48///
49/// </div>
50///
51/// It should be noted that the defined struct always represents the **latest**
52/// version, eg: when defining three versions `v1alpha1`, `v1beta1`, and `v1`,
53/// the struct will describe the structure of the data in `v1`. This behaviour
54/// is especially noticeable in the [`changed()`](#changed-action) action which
55/// works "backwards" by describing how a field looked before the current
56/// (latest) version.
57///
58/// TODO: Version declarations should eventually be moved back to containers.
59///
60/// ```
61/// # use stackable_versioned_macros::versioned;
62/// #[versioned(version(name = "v1alpha1"))]
63/// mod versioned {
64/// struct Foo {
65/// bar: usize,
66/// }
67/// }
68/// ```
69///
70/// <details>
71/// <summary>Generated code</summary>
72///
73/// 1. The `#[automatically_derived]` attribute indicates that the following
74/// piece of code is automatically generated by a macro instead of being
75/// handwritten by a developer. This information is used by cargo and rustc.
76/// 2. For each declared version, a new module containing the containers is
77/// generated. This enables you to reference the container by versions via
78/// `v1alpha1::Foo`.
79/// 3. This `use` statement gives the generated containers access to the imports
80/// at the top of the file. This is a convenience, because otherwise you
81/// would need to prefix used items with `super::`. Additionally, other
82/// macros can have trouble using items referred to with `super::`.
83///
84/// ```ignore
85/// #[automatically_derived] // 1
86/// mod v1alpha1 { // 2
87/// use super::*; // 3
88/// pub struct Foo {
89/// bar: usize,
90/// }
91/// }
92/// ```
93/// </details>
94///
95/// ## Version Deprecation
96///
97/// The `deprecated` flag marks the version as deprecated. This currently adds
98/// the `#[deprecated]` attribute to the appropriate piece of code. In the
99/// future, this will additionally mark the CRD version with `deprecated: true`.
100/// See the official docs on [version deprecation][k8s-crd-ver-deprecation].
101///
102/// ```
103/// # use stackable_versioned_macros::versioned;
104/// #[versioned(version(name = "v1alpha1", deprecated))]
105/// mod versioned {
106/// struct Foo {
107/// bar: usize,
108/// }
109/// }
110/// ```
111///
112/// <details>
113/// <summary>Generated code</summary>
114///
115/// 1. The `deprecated` flag will generate a `#[deprecated]` attribute and the
116/// note is automatically generated.
117///
118/// ```ignore
119/// #[automatically_derived]
120/// #[deprecated = "Version v1alpha1 is deprecated"] // 1
121/// mod v1alpha1 {
122/// use super::*;
123/// pub struct Foo {
124/// pub bar: usize,
125/// }
126/// }
127/// ```
128/// </details>
129///
130/// ## Version Sorting
131///
132/// Additionally, it is ensured that each version is unique. Declaring the same
133/// version multiple times will result in an error. Furthermore, declaring the
134/// versions out-of-order is prohibited by default. It is possible to opt-out
135/// of this check by setting `options(allow_unsorted)`.
136///
137/// <div class="warning">
138///
139/// It is **not** recommended to use this setting and instead use sorted versions
140/// across all versioned items.
141///
142/// </div>
143///
144/// ```
145/// # use stackable_versioned_macros::versioned;
146/// #[versioned(
147/// version(name = "v1beta1"),
148/// version(name = "v1alpha1"),
149/// options(allow_unsorted)
150/// )]
151/// mod versioned {
152/// struct Foo {
153/// bar: usize,
154/// }
155/// }
156/// ```
157///
158/// # Versioning Module
159///
160/// The purpose of the macro is to version Kubernetes CustomResourceDefinitions
161/// (CRDs). As such, the design and how it works is focused on defining and
162/// versioning these CRDs. These CRDs are defined as a top-level struct which
163/// can them self contain many sub structs.
164///
165/// To be able to maximize the visibility on items comprising the CRD, the macro
166/// needs to be applied to module blocks. The name of the module can be freely
167/// chosen. Throughout this guide, the name `versioned` is used. The module is
168/// erased in the generated code. This behaviour can however be
169/// [customized](#preserve-module).
170///
171/// TODO: Mention visibility of module
172///
173/// ## Preserve Module
174///
175/// The previous examples completely replaced the `versioned` module with
176/// top-level version modules. This is the default behaviour. Preserving the
177/// module can however be enabled by setting the `preserve_module` flag.
178///
179/// ```
180/// # use stackable_versioned_macros::versioned;
181/// #[versioned(
182/// version(name = "v1alpha1"),
183/// version(name = "v1"),
184/// options(preserve_module)
185/// )]
186/// mod versioned {
187/// struct Foo {
188/// bar: usize,
189/// }
190///
191/// struct Bar {
192/// baz: String,
193/// }
194/// }
195/// ```
196///
197/// ## Crate Overrides
198///
199/// Override the import path of specific crates which is especially useful if
200/// the crates are brought into scope through re-exports. The following code
201/// block depicts supported overrides and their default values.
202///
203/// ```
204/// # use stackable_versioned_macros::versioned;
205/// #[versioned(
206/// version(name = "v1alpha1"),
207/// version(name = "v1beta1"),
208/// crates(
209/// versioned = "::stackable_versioned",
210/// kube_client = "::kube::client",
211/// k8s_openapi = "::k8s_openapi",
212/// serde_json = "::serde_json",
213/// kube_core = "::kube::core",
214/// schemars = "::schemars",
215/// serde = "::serde",
216/// )
217/// )]
218/// mod versioned {
219/// // ...
220/// }
221/// ```
222///
223/// ## Additional Options
224///
225/// This section contains optional options which influence parts of the code
226/// generation.
227///
228/// ```
229/// # use stackable_versioned_macros::versioned;
230/// #[versioned(
231/// version(name = "v1alpha1"),
232/// version(name = "v1beta1"),
233/// options(k8s(
234/// // Highly experimental conversion tracking. Opting into this feature will
235/// // introduce frequent breaking changes.
236/// experimental_conversion_tracking,
237/// // Enables instrumentation and log events via the tracing crate.
238/// enable_tracing,
239/// ))
240/// )]
241/// mod versioned {
242/// // ...
243/// }
244/// ```
245///
246/// ## Merging Submodules
247///
248/// Modules defined in the versioned module will be re-emitted. This allows for
249/// composition of re-exports to compose easier to use imports for downstream
250/// consumers of versioned containers. The following rules apply:
251///
252/// 1. Only modules named the same like defined versions will be re-emitted.
253/// Using modules with invalid names will return an error.
254/// 2. Only `use` statements defined in the module will be emitted. Declaring
255/// other items will return an error.
256///
257/// ```
258/// # use stackable_versioned_macros::versioned;
259/// # mod a {
260/// # pub mod v1alpha1 {}
261/// # }
262/// # mod b {
263/// # pub mod v1alpha1 {}
264/// # }
265/// #[versioned(version(name = "v1alpha1"), version(name = "v1"))]
266/// mod versioned {
267/// mod v1alpha1 {
268/// pub use a::v1alpha1::*;
269/// pub use b::v1alpha1::*;
270/// }
271///
272/// struct Foo {
273/// bar: usize,
274/// }
275/// }
276/// # fn main() {}
277/// ```
278///
279/// <details>
280/// <summary>Expand Generated Code</summary>
281///
282/// ```ignore
283/// mod v1alpha1 {
284/// use super::*;
285/// pub use a::v1alpha1::*;
286/// pub use b::v1alpha1::*;
287/// pub struct Foo {
288/// pub bar: usize,
289/// }
290/// }
291///
292/// mod v1 {
293/// use super::*;
294/// pub struct Foo {
295/// pub bar: usize,
296/// }
297/// }
298/// ```
299///
300/// </details>
301///
302/// # CRD Spec Definition
303///
304/// ## Arguments
305///
306/// <div class="warning">
307///
308/// It should be noted that not every `#[kube]` argument is supported or
309/// forwarded without changes.
310///
311/// </div>
312///
313/// Structs annotated with `#[versioned(crd()]` are treated as top-level CRD
314/// spec definitions. This section lists all currently supported arguments.
315/// Most of these arguments are directly forwarded to the underlying `#[kube]`
316/// attribute. Some arguments are specific to this macro and don't exist in the
317/// upstream [`kube`] crate.
318///
319/// ```
320/// # use kube::CustomResource;
321/// # use schemars::JsonSchema;
322/// # use serde::{Deserialize, Serialize};
323/// # use stackable_versioned_macros::versioned;
324/// #[versioned(version(name = "v1alpha1"), version(name = "v1beta1"))]
325/// mod versioned {
326/// #[versioned(crd(
327/// // **Required.** Set the group of the CRD, usually the domain of the
328/// // company, like `example.com`.
329/// group = "example.com",
330/// // Override the kind field of the CRD. This defaults to the struct
331/// // name (without the `Spec` suffix). Overriding this value will also
332/// // influence the names of other generated items, like the status
333/// // struct (if used) or the version enum.
334/// kind = "CustomKind",
335/// // Set the singular name. Defaults to lowercased `kind` value.
336/// singular = "...",
337/// // Set the plural name. Defaults to inferring from singular.
338/// plural = "...",
339/// // Indicate that this is a namespaced scoped resource rather than a
340/// // cluster scoped resource.
341/// namespaced,
342/// // Set the specified struct as the status subresource. If conversion
343/// // tracking is enabled, this struct will be automatically merged into
344/// // the generated tracking status struct.
345/// status = "FooStatus",
346/// // Set a shortname. This can be specified multiple times.
347/// shortname = "..."
348/// ))]
349/// # #[derive(Clone, Debug, Deserialize, Serialize, CustomResource, JsonSchema)]
350/// pub struct FooSpec {
351/// #[versioned(deprecated(since = "v1beta1"))]
352/// deprecated_bar: usize,
353/// baz: bool,
354/// }
355/// }
356/// # #[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
357/// # pub struct FooStatus {}
358/// # fn main() {}
359/// ```
360///
361/// ## Field Actions
362///
363/// This crate currently supports three different item actions. Items can
364/// be added, changed, and deprecated. The macro ensures that these actions
365/// adhere to the following set of rules:
366///
367/// 1. Items cannot be added and deprecated in the same version.
368/// 2. Items cannot be added and changed in the same version.
369/// 3. Items cannot be changed and deprecated in the same version.
370/// 4. Items added in version _a_, renamed _0...n_ times in versions
371/// b<sub>1</sub>, ..., b<sub>n</sub> and deprecated in
372/// version _c_ must ensure _a < b<sub>1</sub>, ..., b<sub>n</sub> < c_.
373/// 5. All item actions must use previously declared versions. Using versions
374/// not present at the container level will result in an error.
375///
376/// For items marked as deprecated, one additional rule applies:
377///
378/// - Fields must start with the `deprecated_` and variants with the
379/// `Deprecated` prefix. This is enforced because Kubernetes doesn't allow
380/// removing fields in CRDs entirely. Instead, they should be marked as
381/// deprecated. By convention this is done with the `deprecated` prefix.
382///
383/// ### Added Action
384///
385/// This action indicates that an item is added in a particular version.
386/// Available arguments are:
387///
388/// - `since` to indicate since which version the item is present.
389/// - `default` to customize the default function used to populate the item
390/// in auto-generated conversion implementations.
391///
392/// ```
393/// # use stackable_versioned_macros::versioned;
394/// #[versioned(version(name = "v1alpha1"), version(name = "v1beta1"))]
395/// mod versioned {
396/// pub struct Foo {
397/// #[versioned(added(since = "v1beta1"))]
398/// bar: usize,
399/// baz: bool,
400/// }
401/// }
402/// ```
403///
404/// <details>
405/// <summary>Expand Generated Code</summary>
406///
407/// 1. The field `bar` is not yet present in version `v1alpha1` and is therefore
408/// not generated.
409/// 2. Now the field `bar` is present and uses `Default::default()` to populate
410/// the field during conversion. This function can be customized as shown
411/// later in this guide.
412///
413/// ```ignore
414/// pub mod v1alpha1 {
415/// use super::*;
416/// pub struct Foo { // 1
417/// pub baz: bool,
418/// }
419/// }
420///
421/// impl From<v1alpha1::Foo> for v1beta1::Foo {
422/// fn from(foo: v1alpha1::Foo) -> Self {
423/// Self {
424/// bar: Default::default(), // 2
425/// baz: foo.baz,
426/// }
427/// }
428/// }
429///
430/// pub mod v1beta1 {
431/// use super::*;
432/// pub struct Foo {
433/// pub bar: usize, // 2
434/// pub baz: bool,
435/// }
436/// }
437/// ```
438/// </details>
439///
440/// #### Custom Default Function
441///
442/// To customize the default function used in the generated conversion
443/// implementations the `added` action provides the `default` argument. It
444/// expects a path to a function without braces. This path can for example point
445/// at free-standing or associated functions.
446///
447/// ```
448/// # use stackable_versioned_macros::versioned;
449/// #[versioned(version(name = "v1alpha1"), version(name = "v1beta1"))]
450/// mod versioned {
451/// pub struct Foo {
452/// #[versioned(added(since = "v1beta1", default = "default_bar"))]
453/// bar: usize,
454/// baz: bool,
455/// }
456/// }
457///
458/// fn default_bar() -> usize {
459/// 42
460/// }
461/// ```
462///
463/// <details>
464/// <summary>Expand Generated Code</summary>
465///
466/// 1. Instead of `Default::default()`, the provided function `default_bar()` is
467/// used. It is of course fully type checked and needs to return the expected
468/// type (`usize` in this case).
469///
470/// ```ignore
471/// // Snip
472///
473/// impl From<v1alpha1::Foo> for v1beta1::Foo {
474/// fn from(foo: v1alpha1::Foo) -> Self {
475/// Self {
476/// bar: default_bar(), // 1
477/// baz: foo.baz,
478/// }
479/// }
480/// }
481///
482/// // Snip
483/// ```
484/// </details>
485///
486/// ### Changed Action
487///
488/// This action indicates that an item is changed in a particular version. It
489/// combines renames and type changes into a single action. You can choose to
490/// change the name, change the type or do both. Available arguments are:
491///
492/// - `since` to indicate since which version the item is changed.
493/// - `from_name` to indicate from which previous name the field is renamed.
494/// - `from_type` to indicate from which previous type the field is changed.
495/// - `upgrade_with` to provide a custom upgrade function. This argument can
496/// only be used in combination with the `from_type` argument. The expected
497/// function signature is: `fn (OLD_TYPE) -> NEW_TYPE`. This function must
498/// not fail.
499/// - `downgrade_with` to provide a custom downgrade function. This argument can
500/// only be used in combination with the `from_type` argument. The expected
501/// function signature is: `fn (NEW_TYPE) -> OLD_TYPE`. This function must
502/// not fail.
503/// ```
504/// # use stackable_versioned_macros::versioned;
505/// #[versioned(version(name = "v1alpha1"), version(name = "v1beta1"))]
506/// mod versioned {
507/// pub struct Foo {
508/// #[versioned(changed(
509/// since = "v1beta1",
510/// from_name = "prev_bar",
511/// from_type = "u16",
512/// downgrade_with = usize_to_u16
513/// ))]
514/// bar: usize,
515/// baz: bool,
516/// }
517/// }
518///
519/// fn usize_to_u16(input: usize) -> u16 {
520/// input.try_into().unwrap()
521/// }
522/// ```
523///
524/// <details>
525/// <summary>Expand Generated Code</summary>
526///
527/// 1. In version `v1alpha1` the field is named `prev_bar` and uses a `u16`.
528/// 2. In the next version, `v1beta1`, the field is now named `bar` and uses
529/// `usize` instead of a `u16`. The conversion implementations transforms the
530/// type automatically.
531///
532/// ```ignore
533/// pub mod v1alpha1 {
534/// use super::*;
535/// pub struct Foo {
536/// pub prev_bar: u16, // 1
537/// pub baz: bool,
538/// }
539/// }
540///
541/// impl From<v1alpha1::Foo> for v1beta1::Foo {
542/// fn from(foo: v1alpha1::Foo) -> Self {
543/// Self {
544/// bar: foo.prev_bar.into(), // 2
545/// baz: foo.baz,
546/// }
547/// }
548/// }
549///
550/// pub mod v1beta1 {
551/// use super::*;
552/// pub struct Foo {
553/// pub bar: usize, // 2
554/// pub baz: bool,
555/// }
556/// }
557/// ```
558/// </details>
559///
560/// ### Deprecated Action
561///
562/// This action indicates that an item is deprecated in a particular version.
563/// Deprecated items are not removed. Available arguments are:
564///
565/// - `since` to indicate since which version the item is deprecated.
566/// - `note` to specify an optional deprecation note.
567///
568/// ```
569/// # use stackable_versioned_macros::versioned;
570/// #[versioned(version(name = "v1alpha1"), version(name = "v1beta1"))]
571/// mod versioned {
572/// pub struct Foo {
573/// #[versioned(deprecated(since = "v1beta1"))]
574/// deprecated_bar: usize,
575/// baz: bool,
576/// }
577/// }
578/// ```
579///
580/// <details>
581/// <summary>Expand Generated Code</summary>
582///
583/// 1. In version `v1alpha1` the field `bar` is not yet deprecated and thus uses
584/// the name without the `deprecated_` prefix.
585/// 2. In version `v1beta1` the field is deprecated and now includes the
586/// `deprecated_` prefix. It also uses the `#[deprecated]` attribute to
587/// indicate to Clippy this part of Rust code is deprecated. Therefore, the
588/// conversion implementations include `#[allow(deprecated)]` to allow the
589/// usage of deprecated items in automatically generated code.
590///
591/// ```ignore
592/// pub mod v1alpha1 {
593/// use super::*;
594/// pub struct Foo {
595/// pub bar: usize, // 1
596/// pub baz: bool,
597/// }
598/// }
599///
600/// #[allow(deprecated)] // 2
601/// impl From<v1alpha1::Foo> for v1beta1::Foo {
602/// fn from(foo: v1alpha1::Foo) -> Self {
603/// Self {
604/// deprecated_bar: foo.bar, // 2
605/// baz: foo.baz,
606/// }
607/// }
608/// }
609///
610/// pub mod v1beta1 {
611/// use super::*;
612/// pub struct Foo {
613/// #[deprecated] // 2
614/// pub deprecated_bar: usize,
615/// pub baz: bool,
616/// }
617/// }
618/// ```
619/// </details>
620///
621/// # Generated Helpers
622///
623/// This macro generates a few different helpers to enable different operations
624/// around CRD versioning and conversion. The following sections explain these
625/// helpers and (some) of the code behind them in detail.
626///
627/// All these helpers are generated as associated functions on what this macro
628/// calls an entry enum. When defining the following three versions: `v1alpha1`,
629/// `v1beta1`, and `v1` the following entry enum will be generated:
630///
631/// ```ignore
632/// pub enum Foo {
633/// V1Alpha1(v1alpha1::Foo),
634/// V1Beta1(v1beta1::Foo),
635/// V1(v1::Foo),
636/// }
637/// ```
638///
639/// ## Merge CRD Versions
640///
641/// The generated `merged_crd` method is a wrapper around [kube's `merge_crds`][2]
642/// function. It automatically calls the `crd` methods of the CRD in all of its
643/// versions and additionally provides a strongly typed selector for the stored
644/// API version.
645///
646/// ```
647/// # use stackable_versioned_macros::versioned;
648/// # use kube::CustomResource;
649/// # use schemars::JsonSchema;
650/// # use serde::{Deserialize, Serialize};
651/// #[versioned(version(name = "v1alpha1"), version(name = "v1beta1"))]
652/// mod versioned {
653/// #[versioned(crd(group = "example.com"))]
654/// #[derive(Clone, Debug, Deserialize, Serialize, CustomResource, JsonSchema)]
655/// pub struct FooSpec {
656/// #[versioned(added(since = "v1beta1"))]
657/// bar: usize,
658/// baz: bool,
659/// }
660/// }
661///
662/// # fn main() {
663/// let merged_crd = Foo::merged_crd(FooVersion::V1Beta1).unwrap();
664/// println!("{yaml}", yaml = serde_yaml::to_string(&merged_crd).unwrap());
665/// # }
666/// ```
667///
668/// The strongly typed version enum looks very similar to the entry enum
669/// described above. It additionally provides various associated functions used
670/// for parsing and string representations.
671///
672/// ```
673/// #[derive(Copy, Clone, Debug)]
674/// pub enum FooVersion {
675/// V1Alpha1,
676/// V1Beta1,
677/// }
678/// ```
679///
680/// ---
681///
682/// The generation of merging helpers can be skipped if manual implementation
683/// is desired. The following piece of code lists all possible locations where
684/// this skip flag can be provided.
685///
686/// ```
687/// # use stackable_versioned_macros::versioned;
688/// # use kube::CustomResource;
689/// # use schemars::JsonSchema;
690/// # use serde::{Deserialize, Serialize};
691/// #
692/// # #[versioned(version(name = "v1alpha1"))]
693/// #[versioned(skip(merged_crd))] // Skip generation for ALL specs
694/// mod versioned {
695/// #[versioned(skip(merged_crd))] // Skip generation for specific specs
696///
697/// # #[versioned(crd(group = "example.com"))]
698/// # #[derive(Clone, Debug, CustomResource, Deserialize, Serialize, JsonSchema)]
699/// pub struct FooSpec {}
700/// }
701/// #
702/// # fn main() {}
703/// ```
704///
705/// ## Convert CustomResources
706///
707/// The conversion of CRs is tightly integrated with [`ConversionReview`]s, the
708/// payload which a conversion webhook receives from the Kubernetes apiserver.
709/// Naturally, the `try_convert` function takes in [`ConversionReview`] as a
710/// parameter and also returns a [`ConversionReview`] indicating success or
711/// failure.
712///
713/// ```ignore
714/// # use stackable_versioned_macros::versioned;
715/// # use kube::CustomResource;
716/// # use schemars::JsonSchema;
717/// # use serde::{Deserialize, Serialize};
718/// #[versioned(
719/// version(name = "v1alpha1"),
720/// version(name = "v1beta1"),
721/// version(name = "v1")
722/// )]
723/// mod versioned {
724/// #[versioned(crd(group = "example.com"))]
725/// #[derive(Clone, Debug, Deserialize, Serialize, CustomResource, JsonSchema)]
726/// pub struct FooSpec {
727/// #[versioned(added(since = "v1beta1"))]
728/// bar: usize,
729///
730/// #[versioned(added(since = "v1"))]
731/// baz: bool,
732///
733/// quox: String,
734/// }
735/// }
736///
737/// # fn main() {
738/// let conversion_review = Foo::try_convert(conversion_review);
739/// # }
740/// ```
741///
742/// The generation of conversion helpers can be skipped if manual implementation
743/// is desired. The following piece of code lists all possible locations where
744/// this skip flag can be provided:
745///
746/// ```
747/// # use stackable_versioned_macros::versioned;
748/// # use kube::CustomResource;
749/// # use schemars::JsonSchema;
750/// # use serde::{Deserialize, Serialize};
751/// #
752/// # #[versioned(version(name = "v1alpha1"))]
753/// #[versioned(skip(try_convert))] // Skip generation for ALL specs
754/// mod versioned {
755/// #[versioned(skip(try_convert))] // Skip generation for specific specs
756///
757/// # #[versioned(crd(group = "example.com"))]
758/// # #[derive(Clone, Debug, CustomResource, Deserialize, Serialize, JsonSchema)]
759/// pub struct FooSpec {}
760/// }
761/// #
762/// # fn main() {}
763/// ```
764///
765/// ### Conversion Tracking
766///
767/// <div class="warning">
768///
769/// This is a highly experimental feature. To enable it, provide the
770/// `experimental_conversion_tracking` flag. See the
771/// [additional options](#additional-options) section for more information.
772///
773/// </div>
774///
775/// As per recommendation by the Kubernetes project, conversions should aim to
776/// be infallible and lossless. The above example perfectly illustrates that
777/// achieving this is not as easy as it looks on the surface. Let's assume the
778/// following conditions:
779///
780/// - The CRD's latest version `v1` is marked as the stored version.
781/// - A client requests a CR in an earlier version, in this case `v1alpha1`.
782///
783/// The Kubernetes apiserver retrieves the stored object in `v1` from etcd.
784/// It needs to be downgraded to `v1alpha1` to be able to serve the client the
785/// correct requested version. As defined above, the field `baz` was only added
786/// in `v1` and `bar` was added in `v1beta1` and as such both don't exist in
787/// `v1alpha1`. During the downgrade, the conversion would lose these pieces of
788/// data when upgrading to `v1` again after the client did it's changes. This
789/// macro however provides a mechanism to automatically track values across
790/// conversations without data loss.
791///
792/// <div class="warning">
793///
794/// Currently, only tracking of **added** fields is supported. This will be
795/// expanded to removed fields, field type changes, and fields containing
796/// collections in the future.
797///
798/// </div>
799///
800/// There are many moving parts to enable this mechanism to work automatically
801/// with minimal manual developer input. Pretty much all of the required code
802/// can be generated based on a simple CRD definition described above. The
803/// following paragraphs explain various parts of the system in more detail.
804/// For a complete overview, it is however advised to look at the source code
805/// of the macro and the code it produces.
806///
807/// #### Tracking Values in the Status
808///
809/// Tracking changed values across conversions requires state. In this context,
810/// storing this state as close to the CustomResource as possible is essential.
811/// This is due to various factors such as avoiding external calls (which are
812/// susceptible to network errors), reducing the risk of state drift, and
813/// making the use of conversions as easy as possible straight out of the box;
814/// for both users and cluster administrators.
815///
816/// As such, values are tracked using the CustomResource's status via the
817/// `changedValues` field. This field contains two sections, one for upgrades
818/// and one for downgrades. Both of these sections contain version keys which
819/// list all tracked values for a particular version.
820///
821/// ```yaml
822/// status:
823/// changedValues:
824/// downgrades: null
825/// upgrades:
826/// v1beta1:
827/// - fieldName: "bar"
828/// value: 42
829/// v1:
830/// - fieldName: "baz"
831/// value: true
832/// ```
833///
834/// To continue the above example, upgrading the CustomResource from `v1alpha1`
835/// back to `v1` requires us to re-hydrate the resource with the tracked values.
836/// First, the resource is upgraded to `v1beta1`. During this step, the tracked
837/// value for the `bar` field is applied and removed from the status afterwards.
838/// The final upgrade to `v1` will apply the tracked value for the field `baz`.
839/// Again, it is removed from the status afterwards.
840///
841/// ### Tracking Nested Changes
842///
843/// To be able to automatically track values of changed fields in nested sub
844/// structs of specs, the fields needs to be marked with `#[versioned(nested)]`.
845/// This will indicate the macro to generate the appropriate conversion
846/// functions.
847///
848/// ```
849/// # use stackable_versioned_macros::versioned;
850/// # use kube::CustomResource;
851/// # use schemars::JsonSchema;
852/// # use serde::{Deserialize, Serialize};
853/// #[versioned(
854/// version(name = "v1alpha1"),
855/// version(name = "v1beta1"),
856/// options(k8s(experimental_conversion_tracking))
857/// )]
858/// mod versioned {
859/// #[versioned(crd(group = "example.com"))]
860/// #[derive(Clone, Debug, Deserialize, Serialize, CustomResource, JsonSchema)]
861/// struct FooSpec {
862/// bar: usize,
863///
864/// // TODO: This technically needs to be combined with a change, but
865/// // we want proper, per-container versioning before we add the correct
866/// // attributes here.
867/// #[versioned(nested)]
868/// baz: Baz,
869/// }
870///
871/// #[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
872/// struct Baz {
873/// quax: String,
874///
875/// #[versioned(added(since = "v1beta1"))]
876/// quox: bool,
877/// }
878/// }
879/// # fn main() {}
880/// ```
881///
882/// # OpenTelemetry Semantic Conventions
883///
884/// If tracing is enabled, various traces and events are emitted. The fields of
885/// these signals follow the general rules of OpenTelemetry semantic conventions.
886/// There are currently no agreed-upon semantic conventions for CRD conversions.
887/// In the meantime these fields are used:
888///
889/// | Field | Type (Example) | Description |
890/// | :---- | :------------- | :---------- |
891/// | `k8s.crd.conversion.converted_object_count` | usize (6) | The number of successfully converted objects sent back in a conversion review |
892/// | `k8s.crd.conversion.desired_api_version` | String (v1alpha1) | The desired api version received via a conversion review |
893/// | `k8s.crd.conversion.api_version` | String (v1beta1) | The current api version of an object received via a conversion review |
894/// | `k8s.crd.conversion.steps` | usize (2) | The number of steps required to convert a single object from the current to the desired version |
895/// | `k8s.crd.conversion.kind` | String (Foo) | The kind of the CRD |
896///
897/// [1]: https://docs.rs/schemars/latest/schemars/derive.JsonSchema.html
898/// [2]: https://docs.rs/kube/latest/kube/core/crd/fn.merge_crds.html
899/// [k8s-version-format]: https://kubernetes.io/docs/reference/using-api/#api-versioning
900/// [k8s-crd-ver-deprecation]: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#version-deprecation
901#[proc_macro_attribute]
902pub fn versioned(attrs: TokenStream, input: TokenStream) -> TokenStream {
903 let input = syn::parse_macro_input!(input as Item);
904 versioned_impl(attrs.into(), input).into()
905}
906
907fn versioned_impl(attrs: proc_macro2::TokenStream, input: Item) -> proc_macro2::TokenStream {
908 // TODO (@Techassi): Think about how we can handle nested structs / enums which
909 // are also versioned.
910
911 match input {
912 Item::Mod(item_mod) => {
913 let module_attributes: ModuleAttributes = match parse_outer_attributes(attrs) {
914 Ok(ma) => ma,
915 Err(err) => return err.write_errors(),
916 };
917
918 let module = match Module::new(item_mod, module_attributes) {
919 Ok(module) => module,
920 Err(err) => return err.write_errors(),
921 };
922
923 module.generate_tokens()
924 }
925 _ => Error::new(
926 input.span(),
927 "attribute macro `versioned` can be only be applied to modules",
928 )
929 .into_compile_error(),
930 }
931}
932
933fn parse_outer_attributes<T>(attrs: proc_macro2::TokenStream) -> Result<T, darling::Error>
934where
935 T: FromMeta,
936{
937 let nm = NestedMeta::parse_meta_list(attrs)?;
938 T::from_list(&nm)
939}
940
941#[cfg(test)]
942mod snapshots {
943 use insta::{assert_snapshot, glob};
944
945 use super::*;
946
947 #[test]
948 fn pass() {
949 // TODO (@Techassi): Re-add skip tests
950 let _settings_guard = test_utils::set_snapshot_path().bind_to_scope();
951
952 glob!("../tests/inputs/pass", "*.rs", |path| {
953 let formatted = test_utils::expand_from_file(path)
954 .inspect_err(|err| eprintln!("{err}"))
955 .unwrap();
956 assert_snapshot!(formatted);
957 });
958 }
959}