diff --git a/Cargo.toml b/Cargo.toml index 5db2d87..65e5ac2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,7 @@ hyper = "1" tower-test = "0.4.0" [dependencies.kube] -features = ["runtime", "client", "derive" ] +features = ["runtime", "client", "derive", "unstable-runtime"] version = "0.96.0" # testing new releases - ignore diff --git a/src/controller.rs b/src/controller.rs index a40e69b..dfe4bb7 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,5 +1,5 @@ use crate::{telemetry, Error, Metrics, Result}; -use chrono::{DateTime, Utc}; +use chrono::{DateTime, SecondsFormat, Utc}; use futures::StreamExt; use kube::{ api::{Api, ListParams, Patch, PatchParams, ResourceExt}, @@ -8,7 +8,7 @@ use kube::{ controller::{Action, Controller}, events::{Event, EventType, Recorder, Reporter}, finalizer::{finalizer, Event as Finalizer}, - watcher::Config, + predicates, reflector, watcher, WatchStreamExt, }, CustomResource, Resource, }; @@ -37,6 +37,7 @@ pub struct DocumentSpec { #[derive(Deserialize, Serialize, Clone, Default, Debug, JsonSchema)] pub struct DocumentStatus { pub hidden: bool, + pub last_update: String, } impl Document { @@ -114,6 +115,7 @@ impl Document { "kind": "Document", "status": DocumentStatus { hidden: should_hide, + last_update: Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true), } })); let ps = PatchParams::apply("cntrlr").force(); @@ -209,7 +211,13 @@ pub async fn run(state: State) { info!("Installation: cargo run --bin crdgen | kubectl apply -f -"); std::process::exit(1); } - Controller::new(docs, Config::default().any_semantic()) + // Trigger the controller only when there are real changes to the crd + let (reader, writer) = reflector::store(); + let triggers = reflector(writer, watcher(docs, watcher::Config::default().any_semantic())) + .applied_objects() + .predicate_filter(predicates::generation); + + Controller::for_stream(triggers, reader) .shutdown_on_signal() .run(reconcile, error_policy, state.to_context(client)) .filter_map(|x| async move { std::result::Result::ok(x) }) diff --git a/yaml/crd.yaml b/yaml/crd.yaml index 3ad309c..7275f64 100644 --- a/yaml/crd.yaml +++ b/yaml/crd.yaml @@ -42,8 +42,11 @@ spec: properties: hidden: type: boolean + last_update: + type: string required: - hidden + - last_update type: object required: - spec