Skip to content

Commit 930396f

Browse files
authored
feat(versioned): Add support to override kube crate (#1173)
* feat(versioned): Add support to override kube crate * test(versioned): Add new kube override test * docs(versioned): Add kube crates override to doc comment * chore: Add changelog entry
1 parent d4ed55d commit 930396f

6 files changed

Lines changed: 497 additions & 1 deletion

File tree

crates/stackable-versioned-macros/src/attrs/module.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ pub struct ModuleSkipArguments {
9595

9696
/// This struct contains crate overrides to be passed to `#[kube]`.
9797
#[derive(Clone, Debug, FromMeta)]
98+
#[darling(and_then = "CrateArguments::dynamic_default")]
9899
pub struct CrateArguments {
100+
#[darling(default = default_kube)]
101+
pub kube: Override<Path>,
102+
99103
#[darling(default = default_kube_core)]
100104
pub kube_core: Override<Path>,
101105

@@ -124,6 +128,7 @@ pub struct CrateArguments {
124128
impl Default for CrateArguments {
125129
fn default() -> Self {
126130
Self {
131+
kube: default_kube(),
127132
kube_core: default_kube_core(),
128133
kube_client: default_kube_client(),
129134
k8s_openapi: default_k8s_openapi(),
@@ -136,6 +141,30 @@ impl Default for CrateArguments {
136141
}
137142
}
138143

144+
impl CrateArguments {
145+
fn dynamic_default(mut self) -> Result<Self> {
146+
// Adjust the kube_core and kube_client paths automatically if only the kube path is
147+
// overridden.
148+
if let Override::Explicit(kube_path) = &self.kube {
149+
if self.kube_core.is_explicit() || self.kube_client.is_explicit() {
150+
return Err(
151+
Error::custom("the `kube` crate override is mutually exclusive with `kube_core` and `kube_client`")
152+
.with_span(&kube_path)
153+
);
154+
}
155+
156+
self.kube_core = Override::Explicit(parse_quote! { #kube_path::core });
157+
self.kube_client = Override::Explicit(parse_quote! { #kube_path::client });
158+
}
159+
160+
Ok(self)
161+
}
162+
}
163+
164+
fn default_kube() -> Override<Path> {
165+
Override::Default(parse_quote! { ::kube })
166+
}
167+
139168
fn default_kube_core() -> Override<Path> {
140169
Override::Default(parse_quote! { ::kube::core })
141170
}
@@ -262,3 +291,12 @@ impl<T> Deref for Override<T> {
262291
}
263292
}
264293
}
294+
295+
impl<T> Override<T> {
296+
pub fn is_explicit(&self) -> bool {
297+
match self {
298+
Override::Default(_) => false,
299+
Override::Explicit(_) => true,
300+
}
301+
}
302+
}

crates/stackable-versioned-macros/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ mod utils;
200200
/// the crates are brought into scope through re-exports. The following code
201201
/// block depicts supported overrides and their default values.
202202
///
203-
/// ```
203+
/// ```ignore
204204
/// # use stackable_versioned_macros::versioned;
205205
/// #[versioned(
206206
/// version(name = "v1alpha1"),
@@ -213,6 +213,8 @@ mod utils;
213213
/// kube_core = "::kube::core",
214214
/// schemars = "::schemars",
215215
/// serde = "::serde",
216+
/// // Mutually exclusive with kube_core and kube_client
217+
/// kube = "::kube",
216218
/// )
217219
/// )]
218220
/// mod versioned {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use stackable_versioned::versioned;
2+
// ---
3+
#[versioned(
4+
version(name = "v1alpha1"),
5+
version(name = "v1beta1"),
6+
version(name = "v1"),
7+
crates(
8+
kube = ::kube,
9+
schemars = ::schemars
10+
)
11+
)]
12+
// ---
13+
pub mod versioned {
14+
#[versioned(crd(group = "foo.example.org"))]
15+
#[derive(
16+
Clone,
17+
Debug,
18+
serde::Deserialize,
19+
serde::Serialize,
20+
schemars::JsonSchema,
21+
kube::CustomResource,
22+
)]
23+
pub struct FooSpec {
24+
#[versioned(added(since = "v1beta1"), changed(since = "v1", from_name = "bah"))]
25+
bar: usize,
26+
baz: bool,
27+
}
28+
}
29+
// ---
30+
fn main() {}

0 commit comments

Comments
 (0)