Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document Controller::reconcile_on and remove Err input requirement #1304

Merged
merged 7 commits into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions kube-runtime/src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,15 +1087,14 @@

/// Trigger the reconciliation process for a managed object `ObjectRef<K>` whenever `trigger` emits a value
///
/// For example, this can be used to watch resources once and use the stream to trigger reconciliation and also keep a cache of those objects.
/// That way it's possible to use this up to date cache instead of querying Kubernetes to access those resources
/// This can be used to inject reconciliations for specific objects from an external resource.
///
/// # Example:
///
/// ```no_run
/// # async {
/// # use futures::{StreamExt, TryStreamExt};
/// # use k8s_openapi::api::core::v1::{ConfigMap, Pod};
/// # use futures::{StreamExt, Stream, stream, TryStreamExt};
/// # use k8s_openapi::api::core::v1::{ConfigMap};
/// # use kube::api::Api;
/// # use kube::runtime::controller::Action;
/// # use kube::runtime::reflector::{ObjectRef, Store};
Expand All @@ -1106,37 +1105,34 @@
/// # use std::sync::Arc;
/// #
/// # let client: Client = todo!();
/// # async fn reconcile(_: Arc<ConfigMap>, _: Arc<Store<Pod>>) -> Result<Action, Error> { Ok(Action::await_change()) }
/// # fn error_policy(_: Arc<ConfigMap>, _: &kube::Error, _: Arc<Store<Pod>>) -> Action { Action::await_change() }
/// #
/// // Store can be used in the reconciler instead of querying Kube
/// let (pod_store, writer) = reflector::store();
/// let pod_stream = watcher(Api::<Pod>::all(client.clone()), Config::default())
/// .default_backoff()
/// .reflect(writer)
/// .applied_objects()
/// // Map to the relevant `ObjectRef<K>` to reconcile
/// .map_ok(|pod| ObjectRef::new(&format!("{}-cm", pod.name_any())).within(&pod.namespace().unwrap()));
/// # async fn reconcile(_: Arc<ConfigMap>, _: Arc<()>) -> Result<Action, Error> { Ok(Action::await_change()) }
/// # fn error_policy(_: Arc<ConfigMap>, _: &kube::Error, _: Arc<()>) -> Action { Action::await_change() }
/// # fn watch_external_objects() -> impl Stream<Item = ExternalObject> { stream::iter(vec![]) }
/// # let ns = "controller-ns".to_string();
/// struct ExternalObject {
/// name: String,
/// }
/// let external_stream = watch_external_objects().map(|ext| {
/// ObjectRef::new(&format!("{}-cm", ext.name)).within(&ns)
/// });
///
/// Controller::new(Api::<ConfigMap>::all(client), Config::default())
/// .reconcile_on(pod_stream)
/// // The store can be re-used between controllers and even inspected from the reconciler through [Context]
/// .run(reconcile, error_policy, Arc::new(pod_store))
/// Controller::new(Api::<ConfigMap>::namespaced(client, &ns), Config::default())
Dav1dde marked this conversation as resolved.
Show resolved Hide resolved
/// .reconcile_on(external_stream)
/// .run(reconcile, error_policy, Arc::new(()))
/// .for_each(|_| future::ready(()))
/// .await;
/// # };
/// ```
#[cfg(feature = "unstable-runtime-reconcile-on")]
#[must_use]
pub fn reconcile_on(
mut self,
trigger: impl Stream<Item = Result<ObjectRef<K>, watcher::Error>> + Send + 'static,
) -> Self {
pub fn reconcile_on(mut self, trigger: impl Stream<Item = ObjectRef<K>> + Send + 'static) -> Self {

Check warning on line 1128 in kube-runtime/src/controller/mod.rs

View check run for this annotation

Codecov / codecov/patch

kube-runtime/src/controller/mod.rs#L1128

Added line #L1128 was not covered by tests
self.trigger_selector.push(
trigger
.map_ok(move |obj| ReconcileRequest {
obj_ref: obj,
reason: ReconcileReason::Unknown,
.map(move |obj| {
Ok(ReconcileRequest {
obj_ref: obj,
reason: ReconcileReason::Unknown,

Check warning on line 1134 in kube-runtime/src/controller/mod.rs

View check run for this annotation

Codecov / codecov/patch

kube-runtime/src/controller/mod.rs#L1131-L1134

Added lines #L1131 - L1134 were not covered by tests
})
})
.boxed(),
);
Expand Down
Loading