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

Upgrade kube to 0.98 and migrate event Recorder #116

Merged
merged 7 commits into from
Dec 23, 2024
Merged
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
82 changes: 52 additions & 30 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 @@ -29,7 +29,7 @@ telemetry = ["opentelemetry-otlp"]
actix-web = "4.4.0"
futures = "0.3.31"
tokio = { version = "1.41.1", features = ["macros", "rt-multi-thread"] }
k8s-openapi = { version = "0.23.0", features = ["latest"] }
k8s-openapi = { version = "0.24.0", features = ["latest"] }
schemars = { version = "0.8.12", features = ["chrono"] }
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133"
Expand All @@ -53,7 +53,7 @@ tower-test = "0.4.0"

[dependencies.kube]
features = ["runtime", "client", "derive" ]
version = "0.97.0"
version = "0.98.0"

# testing new releases - ignore
#git = "https://github.com/kube-rs/kube.git"
Expand Down
59 changes: 35 additions & 24 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 All @@ -50,6 +52,8 @@ impl Document {
pub struct Context {
/// Kubernetes client
pub client: Client,
/// Event recorder
pub recorder: Recorder,
/// Diagnostics read by the web server
pub diagnostics: Arc<RwLock<Diagnostics>>,
/// Prometheus metrics
Expand Down Expand Up @@ -88,22 +92,25 @@ 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 oref = self.object_ref(&());
let ns = self.namespace().unwrap();
let name = self.name_any();
let docs: Api<Document> = Api::namespaced(client, &ns);

let should_hide = self.spec.hide;
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,
})
ctx.recorder
.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 +137,19 @@ 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 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,
})
ctx.recorder
.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 Expand Up @@ -193,9 +203,10 @@ impl State {
}

// Create a Controller Context that can update State
pub fn to_context(&self, client: Client) -> Arc<Context> {
pub async fn to_context(&self, client: Client) -> Arc<Context> {
Arc::new(Context {
client,
client: client.clone(),
recorder: self.diagnostics.read().await.recorder(client),
metrics: self.metrics.clone(),
diagnostics: self.diagnostics.clone(),
})
Expand All @@ -213,7 +224,7 @@ pub async fn run(state: State) {
}
Controller::new(docs, Config::default().any_semantic())
.shutdown_on_signal()
.run(reconcile, error_policy, state.to_context(client))
.run(reconcile, error_policy, state.to_context(client).await)
.filter_map(|x| async move { std::result::Result::ok(x) })
.for_each(|_| futures::future::ready(()))
.await;
Expand Down Expand Up @@ -293,7 +304,7 @@ mod test {
#[ignore = "uses k8s current-context"]
async fn integration_reconcile_should_set_status_and_send_event() {
let client = kube::Client::try_default().await.unwrap();
let ctx = super::State::default().to_context(client.clone());
let ctx = super::State::default().to_context(client.clone()).await;

// create a test doc
let doc = Document::test().finalized().needs_hide();
Expand Down
4 changes: 3 additions & 1 deletion src/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{Context, Document, DocumentSpec, DocumentStatus, Result, DOCUMENT_FINALIZER};
use assert_json_diff::assert_json_include;
use http::{Request, Response};
use kube::{client::Body, Client, Resource, ResourceExt};
use kube::{client::Body, runtime::events::Recorder, Client, Resource, ResourceExt};
use std::sync::Arc;

impl Document {
Expand Down Expand Up @@ -210,10 +210,12 @@ impl Context {
pub fn test() -> (Arc<Self>, ApiServerVerifier) {
let (mock_service, handle) = tower_test::mock::pair::<Request<Body>, Response<Body>>();
let mock_client = Client::new(mock_service, "default");
let mock_recorder = Recorder::new(mock_client.clone(), "doc-ctrl-test".into());
let ctx = Self {
client: mock_client,
metrics: Arc::default(),
diagnostics: Arc::default(),
recorder: mock_recorder,
};
(Arc::new(ctx), ApiServerVerifier(handle))
}
Expand Down
Loading
Loading