1
- use std:: { collections :: HashMap , fmt:: Debug } ;
1
+ use std:: fmt:: Debug ;
2
2
3
3
use axum:: { Json , Router , routing:: post} ;
4
4
use k8s_openapi:: {
@@ -74,7 +74,7 @@ pub struct ConversionWebhookServer {
74
74
cert_rx : mpsc:: Receiver < Certificate > ,
75
75
client : Client ,
76
76
field_manager : String ,
77
- crds : HashMap < String , CustomResourceDefinition > ,
77
+ crds : Vec < CustomResourceDefinition > ,
78
78
operator_environment : OperatorEnvironmentOpts ,
79
79
}
80
80
@@ -106,7 +106,7 @@ impl ConversionWebhookServer {
106
106
/// # async fn test() {
107
107
/// let crds_and_handlers = [
108
108
/// (
109
- /// S3Connection::merged_crd(S3ConnectionVersion::V1Alpha1).unwrap( ),
109
+ /// S3Connection::merged_crd(S3ConnectionVersion::V1Alpha1).expect("failed to merge S3Connection CRD" ),
110
110
/// S3Connection::try_convert as fn(ConversionReview) -> ConversionReview,
111
111
/// ),
112
112
/// ];
@@ -151,7 +151,7 @@ impl ConversionWebhookServer {
151
151
let field_manager: String = field_manager. into ( ) ;
152
152
153
153
let mut router = Router :: new ( ) ;
154
- let mut crds = HashMap :: new ( ) ;
154
+ let mut crds = Vec :: new ( ) ;
155
155
for ( crd, handler) in crds_and_handlers {
156
156
let crd_name = crd. name_any ( ) ;
157
157
let handler_fn = |Json ( review) : Json < ConversionReview > | async {
@@ -160,7 +160,7 @@ impl ConversionWebhookServer {
160
160
} ;
161
161
162
162
router = router. route ( & format ! ( "/convert/{crd_name}" ) , post ( handler_fn) ) ;
163
- crds. insert ( crd_name , crd) ;
163
+ crds. push ( crd) ;
164
164
}
165
165
166
166
// This is how Kubernetes calls us, so it decides about the naming.
@@ -238,7 +238,7 @@ impl ConversionWebhookServer {
238
238
mut cert_rx : mpsc:: Receiver < Certificate > ,
239
239
client : & Client ,
240
240
field_manager : & str ,
241
- crds : & HashMap < String , CustomResourceDefinition > ,
241
+ crds : & [ CustomResourceDefinition ] ,
242
242
operator_environment : & OperatorEnvironmentOpts ,
243
243
) -> Result < ( ) , ConversionWebhookError > {
244
244
while let Some ( current_cert) = cert_rx. recv ( ) . await {
@@ -259,18 +259,21 @@ impl ConversionWebhookServer {
259
259
async fn reconcile_crds (
260
260
client : & Client ,
261
261
field_manager : & str ,
262
- crds : & HashMap < String , CustomResourceDefinition > ,
262
+ crds : & [ CustomResourceDefinition ] ,
263
263
operator_environment : & OperatorEnvironmentOpts ,
264
264
current_cert : & Certificate ,
265
265
) -> 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
+ ) ;
267
270
let ca_bundle = current_cert
268
271
. to_pem ( LineEnding :: LF )
269
272
. context ( ConvertCaToPemSnafu ) ?;
270
273
271
274
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 ( ) ;
274
277
275
278
crd. spec . conversion = Some ( CustomResourceConversion {
276
279
strategy : "Webhook" . to_string ( ) ,
@@ -283,7 +286,7 @@ impl ConversionWebhookServer {
283
286
service : Some ( ServiceReference {
284
287
name : operator_environment. operator_service_name . clone ( ) ,
285
288
namespace : operator_environment. operator_namespace . clone ( ) ,
286
- path : Some ( format ! ( "/convert/{kind }" ) ) ,
289
+ path : Some ( format ! ( "/convert/{crd_name }" ) ) ,
287
290
port : Some ( DEFAULT_HTTPS_PORT . into ( ) ) ,
288
291
} ) ,
289
292
ca_bundle : Some ( ByteString ( ca_bundle. as_bytes ( ) . to_vec ( ) ) ) ,
@@ -292,8 +295,6 @@ impl ConversionWebhookServer {
292
295
} ) ,
293
296
} ) ;
294
297
295
- // TODO: Move this into function and do a more clever update mechanism
296
- let crd_name = crd. name_any ( ) ;
297
298
let patch = Patch :: Apply ( & crd) ;
298
299
let patch_params = PatchParams :: apply ( field_manager) ;
299
300
crd_api
@@ -302,7 +303,6 @@ impl ConversionWebhookServer {
302
303
. with_context ( |_| UpdateCRDSnafu {
303
304
crd_name : crd_name. to_string ( ) ,
304
305
} ) ?;
305
- tracing:: info!( crd. name = crd_name, "Reconciled CRD" ) ;
306
306
}
307
307
Ok ( ( ) )
308
308
}
0 commit comments