Skip to content

Commit fbebe74

Browse files
Techassisbernauer
andauthored
feat(versioned): Add support to pass the scale argument (#1185)
* feat(versioned): Add support to pass the scale argument * test(versioned): Add snapshot test for scale argument * chore(versioned): Add changelog entry * refactor: Destructure Scale struct during token generation Co-authored-by: Sebastian Bernauer <sebastian.bernauer@stackable.tech> --------- Co-authored-by: Sebastian Bernauer <sebastian.bernauer@stackable.tech>
1 parent d5ffec2 commit fbebe74

6 files changed

Lines changed: 300 additions & 3 deletions

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub struct ContainerSkipArguments {
5050
/// cluster scoped resource.
5151
/// - `crates`: Override specific crates.
5252
/// - `status`: Set the specified struct as the status subresource.
53+
/// - `scale`: Configure the scale subresource for horizontal pod autoscaling integration.
5354
/// - `shortname`: Set a shortname for the CR object. This can be specified multiple
5455
/// times.
5556
/// - `skip`: Controls skipping parts of the generation.
@@ -64,7 +65,7 @@ pub struct StructCrdArguments {
6465
pub status: Option<Path>,
6566
// derive
6667
// schema
67-
// scale
68+
pub scale: Option<Scale>,
6869
// printcolumn
6970
#[darling(multiple, rename = "shortname")]
7071
pub shortnames: Vec<String>,
@@ -74,3 +75,21 @@ pub struct StructCrdArguments {
7475
// annotation
7576
// label
7677
}
78+
79+
/// Scale subresource configuration for a CRD.
80+
///
81+
/// Mirrors the fields of [`k8s_openapi::CustomResourceSubresourceScale`][1] and what is present in
82+
/// `kube_derive`.
83+
///
84+
/// [1]: k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceSubresourceScale
85+
//
86+
// TODO (@Techassi): This should eventually get replaced by directly using what `kube_derive` offers,
87+
// but that requires an upstream restructure I'm planning to do soon(ish).
88+
#[derive(Clone, Debug, FromMeta)]
89+
pub struct Scale {
90+
pub spec_replicas_path: String,
91+
pub status_replicas_path: String,
92+
93+
#[darling(default)]
94+
pub label_selector_path: Option<String>,
95+
}

crates/stackable-versioned-macros/src/codegen/container/struct/mod.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use quote::quote;
55
use syn::{Generics, ItemStruct};
66

77
use crate::{
8-
attrs::container::{ContainerAttributes, StructCrdArguments},
8+
attrs::container::{ContainerAttributes, Scale, StructCrdArguments},
99
codegen::{
1010
Direction, VersionContext, VersionDefinition,
1111
changes::Neighbors,
@@ -272,6 +272,28 @@ impl Struct {
272272
_ => None,
273273
};
274274

275+
let scale = spec_gen_ctx
276+
.kubernetes_arguments
277+
.scale
278+
.as_ref()
279+
.map(|scale| {
280+
let Scale {
281+
spec_replicas_path,
282+
status_replicas_path,
283+
label_selector_path,
284+
} = scale;
285+
286+
let label_selector_path = label_selector_path
287+
.as_ref()
288+
.map(|p| quote! { , label_selector_path = #p });
289+
290+
quote! { , scale(
291+
spec_replicas_path = #spec_replicas_path,
292+
status_replicas_path = #status_replicas_path
293+
#label_selector_path
294+
)}
295+
});
296+
275297
let shortnames: TokenStream = spec_gen_ctx
276298
.kubernetes_arguments
277299
.shortnames
@@ -286,7 +308,7 @@ impl Struct {
286308
// These must be comma separated (except the last) as they always exist:
287309
group = #group, version = #version, kind = #kind
288310
// These fields are optional, and therefore the token stream must prefix each with a comma:
289-
#singular #plural #namespaced #crates #status #shortnames
311+
#singular #plural #namespaced #crates #status #scale #shortnames
290312
)]
291313
})
292314
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use stackable_versioned::versioned;
2+
// ---
3+
#[versioned(version(name = "v1alpha1"))]
4+
// ---
5+
pub(crate) mod versioned {
6+
#[versioned(crd(
7+
group = "stackable.tech",
8+
scale(
9+
spec_replicas_path = ".spec.replicas",
10+
status_replicas_path = ".status.replicas",
11+
label_selector_path = ".status.selector"
12+
)
13+
))]
14+
#[derive(
15+
Clone,
16+
Debug,
17+
serde::Deserialize,
18+
serde::Serialize,
19+
schemars::JsonSchema,
20+
kube::CustomResource,
21+
)]
22+
struct FooSpec {
23+
bar: usize,
24+
baz: bool,
25+
}
26+
}
27+
// ---
28+
fn main() {}

crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@scale.rs.snap

Lines changed: 221 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/stackable-versioned-macros/tests/trybuild.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod inputs {
3030
// mod module_preserve;
3131
// mod renamed_field;
3232
// mod renamed_kind;
33+
// mod scale;
3334
// mod shortnames;
3435
// mod submodule;
3536
}

crates/stackable-versioned/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- Add support to provide `#[versioned(crd(scale(...)))]` argument to enable the `/scale` subresource ([#1185]).
10+
11+
[#1185]: https://github.com/stackabletech/operator-rs/pull/1185
12+
713
## [0.8.3] - 2025-10-23
814

915
### Fixed

0 commit comments

Comments
 (0)