@@ -24,6 +24,8 @@ pub static DOCUMENT_FINALIZER: &str = "documents.kube.rs";
2424/// Generate the Kubernetes wrapper struct `Document` from our Spec and Status struct
2525///
2626/// This provides a hook for generating the CRD yaml (in crdgen.rs)
27+ /// NB: CustomResource generates a pub struct Document here
28+ /// To query for documents.kube.rs with kube, use Api<Document>.
2729#[ derive( CustomResource , Deserialize , Serialize , Clone , Debug , JsonSchema ) ]
2830#[ cfg_attr( test, derive( Default ) ) ]
2931#[ kube( kind = "Document" , group = "kube.rs" , version = "v1" , namespaced) ]
@@ -50,6 +52,8 @@ impl Document {
5052pub struct Context {
5153 /// Kubernetes client
5254 pub client : Client ,
55+ /// Event recorder
56+ pub recorder : Recorder ,
5357 /// Diagnostics read by the web server
5458 pub diagnostics : Arc < RwLock < Diagnostics > > ,
5559 /// Prometheus metrics
@@ -88,22 +92,25 @@ impl Document {
8892 // Reconcile (for non-finalizer related changes)
8993 async fn reconcile ( & self , ctx : Arc < Context > ) -> Result < Action > {
9094 let client = ctx. client . clone ( ) ;
91- let recorder = ctx . diagnostics . read ( ) . await . recorder ( client . clone ( ) , self ) ;
95+ let oref = self . object_ref ( & ( ) ) ;
9296 let ns = self . namespace ( ) . unwrap ( ) ;
9397 let name = self . name_any ( ) ;
9498 let docs: Api < Document > = Api :: namespaced ( client, & ns) ;
9599
96100 let should_hide = self . spec . hide ;
97101 if !self . was_hidden ( ) && should_hide {
98102 // send an event once per hide
99- recorder
100- . publish ( Event {
101- type_ : EventType :: Normal ,
102- reason : "HideRequested" . into ( ) ,
103- note : Some ( format ! ( "Hiding `{name}`" ) ) ,
104- action : "Hiding" . into ( ) ,
105- secondary : None ,
106- } )
103+ ctx. recorder
104+ . publish (
105+ & Event {
106+ type_ : EventType :: Normal ,
107+ reason : "HideRequested" . into ( ) ,
108+ note : Some ( format ! ( "Hiding `{name}`" ) ) ,
109+ action : "Hiding" . into ( ) ,
110+ secondary : None ,
111+ } ,
112+ & oref,
113+ )
107114 . await
108115 . map_err ( Error :: KubeError ) ?;
109116 }
@@ -130,16 +137,19 @@ impl Document {
130137
131138 // Finalizer cleanup (the object was deleted, ensure nothing is orphaned)
132139 async fn cleanup ( & self , ctx : Arc < Context > ) -> Result < Action > {
133- let recorder = ctx . diagnostics . read ( ) . await . recorder ( ctx . client . clone ( ) , self ) ;
140+ let oref = self . object_ref ( & ( ) ) ;
134141 // Document doesn't have any real cleanup, so we just publish an event
135- recorder
136- . publish ( Event {
137- type_ : EventType :: Normal ,
138- reason : "DeleteRequested" . into ( ) ,
139- note : Some ( format ! ( "Delete `{}`" , self . name_any( ) ) ) ,
140- action : "Deleting" . into ( ) ,
141- secondary : None ,
142- } )
142+ ctx. recorder
143+ . publish (
144+ & Event {
145+ type_ : EventType :: Normal ,
146+ reason : "DeleteRequested" . into ( ) ,
147+ note : Some ( format ! ( "Delete `{}`" , self . name_any( ) ) ) ,
148+ action : "Deleting" . into ( ) ,
149+ secondary : None ,
150+ } ,
151+ & oref,
152+ )
143153 . await
144154 . map_err ( Error :: KubeError ) ?;
145155 Ok ( Action :: await_change ( ) )
@@ -163,8 +173,8 @@ impl Default for Diagnostics {
163173 }
164174}
165175impl Diagnostics {
166- fn recorder ( & self , client : Client , doc : & Document ) -> Recorder {
167- Recorder :: new ( client, self . reporter . clone ( ) , doc . object_ref ( & ( ) ) )
176+ fn recorder ( & self , client : Client ) -> Recorder {
177+ Recorder :: new ( client, self . reporter . clone ( ) )
168178 }
169179}
170180
@@ -193,9 +203,10 @@ impl State {
193203 }
194204
195205 // Create a Controller Context that can update State
196- pub fn to_context ( & self , client : Client ) -> Arc < Context > {
206+ pub async fn to_context ( & self , client : Client ) -> Arc < Context > {
197207 Arc :: new ( Context {
198- client,
208+ client : client. clone ( ) ,
209+ recorder : self . diagnostics . read ( ) . await . recorder ( client) ,
199210 metrics : self . metrics . clone ( ) ,
200211 diagnostics : self . diagnostics . clone ( ) ,
201212 } )
@@ -213,7 +224,7 @@ pub async fn run(state: State) {
213224 }
214225 Controller :: new ( docs, Config :: default ( ) . any_semantic ( ) )
215226 . shutdown_on_signal ( )
216- . run ( reconcile, error_policy, state. to_context ( client) )
227+ . run ( reconcile, error_policy, state. to_context ( client) . await )
217228 . filter_map ( |x| async move { std:: result:: Result :: ok ( x) } )
218229 . for_each ( |_| futures:: future:: ready ( ( ) ) )
219230 . await ;
@@ -293,7 +304,7 @@ mod test {
293304 #[ ignore = "uses k8s current-context" ]
294305 async fn integration_reconcile_should_set_status_and_send_event ( ) {
295306 let client = kube:: Client :: try_default ( ) . await . unwrap ( ) ;
296- let ctx = super :: State :: default ( ) . to_context ( client. clone ( ) ) ;
307+ let ctx = super :: State :: default ( ) . to_context ( client. clone ( ) ) . await ;
297308
298309 // create a test doc
299310 let doc = Document :: test ( ) . finalized ( ) . needs_hide ( ) ;
0 commit comments