Skip to content

Commit 60bcfda

Browse files
committed
refactor: Move create_webhook_and_maintainer function
1 parent ffdab80 commit 60bcfda

4 files changed

Lines changed: 70 additions & 64 deletions

File tree

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

33
use serde::{Deserialize, Serialize};
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-
};
4+
use snafu::Snafu;
5+
use stackable_operator::schemars::{self, JsonSchema};
146

157
mod secret_class;
168
mod trust_store;
@@ -26,11 +18,8 @@ pub mod v1alpha2 {
2618
pub use crate::crd::secret_class::v1alpha2::*;
2719
}
2820

29-
use tokio::sync::oneshot;
3021
pub use trust_store::{TrustStore, TrustStoreVersion};
3122

32-
use crate::FIELD_MANAGER;
33-
3423
#[derive(Debug, Snafu)]
3524
#[snafu(module)]
3625
pub enum InvalidKerberosPrincipal {
@@ -87,50 +76,3 @@ impl Deref for KerberosPrincipal {
8776
&self.0
8877
}
8978
}
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ use tokio_stream::wrappers::UnixListenerStream;
3030
use tonic::transport::Server;
3131
use utils::{TonicUnixStream, uds_bind_private};
3232

33-
use crate::crd::{
34-
SecretClass, SecretClassVersion, TrustStore, TrustStoreVersion,
35-
create_conversion_webhook_and_maintainer, v1alpha2,
33+
use crate::{
34+
crd::{SecretClass, SecretClassVersion, TrustStore, TrustStoreVersion, v1alpha2},
35+
webhooks::conversion::create_webhook_and_maintainer,
3636
};
3737

3838
mod backend;
@@ -43,6 +43,7 @@ mod format;
4343
mod grpc;
4444
mod truststore_controller;
4545
mod utils;
46+
mod webhooks;
4647

4748
pub const OPERATOR_NAME: &str = "secrets.stackable.tech";
4849
pub const FIELD_MANAGER: &str = "secret-operator";
@@ -139,7 +140,7 @@ async fn main() -> anyhow::Result<()> {
139140
}
140141

141142
let (conversion_webhook, crd_maintainer, initial_reconcile_rx) =
142-
create_conversion_webhook_and_maintainer(
143+
create_webhook_and_maintainer(
143144
&operator_environment,
144145
&maintenance,
145146
client.as_kube_client(),
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use snafu::{ResultExt, Snafu};
2+
use stackable_operator::{
3+
cli::{MaintenanceOptions, OperatorEnvironmentOptions},
4+
kube::{Client, core::crd::MergeError},
5+
webhook::{
6+
maintainer::CustomResourceDefinitionMaintainer,
7+
servers::{ConversionWebhookError, ConversionWebhookServer},
8+
},
9+
};
10+
use tokio::sync::oneshot;
11+
12+
use crate::{
13+
FIELD_MANAGER,
14+
crd::{SecretClass, SecretClassVersion, TrustStore, TrustStoreVersion},
15+
};
16+
17+
/// Contains errors which can be encountered when creating the conversion webhook server and the
18+
/// CRD maintainer.
19+
#[derive(Debug, Snafu)]
20+
pub enum Error {
21+
#[snafu(display("failed to merge CRD"))]
22+
MergeCrd { source: MergeError },
23+
24+
#[snafu(display("failed to create conversion webhook server"))]
25+
CreateConversionWebhook { source: ConversionWebhookError },
26+
}
27+
28+
/// Creates and returns a [`ConversionWebhookServer`] and a [`CustomResourceDefinitionMaintainer`].
29+
pub async fn create_webhook_and_maintainer<'a>(
30+
operator_environment: &'a OperatorEnvironmentOptions,
31+
maintenance: &MaintenanceOptions,
32+
client: Client,
33+
) -> Result<
34+
(
35+
ConversionWebhookServer,
36+
CustomResourceDefinitionMaintainer<'a>,
37+
oneshot::Receiver<()>,
38+
),
39+
Error,
40+
> {
41+
let crds_and_handlers = [
42+
(
43+
SecretClass::merged_crd(SecretClassVersion::V1Alpha2).context(MergeCrdSnafu)?,
44+
SecretClass::try_convert as fn(_) -> _,
45+
),
46+
(
47+
TrustStore::merged_crd(TrustStoreVersion::V1Alpha1).context(MergeCrdSnafu)?,
48+
TrustStore::try_convert as fn(_) -> _,
49+
),
50+
];
51+
52+
ConversionWebhookServer::with_maintainer(
53+
crds_and_handlers,
54+
&operator_environment.operator_service_name,
55+
&operator_environment.operator_namespace,
56+
FIELD_MANAGER,
57+
maintenance.disable_crd_maintenance,
58+
client,
59+
)
60+
.await
61+
.context(CreateConversionWebhookSnafu)
62+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod conversion;

0 commit comments

Comments
 (0)