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

Adds example to Controller::watches #1026

Merged
merged 1 commit into from Sep 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions kube-runtime/src/controller/mod.rs
Expand Up @@ -593,6 +593,59 @@ where
/// The [`ListParams`] refer to the possible subset of `Watched` objects that you want the [`Api`]
/// to watch - in the Api's configured scope - and run through the custom mapper.
/// To watch the full set of `Watched` objects in given the `Api` scope, you can use [`ListParams::default`].
///
/// # Example
///
/// Tracking cross cluster references using the [Operator-SDK] annotations.
///
/// ```
/// # use kube::runtime::{Controller, controller::Action, reflector::ObjectRef};
/// # use kube::api::{Api, ListParams};
/// # use kube::ResourceExt;
/// # use k8s_openapi::api::core::v1::{ConfigMap, Namespace};
/// # use futures::StreamExt;
/// # use std::sync::Arc;
/// # type WatchedResource = Namespace;
/// # struct Context;
/// # async fn reconcile(_: Arc<ConfigMap>, _: Arc<Context>) -> Result<Action, kube::Error> {
/// # Ok(Action::await_change())
/// # };
/// # fn error_policy(_: Arc<ConfigMap>, _: &kube::Error, _: Arc<Context>) -> Action {
/// # Action::await_change()
/// # }
/// # async fn doc(client: kube::Client) -> Result<(), Box<dyn std::error::Error>> {
/// # let memcached = Api::<ConfigMap>::all(client.clone());
clux marked this conversation as resolved.
Show resolved Hide resolved
/// # let context = Arc::new(Context);
/// Controller::new(memcached, ListParams::default())
/// .watches(
/// Api::<WatchedResource>::all(client.clone()),
/// ListParams::default(),
/// |ar| {
clux marked this conversation as resolved.
Show resolved Hide resolved
/// let prt = ar
/// .annotations()
/// .get("operator-sdk/primary-resource-type")
/// .map(String::as_str);
///
/// if prt != Some("Memcached.cache.example.com") {
/// return None;
/// }
///
/// let (namespace, name) = ar
/// .annotations()
/// .get("operator-sdk/primary-resource")?
/// .split_once('/')?;
///
/// Some(ObjectRef::new(name).within(namespace))
/// }
/// )
/// .run(reconcile, error_policy, context)
/// .for_each(|_| futures::future::ready(()))
/// .await;
/// # Ok(())
/// # }
/// ```
///
/// [Operator-SDK]: https://sdk.operatorframework.io/docs/building-operators/ansible/reference/retroactively-owned-resources/
#[must_use]
pub fn watches<
Other: Clone + Resource<DynamicType = ()> + DeserializeOwned + Debug + Send + 'static,
Expand Down