Skip to content

Commit 1b35add

Browse files
committed
chore: Split CRD and YAML specific code
1 parent 209a7cc commit 1b35add

File tree

5 files changed

+69
-38
lines changed

5 files changed

+69
-38
lines changed

crates/stackable-operator/src/cli.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! use kube::CustomResource;
1515
//! use schemars::JsonSchema;
1616
//! use serde::{Deserialize, Serialize};
17-
//! use stackable_operator::{CustomResourceExt, cli, crd};
17+
//! use stackable_operator::{CustomResourceExt, cli, shared::crd};
1818
//!
1919
//! const OPERATOR_VERSION: &str = "23.1.1";
2020
//!
@@ -106,16 +106,17 @@
106106
//! ```
107107
//!
108108
//!
109-
use crate::logging::TracingTarget;
110-
use crate::namespace::WatchNamespace;
111-
use clap::Args;
112-
use product_config::ProductConfigManager;
113-
use snafu::{ResultExt, Snafu};
114109
use std::{
115110
ffi::OsStr,
116111
path::{Path, PathBuf},
117112
};
118113

114+
use clap::Args;
115+
use product_config::ProductConfigManager;
116+
use snafu::{ResultExt, Snafu};
117+
118+
use crate::{logging::TracingTarget, namespace::WatchNamespace};
119+
119120
pub const AUTHOR: &str = "Stackable GmbH - [email protected]";
120121

121122
type Result<T, E = Error> = std::result::Result<T, E>;

crates/stackable-operator/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ pub mod utils;
2222
pub mod validation;
2323

2424
// Internal re-exports
25-
pub use stackable_shared::yaml::CustomResourceExt;
25+
pub use stackable_shared::{crd::CustomResourceExt, yaml::YamlSchema};
26+
27+
pub mod shared {
28+
pub use stackable_shared::*;
29+
}
2630

2731
// External re-exports
2832
pub use ::k8s_openapi;

crates/stackable-shared/src/crd.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::path::Path;
2+
3+
use snafu::{ResultExt, Snafu};
4+
5+
use crate::yaml::{SerializeOptions, YamlSchema};
6+
7+
pub type Result<T, E = Error> = std::result::Result<T, E>;
8+
9+
#[derive(Debug, Snafu)]
10+
pub enum Error {
11+
#[snafu(display("failed to write CRD YAML schema to file"))]
12+
WriteToFile { source: crate::yaml::Error },
13+
14+
#[snafu(display("failed to write CRD YAML schema to stdout"))]
15+
WriteToStdout { source: crate::yaml::Error },
16+
17+
#[snafu(display("failed to generate CRD YAML schema"))]
18+
GenerateSchema { source: crate::yaml::Error },
19+
}
20+
21+
/// Provides YAML schema generation and output capabilities for Kubernetes custom resources.
22+
pub trait CustomResourceExt: kube::CustomResourceExt {
23+
/// Generates the YAML schema of a `CustomResourceDefinition` and writes it to the specified
24+
/// file at `path`.
25+
///
26+
/// It additionally replaces the documentation URL placeholder with the correct value based on
27+
/// the provided `operator_version`. The written YAML string is an explicit document with
28+
/// leading dashes (`---`).
29+
fn write_yaml_schema<P: AsRef<Path>>(path: P, operator_version: &str) -> Result<()> {
30+
Self::crd()
31+
.write_yaml_schema(path, operator_version, SerializeOptions::default())
32+
.context(WriteToFileSnafu)
33+
}
34+
35+
/// Generates the YAML schema of a `CustomResourceDefinition` and prints it to [stdout].
36+
///
37+
/// It additionally replaces the documentation URL placeholder with the correct value based on
38+
/// the provided `operator_version`. The written YAML string is an explicit document with
39+
/// leading dashes (`---`).
40+
///
41+
/// [stdout]: std::io::stdout
42+
fn print_yaml_schema(operator_version: &str) -> Result<()> {
43+
Self::crd()
44+
.print_yaml_schema(operator_version, SerializeOptions::default())
45+
.context(WriteToStdoutSnafu)
46+
}
47+
48+
/// Generates the YAML schema of a `CustomResourceDefinition` and returns it as a [`String`].
49+
fn yaml_schema(operator_version: &str) -> Result<String> {
50+
Self::crd()
51+
.generate_yaml_schema(operator_version, SerializeOptions::default())
52+
.context(GenerateSchemaSnafu)
53+
}
54+
}
55+
56+
impl<T> CustomResourceExt for T where T: kube::CustomResourceExt {}

crates/stackable-shared/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! This crate contains various shared helpers and utilities used across other crates in this
22
//! workspace.
33
4+
pub mod crd;
45
pub mod yaml;

crates/stackable-shared/src/yaml/mod.rs renamed to crates/stackable-shared/src/yaml.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -128,37 +128,6 @@ pub trait YamlSchema: Sized + serde::Serialize {
128128

129129
impl<T> YamlSchema for T where T: serde::ser::Serialize {}
130130

131-
/// Provides YAML schema generation and output capabilities for Kubernetes custom resources.
132-
pub trait CustomResourceExt: kube::CustomResourceExt {
133-
/// Generates the YAML schema of a `CustomResourceDefinition` and writes it to the specified
134-
/// file at `path`.
135-
///
136-
/// It additionally replaces the documentation URL placeholder with the correct value based on
137-
/// the provided `operator_version`. The written YAML string is an explicit document with
138-
/// leading dashes (`---`).
139-
fn write_yaml_schema<P: AsRef<Path>>(path: P, operator_version: &str) -> Result<()> {
140-
Self::crd().write_yaml_schema(path, operator_version, SerializeOptions::default())
141-
}
142-
143-
/// Generates the YAML schema of a `CustomResourceDefinition` and prints it to [stdout].
144-
///
145-
/// It additionally replaces the documentation URL placeholder with the correct value based on
146-
/// the provided `operator_version`. The written YAML string is an explicit document with
147-
/// leading dashes (`---`).
148-
///
149-
/// [stdout]: std::io::stdout
150-
fn print_yaml_schema(operator_version: &str) -> Result<()> {
151-
Self::crd().print_yaml_schema(operator_version, SerializeOptions::default())
152-
}
153-
154-
/// Generates the YAML schema of a `CustomResourceDefinition` and returns it as a [`String`].
155-
fn yaml_schema(operator_version: &str) -> Result<String> {
156-
Self::crd().generate_yaml_schema(operator_version, SerializeOptions::default())
157-
}
158-
}
159-
160-
impl<T> CustomResourceExt for T where T: kube::CustomResourceExt {}
161-
162131
/// Serializes the given data structure and writes it to a [`Writer`](Write).
163132
pub fn serialize<T, W>(value: &T, mut writer: W, options: SerializeOptions) -> Result<()>
164133
where

0 commit comments

Comments
 (0)