Skip to content

Commit bf094e4

Browse files
committed
chore: Aggregate emitted Kubernetes events
1 parent e0827b9 commit bf094e4

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

crates/stackable-operator/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- BREAKING: Aggregate emitted Kubernetes events on the CustomResources thanks to the new [kube feature](https://github.com/kube-rs/controller-rs/pull/116).
10+
Instead of spamming the same Warning over and over it now uses `EventSeries` to have a nice - single - Age `3s (x11 over 53s)`.
11+
12+
First off this makes `report_controller_error` async.
13+
Also you now need to pass a `Recorder` instead of a `Client` instead.
14+
You need to store the `Recorder` across all `reconcile` invocations, so that it can do the aggregation correctly.
15+
16+
Also make sure that the operator has the permission to `patch` events (previously only `create` was needed)! ([#867]).
17+
718
### Changed
819

920
- BREAKING: Bump all Rust dependencies (except opentelemetry-related) and enable Kubernetes 1.32 (via `kube` 0.98.0 and `k8s-openapi` 0.23.0) ([#867]).

crates/stackable-operator/src/logging/controller.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ use kube::{
88
core::DynamicObject,
99
runtime::{
1010
controller::{self, Action},
11+
events::Recorder,
1112
reflector::ObjectRef,
1213
},
1314
Resource,
1415
};
1516
use tracing;
1617

17-
use crate::{client::Client, logging::k8s_events::publish_controller_error_as_k8s_event};
18+
use crate::logging::k8s_events::publish_controller_error_as_k8s_event;
1819

1920
/// [`Error`] extensions that help report reconciliation errors
2021
///
@@ -43,8 +44,8 @@ pub trait ReconcilerError: Error {
4344
/// * Kubernetes [`Event`]s, if there is an error that is relevant to the end user
4445
///
4546
/// [`Event`]: `k8s_openapi::api::events::v1::Event`
46-
pub fn report_controller_reconciled<K, ReconcileErr, QueueErr>(
47-
client: &Client,
47+
pub async fn report_controller_reconciled<K, ReconcileErr, QueueErr>(
48+
recorder: &Recorder,
4849
controller_name: &str,
4950
result: &Result<(ObjectRef<K>, Action), controller::Error<ReconcileErr, QueueErr>>,
5051
) where
@@ -60,13 +61,13 @@ pub fn report_controller_reconciled<K, ReconcileErr, QueueErr>(
6061
"Reconciled object"
6162
);
6263
}
63-
Err(err) => report_controller_error(client, controller_name, err),
64+
Err(err) => report_controller_error(recorder, controller_name, err).await,
6465
}
6566
}
6667

6768
/// Reports an error to the operator administrator and, if relevant, the end user
68-
fn report_controller_error<ReconcileErr, QueueErr>(
69-
client: &Client,
69+
async fn report_controller_error<ReconcileErr, QueueErr>(
70+
recorder: &Recorder,
7071
controller_name: &str,
7172
error: &controller::Error<ReconcileErr, QueueErr>,
7273
) where
@@ -78,5 +79,5 @@ fn report_controller_error<ReconcileErr, QueueErr>(
7879
error = error as &dyn std::error::Error,
7980
"Failed to reconcile object",
8081
);
81-
publish_controller_error_as_k8s_event(client, controller_name, error);
82+
publish_controller_error_as_k8s_event(recorder, error).await;
8283
}

crates/stackable-operator/src/logging/k8s_events.rs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
33
use std::error::Error;
44

5-
use crate::client::Client;
65
use kube::runtime::{
76
controller,
8-
events::{Event, EventType, Recorder, Reporter},
7+
events::{Event, EventType, Recorder},
98
};
10-
use tracing::Instrument;
119

1210
use super::controller::ReconcilerError;
1311

@@ -41,10 +39,9 @@ fn error_to_event<E: ReconcilerError>(err: &E) -> Event {
4139
/// Reports an error coming from a controller to Kubernetes
4240
///
4341
/// This is intended to be executed on the log entries returned by [`kube::runtime::Controller::run`]
44-
#[tracing::instrument(skip(client))]
45-
pub fn publish_controller_error_as_k8s_event<ReconcileErr, QueueErr>(
46-
client: &Client,
47-
controller: &str,
42+
#[tracing::instrument(skip(recorder))]
43+
pub async fn publish_controller_error_as_k8s_event<ReconcileErr, QueueErr>(
44+
recorder: &Recorder,
4845
controller_error: &controller::Error<ReconcileErr, QueueErr>,
4946
) where
5047
ReconcileErr: ReconcilerError,
@@ -55,27 +52,15 @@ pub fn publish_controller_error_as_k8s_event<ReconcileErr, QueueErr>(
5552
return;
5653
};
5754

58-
let recorder = Recorder::new(
59-
client.as_kube_client(),
60-
Reporter {
61-
controller: controller.to_string(),
62-
instance: None,
63-
},
64-
);
65-
let event = error_to_event(error);
66-
let obj_ref = obj.clone().into();
67-
// Run in the background
68-
tokio::spawn(
69-
async move {
70-
if let Err(err) = recorder.publish(&event, &obj_ref).await {
71-
tracing::error!(
72-
error = &err as &dyn std::error::Error,
73-
"Failed to report error as K8s event"
74-
);
75-
}
76-
}
77-
.in_current_span(),
78-
);
55+
if let Err(err) = recorder
56+
.publish(&error_to_event(error), &obj.clone().into())
57+
.await
58+
{
59+
tracing::error!(
60+
error = &err as &dyn std::error::Error,
61+
"Failed to report error as K8s event"
62+
);
63+
}
7964
}
8065

8166
mod message {

0 commit comments

Comments
 (0)