From c7514498cca9134cc397e587ed2d21b545987bfe Mon Sep 17 00:00:00 2001 From: clux Date: Mon, 9 Dec 2024 20:59:31 +0700 Subject: [PATCH] test out new event recorder interface for https://github.com/kube-rs/kube/pull/1655 Signed-off-by: clux --- Cargo.lock | 30 +++++++++++++++++++----------- Cargo.toml | 4 ++-- src/controller.rs | 46 ++++++++++++++++++++++++++++------------------ 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index df5a324..f1487fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "actix-codec" @@ -1031,6 +1031,17 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi", +] + [[package]] name = "http" version = "0.2.12" @@ -1468,8 +1479,6 @@ dependencies = [ [[package]] name = "kube" version = "0.97.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5fd2596428f922f784ca43907c449f104d69055c811135684474143736c67ae" dependencies = [ "k8s-openapi", "kube-client", @@ -1481,8 +1490,6 @@ dependencies = [ [[package]] name = "kube-client" version = "0.97.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d539b6493d162ae5ab691762be972b6a1c20f6d8ddafaae305c0e2111b589d99" dependencies = [ "base64 0.22.1", "bytes", @@ -1519,8 +1526,6 @@ dependencies = [ [[package]] name = "kube-core" version = "0.97.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98a87cc0046cf6b62cbb63ae1fbc366ee8ba29269f575289679473754ff5d7a7" dependencies = [ "chrono", "form_urlencoded", @@ -1537,8 +1542,6 @@ dependencies = [ [[package]] name = "kube-derive" version = "0.97.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65576922713e6154a89b5a8d2747adca15725b90fa64fc2b828774bf96d6acd8" dependencies = [ "darling", "proc-macro2", @@ -1550,8 +1553,6 @@ dependencies = [ [[package]] name = "kube-runtime" version = "0.97.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f348cc3e6c9be0ae17f300594bde541b667d10ab8934a119edd61ab5123c43e" dependencies = [ "ahash", "async-broadcast", @@ -1561,6 +1562,7 @@ dependencies = [ "educe", "futures", "hashbrown 0.15.2", + "hostname", "json-patch", "jsonptr", "k8s-openapi", @@ -1632,6 +1634,12 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + [[package]] name = "matchers" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 9ec6e5e..2ef8577 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,10 +53,10 @@ tower-test = "0.4.0" [dependencies.kube] features = ["runtime", "client", "derive" ] -version = "0.97.0" +#version = "0.97.0" # testing new releases - ignore #git = "https://github.com/kube-rs/kube.git" #branch = "main" #rev = "19b90ad3a4dbc83e1dd742847c7707333259b1bb" -#path = "../kube/kube" +path = "../kube/kube" diff --git a/src/controller.rs b/src/controller.rs index 165b9f7..6bee946 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -24,6 +24,8 @@ pub static DOCUMENT_FINALIZER: &str = "documents.kube.rs"; /// Generate the Kubernetes wrapper struct `Document` from our Spec and Status struct /// /// This provides a hook for generating the CRD yaml (in crdgen.rs) +/// NB: CustomResource generates a pub struct Document here +/// To query for documents.kube.rs with kube, use Api. #[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)] #[cfg_attr(test, derive(Default))] #[kube(kind = "Document", group = "kube.rs", version = "v1", namespaced)] @@ -88,7 +90,8 @@ impl Document { // Reconcile (for non-finalizer related changes) async fn reconcile(&self, ctx: Arc) -> Result { let client = ctx.client.clone(); - let recorder = ctx.diagnostics.read().await.recorder(client.clone(), self); + let recorder = ctx.diagnostics.read().await.recorder(client.clone()); + let oref = self.object_ref(&()); let ns = self.namespace().unwrap(); let name = self.name_any(); let docs: Api = Api::namespaced(client, &ns); @@ -97,13 +100,16 @@ impl Document { if !self.was_hidden() && should_hide { // send an event once per hide recorder - .publish(Event { - type_: EventType::Normal, - reason: "HideRequested".into(), - note: Some(format!("Hiding `{name}`")), - action: "Hiding".into(), - secondary: None, - }) + .publish( + Event { + type_: EventType::Normal, + reason: "HideRequested".into(), + note: Some(format!("Hiding `{name}`")), + action: "Hiding".into(), + secondary: None, + }, + &oref, + ) .await .map_err(Error::KubeError)?; } @@ -130,16 +136,20 @@ impl Document { // Finalizer cleanup (the object was deleted, ensure nothing is orphaned) async fn cleanup(&self, ctx: Arc) -> Result { - let recorder = ctx.diagnostics.read().await.recorder(ctx.client.clone(), self); + let recorder = ctx.diagnostics.read().await.recorder(ctx.client.clone()); + let oref = self.object_ref(&()); // Document doesn't have any real cleanup, so we just publish an event recorder - .publish(Event { - type_: EventType::Normal, - reason: "DeleteRequested".into(), - note: Some(format!("Delete `{}`", self.name_any())), - action: "Deleting".into(), - secondary: None, - }) + .publish( + Event { + type_: EventType::Normal, + reason: "DeleteRequested".into(), + note: Some(format!("Delete `{}`", self.name_any())), + action: "Deleting".into(), + secondary: None, + }, + &oref, + ) .await .map_err(Error::KubeError)?; Ok(Action::await_change()) @@ -163,8 +173,8 @@ impl Default for Diagnostics { } } impl Diagnostics { - fn recorder(&self, client: Client, doc: &Document) -> Recorder { - Recorder::new(client, self.reporter.clone(), doc.object_ref(&())) + fn recorder(&self, client: Client) -> Recorder { + Recorder::new(client, self.reporter.clone()) } }