cdl_openapi/
model_storage_binding.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use chrono::{DateTime, Utc};
use k8s_openapi::api::core::v1::ResourceRequirements;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};

use crate::{model::ModelSpec, model_storage::ModelStorageSpec};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
#[cfg_attr(feature = "k8s", derive(::kube::CustomResource))]
#[cfg_attr(
    feature = "k8s",
    kube(
        group = "cdl.ulagbulag.io",
        version = "v1alpha1",
        kind = "ModelStorageBinding",
        root = "ModelStorageBindingCrd",
        status = "ModelStorageBindingStatus",
        shortname = "msb",
        namespaced,
        printcolumn = r#"{
            "name": "state",
            "type": "string",
            "description": "state of the binding",
            "jsonPath": ".status.state"
        }"#,
        printcolumn = r#"{
            "name": "created-at",
            "type": "date",
            "description": "created time",
            "jsonPath": ".metadata.creationTimestamp"
        }"#,
        printcolumn = r#"{
            "name": "updated-at",
            "type": "date",
            "description": "updated time",
            "jsonPath": ".status.lastUpdated"
        }"#,
        printcolumn = r#"{
            "name": "version",
            "type": "integer",
            "description": "binding version",
            "jsonPath": ".metadata.generation"
        }"#,
    )
)]
#[serde(rename_all = "camelCase")]
pub struct ModelStorageBindingSpec {
    #[serde(default)]
    pub deletion_policy: ModelStorageBindingDeletionPolicy,
    pub model: String,
    #[serde(default)]
    pub resources: Option<ResourceRequirements>,
    pub storage: ModelStorageBindingStorageKind<String>,
}

#[cfg(feature = "k8s")]
impl ModelStorageBindingCrd {
    pub const FINALIZER_NAME: &'static str = "cdl.ulagbulag.io/finalizer-model-storage-bindings";
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum ModelStorageBindingStorageKind<Storage> {
    Cloned(ModelStorageBindingStorageKindClonedSpec<Storage>),
    Owned(ModelStorageBindingStorageKindOwnedSpec<Storage>),
}

impl<Storage> ModelStorageBindingStorageKind<Storage> {
    pub fn source(&self) -> Option<(&Storage, ModelStorageBindingSyncPolicy)> {
        match self {
            Self::Cloned(spec) => Some((&spec.source, spec.sync_policy)),
            Self::Owned(_) => None,
        }
    }

    pub fn source_binding_name(&self) -> Option<&str> {
        match self {
            Self::Cloned(spec) => spec.source_binding_name.as_deref(),
            Self::Owned(_) => None,
        }
    }

    pub fn sync_policy(&self) -> Option<ModelStorageBindingSyncPolicy> {
        match self {
            Self::Cloned(spec) => Some(spec.sync_policy),
            Self::Owned(_) => None,
        }
    }

    pub fn target(&self) -> &Storage {
        match self {
            Self::Cloned(spec) => &spec.target,
            Self::Owned(spec) => &spec.target,
        }
    }

    pub fn into_target(self) -> Storage {
        match self {
            Self::Cloned(spec) => spec.target,
            Self::Owned(spec) => spec.target,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ModelStorageBindingStorageKindClonedSpec<Storage> {
    pub source: Storage,
    #[serde(default)]
    pub source_binding_name: Option<String>,
    pub target: Storage,
    #[serde(default)]
    pub sync_policy: ModelStorageBindingSyncPolicy,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ModelStorageBindingStorageSpec<'name, Storage> {
    pub source: Option<ModelStorageBindingStorageSourceSpec<'name, Storage>>,
    pub source_binding_name: Option<&'name str>,
    pub target: Storage,
    pub target_name: &'name str,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ModelStorageBindingStorageSourceSpec<'name, Storage> {
    pub name: &'name str,
    pub storage: Storage,
    pub sync_policy: ModelStorageBindingSyncPolicy,
}

impl<'name, Storage> ModelStorageBindingStorageSourceSpec<'name, Storage> {
    pub fn as_deref(&self) -> ModelStorageBindingStorageSourceSpec<'name, &'_ Storage> {
        ModelStorageBindingStorageSourceSpec {
            name: self.name,
            storage: &self.storage,
            sync_policy: self.sync_policy,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ModelStorageBindingStorageKindOwnedSpec<Storage> {
    pub target: Storage,
}

#[derive(
    Copy,
    Clone,
    Debug,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
    JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct ModelStorageBindingSyncPolicy {
    #[serde(default)]
    pub pull: ModelStorageBindingSyncPolicyPull,
    #[serde(default)]
    pub push: ModelStorageBindingSyncPolicyPush,
}

impl ModelStorageBindingSyncPolicy {
    pub fn is_none(&self) -> bool {
        self.pull == ModelStorageBindingSyncPolicyPull::Never
            && self.push == ModelStorageBindingSyncPolicyPush::Never
    }
}

#[derive(
    Copy,
    Clone,
    Debug,
    Display,
    Default,
    EnumString,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
    JsonSchema,
)]
pub enum ModelStorageBindingSyncPolicyPull {
    #[default]
    Always,
    OnCreate,
    Never,
}

#[derive(
    Copy,
    Clone,
    Debug,
    Display,
    Default,
    EnumString,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
    JsonSchema,
)]
pub enum ModelStorageBindingSyncPolicyPush {
    #[default]
    Always,
    OnDelete,
    Never,
}

#[derive(
    Copy,
    Clone,
    Debug,
    Display,
    Default,
    EnumString,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
    JsonSchema,
)]
pub enum ModelStorageBindingDeletionPolicy {
    Delete,
    #[default]
    Retain,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ModelStorageBindingStatus {
    #[serde(default)]
    pub state: ModelStorageBindingState,
    #[serde(default)]
    pub deletion_policy: ModelStorageBindingDeletionPolicy,
    #[serde(default)]
    pub model: Option<ModelSpec>,
    #[serde(default)]
    pub model_name: Option<String>,
    #[serde(default)]
    pub resources: Option<ResourceRequirements>,
    #[serde(default)]
    pub storage_source: Option<ModelStorageSpec>,
    #[serde(default)]
    pub storage_source_binding_name: Option<String>,
    #[serde(default)]
    pub storage_source_name: Option<String>,
    #[serde(default)]
    pub storage_source_uid: Option<String>,
    #[serde(default)]
    pub storage_sync_policy: Option<ModelStorageBindingSyncPolicy>,
    #[serde(default)]
    pub storage_target: Option<ModelStorageSpec>,
    #[serde(default)]
    pub storage_target_name: Option<String>,
    #[serde(default)]
    pub storage_target_uid: Option<String>,
    pub last_updated: DateTime<Utc>,
}

#[derive(
    Copy,
    Clone,
    Debug,
    Display,
    Default,
    EnumString,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
    JsonSchema,
)]
pub enum ModelStorageBindingState {
    #[default]
    Pending,
    Ready,
    Deleting,
}