Skip to content

Commit 2e32cd7

Browse files
committed
refactor: Move webhook and CRD maintainer creation into function
1 parent 1e33f3d commit 2e32cd7

2 files changed

Lines changed: 70 additions & 27 deletions

File tree

rust/operator-binary/src/crd/mod.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
use std::{fmt::Display, ops::Deref};
22

33
use serde::{Deserialize, Serialize};
4-
use snafu::Snafu;
5-
use stackable_operator::schemars::{self, JsonSchema};
4+
use snafu::{ResultExt, Snafu};
5+
use stackable_operator::{
6+
cli::{MaintenanceOptions, OperatorEnvironmentOptions},
7+
kube::{Client, core::crd::MergeError},
8+
schemars::{self, JsonSchema},
9+
webhook::{
10+
maintainer::CustomResourceDefinitionMaintainer,
11+
servers::{ConversionWebhookError, ConversionWebhookServer},
12+
},
13+
};
614

715
mod secret_class;
816
mod trust_store;
@@ -18,8 +26,11 @@ pub mod v1alpha2 {
1826
pub use crate::crd::secret_class::v1alpha2::*;
1927
}
2028

29+
use tokio::sync::oneshot;
2130
pub use trust_store::{TrustStore, TrustStoreVersion};
2231

32+
use crate::FIELD_MANAGER;
33+
2334
#[derive(Debug, Snafu)]
2435
#[snafu(module)]
2536
pub enum InvalidKerberosPrincipal {
@@ -76,3 +87,50 @@ impl Deref for KerberosPrincipal {
7687
&self.0
7788
}
7889
}
90+
91+
/// Contains errors which can be encountered when creating the conversion webhook server and the
92+
/// CRD maintainer.
93+
#[derive(Debug, Snafu)]
94+
pub enum Error {
95+
#[snafu(display("failed to merge CRD"))]
96+
MergeCrd { source: MergeError },
97+
98+
#[snafu(display("failed to create conversion webhook server"))]
99+
CreateConversionWebhook { source: ConversionWebhookError },
100+
}
101+
102+
/// Creates and returns a [`ConversionWebhookServer`] and a [`CustomResourceDefinitionMaintainer`].
103+
pub async fn create_conversion_webhook_and_maintainer<'a>(
104+
operator_environment: &'a OperatorEnvironmentOptions,
105+
maintenance: &MaintenanceOptions,
106+
client: Client,
107+
) -> Result<
108+
(
109+
ConversionWebhookServer,
110+
CustomResourceDefinitionMaintainer<'a>,
111+
oneshot::Receiver<()>,
112+
),
113+
Error,
114+
> {
115+
let crds_and_handlers = [
116+
(
117+
SecretClass::merged_crd(SecretClassVersion::V1Alpha2).context(MergeCrdSnafu)?,
118+
SecretClass::try_convert as fn(_) -> _,
119+
),
120+
(
121+
TrustStore::merged_crd(TrustStoreVersion::V1Alpha1).context(MergeCrdSnafu)?,
122+
TrustStore::try_convert as fn(_) -> _,
123+
),
124+
];
125+
126+
ConversionWebhookServer::with_maintainer(
127+
crds_and_handlers,
128+
&operator_environment.operator_service_name,
129+
&operator_environment.operator_namespace,
130+
FIELD_MANAGER,
131+
maintenance.disable_crd_maintenance,
132+
client,
133+
)
134+
.await
135+
.context(CreateConversionWebhookSnafu)
136+
}

rust/operator-binary/src/main.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use stackable_operator::{
2121
kvp::{Label, LabelExt},
2222
shared::yaml::SerializeOptions,
2323
telemetry::Tracing,
24-
webhook::servers::ConversionWebhookServer,
2524
};
2625
use tokio::{
2726
signal::unix::{SignalKind, signal},
@@ -31,7 +30,10 @@ use tokio_stream::wrappers::UnixListenerStream;
3130
use tonic::transport::Server;
3231
use utils::{TonicUnixStream, uds_bind_private};
3332

34-
use crate::crd::{SecretClass, SecretClassVersion, TrustStore, TrustStoreVersion, v1alpha2};
33+
use crate::crd::{
34+
SecretClass, SecretClassVersion, TrustStore, TrustStoreVersion,
35+
create_conversion_webhook_and_maintainer, v1alpha2,
36+
};
3537

3638
mod backend;
3739
mod crd;
@@ -79,9 +81,9 @@ async fn main() -> anyhow::Result<()> {
7981
let opts = Opts::parse();
8082
match opts.cmd {
8183
stackable_operator::cli::Command::Crd => {
82-
SecretClass::merged_crd(crd::SecretClassVersion::V1Alpha2)?
84+
SecretClass::merged_crd(SecretClassVersion::V1Alpha2)?
8385
.print_yaml_schema(built_info::PKG_VERSION, SerializeOptions::default())?;
84-
TrustStore::merged_crd(crd::TrustStoreVersion::V1Alpha1)?
86+
TrustStore::merged_crd(TrustStoreVersion::V1Alpha1)?
8587
.print_yaml_schema(built_info::PKG_VERSION, SerializeOptions::default())?;
8688
}
8789
stackable_operator::cli::Command::Run(SecretOperatorRun {
@@ -129,30 +131,13 @@ async fn main() -> anyhow::Result<()> {
129131
let _ = std::fs::remove_file(&csi_endpoint);
130132
}
131133

132-
// NOTE (@Techassi): This could maybe be moved into a setup function again. For now,
133-
// it is here.
134-
let crds_and_handlers = [
135-
(
136-
SecretClass::merged_crd(SecretClassVersion::V1Alpha2)?,
137-
SecretClass::try_convert as fn(_) -> _,
138-
),
139-
(
140-
TrustStore::merged_crd(TrustStoreVersion::V1Alpha1)?,
141-
TrustStore::try_convert as fn(_) -> _,
142-
),
143-
];
144-
145134
let (conversion_webhook, crd_maintainer, initial_reconcile_rx) =
146-
ConversionWebhookServer::with_maintainer(
147-
crds_and_handlers,
148-
&operator_environment.operator_service_name,
149-
&operator_environment.operator_namespace,
150-
FIELD_MANAGER,
151-
maintenance.disable_crd_maintenance,
135+
create_conversion_webhook_and_maintainer(
136+
&operator_environment,
137+
&maintenance,
152138
client.as_kube_client(),
153139
)
154-
.await
155-
.context("failed to create conversion webhook server and CRD maintainer")?;
140+
.await?;
156141

157142
let mut sigterm = signal(SignalKind::terminate())?;
158143
let csi_server = Server::builder()

0 commit comments

Comments
 (0)