1- use std:: { fmt:: Debug , marker:: PhantomData } ;
1+ use std:: { fmt:: Debug , marker:: PhantomData , sync :: Arc } ;
22
33use async_trait:: async_trait;
44use axum:: { Json , Router , routing:: post} ;
@@ -31,9 +31,10 @@ pub enum MutatingWebhookError {
3131
3232/// As the webhook is typed with the Resource type `R`, it can only handle a single resource
3333/// mutation. Use multiple [`MutatingWebhookServer`] if you need to mutate multiple resource kinds.
34- pub struct MutatingWebhookServer < H , R > {
34+ pub struct MutatingWebhookServer < H , S , R > {
3535 mutating_webhook_configuration : MutatingWebhookConfiguration ,
3636 handler : H ,
37+ handler_state : Arc < S > ,
3738 resource : PhantomData < R > ,
3839
3940 disable_mutating_webhook_configuration_maintenance : bool ,
@@ -43,17 +44,19 @@ pub struct MutatingWebhookServer<H, R> {
4344 field_manager : String ,
4445}
4546
46- impl < H , R > MutatingWebhookServer < H , R > {
47+ impl < H , S , R > MutatingWebhookServer < H , S , R > {
4748 pub fn new (
4849 mutating_webhook_configuration : MutatingWebhookConfiguration ,
4950 handler : H ,
51+ handler_state : Arc < S > ,
5052 disable_mutating_webhook_configuration_maintenance : bool ,
5153 client : Client ,
5254 field_manager : String ,
5355 ) -> Self {
5456 Self {
5557 mutating_webhook_configuration,
5658 handler,
59+ handler_state,
5760 resource : PhantomData ,
5861 disable_mutating_webhook_configuration_maintenance,
5962 client,
@@ -67,14 +70,17 @@ impl<H, R> MutatingWebhookServer<H, R> {
6770}
6871
6972#[ async_trait]
70- impl < H , R > WebhookServerImplementation for MutatingWebhookServer < H , R >
73+ impl < H , S , R , Fut > WebhookServerImplementation for MutatingWebhookServer < H , S , R >
7174where
72- H : FnOnce ( AdmissionRequest < R > ) -> AdmissionResponse + Clone + Send + Sync + ' static ,
75+ H : Fn ( Arc < S > , AdmissionRequest < R > ) -> Fut + Clone + Send + Sync + ' static ,
76+ Fut : Future < Output = AdmissionResponse > + Send + ' static ,
7377 R : Resource + Send + Sync + DeserializeOwned + Serialize + ' static ,
78+ S : Send + Sync + ' static ,
7479{
7580 fn register_routes ( & self , router : Router ) -> Router {
81+ let handler_state = self . handler_state . clone ( ) ;
7682 let handler = self . handler . clone ( ) ;
77- let handler_fn = |Json ( review) : Json < AdmissionReview < R > > | async {
83+ let handler_fn = |Json ( review) : Json < AdmissionReview < R > > | async move {
7884 let request: AdmissionRequest < R > = match review. try_into ( ) {
7985 Ok ( request) => request,
8086 Err ( err) => {
8591 }
8692 } ;
8793
88- let response = handler ( request) ;
94+ let response = handler ( handler_state , request) . await ;
8995 let review = response. into_review ( ) ;
9096 Json ( review)
9197 } ;
0 commit comments