Skip to content

Commit daf9908

Browse files
authored
feat(stackable-versioned): Add YAML serialization for merged CRD (#884)
* feat(stackable-versioned): Add YAML serialization for merged CRD * chore: Add versions to local dependencies * chore: Add changelog entry * feat: Make YAML helpers fallible * test: Update snapshot file * chore: Remove explicit dependency versions * ci: Run cargo test on all features * test: Fix K8s doc test * ci: Run cargo-udeps with all features * chore: Add cargo-udeps ignores
1 parent 194d33e commit daf9908

File tree

9 files changed

+134
-15
lines changed

9 files changed

+134
-15
lines changed

.github/workflows/build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
with:
3939
key: udeps
4040
- run: cargo install --locked cargo-udeps@0.1.50
41-
- run: cargo udeps --all-targets
41+
- run: cargo udeps --all-targets --all-features
4242

4343
run_cargodeny:
4444
name: Run Cargo Deny
@@ -142,7 +142,7 @@ jobs:
142142
- uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3
143143
with:
144144
key: test
145-
- run: cargo test
145+
- run: cargo test --all-features
146146

147147
tests_passed:
148148
name: All tests passed

Cargo.lock

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/stackable-operator/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ repository.workspace = true
1111
time = ["dep:time"]
1212

1313
[dependencies]
14-
stackable-operator-derive = { path = "../stackable-operator-derive", version = "0.3.1" }
15-
stackable-shared = { path = "../stackable-shared", version = "0.0.1" }
14+
stackable-operator-derive = { path = "../stackable-operator-derive" }
15+
stackable-shared = { path = "../stackable-shared" }
1616

1717
chrono.workspace = true
1818
clap.workspace = true

crates/stackable-versioned-macros/Cargo.toml

+13-5
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,26 @@ repository.workspace = true
1010
[package.metadata."docs.rs"]
1111
all-features = true
1212

13-
# cargo-udeps throws an error that these dependencies are unused. They are,
14-
# however, used in K8s specific test cases. This is a false-positive and an
13+
[package.metadata.cargo-udeps.ignore]
14+
# cargo-udeps throws an error stating that these dependencies are unused. They
15+
# are, however, used in K8s specific test cases. This is a false-positive and an
1516
# apparent limitation of cargo-udeps. These entries can be removed once
1617
# cargo-udeps supports detecting usage of such dependencies.
17-
[package.metadata.cargo-udeps.ignore]
18-
development = ["schemars", "serde_yaml"]
18+
development = ["schemars", "serde_yaml", "stackable-versioned"]
19+
20+
# cargo-udeps throws an error stating that these dependencies are unused. They are all marked as
21+
# optional, which trips up cargo-udeps for whatever reason...
22+
normal = ["k8s-openapi", "kube", "stackable-shared"]
1923

2024
[lib]
2125
proc-macro = true
2226

2327
[features]
2428
full = ["k8s"]
25-
k8s = ["dep:kube", "dep:k8s-openapi"]
29+
k8s = ["dep:kube", "dep:k8s-openapi", "dep:stackable-shared"]
2630

2731
[dependencies]
32+
stackable-shared = { path = "../stackable-shared", optional = true }
2833
k8s-version = { path = "../k8s-version", features = ["darling"] }
2934

3035
convert_case.workspace = true
@@ -38,6 +43,9 @@ syn.workspace = true
3843
quote.workspace = true
3944

4045
[dev-dependencies]
46+
# Only needed for doc tests / examples
47+
stackable-versioned = { path = "../stackable-versioned", features = ["k8s"] }
48+
4149
insta.workspace = true
4250
prettyplease.workspace = true
4351
regex.workspace = true

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots.snap

+46-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/stackable-versioned-macros/src/codegen/vstruct/mod.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,51 @@ impl VersionedStruct {
382382

383383
#[automatically_derived]
384384
impl #enum_ident {
385-
/// Generates a merged CRD which contains all versions defined using the
386-
/// `#[versioned()]` macro.
385+
/// Generates a merged CRD which contains all versions defined using the `#[versioned()]` macro.
387386
pub fn merged_crd(
388387
stored_apiversion: Self
389388
) -> ::std::result::Result<::k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition, ::kube::core::crd::MergeError> {
390389
::kube::core::crd::merge_crds(vec![#(#crd_fn_calls),*], &stored_apiversion.to_string())
391390
}
391+
392+
/// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]`
393+
/// macro to a file located at `path`.
394+
pub fn write_merged_crd<P>(path: P, stored_apiversion: Self, operator_version: &str) -> Result<(), ::stackable_versioned::Error>
395+
where P: AsRef<::std::path::Path>
396+
{
397+
use ::stackable_shared::yaml::{YamlSchema, SerializeOptions};
398+
399+
let merged_crd = Self::merged_crd(stored_apiversion).map_err(|err| ::stackable_versioned::Error::MergeCrd {
400+
source: err,
401+
})?;
402+
403+
YamlSchema::write_yaml_schema(
404+
&merged_crd,
405+
path,
406+
operator_version,
407+
SerializeOptions::default()
408+
).map_err(|err| ::stackable_versioned::Error::SerializeYaml {
409+
source: err,
410+
})
411+
}
412+
413+
/// Generates and writes a merged CRD which contains all versions defined using the `#[versioned()]`
414+
/// macro to stdout.
415+
pub fn print_merged_crd(stored_apiversion: Self, operator_version: &str) -> Result<(), ::stackable_versioned::Error> {
416+
use ::stackable_shared::yaml::{YamlSchema, SerializeOptions};
417+
418+
let merged_crd = Self::merged_crd(stored_apiversion).map_err(|err| ::stackable_versioned::Error::MergeCrd {
419+
source: err,
420+
})?;
421+
422+
YamlSchema::print_yaml_schema(
423+
&merged_crd,
424+
operator_version,
425+
SerializeOptions::default()
426+
).map_err(|err| ::stackable_versioned::Error::SerializeYaml {
427+
source: err,
428+
})
429+
}
392430
}
393431
}
394432
}

crates/stackable-versioned/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ All notable changes to this project will be documented in this file.
66

77
### Added
88

9+
- Add YAML serialization for merged CRD schema. The schema can now be printed to stdout or written
10+
to file ([#884]).
911
- Add snapshot tests to verify generated code matches expected output ([#881]).
1012

1113
[#881]: https://github.com/stackabletech/operator-rs/pull/881
14+
[#884]: https://github.com/stackabletech/operator-rs/pull/884
1215

1316
## [0.3.0] - 2024-09-26
1417

crates/stackable-versioned/Cargo.toml

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ all-features = true
1212

1313
[features]
1414
full = ["k8s"]
15-
# Forward the k8s feature to the underlying macro crate
16-
k8s = ["stackable-versioned-macros/k8s"]
15+
k8s = [
16+
"stackable-versioned-macros/k8s", # Forward the k8s feature to the underlying macro crate
17+
"dep:stackable-shared",
18+
"dep:snafu",
19+
"dep:kube",
20+
]
1721

1822
[dependencies]
1923
stackable-versioned-macros = { path = "../stackable-versioned-macros" }
24+
stackable-shared = { path = "../stackable-shared", optional = true }
25+
26+
kube = { workspace = true, optional = true }
27+
snafu = { workspace = true, optional = true }

crates/stackable-versioned/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,21 @@
1212
//! See [`versioned`] for an in-depth usage guide and a list of supported
1313
//! parameters.
1414
15+
// Re-export macro
1516
pub use stackable_versioned_macros::*;
1617

18+
#[cfg(feature = "k8s")]
19+
#[derive(Debug, snafu::Snafu)]
20+
pub enum Error {
21+
#[snafu(display("failed to merge CRDs"))]
22+
MergeCrd { source: kube::core::crd::MergeError },
23+
24+
#[snafu(display("failed to serialize YAML"))]
25+
SerializeYaml {
26+
source: stackable_shared::yaml::Error,
27+
},
28+
}
29+
1730
// Unused for now, might get picked up again in the future.
1831
#[doc(hidden)]
1932
pub trait AsVersionStr {

0 commit comments

Comments
 (0)