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

Showcase predicate filters with timestamp in status #50

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/controller.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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,
};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) })
Expand Down
3 changes: 3 additions & 0 deletions yaml/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ spec:
properties:
hidden:
type: boolean
last_update:
type: string
required:
- hidden
- last_update
type: object
required:
- spec
Expand Down
Loading