1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
use crate::{ reflector::{ reflector, store::{Store, Writer}, ErasedResource, ObjectRef, }, scheduler::{self, scheduler, ScheduleRequest}, utils::{try_flatten_applied, try_flatten_touched, trystream_try_via}, watcher::{self, watcher}, }; use derivative::Derivative; use futures::{ channel, future, stream::{self, SelectAll}, FutureExt, SinkExt, Stream, StreamExt, TryFuture, TryFutureExt, TryStream, TryStreamExt, }; use kube::api::{Api, ListParams, Meta}; use serde::de::DeserializeOwned; use snafu::{futures::TryStreamExt as SnafuTryStreamExt, Backtrace, OptionExt, ResultExt, Snafu}; use std::{sync::Arc, time::Duration}; use stream::BoxStream; use tokio::time::Instant; #[derive(Snafu, Debug)] pub enum Error<ReconcilerErr: std::error::Error + 'static, QueueErr: std::error::Error + 'static> { ObjectNotFound { obj_ref: ObjectRef<ErasedResource>, backtrace: Backtrace, }, ReconcilerFailed { source: ReconcilerErr, backtrace: Backtrace, }, SchedulerDequeueFailed { #[snafu(backtrace)] source: scheduler::Error, }, QueueError { source: QueueErr, backtrace: Backtrace, }, } /// Results of the reconciliation attempt #[derive(Debug, Clone)] pub struct ReconcilerAction { /// Whether (and when) to next trigger the reconciliation if no external watch triggers hit /// /// For example, use this to query external systems for updates, expire time-limited resources, or /// (in your `error_policy`) retry after errors. pub requeue_after: Option<Duration>, } /// Helper for building custom trigger filters, see `trigger_self` and `trigger_owners` for some examples pub fn trigger_with<T, K, I, S>( stream: S, mapper: impl Fn(T) -> I, ) -> impl Stream<Item = Result<ObjectRef<K>, S::Error>> where S: TryStream<Ok = T>, I: IntoIterator<Item = ObjectRef<K>>, K: Meta, { stream .map_ok(move |obj| stream::iter(mapper(obj).into_iter().map(Ok))) .try_flatten() } /// Enqueues the object itself for reconciliation pub fn trigger_self<S>(stream: S) -> impl Stream<Item = Result<ObjectRef<S::Ok>, S::Error>> where S: TryStream, S::Ok: Meta, { trigger_with(stream, |obj| Some(ObjectRef::from_obj(&obj))) } /// Enqueues any owners of type `KOwner` for reconciliation pub fn trigger_owners<KOwner, S>(stream: S) -> impl Stream<Item = Result<ObjectRef<KOwner>, S::Error>> where S: TryStream, S::Ok: Meta, KOwner: Meta, { trigger_with(stream, |obj| { let meta = obj.meta().clone(); let ns = meta.namespace; meta.owner_references .into_iter() .flatten() .flat_map(move |owner| ObjectRef::from_owner_ref(ns.as_deref(), &owner)) }) } /// A context data type that's passed through to the controllers callbacks /// /// Context<T> gets passed to both the `reconciler` and the `error_policy` callbacks. /// allowing a read-only view of the world without creating a big nested lambda. /// More or less the same as actix's Data<T> #[derive(Debug, Derivative)] #[derivative(Clone(bound = ""))] pub struct Context<T>(Arc<T>); impl<T> Context<T> { /// Create new `Data` instance. #[must_use] pub fn new(state: T) -> Context<T> { Context(Arc::new(state)) } /// Get reference to inner controller data. #[must_use] pub fn get_ref(&self) -> &T { self.0.as_ref() } /// Convert to the internal Arc<T> #[must_use] pub fn into_inner(self) -> Arc<T> { self.0 } } /// Apply a reconciler to an input stream, with a given retry policy /// /// Takes a `store` parameter for the main object which should be updated by a `reflector`. /// /// The `queue` is a source of external events that trigger the reconciler, /// usually taken from a `reflector` and then passed through a trigger function such as /// `trigger_self`. /// /// This is the "hard-mode" version of `Controller`, which allows you some more customization /// (such as triggering from arbitrary `Stream`s), at the cost of some more verbosity. pub fn applier<K, QueueStream, ReconcilerFut, T>( mut reconciler: impl FnMut(K, Context<T>) -> ReconcilerFut, mut error_policy: impl FnMut(&ReconcilerFut::Error, Context<T>) -> ReconcilerAction, context: Context<T>, store: Store<K>, queue: QueueStream, ) -> impl Stream<Item = Result<(ObjectRef<K>, ReconcilerAction), Error<ReconcilerFut::Error, QueueStream::Error>>> where K: Clone + Meta + 'static, ReconcilerFut: TryFuture<Ok = ReconcilerAction>, ReconcilerFut::Error: std::error::Error + 'static, QueueStream: TryStream<Ok = ObjectRef<K>>, QueueStream::Error: std::error::Error + 'static, { let err_context = context.clone(); let (scheduler_tx, scheduler_rx) = channel::mpsc::channel::<ScheduleRequest<ObjectRef<K>>>(100); // Create a stream of ObjectRefs that need to be reconciled trystream_try_via( // input: stream combining scheduled tasks and user specified inputs event Box::pin(stream::select( // 1. inputs from users queue stream queue.context(QueueError).map_ok(|obj_ref| ScheduleRequest { message: obj_ref, run_at: Instant::now() + Duration::from_millis(1), }), // 2. requests sent to scheduler_tx scheduler_rx.map(Ok), )), // all the Oks from the select gets passed through the scheduler stream |s| scheduler(s).context(SchedulerDequeueFailed), ) // now have ObjectRefs that we turn into pairs inside (no extra waiting introduced) .and_then(move |obj_ref| { future::ready( store .get(&obj_ref) .context(ObjectNotFound { obj_ref: obj_ref.clone(), }) .map(|obj| (obj_ref, obj)), ) }) // then reconcile every object .and_then(move |(obj_ref, obj)| { reconciler(obj, context.clone()) // TODO: add a context argument to the reconcile .into_future() // TryFuture -> impl Future .map(|result| (obj_ref, result)) // turn into pair and ok wrap .map(Ok) // (this lets us deal with errors from reconciler below) }) // finally, for each completed reconcile call: .and_then(move |(obj_ref, reconciler_result)| { let ReconcilerAction { requeue_after } = match &reconciler_result { Ok(action) => action.clone(), // do what user told us Err(err) => error_policy(err, err_context.clone()), // reconciler fn call failed }; // we should always requeue at some point in case of network errors ^ let mut scheduler_tx = scheduler_tx.clone(); async move { // Transmit the requeue request to the scheduler (picked up again at top) if let Some(delay) = requeue_after { scheduler_tx .send(ScheduleRequest { message: obj_ref.clone(), run_at: Instant::now() + delay, }) .await .expect("Message could not be sent to scheduler_rx"); } // NB: no else clause ^ because we don't allow not requeuing atm. reconciler_result .map(|action| (obj_ref, action)) .context(ReconcilerFailed) } }) } /// Controller /// /// A controller is made up of: /// - 1 `reflector` (for the core object) /// - N `watcher` objects for each object child object /// - user defined `reconcile` + `error_policy` callbacks /// - a generated input stream considering all sources /// /// And all reconcile requests through an internal scheduler /// /// Pieces: /// ```no_run /// use kube::{Client, api::{Api, ListParams}}; /// use kube_derive::CustomResource; /// use serde::{Deserialize, Serialize}; /// use tokio::time::Duration; /// use futures::StreamExt; /// use kube_runtime::controller::{Context, Controller, ReconcilerAction}; /// use k8s_openapi::api::core::v1::ConfigMap; /// /// use snafu::{Backtrace, OptionExt, ResultExt, Snafu}; /// #[derive(Debug, Snafu)] /// enum Error {} /// /// A custom resource /// #[derive(CustomResource, Debug, Clone, Deserialize, Serialize)] /// #[kube(group = "nullable.se", version = "v1", kind = "ConfigMapGenerator", namespaced)] /// struct ConfigMapGeneratorSpec { /// content: String, /// } /// /// /// The reconciler that will be called when either object change /// async fn reconcile(g: ConfigMapGenerator, _ctx: Context<()>) -> Result<ReconcilerAction, Error> { /// // .. use api here to reconcile a child ConfigMap with ownerreferences /// // see configmapgen_controller example for full info /// Ok(ReconcilerAction { /// requeue_after: Some(Duration::from_secs(300)), /// }) /// } /// /// an error handler that will be called when the reconciler fails /// fn error_policy(_error: &Error, _ctx: Context<()>) -> ReconcilerAction { /// ReconcilerAction { /// requeue_after: Some(Duration::from_secs(60)), /// } /// } /// /// /// something to drive the controller /// #[tokio::main] /// async fn main() -> Result<(), kube::Error> { /// let client = Client::try_default().await?; /// let context = Context::new(()); // bad empty context - put client in here /// let cmgs = Api::<ConfigMapGenerator>::all(client.clone()); /// let cms = Api::<ConfigMap>::all(client.clone()); /// Controller::new(cmgs, ListParams::default()) /// .owns(cms, ListParams::default()) /// .run(reconcile, error_policy, context) /// .for_each(|res| async move { /// match res { /// Ok(o) => println!("reconciled {:?}", o), /// Err(e) => println!("reconcile failed: {:?}", e), /// } /// }) /// .await; // controller does nothing unless polled /// Ok(()) /// } /// ``` pub struct Controller<K> where K: Clone + Meta + 'static, { // NB: Need to Unpin for stream::select_all // TODO: get an arbitrary std::error::Error in here? selector: SelectAll<BoxStream<'static, Result<ObjectRef<K>, watcher::Error>>>, reader: Store<K>, } impl<K> Controller<K> where K: Clone + Meta + DeserializeOwned + Send + Sync + 'static, { /// Create a Controller on a type `K` /// /// Configure `ListParams` and `Api` so you only get reconcile events /// for the correct `Api` scope (cluster/all/namespaced), or `ListParams` subset pub fn new(owned_api: Api<K>, lp: ListParams) -> Self { let writer = Writer::<K>::default(); let reader = writer.as_reader(); let mut selector = stream::SelectAll::new(); let self_watcher = trigger_self(try_flatten_applied(reflector(writer, watcher(owned_api, lp)))).boxed(); selector.push(self_watcher); Self { selector, reader } } /// Retrieve a copy of the reader before starting the controller pub fn store(&self) -> Store<K> { self.reader.clone() } /// Indicate child objets `K` owns and be notified when they change /// /// This type `Child` must have `OwnerReference`s set to point back to `K`. /// You can customize the parameters used by the underlying `watcher` if /// only a subset of `Child` entries are required. /// The `api` must have the correct scope (cluster/all namespaces, or namespaced) pub fn owns<Child: Clone + Meta + DeserializeOwned + Send + 'static>( mut self, api: Api<Child>, lp: ListParams, ) -> Self { let child_watcher = trigger_owners(try_flatten_touched(watcher(api, lp))); self.selector.push(child_watcher.boxed()); self } /// Indicate an object to watch with a custom mapper /// /// This mapper should return something like Option<ObjectRef<K>> pub fn watches< Other: Clone + Meta + DeserializeOwned + Send + 'static, I: 'static + IntoIterator<Item = ObjectRef<K>>, >( mut self, api: Api<Other>, lp: ListParams, mapper: impl Fn(Other) -> I + Send + 'static, ) -> Self where I::IntoIter: Send, { let other_watcher = trigger_with(try_flatten_touched(watcher(api, lp)), mapper); self.selector.push(other_watcher.boxed()); self } /// Consume all the parameters of the Controller and start the applier stream /// /// This creates a stream from all builder calls and starts an applier with /// a specified `reconciler` and `error_policy` callbacks. Each of these will be called /// with a configurable `Context`. pub fn run<ReconcilerFut, T>( self, reconciler: impl FnMut(K, Context<T>) -> ReconcilerFut, error_policy: impl FnMut(&ReconcilerFut::Error, Context<T>) -> ReconcilerAction, context: Context<T>, ) -> impl Stream<Item = Result<(ObjectRef<K>, ReconcilerAction), Error<ReconcilerFut::Error, watcher::Error>>> where K: Clone + Meta + 'static, ReconcilerFut: TryFuture<Ok = ReconcilerAction>, ReconcilerFut::Error: std::error::Error + 'static, { applier(reconciler, error_policy, context, self.reader, self.selector) } } #[cfg(test)] mod tests { use super::{Context, ReconcilerAction}; use crate::Controller; use k8s_openapi::api::core::v1::ConfigMap; use kube::Api; fn assert_send<T: Send>(x: T) -> T { x } fn mock_type<T>() -> T { unimplemented!( "mock_type is not supposed to be called, only used for filling holes in type assertions" ) } // not #[test] because we don't want to actually run it, we just want to assert that it typechecks #[allow(dead_code, unused_must_use)] fn test_controller_should_be_send() { assert_send( Controller::new(mock_type::<Api<ConfigMap>>(), Default::default()).run( |_, _| async { Ok(mock_type::<ReconcilerAction>()) }, |_: &std::io::Error, _| mock_type::<ReconcilerAction>(), Context::new(()), ), ); } }