use crate::error::KubeError;
use crate::kind::KubeResource;
use std::borrow::Cow;
#[async_trait::async_trait]
pub trait EventHandler<R: KubeResource>: Send + Sync {
async fn on_add(&self, obj: &R);
async fn on_update(&self, old: &R, new: &R);
async fn on_delete(&self, obj: &R);
}
pub trait Cache<R: KubeResource>: Send + Sync {
fn get(&self, namespace: Option<&str>, name: &str) -> Option<R>;
fn list(&self) -> Vec<R>;
fn len(&self) -> usize;
fn is_empty(&self) -> bool { self.len() == 0 }
}
#[async_trait::async_trait]
pub trait Informer<R: KubeResource>: Cache<R> {
async fn add_event_handler(&self, handler: Box<dyn EventHandler<R>>);
async fn wait_for_initial_sync(&self) -> Result<(), KubeError>;
async fn shutdown(&self);
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CacheKey<'a> {
pub namespace: Cow<'a, str>,
pub name: Cow<'a, str>,
}
impl<'a> CacheKey<'a> {
#[must_use]
pub fn from_resource<R: KubeResource>(r: &'a R) -> Self {
Self {
namespace: r.namespace().unwrap_or(Cow::Borrowed("")),
name: r.name(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cache_key_equality() {
let a = CacheKey { namespace: "default".into(), name: "p1".into() };
let b = CacheKey { namespace: "default".into(), name: "p1".into() };
assert_eq!(a, b);
}
}