Skip to main content

modelexpress_server/registry/
k8s_types.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Kubernetes CRD types for the model registry.
5//!
6//! Defines `ModelCacheEntry` — a custom resource that holds model-download lifecycle
7//! state (phase + timestamps + message) for one cached model. This is the registry
8//! analogue of the P2P `ModelMetadata` CRD, kept in a separate Kind so cardinality,
9//! lifecycle, and RBAC scopes stay independent.
10
11use kube::CustomResource;
12use schemars::JsonSchema;
13use serde::{Deserialize, Serialize};
14
15/// ModelCacheEntry spec - desired state. One CR per cached model.
16#[derive(CustomResource, Clone, Debug, Deserialize, Serialize, JsonSchema)]
17#[kube(
18    group = "modelexpress.nvidia.com",
19    version = "v1alpha1",
20    kind = "ModelCacheEntry",
21    plural = "modelcacheentries",
22    shortname = "mxcache",
23    namespaced,
24    status = "ModelCacheEntryStatus",
25    doc = "ModelExpress model-download lifecycle metadata",
26    printcolumn = r#"{"name":"Model","type":"string","jsonPath":".spec.modelName"}"#,
27    printcolumn = r#"{"name":"Phase","type":"string","jsonPath":".status.phase"}"#,
28    printcolumn = r#"{"name":"LastUsed","type":"date","jsonPath":".status.lastUsedAt"}"#,
29    printcolumn = r#"{"name":"Claim","type":"string","jsonPath":".status.claimId","priority":1}"#,
30    printcolumn = r#"{"name":"Lease","type":"date","jsonPath":".status.leaseExpiresAt","priority":1}"#,
31    printcolumn = r#"{"name":"Age","type":"date","jsonPath":".metadata.creationTimestamp"}"#
32)]
33pub struct ModelCacheEntrySpec {
34    /// Full model name (e.g., `meta-llama/Llama-3.1-70B`). Preserved in spec so
35    /// operators can recover the original name even when the CR's metadata.name is a
36    /// sanitized/hashed form.
37    #[serde(rename = "modelName")]
38    pub model_name: String,
39
40    /// Provider string — `"HuggingFace"`, `"Ngc"`, `"Gcs"`, or `"S3"`.
41    #[schemars(with = "ProviderSchema")]
42    pub provider: String,
43}
44
45/// ModelCacheEntry status - observed state.
46#[derive(Clone, Debug, Default, Deserialize, Serialize, JsonSchema)]
47pub struct ModelCacheEntryStatus {
48    /// One of `"Downloading"`, `"Downloaded"`, `"Error"`. Empty string on freshly-created
49    /// records that haven't yet received a status patch.
50    #[serde(default)]
51    #[schemars(with = "PhaseSchema")]
52    pub phase: String,
53
54    /// RFC3339 timestamp of first write. Omitted until the first status patch.
55    #[serde(rename = "createdAt", default)]
56    #[schemars(with = "Option<chrono::DateTime<chrono::Utc>>")]
57    pub created_at: Option<String>,
58
59    /// RFC3339 timestamp of most recent status write or touch.
60    #[serde(rename = "lastUsedAt", default)]
61    #[schemars(with = "Option<chrono::DateTime<chrono::Utc>>")]
62    pub last_used_at: Option<String>,
63
64    /// Optional human-readable message (download progress, error reason).
65    #[serde(default)]
66    pub message: Option<String>,
67
68    /// Opaque identity of the replica that currently owns a download.
69    #[serde(rename = "claimId", default)]
70    pub claim_id: Option<String>,
71
72    /// Owner-generated RFC3339 heartbeat deadline. Observers use changes in this value
73    /// to measure liveness locally instead of comparing clocks across replicas.
74    #[serde(rename = "leaseExpiresAt", default)]
75    #[schemars(with = "Option<chrono::DateTime<chrono::Utc>>")]
76    pub lease_expires_at: Option<String>,
77}
78
79#[derive(JsonSchema)]
80#[allow(dead_code)]
81enum ProviderSchema {
82    HuggingFace,
83    Ngc,
84    Gcs,
85    S3,
86}
87
88#[derive(JsonSchema)]
89#[allow(dead_code)]
90enum PhaseSchema {
91    #[schemars(rename = "")]
92    Empty,
93    Downloading,
94    Downloaded,
95    Error,
96}
97
98/// Phase strings used by the CRD. Match `ModelStatus` one-for-one.
99pub mod phase {
100    pub const DOWNLOADING: &str = "Downloading";
101    pub const DOWNLOADED: &str = "Downloaded";
102    pub const ERROR: &str = "Error";
103}
104
105#[cfg(test)]
106#[allow(clippy::expect_used)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn spec_roundtrips_through_json() {
112        let spec = ModelCacheEntrySpec {
113            model_name: "meta-llama/Llama-3.1-70B".to_string(),
114            provider: "HuggingFace".to_string(),
115        };
116        let json = serde_json::to_string(&spec).expect("serialize");
117        assert!(json.contains("\"modelName\":\"meta-llama/Llama-3.1-70B\""));
118        let back: ModelCacheEntrySpec = serde_json::from_str(&json).expect("deserialize");
119        assert_eq!(back.model_name, spec.model_name);
120        assert_eq!(back.provider, spec.provider);
121    }
122
123    #[test]
124    fn status_default_is_empty() {
125        let status = ModelCacheEntryStatus::default();
126        assert_eq!(status.phase, "");
127        assert!(status.created_at.is_none());
128        assert!(status.last_used_at.is_none());
129        assert!(status.message.is_none());
130        assert!(status.claim_id.is_none());
131        assert!(status.lease_expires_at.is_none());
132    }
133}