From bc7e95e26c96ba0f2f37cd2e1556bff2b276217b Mon Sep 17 00:00:00 2001 From: clux Date: Fri, 7 Apr 2023 20:43:38 +0100 Subject: [PATCH 1/4] Showcase predicate filters with timestamp in status Signed-off-by: clux --- Cargo.toml | 4 ++-- src/controller.rs | 15 ++++++++++++--- src/telemetry.rs | 2 +- yaml/crd.yaml | 3 +++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c5b21fe..54f9494 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,8 +52,8 @@ hyper = "0.14.23" tower-test = "0.4.0" [dependencies.kube] -features = ["runtime", "client", "derive"] -#version = "0.81.0" +features = ["runtime", "client", "derive", "unstable-runtime"] +version = "0.81.0" # testing new releases - ignore #git = "https://github.com/kube-rs/kube.git" diff --git a/src/controller.rs b/src/controller.rs index 88e8972..b77121b 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, Utc, SecondsFormat}; 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,9 +115,11 @@ 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(); + info!("patching status!"); let _o = docs .patch_status(&name, &ps, &new_status) .await @@ -206,7 +209,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/src/telemetry.rs b/src/telemetry.rs index f100588..43936b8 100644 --- a/src/telemetry.rs +++ b/src/telemetry.rs @@ -42,7 +42,7 @@ pub async fn init() { // Setup tracing layers #[cfg(feature = "telemetry")] let telemetry = tracing_opentelemetry::layer().with_tracer(init_tracer().await); - let logger = tracing_subscriber::fmt::layer(); + let logger = tracing_subscriber::fmt::layer().compact(); let env_filter = EnvFilter::try_from_default_env() .or_else(|_| EnvFilter::try_new("info")) .unwrap(); diff --git a/yaml/crd.yaml b/yaml/crd.yaml index c61170b..712a3b2 100644 --- a/yaml/crd.yaml +++ b/yaml/crd.yaml @@ -41,8 +41,11 @@ spec: properties: hidden: type: boolean + last_update: + type: string required: - hidden + - last_update type: object required: - spec From 5e3653f2749472110cce10ff8717d3c55c37b79a Mon Sep 17 00:00:00 2001 From: clux Date: Fri, 7 Apr 2023 20:48:43 +0100 Subject: [PATCH 2/4] fmt Signed-off-by: clux --- src/controller.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controller.rs b/src/controller.rs index b77121b..bf5614f 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,5 +1,5 @@ use crate::{telemetry, Error, Metrics, Result}; -use chrono::{DateTime, Utc, SecondsFormat}; +use chrono::{DateTime, SecondsFormat, Utc}; use futures::StreamExt; use kube::{ api::{Api, ListParams, Patch, PatchParams, ResourceExt}, From 4b3ff37b0da6f1a730ea26b41d35ce77549fcb1c Mon Sep 17 00:00:00 2001 From: clux Date: Fri, 7 Apr 2023 20:52:38 +0100 Subject: [PATCH 3/4] stray info Signed-off-by: clux --- src/controller.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/controller.rs b/src/controller.rs index bf5614f..57b3c16 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -119,7 +119,6 @@ impl Document { } })); let ps = PatchParams::apply("cntrlr").force(); - info!("patching status!"); let _o = docs .patch_status(&name, &ps, &new_status) .await From 1a6aef5f230a7dc4b9dd38b36bba3dbcf7fe4bef Mon Sep 17 00:00:00 2001 From: clux Date: Wed, 9 Oct 2024 21:45:01 +0100 Subject: [PATCH 4/4] actually still need unstable-runtime for streams api of controller Signed-off-by: clux --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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