use kube::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(CustomResource, Clone, Debug, Deserialize, Serialize, JsonSchema)]
#[kube(
group = "modelexpress.nvidia.com",
version = "v1alpha1",
kind = "ModelCacheEntry",
plural = "modelcacheentries",
shortname = "mxcache",
namespaced,
status = "ModelCacheEntryStatus",
doc = "ModelExpress model-download lifecycle metadata",
printcolumn = r#"{"name":"Model","type":"string","jsonPath":".spec.modelName"}"#,
printcolumn = r#"{"name":"Phase","type":"string","jsonPath":".status.phase"}"#,
printcolumn = r#"{"name":"LastUsed","type":"date","jsonPath":".status.lastUsedAt"}"#,
printcolumn = r#"{"name":"Claim","type":"string","jsonPath":".status.claimId","priority":1}"#,
printcolumn = r#"{"name":"Lease","type":"date","jsonPath":".status.leaseExpiresAt","priority":1}"#,
printcolumn = r#"{"name":"Age","type":"date","jsonPath":".metadata.creationTimestamp"}"#
)]
pub struct ModelCacheEntrySpec {
#[serde(rename = "modelName")]
pub model_name: String,
#[schemars(with = "ProviderSchema")]
pub provider: String,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, JsonSchema)]
pub struct ModelCacheEntryStatus {
#[serde(default)]
#[schemars(with = "PhaseSchema")]
pub phase: String,
#[serde(rename = "createdAt", default)]
#[schemars(with = "Option<chrono::DateTime<chrono::Utc>>")]
pub created_at: Option<String>,
#[serde(rename = "lastUsedAt", default)]
#[schemars(with = "Option<chrono::DateTime<chrono::Utc>>")]
pub last_used_at: Option<String>,
#[serde(default)]
pub message: Option<String>,
#[serde(rename = "claimId", default)]
pub claim_id: Option<String>,
#[serde(rename = "leaseExpiresAt", default)]
#[schemars(with = "Option<chrono::DateTime<chrono::Utc>>")]
pub lease_expires_at: Option<String>,
}
#[derive(JsonSchema)]
#[allow(dead_code)]
enum ProviderSchema {
HuggingFace,
Ngc,
Gcs,
S3,
}
#[derive(JsonSchema)]
#[allow(dead_code)]
enum PhaseSchema {
#[schemars(rename = "")]
Empty,
Downloading,
Downloaded,
Error,
}
pub mod phase {
pub const DOWNLOADING: &str = "Downloading";
pub const DOWNLOADED: &str = "Downloaded";
pub const ERROR: &str = "Error";
}
#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn spec_roundtrips_through_json() {
let spec = ModelCacheEntrySpec {
model_name: "meta-llama/Llama-3.1-70B".to_string(),
provider: "HuggingFace".to_string(),
};
let json = serde_json::to_string(&spec).expect("serialize");
assert!(json.contains("\"modelName\":\"meta-llama/Llama-3.1-70B\""));
let back: ModelCacheEntrySpec = serde_json::from_str(&json).expect("deserialize");
assert_eq!(back.model_name, spec.model_name);
assert_eq!(back.provider, spec.provider);
}
#[test]
fn status_default_is_empty() {
let status = ModelCacheEntryStatus::default();
assert_eq!(status.phase, "");
assert!(status.created_at.is_none());
assert!(status.last_used_at.is_none());
assert!(status.message.is_none());
assert!(status.claim_id.is_none());
assert!(status.lease_expires_at.is_none());
}
}