Skip to content

Commit 713716a

Browse files
committed
refactor: Move out into get_webhook_client_config
1 parent bea8241 commit 713716a

3 files changed

Lines changed: 34 additions & 37 deletions

File tree

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use async_trait::async_trait;
22
use axum::Router;
33
pub use conversion_webhook::{ConversionReview, ConversionWebhook, ConversionWebhookError};
4-
use k8s_openapi::ByteString;
4+
use k8s_openapi::{
5+
ByteString,
6+
api::admissionregistration::v1::{ServiceReference, WebhookClientConfig},
7+
};
58
pub use mutating_webhook::{MutatingWebhook, MutatingWebhookError};
69
use snafu::Snafu;
710
pub use validating_webhook::{ValidatingWebhook, ValidatingWebhookError};
@@ -53,3 +56,25 @@ pub trait Webhook {
5356
options: &WebhookServerOptions,
5457
) -> Result<(), WebhookError>;
5558
}
59+
60+
/// Returns the client config that can be used in admission webhooks.
61+
///
62+
/// It is used to concat the correct HTTP endpoint, which is calculated from the given parameters.
63+
/// (CRD conversions require a similar, but different, client config).
64+
fn get_webhook_client_config(
65+
options: &WebhookServerOptions,
66+
ca_bundle: ByteString,
67+
http_path: impl Into<String>,
68+
) -> WebhookClientConfig {
69+
WebhookClientConfig {
70+
service: Some(ServiceReference {
71+
name: options.webhook_service_name.to_owned(),
72+
namespace: options.webhook_namespace.to_owned(),
73+
path: Some(http_path.into()),
74+
port: Some(options.socket_addr.port().into()),
75+
}),
76+
// Here, ByteString takes care of encoding the provided content as base64.
77+
ca_bundle: Some(ca_bundle),
78+
url: None,
79+
}
80+
}

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ use std::{fmt::Debug, marker::PhantomData, sync::Arc};
22

33
use async_trait::async_trait;
44
use axum::{Json, Router, routing::post};
5-
use k8s_openapi::{
6-
ByteString,
7-
api::admissionregistration::v1::{
8-
MutatingWebhookConfiguration, ServiceReference, WebhookClientConfig,
9-
},
10-
};
5+
use k8s_openapi::{ByteString, api::admissionregistration::v1::MutatingWebhookConfiguration};
116
use kube::{
127
Api, Client, Resource, ResourceExt,
138
api::{Patch, PatchParams},
@@ -19,7 +14,7 @@ use tracing::instrument;
1914
use x509_cert::Certificate;
2015

2116
use super::{Webhook, WebhookError};
22-
use crate::WebhookServerOptions;
17+
use crate::{WebhookServerOptions, webhooks::get_webhook_client_config};
2318

2419
#[derive(Debug, Snafu)]
2520
pub enum MutatingWebhookError {
@@ -203,17 +198,8 @@ where
203198

204199
for webhook in mutating_webhook_configuration.webhooks.iter_mut().flatten() {
205200
// We know how we can be called (and with what certificate), so we can always set that
206-
webhook.client_config = WebhookClientConfig {
207-
service: Some(ServiceReference {
208-
name: options.webhook_service_name.to_owned(),
209-
namespace: options.webhook_namespace.to_owned(),
210-
path: Some(self.http_path()),
211-
port: Some(options.socket_addr.port().into()),
212-
}),
213-
// Here, ByteString takes care of encoding the provided content as base64.
214-
ca_bundle: Some(new_ca_bundle.to_owned()),
215-
url: None,
216-
};
201+
webhook.client_config =
202+
get_webhook_client_config(options, new_ca_bundle.to_owned(), self.http_path());
217203
}
218204

219205
let mwc_api: Api<MutatingWebhookConfiguration> = Api::all(self.client.clone());

crates/stackable-webhook/src/webhooks/validating_webhook.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ use std::{fmt::Debug, marker::PhantomData, sync::Arc};
22

33
use async_trait::async_trait;
44
use axum::{Json, Router, routing::post};
5-
use k8s_openapi::{
6-
ByteString,
7-
api::admissionregistration::v1::{
8-
ServiceReference, ValidatingWebhookConfiguration, WebhookClientConfig,
9-
},
10-
};
5+
use k8s_openapi::{ByteString, api::admissionregistration::v1::ValidatingWebhookConfiguration};
116
use kube::{
127
Api, Client, Resource, ResourceExt,
138
api::{Patch, PatchParams},
@@ -19,7 +14,7 @@ use tracing::instrument;
1914
use x509_cert::Certificate;
2015

2116
use super::{Webhook, WebhookError};
22-
use crate::WebhookServerOptions;
17+
use crate::{WebhookServerOptions, webhooks::get_webhook_client_config};
2318

2419
#[derive(Debug, Snafu)]
2520
pub enum ValidatingWebhookError {
@@ -148,17 +143,8 @@ where
148143
.flatten()
149144
{
150145
// We know how we can be called (and with what certificate), so we can always set that
151-
webhook.client_config = WebhookClientConfig {
152-
service: Some(ServiceReference {
153-
name: options.webhook_service_name.to_owned(),
154-
namespace: options.webhook_namespace.to_owned(),
155-
path: Some(self.http_path()),
156-
port: Some(options.socket_addr.port().into()),
157-
}),
158-
// Here, ByteString takes care of encoding the provided content as base64.
159-
ca_bundle: Some(new_ca_bundle.to_owned()),
160-
url: None,
161-
};
146+
webhook.client_config =
147+
get_webhook_client_config(options, new_ca_bundle.to_owned(), self.http_path());
162148
}
163149

164150
let vwc_api: Api<ValidatingWebhookConfiguration> = Api::all(self.client.clone());

0 commit comments

Comments
 (0)