Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::reconcile::{

use crate::error::OperatorResult;
use crate::finalizer;
use crate::podutils;

use futures::StreamExt;
use kube::api::{ListParams, Meta};
use kube::Api;
Expand Down Expand Up @@ -223,19 +225,22 @@ async fn handle_deletion<T>(
where
T: Clone + DeserializeOwned + Meta + Send + Sync + 'static,
{
let address = format!("[{:?}/{}]", Meta::namespace(resource), Meta::name(resource));
trace!("Reconciler [handle_deletion] for {}", address);
trace!(
"Reconciler [handle_deletion] for {}",
podutils::get_log_name(resource)
);
if !finalizer::has_deletion_stamp(resource) {
debug!(
"[handle_deletion] for {}: Not deleted, continuing...",
address
podutils::get_log_name(resource)
);
return Ok(ReconcileFunctionAction::Continue);
}

info!(
"Removing finalizer [{}] for resource {}",
finalizer_name, address
finalizer_name,
podutils::get_log_name(resource)
);
finalizer::remove_finalizer(client, resource, finalizer_name).await?;

Expand All @@ -246,18 +251,17 @@ async fn add_finalizer<T>(resource: &T, client: Client, finalizer_name: &str) ->
where
T: Clone + Debug + DeserializeOwned + Meta + Send + Sync + 'static,
{
let address = format!("[{:?}/{}]", Meta::namespace(resource), Meta::name(resource));
trace!(resource = ?resource, "Reconciler [add_finalizer] for {}", address);
trace!(resource = ?resource, "Reconciler [add_finalizer] for {}", podutils::get_log_name(resource));

if finalizer::has_finalizer(resource, finalizer_name) {
debug!(
"[add_finalizer] for {}: Finalizer already exists, continuing...",
address
podutils::get_log_name(resource)
);
} else {
debug!(
"[add_finalizer] for {}: Finalizer missing, adding now and continuing...",
address
podutils::get_log_name(resource)
);
finalizer::add_finalizer(client, resource, finalizer_name).await?;
}
Expand Down
37 changes: 36 additions & 1 deletion src/podutils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::fmt;
use k8s_openapi::api::core::v1::{Pod, PodCondition, PodStatus};
use kube::api::Meta;

/// While the `phase` field of a Pod is a string only the values from this enum are allowed.
#[derive(Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -75,10 +76,44 @@ fn get_pod_condition<'a>(status: &'a PodStatus, condition: &str) -> Option<&'a P
}
}

/// Returns a name that is suitable for directly passing to a log macro.
///
/// It'll contain the namespace and the name wrapped in square brackets.
/// Example output: `[foo/bar]`
///
/// If the resource has no namespace, it'll print `<no namespace>` instead: `[<no namespace>/bar]`
pub fn get_log_name<T>(resource: &T) -> String
where
T: Meta,
{
format!(
"[{}/{}]",
Meta::namespace(resource).unwrap_or("<no namespace>".to_string()),
Meta::name(resource)
)
}

#[cfg(test)]
mod tests {
use crate::podutils::{get_pod_condition, is_pod_created, is_pod_running_and_ready};
use super::*;
use k8s_openapi::api::core::v1::{Pod, PodCondition, PodStatus};
use kube::api::ObjectMeta;

#[test]
fn test_get_log_name() {
let mut pod = Pod {
metadata: ObjectMeta {
name: Some("bar".to_string()),
..ObjectMeta::default()
},
..Pod::default()
};

assert_eq!("[<no namespace>/bar]", get_log_name(&pod));

pod.metadata.namespace = Some("foo".to_string());
assert_eq!("[foo/bar]", get_log_name(&pod));
}

#[test]
fn test_is_pod_created() {
Expand Down
8 changes: 8 additions & 0 deletions src/reconcile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::client::Client;
use crate::error::Error;
use crate::podutils;
use k8s_openapi::api::core::v1::Pod;
use kube::api::{ListParams, Meta, ObjectMeta};
use kube_runtime::controller::ReconcilerAction;
Expand Down Expand Up @@ -60,6 +61,13 @@ where
Meta::namespace(&self.resource).expect("Resources are namespaced")
}

/// Returns a name that is suitable for directly passing to a log macro.
///
/// See [`crate::podutils::get_log_name()`] for details.
pub fn log_name(&self) -> String {
podutils::get_log_name(&self.resource)
}

pub fn metadata(&self) -> ObjectMeta {
self.resource.meta().clone()
}
Expand Down