Skip to content

Commit 587f296

Browse files
committed
refactor: Rename traits and structs
1 parent caa81fc commit 587f296

File tree

5 files changed

+44
-46
lines changed

5 files changed

+44
-46
lines changed

crates/stackable-webhook/src/lib.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ::x509_cert::Certificate;
44
use axum::{Router, routing::get};
55
use futures_util::{FutureExt as _, TryFutureExt, select};
66
use k8s_openapi::ByteString;
7-
use servers::{WebhookServerImplementation, WebhookServerImplementationError};
7+
use servers::{Webhook, WebhookError};
88
use snafu::{ResultExt, Snafu};
99
use stackable_telemetry::AxumTraceLayer;
1010
use tokio::{
@@ -21,34 +21,32 @@ pub mod servers;
2121
pub mod tls;
2222

2323
/// A result type alias with the [`WebhookError`] type as the default error type.
24-
pub type Result<T, E = WebhookError> = std::result::Result<T, E>;
24+
pub type Result<T, E = WebhookServerError> = std::result::Result<T, E>;
2525

2626
#[derive(Debug, Snafu)]
27-
pub enum WebhookError {
27+
pub enum WebhookServerError {
2828
#[snafu(display("failed to create TLS server"))]
2929
CreateTlsServer { source: tls::TlsServerError },
3030

3131
#[snafu(display("failed to run TLS server"))]
3232
RunTlsServer { source: tls::TlsServerError },
3333

3434
#[snafu(display("failed to update certificate"))]
35-
UpdateCertificate {
36-
source: WebhookServerImplementationError,
37-
},
35+
UpdateCertificate { source: WebhookError },
3836

3937
#[snafu(display("failed to encode CA certificate as PEM format"))]
4038
EncodeCertificateAuthorityAsPem { source: x509_cert::der::Error },
4139
}
4240

4341
pub struct WebhookServer {
44-
options: WebhookOptions,
45-
webhooks: Vec<Box<dyn WebhookServerImplementation>>,
42+
options: WebhookServerOptions,
43+
webhooks: Vec<Box<dyn Webhook>>,
4644
tls_server: TlsServer,
4745
cert_rx: mpsc::Receiver<Certificate>,
4846
}
4947

5048
#[derive(Clone, Debug)]
51-
pub struct WebhookOptions {
49+
pub struct WebhookServerOptions {
5250
/// The default HTTPS socket address the [`TcpListener`][tokio::net::TcpListener]
5351
/// binds to.
5452
pub socket_addr: SocketAddr,
@@ -75,8 +73,8 @@ impl WebhookServer {
7573
SocketAddr::new(Self::DEFAULT_LISTEN_ADDRESS, Self::DEFAULT_HTTPS_PORT);
7674

7775
pub async fn new(
78-
options: WebhookOptions,
79-
webhooks: Vec<Box<dyn WebhookServerImplementation>>,
76+
options: WebhookServerOptions,
77+
webhooks: Vec<Box<dyn Webhook>>,
8078
) -> Result<Self> {
8179
tracing::trace!("create new webhook server");
8280

@@ -170,7 +168,7 @@ impl WebhookServer {
170168
} = self;
171169
let tls_server = tls_server
172170
.run()
173-
.map_err(|err| WebhookError::RunTlsServer { source: err });
171+
.map_err(|err| WebhookServerError::RunTlsServer { source: err });
174172

175173
let cert_update_loop = async {
176174
loop {

crates/stackable-webhook/src/servers/conversion_webhook.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use snafu::{ResultExt, Snafu, ensure};
2222
use tokio::sync::oneshot;
2323
use x509_cert::Certificate;
2424

25-
use super::{WebhookServerImplementation, WebhookServerImplementationError};
26-
use crate::WebhookOptions;
25+
use super::{Webhook, WebhookError};
26+
use crate::WebhookServerOptions;
2727

2828
#[derive(Debug, Snafu)]
2929
pub enum ConversionWebhookError {
@@ -37,7 +37,7 @@ pub enum ConversionWebhookError {
3737
},
3838
}
3939

40-
pub struct ConversionWebhookServer<H> {
40+
pub struct ConversionWebhook<H> {
4141
crds_and_handlers: Vec<(CustomResourceDefinition, H)>,
4242
disable_crd_maintenance: bool,
4343
client: Client,
@@ -49,7 +49,7 @@ pub struct ConversionWebhookServer<H> {
4949
initial_reconcile_tx: Option<oneshot::Sender<()>>,
5050
}
5151

52-
impl<H> ConversionWebhookServer<H> {
52+
impl<H> ConversionWebhook<H> {
5353
pub fn new(
5454
crds_and_handlers: impl IntoIterator<Item = (CustomResourceDefinition, H)>,
5555
disable_crd_maintenance: bool,
@@ -72,7 +72,7 @@ impl<H> ConversionWebhookServer<H> {
7272
}
7373

7474
#[async_trait]
75-
impl<H> WebhookServerImplementation for ConversionWebhookServer<H>
75+
impl<H> Webhook for ConversionWebhook<H>
7676
where
7777
H: FnOnce(ConversionReview) -> ConversionReview + Clone + Send + Sync + 'static,
7878
{
@@ -95,8 +95,8 @@ where
9595
&mut self,
9696
_new_certificate: &Certificate,
9797
new_ca_bundle: &ByteString,
98-
options: &WebhookOptions,
99-
) -> Result<(), WebhookServerImplementationError> {
98+
options: &WebhookServerOptions,
99+
) -> Result<(), WebhookError> {
100100
if self.disable_crd_maintenance {
101101
return Ok(());
102102
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use async_trait::async_trait;
22
use axum::Router;
3-
pub use conversion_webhook::{ConversionReview, ConversionWebhookError, ConversionWebhookServer};
3+
pub use conversion_webhook::{ConversionReview, ConversionWebhook, ConversionWebhookError};
44
use k8s_openapi::ByteString;
5-
pub use mutating_webhook::{MutatingWebhookError, MutatingWebhookServer};
5+
pub use mutating_webhook::{MutatingWebhook, MutatingWebhookError};
66
use snafu::Snafu;
77
use x509_cert::Certificate;
88

9-
use crate::WebhookOptions;
9+
use crate::WebhookServerOptions;
1010

1111
mod conversion_webhook;
1212
mod mutating_webhook;
1313

1414
#[derive(Snafu, Debug)]
15-
pub enum WebhookServerImplementationError {
15+
pub enum WebhookError {
1616
#[snafu(display("conversion webhook error"), context(false))]
1717
ConversionWebhookError {
1818
source: conversion_webhook::ConversionWebhookError,
@@ -27,13 +27,13 @@ pub enum WebhookServerImplementationError {
2727
// We still need to use the async-trait crate, as Rust 1.91.1 does not support dynamic dispatch
2828
// in combination with async functions.
2929
#[async_trait]
30-
pub trait WebhookServerImplementation {
30+
pub trait Webhook {
3131
fn register_routes(&self, router: Router) -> Router;
3232

3333
async fn handle_certificate_rotation(
3434
&mut self,
3535
new_certificate: &Certificate,
3636
new_ca_bundle: &ByteString,
37-
options: &WebhookOptions,
38-
) -> Result<(), WebhookServerImplementationError>;
37+
options: &WebhookServerOptions,
38+
) -> Result<(), WebhookError>;
3939
}

crates/stackable-webhook/src/servers/mutating_webhook.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ use serde::{Serialize, de::DeserializeOwned};
1717
use snafu::{ResultExt, Snafu};
1818
use x509_cert::Certificate;
1919

20-
use super::{WebhookServerImplementation, WebhookServerImplementationError};
21-
use crate::WebhookOptions;
20+
use super::{Webhook, WebhookError};
21+
use crate::WebhookServerOptions;
2222

2323
#[derive(Debug, Snafu)]
2424
pub enum MutatingWebhookError {
25-
#[snafu(display("failed to patch MutatingWebhookConfiguration {vwc_name:?}"))]
25+
#[snafu(display("failed to patch MutatingWebhookConfiguration {mwc_name:?}"))]
2626
PatchMutatingWebhookConfiguration {
2727
source: kube::Error,
28-
vwc_name: String,
28+
mwc_name: String,
2929
},
3030
}
3131

@@ -99,11 +99,11 @@ pub enum MutatingWebhookError {
9999
/// AdmissionResponse::from(&request)
100100
/// }
101101
/// ```
102-
pub struct MutatingWebhookServer<H, S, R> {
102+
pub struct MutatingWebhook<H, S, R> {
103103
mutating_webhook_configuration: MutatingWebhookConfiguration,
104104
handler: H,
105105
handler_state: Arc<S>,
106-
resource: PhantomData<R>,
106+
_resource: PhantomData<R>,
107107

108108
disable_mutating_webhook_configuration_maintenance: bool,
109109
client: Client,
@@ -112,7 +112,7 @@ pub struct MutatingWebhookServer<H, S, R> {
112112
field_manager: String,
113113
}
114114

115-
impl<H, S, R> MutatingWebhookServer<H, S, R> {
115+
impl<H, S, R> MutatingWebhook<H, S, R> {
116116
/// All webhooks need to set the admissionReviewVersions to `["v1"]`, as this mutating webhook
117117
/// only supports that version! A failure to do so will result in a panic.
118118
pub fn new(
@@ -135,7 +135,7 @@ impl<H, S, R> MutatingWebhookServer<H, S, R> {
135135
mutating_webhook_configuration,
136136
handler,
137137
handler_state,
138-
resource: PhantomData,
138+
_resource: PhantomData,
139139
disable_mutating_webhook_configuration_maintenance,
140140
client,
141141
field_manager,
@@ -148,7 +148,7 @@ impl<H, S, R> MutatingWebhookServer<H, S, R> {
148148
}
149149

150150
#[async_trait]
151-
impl<H, S, R, Fut> WebhookServerImplementation for MutatingWebhookServer<H, S, R>
151+
impl<H, S, R, Fut> Webhook for MutatingWebhook<H, S, R>
152152
where
153153
H: Fn(Arc<S>, AdmissionRequest<R>) -> Fut + Clone + Send + Sync + 'static,
154154
Fut: Future<Output = AdmissionResponse> + Send + 'static,
@@ -182,16 +182,16 @@ where
182182
&mut self,
183183
_new_certificate: &Certificate,
184184
new_ca_bundle: &ByteString,
185-
options: &WebhookOptions,
186-
) -> Result<(), WebhookServerImplementationError> {
185+
options: &WebhookServerOptions,
186+
) -> Result<(), WebhookError> {
187187
if self.disable_mutating_webhook_configuration_maintenance {
188188
return Ok(());
189189
}
190190

191191
let mut mutating_webhook_configuration = self.mutating_webhook_configuration.clone();
192-
let vwc_name = mutating_webhook_configuration.name_any();
192+
let mwc_name = mutating_webhook_configuration.name_any();
193193
tracing::info!(
194-
k8s.MutatingWebhookConfiguration.name = vwc_name,
194+
k8s.mutatingwebhookconfiguration.name = mwc_name,
195195
"reconciling mutating webhook configurations"
196196
);
197197

@@ -210,15 +210,15 @@ where
210210
};
211211
}
212212

213-
let vwc_api: Api<MutatingWebhookConfiguration> = Api::all(self.client.clone());
213+
let mwc_api: Api<MutatingWebhookConfiguration> = Api::all(self.client.clone());
214214
// Other than with the CRDs we don't need to force-apply the MutatingWebhookConfiguration
215215
let patch = Patch::Apply(&mutating_webhook_configuration);
216216
let patch_params = PatchParams::apply(&self.field_manager);
217217

218-
vwc_api
219-
.patch(&vwc_name, &patch_params, &patch)
218+
mwc_api
219+
.patch(&mwc_name, &patch_params, &patch)
220220
.await
221-
.with_context(|_| PatchMutatingWebhookConfigurationSnafu { vwc_name })?;
221+
.with_context(|_| PatchMutatingWebhookConfigurationSnafu { mwc_name })?;
222222

223223
Ok(())
224224
}

crates/stackable-webhook/src/tls/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use tracing_opentelemetry::OpenTelemetrySpanExt;
3131
use x509_cert::Certificate;
3232

3333
use crate::{
34-
WebhookOptions,
34+
WebhookServerOptions,
3535
tls::cert_resolver::{CertificateResolver, CertificateResolverError},
3636
};
3737

@@ -86,11 +86,11 @@ impl TlsServer {
8686
#[instrument(name = "create_tls_server", skip(router))]
8787
pub async fn new(
8888
router: Router,
89-
options: WebhookOptions,
89+
options: WebhookServerOptions,
9090
) -> Result<(Self, mpsc::Receiver<Certificate>)> {
9191
let (certificate_tx, certificate_rx) = mpsc::channel(1);
9292

93-
let WebhookOptions {
93+
let WebhookServerOptions {
9494
socket_addr,
9595
operator_namespace,
9696
operator_service_name,

0 commit comments

Comments
 (0)