|
| 1 | +use darling::{FromMeta, util::Flag}; |
| 2 | +use proc_macro2::TokenStream; |
| 3 | +use quote::{ToTokens, quote}; |
| 4 | +use syn::{Path, parse_quote}; |
| 5 | + |
| 6 | +use crate::attrs::common::Override; |
| 7 | + |
| 8 | +/// This struct contains supported Kubernetes arguments. |
| 9 | +/// |
| 10 | +/// The arguments are passed through to the `#[kube]` attribute. More details can be found in the |
| 11 | +/// official docs: <https://docs.rs/kube/latest/kube/derive.CustomResource.html>. |
| 12 | +/// |
| 13 | +/// Supported arguments are: |
| 14 | +/// |
| 15 | +/// - `group`: Set the group of the CR object, usually the domain of the company. |
| 16 | +/// This argument is Required. |
| 17 | +/// - `kind`: Override the kind field of the CR object. This defaults to the struct |
| 18 | +/// name (without the 'Spec' suffix). |
| 19 | +/// - `singular`: Set the singular name of the CR object. |
| 20 | +/// - `plural`: Set the plural name of the CR object. |
| 21 | +/// - `namespaced`: Indicate that this is a namespaced scoped resource rather than a |
| 22 | +/// cluster scoped resource. |
| 23 | +/// - `crates`: Override specific crates. |
| 24 | +/// - `status`: Set the specified struct as the status subresource. |
| 25 | +/// - `shortname`: Set a shortname for the CR object. This can be specified multiple |
| 26 | +/// times. |
| 27 | +/// - `skip`: Controls skipping parts of the generation. |
| 28 | +#[derive(Clone, Debug, FromMeta)] |
| 29 | +pub struct KubernetesArguments { |
| 30 | + pub group: String, |
| 31 | + pub kind: Option<String>, |
| 32 | + pub singular: Option<String>, |
| 33 | + pub plural: Option<String>, |
| 34 | + pub namespaced: Flag, |
| 35 | + // root |
| 36 | + #[darling(default)] |
| 37 | + pub crates: KubernetesCrateArguments, |
| 38 | + pub status: Option<Path>, |
| 39 | + // derive |
| 40 | + // schema |
| 41 | + // scale |
| 42 | + // printcolumn |
| 43 | + #[darling(multiple, rename = "shortname")] |
| 44 | + pub shortnames: Vec<String>, |
| 45 | + // category |
| 46 | + // selectable |
| 47 | + // doc |
| 48 | + // annotation |
| 49 | + // label |
| 50 | + pub skip: Option<KubernetesSkipArguments>, |
| 51 | + |
| 52 | + #[darling(default)] |
| 53 | + pub options: KubernetesConfigOptions, |
| 54 | +} |
| 55 | + |
| 56 | +/// This struct contains supported kubernetes skip arguments. |
| 57 | +/// |
| 58 | +/// Supported arguments are: |
| 59 | +/// |
| 60 | +/// - `merged_crd` flag, which skips generating the `crd()` and `merged_crd()` functions are |
| 61 | +/// generated. |
| 62 | +#[derive(Clone, Debug, FromMeta)] |
| 63 | +pub struct KubernetesSkipArguments { |
| 64 | + /// Whether the `crd()` and `merged_crd()` generation should be skipped for |
| 65 | + /// this container. |
| 66 | + pub merged_crd: Flag, |
| 67 | +} |
| 68 | + |
| 69 | +/// This struct contains crate overrides to be passed to `#[kube]`. |
| 70 | +#[derive(Clone, Debug, FromMeta)] |
| 71 | +pub struct KubernetesCrateArguments { |
| 72 | + #[darling(default = default_kube_core)] |
| 73 | + pub kube_core: Override<Path>, |
| 74 | + |
| 75 | + #[darling(default = default_kube_client)] |
| 76 | + pub kube_client: Override<Path>, |
| 77 | + |
| 78 | + #[darling(default = default_k8s_openapi)] |
| 79 | + pub k8s_openapi: Override<Path>, |
| 80 | + |
| 81 | + #[darling(default = default_schemars)] |
| 82 | + pub schemars: Override<Path>, |
| 83 | + |
| 84 | + #[darling(default = default_serde)] |
| 85 | + pub serde: Override<Path>, |
| 86 | + |
| 87 | + #[darling(default = default_serde_json)] |
| 88 | + pub serde_json: Override<Path>, |
| 89 | + |
| 90 | + #[darling(default = default_versioned)] |
| 91 | + pub versioned: Override<Path>, |
| 92 | +} |
| 93 | + |
| 94 | +impl Default for KubernetesCrateArguments { |
| 95 | + fn default() -> Self { |
| 96 | + Self { |
| 97 | + kube_core: default_kube_core(), |
| 98 | + kube_client: default_kube_client(), |
| 99 | + k8s_openapi: default_k8s_openapi(), |
| 100 | + schemars: default_schemars(), |
| 101 | + serde: default_serde(), |
| 102 | + serde_json: default_serde_json(), |
| 103 | + versioned: default_versioned(), |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +fn default_kube_core() -> Override<Path> { |
| 109 | + Override::Default(parse_quote! { ::kube::core }) |
| 110 | +} |
| 111 | + |
| 112 | +fn default_kube_client() -> Override<Path> { |
| 113 | + Override::Default(parse_quote! { ::kube::client }) |
| 114 | +} |
| 115 | + |
| 116 | +fn default_k8s_openapi() -> Override<Path> { |
| 117 | + Override::Default(parse_quote! { ::k8s_openapi }) |
| 118 | +} |
| 119 | + |
| 120 | +fn default_schemars() -> Override<Path> { |
| 121 | + Override::Default(parse_quote! { ::schemars }) |
| 122 | +} |
| 123 | + |
| 124 | +fn default_serde() -> Override<Path> { |
| 125 | + Override::Default(parse_quote! { ::serde }) |
| 126 | +} |
| 127 | + |
| 128 | +fn default_serde_json() -> Override<Path> { |
| 129 | + Override::Default(parse_quote! { ::serde_json }) |
| 130 | +} |
| 131 | + |
| 132 | +fn default_versioned() -> Override<Path> { |
| 133 | + Override::Default(parse_quote! { ::stackable_versioned }) |
| 134 | +} |
| 135 | + |
| 136 | +impl ToTokens for KubernetesCrateArguments { |
| 137 | + fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { |
| 138 | + let mut crate_overrides = TokenStream::new(); |
| 139 | + |
| 140 | + let KubernetesCrateArguments { |
| 141 | + kube_client: _, |
| 142 | + k8s_openapi, |
| 143 | + serde_json, |
| 144 | + kube_core, |
| 145 | + schemars, |
| 146 | + serde, |
| 147 | + .. |
| 148 | + } = self; |
| 149 | + |
| 150 | + if let Override::Explicit(k8s_openapi) = k8s_openapi { |
| 151 | + crate_overrides.extend(quote! { k8s_openapi = #k8s_openapi, }); |
| 152 | + } |
| 153 | + |
| 154 | + if let Override::Explicit(serde_json) = serde_json { |
| 155 | + crate_overrides.extend(quote! { serde_json = #serde_json, }); |
| 156 | + } |
| 157 | + |
| 158 | + if let Override::Explicit(kube_core) = kube_core { |
| 159 | + crate_overrides.extend(quote! { kube_core = #kube_core, }); |
| 160 | + } |
| 161 | + |
| 162 | + if let Override::Explicit(schemars) = schemars { |
| 163 | + crate_overrides.extend(quote! { schemars = #schemars, }); |
| 164 | + } |
| 165 | + |
| 166 | + if let Override::Explicit(serde) = serde { |
| 167 | + crate_overrides.extend(quote! { serde = #serde, }); |
| 168 | + } |
| 169 | + |
| 170 | + if !crate_overrides.is_empty() { |
| 171 | + tokens.extend(quote! { , crates(#crate_overrides) }); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +#[derive(Clone, Default, Debug, FromMeta)] |
| 177 | +pub struct KubernetesConfigOptions { |
| 178 | + pub experimental_conversion_tracking: Flag, |
| 179 | +} |
0 commit comments