Skip to content

Commit

Permalink
test out new event recorder interface
Browse files Browse the repository at this point in the history
for kube-rs/kube#1655

Signed-off-by: clux <[email protected]>
  • Loading branch information
clux committed Dec 9, 2024
1 parent 80c856e commit c751449
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
30 changes: 19 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
46 changes: 28 additions & 18 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Document>.
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
#[cfg_attr(test, derive(Default))]
#[kube(kind = "Document", group = "kube.rs", version = "v1", namespaced)]
Expand Down Expand Up @@ -88,7 +90,8 @@ impl Document {
// Reconcile (for non-finalizer related changes)
async fn reconcile(&self, ctx: Arc<Context>) -> Result<Action> {
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<Document> = Api::namespaced(client, &ns);
Expand All @@ -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)?;
}
Expand All @@ -130,16 +136,20 @@ impl Document {

// Finalizer cleanup (the object was deleted, ensure nothing is orphaned)
async fn cleanup(&self, ctx: Arc<Context>) -> Result<Action> {
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())
Expand All @@ -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())
}
}

Expand Down

0 comments on commit c751449

Please sign in to comment.