Replies: 1 comment
-
This worked for me: use std::{sync::Arc, time::Duration};
use anyhow::{Result, bail};
use futures::stream::StreamExt;
use kube::{
Api, Client,
runtime::{Controller, controller::Action, watcher::Config},
};
use crate::crds::myresource::MyResource;
struct ContextData {
client: Client,
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
struct Error(anyhow::Error);
pub async fn controller(client: Client) -> Result<(), kube::Error> {
let context = Arc::new(ContextData {
client: client.clone(),
});
let api: Api<MyResource> = Api::all(client.clone());
Controller::new(api.clone(), Config::default())
.run(
|obj, ctx| async move { reconcile(obj, ctx).await.map_err(Error) },
error_policy,
context,
)
.for_each(|result| async move {
match result {
Ok(obj) => {
println!("Reconciliation success: {:?}", obj);
}
Err(err) => {
eprintln!("Reconciliation error: {:?}", err);
}
}
})
.await;
Ok(())
}
fn error_policy(obj: Arc<MyResource>, error: &Error, _context: Arc<ContextData>) -> Action {
eprintln!(
"Reconciliation error, retrying in 5 secs. obj={:?} error={:?}",
obj, error
);
Action::requeue(Duration::from_secs(5))
}
async fn reconcile(obj: Arc<MyResource>, ctx: Arc<ContextData>) -> Result<Action> {
let client = ctx.client.clone();
println!("apiserver version: {:?}", client.apiserver_version().await?);
if obj.spec.backup_location == "fail" {
bail!("oops");
}
Ok(Action::await_change())
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When trying to write a controller that uses
anyhow::Error
(e.g. like in the example https://docs.rs/kube/latest/kube/runtime/controller/struct.Controller.html, replacingerror
withanyhow::Error
), I get the errorwhich is kind-of expected: It's not implemented in anyhow: dtolnay/anyhow#25.
I'd like to use the same error library in all of the application and so would like to use anyhow's error. Is that possible? If so, how?
Beta Was this translation helpful? Give feedback.
All reactions