Skip to content

Commit ac0b94a

Browse files
authored
Upgrade kube to 0.98 and migrate event Recorder (#116)
* test out new event recorder interface for kube-rs/kube#1655 Signed-off-by: clux <[email protected]> * cache recorder in Context to use the cache correctly Signed-off-by: clux <[email protected]> * event by ref Signed-off-by: clux <[email protected]> * use official now that it's released Signed-off-by: clux <[email protected]> * merge conflicts Signed-off-by: clux <[email protected]> * fix test compile Signed-off-by: clux <[email protected]> * gen Signed-off-by: clux <[email protected]> --------- Signed-off-by: clux <[email protected]>
1 parent 1f178f3 commit ac0b94a

File tree

5 files changed

+76
-41
lines changed

5 files changed

+76
-41
lines changed

Cargo.lock

Lines changed: 35 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ telemetry = ["opentelemetry-otlp"]
2929
actix-web = "4.4.0"
3030
futures = "0.3.31"
3131
tokio = { version = "1.41.1", features = ["macros", "rt-multi-thread"] }
32-
k8s-openapi = { version = "0.23.0", features = ["latest"] }
32+
k8s-openapi = { version = "0.24.0", features = ["latest"] }
3333
schemars = { version = "0.8.12", features = ["chrono"] }
3434
serde = { version = "1.0.216", features = ["derive"] }
3535
serde_json = "1.0.133"
@@ -53,7 +53,7 @@ tower-test = "0.4.0"
5353

5454
[dependencies.kube]
5555
features = ["runtime", "client", "derive" ]
56-
version = "0.97.0"
56+
version = "0.98.0"
5757

5858
# testing new releases - ignore
5959
#git = "https://github.com/kube-rs/kube.git"

src/controller.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub static DOCUMENT_FINALIZER: &str = "documents.kube.rs";
2424
/// Generate the Kubernetes wrapper struct `Document` from our Spec and Status struct
2525
///
2626
/// This provides a hook for generating the CRD yaml (in crdgen.rs)
27+
/// NB: CustomResource generates a pub struct Document here
28+
/// To query for documents.kube.rs with kube, use Api<Document>.
2729
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
2830
#[cfg_attr(test, derive(Default))]
2931
#[kube(kind = "Document", group = "kube.rs", version = "v1", namespaced)]
@@ -50,6 +52,8 @@ impl Document {
5052
pub struct Context {
5153
/// Kubernetes client
5254
pub client: Client,
55+
/// Event recorder
56+
pub recorder: Recorder,
5357
/// Diagnostics read by the web server
5458
pub diagnostics: Arc<RwLock<Diagnostics>>,
5559
/// Prometheus metrics
@@ -88,22 +92,25 @@ impl Document {
8892
// Reconcile (for non-finalizer related changes)
8993
async fn reconcile(&self, ctx: Arc<Context>) -> Result<Action> {
9094
let client = ctx.client.clone();
91-
let recorder = ctx.diagnostics.read().await.recorder(client.clone(), self);
95+
let oref = self.object_ref(&());
9296
let ns = self.namespace().unwrap();
9397
let name = self.name_any();
9498
let docs: Api<Document> = Api::namespaced(client, &ns);
9599

96100
let should_hide = self.spec.hide;
97101
if !self.was_hidden() && should_hide {
98102
// send an event once per hide
99-
recorder
100-
.publish(Event {
101-
type_: EventType::Normal,
102-
reason: "HideRequested".into(),
103-
note: Some(format!("Hiding `{name}`")),
104-
action: "Hiding".into(),
105-
secondary: None,
106-
})
103+
ctx.recorder
104+
.publish(
105+
&Event {
106+
type_: EventType::Normal,
107+
reason: "HideRequested".into(),
108+
note: Some(format!("Hiding `{name}`")),
109+
action: "Hiding".into(),
110+
secondary: None,
111+
},
112+
&oref,
113+
)
107114
.await
108115
.map_err(Error::KubeError)?;
109116
}
@@ -130,16 +137,19 @@ impl Document {
130137

131138
// Finalizer cleanup (the object was deleted, ensure nothing is orphaned)
132139
async fn cleanup(&self, ctx: Arc<Context>) -> Result<Action> {
133-
let recorder = ctx.diagnostics.read().await.recorder(ctx.client.clone(), self);
140+
let oref = self.object_ref(&());
134141
// Document doesn't have any real cleanup, so we just publish an event
135-
recorder
136-
.publish(Event {
137-
type_: EventType::Normal,
138-
reason: "DeleteRequested".into(),
139-
note: Some(format!("Delete `{}`", self.name_any())),
140-
action: "Deleting".into(),
141-
secondary: None,
142-
})
142+
ctx.recorder
143+
.publish(
144+
&Event {
145+
type_: EventType::Normal,
146+
reason: "DeleteRequested".into(),
147+
note: Some(format!("Delete `{}`", self.name_any())),
148+
action: "Deleting".into(),
149+
secondary: None,
150+
},
151+
&oref,
152+
)
143153
.await
144154
.map_err(Error::KubeError)?;
145155
Ok(Action::await_change())
@@ -163,8 +173,8 @@ impl Default for Diagnostics {
163173
}
164174
}
165175
impl Diagnostics {
166-
fn recorder(&self, client: Client, doc: &Document) -> Recorder {
167-
Recorder::new(client, self.reporter.clone(), doc.object_ref(&()))
176+
fn recorder(&self, client: Client) -> Recorder {
177+
Recorder::new(client, self.reporter.clone())
168178
}
169179
}
170180

@@ -193,9 +203,10 @@ impl State {
193203
}
194204

195205
// Create a Controller Context that can update State
196-
pub fn to_context(&self, client: Client) -> Arc<Context> {
206+
pub async fn to_context(&self, client: Client) -> Arc<Context> {
197207
Arc::new(Context {
198-
client,
208+
client: client.clone(),
209+
recorder: self.diagnostics.read().await.recorder(client),
199210
metrics: self.metrics.clone(),
200211
diagnostics: self.diagnostics.clone(),
201212
})
@@ -213,7 +224,7 @@ pub async fn run(state: State) {
213224
}
214225
Controller::new(docs, Config::default().any_semantic())
215226
.shutdown_on_signal()
216-
.run(reconcile, error_policy, state.to_context(client))
227+
.run(reconcile, error_policy, state.to_context(client).await)
217228
.filter_map(|x| async move { std::result::Result::ok(x) })
218229
.for_each(|_| futures::future::ready(()))
219230
.await;
@@ -293,7 +304,7 @@ mod test {
293304
#[ignore = "uses k8s current-context"]
294305
async fn integration_reconcile_should_set_status_and_send_event() {
295306
let client = kube::Client::try_default().await.unwrap();
296-
let ctx = super::State::default().to_context(client.clone());
307+
let ctx = super::State::default().to_context(client.clone()).await;
297308

298309
// create a test doc
299310
let doc = Document::test().finalized().needs_hide();

src/fixtures.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use crate::{Context, Document, DocumentSpec, DocumentStatus, Result, DOCUMENT_FINALIZER};
33
use assert_json_diff::assert_json_include;
44
use http::{Request, Response};
5-
use kube::{client::Body, Client, Resource, ResourceExt};
5+
use kube::{client::Body, runtime::events::Recorder, Client, Resource, ResourceExt};
66
use std::sync::Arc;
77

88
impl Document {
@@ -210,10 +210,12 @@ impl Context {
210210
pub fn test() -> (Arc<Self>, ApiServerVerifier) {
211211
let (mock_service, handle) = tower_test::mock::pair::<Request<Body>, Response<Body>>();
212212
let mock_client = Client::new(mock_service, "default");
213+
let mock_recorder = Recorder::new(mock_client.clone(), "doc-ctrl-test".into());
213214
let ctx = Self {
214215
client: mock_client,
215216
metrics: Arc::default(),
216217
diagnostics: Arc::default(),
218+
recorder: mock_recorder,
217219
};
218220
(Arc::new(ctx), ApiServerVerifier(handle))
219221
}

yaml/crd.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec:
2323
description: |-
2424
Generate the Kubernetes wrapper struct `Document` from our Spec and Status struct
2525
26-
This provides a hook for generating the CRD yaml (in crdgen.rs)
26+
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>.
2727
properties:
2828
content:
2929
type: string

0 commit comments

Comments
 (0)