gitdataai/apps/operator/src/context.rs
2026-04-14 19:02:01 +08:00

45 lines
1.4 KiB
Rust

//! Shared reconcile context.
use kube::Client;
/// Context passed to every reconcile call.
#[derive(Clone)]
pub struct ReconcileCtx {
pub client: Client,
/// Default image registry prefix (e.g. "myapp/").
pub image_prefix: String,
/// Operator's own namespace.
pub operator_namespace: String,
}
impl ReconcileCtx {
pub async fn from_env() -> anyhow::Result<Self> {
let client = Client::try_default().await?;
let ns = std::env::var("POD_NAMESPACE").unwrap_or_else(|_| "default".to_string());
let prefix =
std::env::var("OPERATOR_IMAGE_PREFIX").unwrap_or_else(|_| "myapp/".to_string());
Ok(Self {
client,
image_prefix: prefix,
operator_namespace: ns,
})
}
/// Prepend image_prefix to an unqualified image name.
/// E.g. "app:latest" → "myapp/app:latest"
pub fn resolve_image(&self, image: &str) -> String {
// If it already has a registry/domain component, leave it alone.
if image.contains('/') && !image.starts_with(&self.image_prefix) {
image.to_string()
} else if image.starts_with(&self.image_prefix) {
image.to_string()
} else {
// Unqualified name: prepend prefix.
format!("{}{}", self.image_prefix, image)
}
}
}
pub type ReconcileState = ReconcileCtx;