@@ -2,40 +2,7 @@ use std::marker::PhantomData;
22
33use derivative:: Derivative ;
44use schemars:: JsonSchema ;
5- use semver:: Version ;
65use serde:: { Deserialize , Serialize } ;
7- use snafu:: { ResultExt , Snafu } ;
8-
9- use crate :: yaml;
10- use std:: fs:: File ;
11- use std:: io:: Write ;
12- use std:: path:: Path ;
13-
14- const DOCS_HOME_URL_PLACEHOLDER : & str = "DOCS_BASE_URL_PLACEHOLDER" ;
15- const DOCS_HOME_BASE_URL : & str = "https://docs.stackable.tech/home" ;
16-
17- type Result < T , E = Error > = std:: result:: Result < T , E > ;
18-
19- #[ derive( Debug , Snafu ) ]
20- pub enum Error {
21- #[ snafu( display( "cannot parse version {version:?} as a semantic version" ) ) ]
22- InvalidSemverVersion {
23- source : semver:: Error ,
24- version : String ,
25- } ,
26-
27- #[ snafu( display( "error converting CRD byte array to UTF-8" ) ) ]
28- ConvertByteArrayToUtf8 { source : std:: string:: FromUtf8Error } ,
29-
30- #[ snafu( display( "failed to serialize YAML" ) ) ]
31- SerializeYaml { source : yaml:: Error } ,
32-
33- #[ snafu( display( "failed to write YAML" ) ) ]
34- WriteYamlSchema { source : std:: io:: Error } ,
35-
36- #[ snafu( display( "failed to create YAML file" ) ) ]
37- CreateYamlFile { source : std:: io:: Error } ,
38- }
396
407/// A reference to a product cluster (for example, a `ZookeeperCluster`)
418///
@@ -96,83 +63,6 @@ pub trait HasApplication {
9663 fn get_application_name ( ) -> & ' static str ;
9764}
9865
99- /// Takes an operator version and returns a docs version
100- fn docs_version ( operator_version : & str ) -> Result < String > {
101- if operator_version == "0.0.0-dev" {
102- Ok ( "nightly" . to_owned ( ) )
103- } else {
104- let v = Version :: parse ( operator_version) . context ( InvalidSemverVersionSnafu {
105- version : operator_version. to_owned ( ) ,
106- } ) ?;
107- Ok ( format ! ( "{}.{}" , v. major, v. minor) )
108- }
109- }
110-
111- /// Given an operator version like 0.0.0-dev or 23.1.1, generate a docs home
112- /// component base URL like `https://docs.stackable.tech/home/nightly/` or
113- /// `https://docs.stackable.tech/home/23.1/`.
114- fn docs_home_versioned_base_url ( operator_version : & str ) -> Result < String > {
115- Ok ( format ! (
116- "{}/{}" ,
117- DOCS_HOME_BASE_URL ,
118- docs_version( operator_version) ?
119- ) )
120- }
121-
122- /// This trait can be implemented to allow automatic handling
123- /// (e.g. creation) of `CustomResourceDefinition`s in Kubernetes.
124- pub trait CustomResourceExt : kube:: CustomResourceExt {
125- /// Generates a YAML CustomResourceDefinition and writes it to a `Write`.
126- ///
127- /// The generated YAML string is an explicit document with leading dashes (`---`).
128- fn generate_yaml_schema < W > ( mut writer : W , operator_version : & str ) -> Result < ( ) >
129- where
130- W : Write ,
131- {
132- let mut buffer = Vec :: new ( ) ;
133- yaml:: serialize_to_explicit_document ( & mut buffer, & Self :: crd ( ) )
134- . context ( SerializeYamlSnafu ) ?;
135-
136- let yaml_schema = String :: from_utf8 ( buffer)
137- . context ( ConvertByteArrayToUtf8Snafu ) ?
138- . replace (
139- DOCS_HOME_URL_PLACEHOLDER ,
140- & docs_home_versioned_base_url ( operator_version) ?,
141- ) ;
142-
143- writer
144- . write_all ( yaml_schema. as_bytes ( ) )
145- . context ( WriteYamlSchemaSnafu )
146- }
147-
148- /// Generates a YAML CustomResourceDefinition and writes it to the specified file.
149- ///
150- /// The written YAML string is an explicit document with leading dashes (`---`).
151- fn write_yaml_schema < P : AsRef < Path > > ( path : P , operator_version : & str ) -> Result < ( ) > {
152- let writer = File :: create ( path) . context ( CreateYamlFileSnafu ) ?;
153- Self :: generate_yaml_schema ( writer, operator_version)
154- }
155-
156- /// Generates a YAML CustomResourceDefinition and prints it to stdout.
157- ///
158- /// The printed YAML string is an explicit document with leading dashes (`---`).
159- fn print_yaml_schema ( operator_version : & str ) -> Result < ( ) > {
160- let writer = std:: io:: stdout ( ) ;
161- Self :: generate_yaml_schema ( writer, operator_version)
162- }
163-
164- /// Returns the YAML schema of this CustomResourceDefinition as a string.
165- ///
166- /// The written YAML string is an explicit document with leading dashes (`---`).
167- fn yaml_schema ( operator_version : & str ) -> Result < String > {
168- let mut writer = Vec :: new ( ) ;
169- Self :: generate_yaml_schema ( & mut writer, operator_version) ?;
170- String :: from_utf8 ( writer) . context ( ConvertByteArrayToUtf8Snafu )
171- }
172- }
173-
174- impl < T > CustomResourceExt for T where T : kube:: CustomResourceExt { }
175-
17666#[ cfg( test) ]
17767mod tests {
17868 use k8s_openapi:: api:: core:: v1:: ConfigMap ;
0 commit comments