@@ -24,6 +24,8 @@ pub static DOCUMENT_FINALIZER: &str = "documents.kube.rs";
24
24
/// Generate the Kubernetes wrapper struct `Document` from our Spec and Status struct
25
25
///
26
26
/// 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>.
27
29
#[ derive( CustomResource , Deserialize , Serialize , Clone , Debug , JsonSchema ) ]
28
30
#[ cfg_attr( test, derive( Default ) ) ]
29
31
#[ kube( kind = "Document" , group = "kube.rs" , version = "v1" , namespaced) ]
@@ -50,6 +52,8 @@ impl Document {
50
52
pub struct Context {
51
53
/// Kubernetes client
52
54
pub client : Client ,
55
+ /// Event recorder
56
+ pub recorder : Recorder ,
53
57
/// Diagnostics read by the web server
54
58
pub diagnostics : Arc < RwLock < Diagnostics > > ,
55
59
/// Prometheus metrics
@@ -88,22 +92,25 @@ impl Document {
88
92
// Reconcile (for non-finalizer related changes)
89
93
async fn reconcile ( & self , ctx : Arc < Context > ) -> Result < Action > {
90
94
let client = ctx. client . clone ( ) ;
91
- let recorder = ctx . diagnostics . read ( ) . await . recorder ( client . clone ( ) , self ) ;
95
+ let oref = self . object_ref ( & ( ) ) ;
92
96
let ns = self . namespace ( ) . unwrap ( ) ;
93
97
let name = self . name_any ( ) ;
94
98
let docs: Api < Document > = Api :: namespaced ( client, & ns) ;
95
99
96
100
let should_hide = self . spec . hide ;
97
101
if !self . was_hidden ( ) && should_hide {
98
102
// 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
+ )
107
114
. await
108
115
. map_err ( Error :: KubeError ) ?;
109
116
}
@@ -130,16 +137,19 @@ impl Document {
130
137
131
138
// Finalizer cleanup (the object was deleted, ensure nothing is orphaned)
132
139
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 ( & ( ) ) ;
134
141
// 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
+ )
143
153
. await
144
154
. map_err ( Error :: KubeError ) ?;
145
155
Ok ( Action :: await_change ( ) )
@@ -163,8 +173,8 @@ impl Default for Diagnostics {
163
173
}
164
174
}
165
175
impl 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 ( ) )
168
178
}
169
179
}
170
180
@@ -193,9 +203,10 @@ impl State {
193
203
}
194
204
195
205
// 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 > {
197
207
Arc :: new ( Context {
198
- client,
208
+ client : client. clone ( ) ,
209
+ recorder : self . diagnostics . read ( ) . await . recorder ( client) ,
199
210
metrics : self . metrics . clone ( ) ,
200
211
diagnostics : self . diagnostics . clone ( ) ,
201
212
} )
@@ -213,7 +224,7 @@ pub async fn run(state: State) {
213
224
}
214
225
Controller :: new ( docs, Config :: default ( ) . any_semantic ( ) )
215
226
. shutdown_on_signal ( )
216
- . run ( reconcile, error_policy, state. to_context ( client) )
227
+ . run ( reconcile, error_policy, state. to_context ( client) . await )
217
228
. filter_map ( |x| async move { std:: result:: Result :: ok ( x) } )
218
229
. for_each ( |_| futures:: future:: ready ( ( ) ) )
219
230
. await ;
@@ -293,7 +304,7 @@ mod test {
293
304
#[ ignore = "uses k8s current-context" ]
294
305
async fn integration_reconcile_should_set_status_and_send_event ( ) {
295
306
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 ;
297
308
298
309
// create a test doc
299
310
let doc = Document :: test ( ) . finalized ( ) . needs_hide ( ) ;
0 commit comments