Skip to content

Commit b3c17b3

Browse files
committed
HashMap -> Vec
1 parent fd4d94b commit b3c17b3

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, fmt::Debug};
1+
use std::fmt::Debug;
22

33
use axum::{Json, Router, routing::post};
44
use k8s_openapi::{
@@ -74,7 +74,7 @@ pub struct ConversionWebhookServer {
7474
cert_rx: mpsc::Receiver<Certificate>,
7575
client: Client,
7676
field_manager: String,
77-
crds: HashMap<String, CustomResourceDefinition>,
77+
crds: Vec<CustomResourceDefinition>,
7878
operator_environment: OperatorEnvironmentOpts,
7979
}
8080

@@ -106,7 +106,7 @@ impl ConversionWebhookServer {
106106
/// # async fn test() {
107107
/// let crds_and_handlers = [
108108
/// (
109-
/// S3Connection::merged_crd(S3ConnectionVersion::V1Alpha1).unwrap(),
109+
/// S3Connection::merged_crd(S3ConnectionVersion::V1Alpha1).expect("failed to merge S3Connection CRD"),
110110
/// S3Connection::try_convert as fn(ConversionReview) -> ConversionReview,
111111
/// ),
112112
/// ];
@@ -151,7 +151,7 @@ impl ConversionWebhookServer {
151151
let field_manager: String = field_manager.into();
152152

153153
let mut router = Router::new();
154-
let mut crds = HashMap::new();
154+
let mut crds = Vec::new();
155155
for (crd, handler) in crds_and_handlers {
156156
let crd_name = crd.name_any();
157157
let handler_fn = |Json(review): Json<ConversionReview>| async {
@@ -160,7 +160,7 @@ impl ConversionWebhookServer {
160160
};
161161

162162
router = router.route(&format!("/convert/{crd_name}"), post(handler_fn));
163-
crds.insert(crd_name, crd);
163+
crds.push(crd);
164164
}
165165

166166
// This is how Kubernetes calls us, so it decides about the naming.
@@ -238,7 +238,7 @@ impl ConversionWebhookServer {
238238
mut cert_rx: mpsc::Receiver<Certificate>,
239239
client: &Client,
240240
field_manager: &str,
241-
crds: &HashMap<String, CustomResourceDefinition>,
241+
crds: &[CustomResourceDefinition],
242242
operator_environment: &OperatorEnvironmentOpts,
243243
) -> Result<(), ConversionWebhookError> {
244244
while let Some(current_cert) = cert_rx.recv().await {
@@ -259,18 +259,21 @@ impl ConversionWebhookServer {
259259
async fn reconcile_crds(
260260
client: &Client,
261261
field_manager: &str,
262-
crds: &HashMap<String, CustomResourceDefinition>,
262+
crds: &[CustomResourceDefinition],
263263
operator_environment: &OperatorEnvironmentOpts,
264264
current_cert: &Certificate,
265265
) -> Result<(), ConversionWebhookError> {
266-
tracing::info!(kinds = ?crds.keys(), "Reconciling CRDs");
266+
tracing::info!(
267+
crds = ?crds.iter().map(CustomResourceDefinition::name_any).collect::<Vec<_>>(),
268+
"Reconciling CRDs"
269+
);
267270
let ca_bundle = current_cert
268271
.to_pem(LineEnding::LF)
269272
.context(ConvertCaToPemSnafu)?;
270273

271274
let crd_api: Api<CustomResourceDefinition> = Api::all(client.clone());
272-
for (kind, crd) in crds {
273-
let mut crd = crd.clone();
275+
for mut crd in crds.iter().cloned() {
276+
let crd_name = crd.name_any();
274277

275278
crd.spec.conversion = Some(CustomResourceConversion {
276279
strategy: "Webhook".to_string(),
@@ -283,7 +286,7 @@ impl ConversionWebhookServer {
283286
service: Some(ServiceReference {
284287
name: operator_environment.operator_service_name.clone(),
285288
namespace: operator_environment.operator_namespace.clone(),
286-
path: Some(format!("/convert/{kind}")),
289+
path: Some(format!("/convert/{crd_name}")),
287290
port: Some(DEFAULT_HTTPS_PORT.into()),
288291
}),
289292
ca_bundle: Some(ByteString(ca_bundle.as_bytes().to_vec())),
@@ -292,8 +295,6 @@ impl ConversionWebhookServer {
292295
}),
293296
});
294297

295-
// TODO: Move this into function and do a more clever update mechanism
296-
let crd_name = crd.name_any();
297298
let patch = Patch::Apply(&crd);
298299
let patch_params = PatchParams::apply(field_manager);
299300
crd_api
@@ -302,7 +303,6 @@ impl ConversionWebhookServer {
302303
.with_context(|_| UpdateCRDSnafu {
303304
crd_name: crd_name.to_string(),
304305
})?;
305-
tracing::info!(crd.name = crd_name, "Reconciled CRD");
306306
}
307307
Ok(())
308308
}

0 commit comments

Comments
 (0)