orign 0.2.3

A globally distributed container orchestrator
Documentation
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
// src/models.rs

use kube::CustomResource;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

pub trait Request: Serialize + for<'de> Deserialize<'de> {
    fn set_user_id(&mut self, user_id: String);
    fn set_output_topic(&mut self, output_topic: String);
    fn set_request_id(&mut self, request_id: String);
    fn set_organizations(&mut self, organizations: HashMap<String, HashMap<String, String>>);
    fn set_handle(&mut self, handle: String);
}

// Implement Request trait for ChatRequest
impl Request for ChatRequest {
    fn set_user_id(&mut self, user_id: String) {
        self.user_id = Some(user_id);
    }
    fn set_output_topic(&mut self, output_topic: String) {
        self.output_topic = Some(output_topic);
    }
    fn set_request_id(&mut self, request_id: String) {
        self.request_id = Some(request_id);
    }
    fn set_organizations(&mut self, organizations: HashMap<String, HashMap<String, String>>) {
        self.organizations = Some(organizations);
    }
    fn set_handle(&mut self, handle: String) {
        self.handle = Some(handle);
    }
}

// Implement Request trait for EmbeddingRequest
impl Request for EmbeddingRequest {
    fn set_user_id(&mut self, user_id: String) {
        self.user_id = Some(user_id);
    }
    fn set_output_topic(&mut self, output_topic: String) {
        self.output_topic = Some(output_topic);
    }
    fn set_request_id(&mut self, request_id: String) {
        self.request_id = Some(request_id);
    }
    fn set_organizations(&mut self, organizations: HashMap<String, HashMap<String, String>>) {
        self.organizations = Some(organizations);
    }
    fn set_handle(&mut self, handle: String) {
        self.handle = Some(handle);
    }
}

// Implement Request trait for OCRRequest
impl Request for OCRRequest {
    fn set_user_id(&mut self, user_id: String) {
        self.user_id = Some(user_id);
    }
    fn set_output_topic(&mut self, output_topic: String) {
        self.output_topic = Some(output_topic);
    }
    fn set_request_id(&mut self, request_id: String) {
        self.request_id = Some(request_id);
    }
    fn set_organizations(&mut self, organizations: HashMap<String, HashMap<String, String>>) {
        self.organizations = Some(organizations);
    }
    fn set_handle(&mut self, handle: String) {
        self.handle = Some(handle);
    }
}

// === Streaming Chat Request ===

#[derive(Deserialize, Serialize, Default)]
/// Image URL content for chat requests
pub struct ImageUrlContent {
    pub url: String,
}

#[derive(Deserialize, Serialize, Default)]
/// Content item for chat requests
pub struct ContentItem {
    #[serde(rename = "type")]
    pub content_type: String,
    pub text: Option<String>,
    pub image_url: Option<ImageUrlContent>,
}

#[derive(Deserialize, Serialize, Default)]
/// Batch item with content for chat requests
pub struct BatchItemWithContent {
    pub role: String,
    pub content: Vec<ContentItem>,
}

#[derive(Deserialize, Serialize)]
#[serde(untagged)]
/// Message content can be either a string or a ContentItem
pub enum MessageContent {
    Text(String),
    Item(Vec<ContentItem>),
}

#[derive(Deserialize, Serialize)]
/// Message item for chat requests
pub struct MessageItem {
    pub role: String,
    pub content: MessageContent,
}

#[derive(Deserialize, Serialize)]
/// Prompt for chat requests
pub struct Prompt {
    pub messages: Vec<MessageItem>,
}

#[derive(Deserialize, Serialize, Default)]
/// Sampling parameters for chat requests
pub struct SamplingParams {
    pub n: i32,
    pub best_of: Option<i32>,
    pub presence_penalty: f32,
    pub frequency_penalty: f32,
    pub repetition_penalty: f32,
    pub temperature: f32,
    pub top_p: f32,
    pub top_k: i32,
    pub min_p: f32,
    pub seed: Option<i32>,
    pub stop: Option<Vec<String>>,
    pub stop_token_ids: Option<Vec<i32>>,
    pub min_tokens: i32,
    pub logprobs: Option<i32>,
    pub prompt_logprobs: Option<i32>,
    pub detokenize: bool,
    pub skip_special_tokens: bool,
    pub spaces_between_special_tokens: bool,
    pub truncate_prompt_tokens: Option<i32>,
}

#[derive(Deserialize, Serialize, Default)]
pub struct Usage {
    pub prompt_tokens: usize,
    pub completion_tokens: usize,
    pub total_tokens: usize,
}

#[derive(Deserialize, Serialize, Default)]
/// Chat request
pub struct ChatRequest {
    #[serde(rename = "type")]
    #[serde(default = "default_chat_request_type")]
    pub request_type: String,
    pub request_id: Option<String>,
    pub model: Option<String>,
    pub framework: Option<String>,
    pub kind: Option<String>,
    pub namespace: Option<String>,
    pub prompt: Option<Prompt>,
    pub batch: Option<Vec<Prompt>>,
    pub adapter: Option<String>,
    pub max_tokens: Option<i32>,
    pub sampling_params: Option<SamplingParams>,
    #[serde(default)]
    pub stream: bool,
    pub user_id: Option<String>,
    pub organizations: Option<HashMap<String, HashMap<String, String>>>,
    pub handle: Option<String>,
    pub output_topic: Option<String>,
    pub output_partition: Option<i32>,
}

fn default_chat_request_type() -> String {
    "ChatRequest".to_string()
}

#[derive(Deserialize, Serialize, Default)]
/// Individual choice in the token response
pub struct Choice {
    pub index: i32,
    pub text: String,
    pub tokens: Option<Vec<String>>,
    pub token_ids: Option<Vec<i32>>,
    pub logprobs: Option<Vec<HashMap<String, serde_json::Value>>>,
    pub finish_reason: Option<String>,
}

#[derive(Deserialize, Serialize, Default)]
/// Chat response
pub struct ChatResponse {
    #[serde(rename = "type", default = "default_chat_response_type")]
    pub response_type: String,
    pub request_id: String,
    pub choices: Vec<Choice>,
    pub usage: Option<Usage>,
    pub trip_time: Option<f64>,
}

fn default_chat_response_type() -> String {
    "ChatResponse".to_string()
}

#[derive(Deserialize, Serialize, Default)]
/// Token response
pub struct TokenResponse {
    #[serde(rename = "type", default = "default_token_response_type")]
    pub response_type: String,
    pub request_id: String,
    pub tokens: Vec<String>,
    pub token_ids: Option<Vec<i32>>,
    pub usage: Option<Usage>,
    pub logprobs: Option<Vec<HashMap<String, serde_json::Value>>>,
}

fn default_token_response_type() -> String {
    "TokenResponse".to_string()
}

fn default_chat_event_type() -> String {
    "ChatEvent".to_string()
}

#[derive(Deserialize, Serialize, Default)]
pub struct ChatEvent {
    #[serde(rename = "type", default = "default_chat_event_type")]
    pub event_type: String,
    pub id: Option<String>,
    pub request: ChatRequest,
    pub response: ChatResponse,
    pub token_count: Option<i32>,
    pub trip_time: Option<f64>,
    pub approved: Option<bool>,
    pub metadata: Option<HashMap<String, String>>,
    pub owner_id: Option<String>,
    pub organization_id: Option<String>,
    pub handle: Option<String>,
    pub created: Option<i64>,
}

// === Model Instances ===

#[derive(Deserialize, Serialize)]
pub struct ModelReadyResponse {
    #[serde(rename = "type")]
    pub response_type: String,
    pub request_id: String,
    pub ready: bool,
    pub error: Option<String>,
}

impl Default for ModelReadyResponse {
    fn default() -> Self {
        Self {
            response_type: "ModelReadyResponse".to_string(),
            request_id: "".to_string(),
            ready: false,
            error: None,
        }
    }
}

#[derive(Serialize, Deserialize, Default)]
pub struct ReplicasInfo {
    pub desired: i32,
    pub ready: i32,
    pub updated: i32,
    pub available: i32,
    pub unavailable: i32,
}

// class SentenceTFConfig(BaseSettings):
//     model: str = "clip-ViT-B-32"
//     device: str = "cuda"
#[derive(Deserialize, Serialize)]
pub struct SentenceTFParams {
    #[serde(default = "default_sentence_tf_model")]
    pub model: String,

    #[serde(default = "default_cuda_device")]
    pub device: String,
}

impl Default for SentenceTFParams {
    fn default() -> Self {
        Self {
            model: default_sentence_tf_model(),
            device: default_cuda_device(),
        }
    }
}
// class vLLMConfig(BaseSettings):
//     model: str
//     trust_remote_code: bool = True
//     tensor_parallel_size: int = 1
//     dtype: str = "auto"
//     max_images_per_prompt: int = 1
//     device: str = "cuda"
#[derive(Deserialize, Serialize)]
pub struct VLLMParams {
    pub model: String,
    pub model_type: Option<String>,

    #[serde(default = "default_option_true")]
    pub trust_remote_code: Option<bool>,

    #[serde(default = "default_tensor_parallel_size")]
    pub tensor_parallel_size: Option<i32>,

    #[serde(default = "default_auto_dtype")]
    pub dtype: Option<String>,

    #[serde(default = "default_max_images")]
    pub max_images_per_prompt: Option<i32>,

    #[serde(default = "default_cuda_device_option")]
    pub device: Option<String>,

    #[serde(default = "default_max_model_len")]
    pub max_model_len: Option<i32>,

    #[serde(default = "default_max_num_seqs")]
    pub max_num_seqs: Option<i32>,

    #[serde(default = "default_gpu_memory_utilization")]
    pub gpu_memory_utilization: Option<f32>,

    #[serde(default = "default_enforce_eager")]
    pub enforce_eager: Option<bool>,

    #[serde(default = "default_enable_adapter")]
    pub enable_adapter: Option<bool>,
}

// class DoctrConfig(BaseSettings):
//     det_arch: str = "fast_base"
//     reco_arch: str = "crnn_vgg16_bn"
//     pretrained: bool = True
#[derive(Deserialize, Serialize)]
pub struct DoctrParams {
    #[serde(default = "default_det_arch")]
    pub det_arch: Option<String>,

    #[serde(default = "default_reco_arch")]
    pub reco_arch: Option<String>,

    #[serde(default = "default_option_true")]
    pub pretrained: Option<bool>,
}

// class EasyOCRConfig(BaseSettings):
//     device: str = "cuda"
//     gpu: bool = True
//     lang_list: list[str] = ["en"]
//     quantize: bool = False
#[derive(Deserialize, Serialize)]
pub struct EasyOCRParams {
    #[serde(default = "default_cuda_device_option")]
    pub device: Option<String>,

    #[serde(default = "default_option_true")]
    pub gpu: Option<bool>,

    #[serde(default = "default_lang_list")]
    pub lang_list: Option<Vec<String>>,

    #[serde(default = "default_option_false")]
    pub quantize: Option<bool>,
}

// class LiteLLMConfig(BaseSettings):
//     api_keys: dict[str, str] = {}
//     lang_list: list[str] = ["en"]
//     quantize: bool = False
#[derive(Deserialize, Serialize)]
pub struct LiteLLMParams {
    #[serde()]
    pub api_keys: HashMap<String, String>,
}

#[derive(Deserialize, Serialize, Default)]
pub struct ModelDeployment {
    pub id: String,
    pub namespace: String,
    pub framework: String,
    pub kind: String,
    pub replicas: Option<ReplicasInfo>,
    pub status: String,
    pub image: String,
    pub vram_request: Option<String>,
    pub accelerators: Option<Vec<String>>,
    pub gpu_type: Option<String>,
    pub cpu_request: Option<String>,
    pub vllm_params: Option<VLLMParams>,
    pub sentence_tf_params: Option<SentenceTFParams>,
    pub doctr_params: Option<DoctrParams>,
    pub easyocr_params: Option<EasyOCRParams>,
    pub litellm_params: Option<LiteLLMParams>,
    pub platform: Option<String>,
}

#[derive(Deserialize, Serialize, Default)]
pub struct ModelDeploymentRequest {
    pub namespace: Option<String>,
    pub framework: String,
    pub vram_request: Option<String>,
    pub accelerators: Option<Vec<String>>,
    pub memory_request: Option<String>,
    pub cpu_request: Option<String>,
    pub vllm_params: Option<VLLMParams>,
    pub sentence_tf_params: Option<SentenceTFParams>,
    pub doctr_params: Option<DoctrParams>,
    pub easyocr_params: Option<EasyOCRParams>,
    pub litellm_params: Option<LiteLLMParams>,
    pub max_pixels: Option<i32>,
    pub platform: Option<String>,
}

// === OCR Models ===

#[derive(Deserialize, Serialize, Default)]
pub struct OCRRequest {
    #[serde(rename = "type")]
    #[serde(default = "default_ocr_request_type")]
    pub request_type: String,

    pub request_id: Option<String>,
    pub model: Option<String>,
    pub framework: Option<String>,
    pub image: String,
    pub languages: Vec<String>,

    #[serde(default = "default_true")]
    pub gpu: bool,

    #[serde(default = "default_true")]
    pub detail: bool,

    #[serde(default = "default_false")]
    pub paragraph: bool,

    #[serde(default = "default_min_confidence")]
    pub min_confidence: Option<f32>,

    pub user_id: Option<String>,
    pub organizations: Option<HashMap<String, HashMap<String, String>>>,
    pub handle: Option<String>,
    pub output_topic: Option<String>,
    pub output_partition: Option<i32>,
}

fn default_ocr_request_type() -> String {
    "OCRRequest".to_string()
}

fn default_min_confidence() -> Option<f32> {
    Some(0.0)
}

#[derive(Deserialize, Serialize, Default)]
pub struct BoundingBox {
    pub points: Vec<Vec<i32>>,
    pub text: String,
    pub confidence: f32,
}

#[derive(Deserialize, Serialize)]
#[serde(untagged)]
pub enum OCRResult {
    Detailed(Vec<BoundingBox>),
    Simple(Vec<String>),
}

impl Default for OCRResult {
    fn default() -> Self {
        OCRResult::Detailed(Vec::new())
    }
}

#[derive(Deserialize, Serialize, Default)]
pub struct OCRResponse {
    #[serde(rename = "type")]
    #[serde(default = "default_ocr_response_type")]
    pub response_type: String,
    pub request_id: String,
    pub results: OCRResult,
    pub processing_time: Option<f64>,
    pub usage: Option<Usage>,
}

fn default_ocr_response_type() -> String {
    "OCRResponse".to_string()
}

// === Embedding Models ===

#[derive(Deserialize, Serialize, Default)]
pub struct EmbeddingRequest {
    #[serde(rename = "type")]
    #[serde(default = "default_embedding_request_type")]
    pub request_type: String,
    pub request_id: Option<String>,
    pub model: Option<String>,
    pub framework: Option<String>,
    pub text: Option<String>,
    pub image: Option<String>,
    pub user_id: Option<String>,
    pub organizations: Option<HashMap<String, HashMap<String, String>>>,
    pub handle: Option<String>,
    pub output_topic: Option<String>,
    pub output_partition: Option<i32>,
}

fn default_embedding_request_type() -> String {
    "EmbeddingRequest".to_string()
}

#[derive(Deserialize, Serialize, Default)]
pub struct Embedding {
    pub object: String,
    pub index: i32,
    pub embedding: Vec<f32>,
}

#[derive(Deserialize, Serialize, Default)]
pub struct EmbeddingResponse {
    #[serde(rename = "type")]
    #[serde(default = "default_embedding_response_type")]
    pub response_type: String,
    pub request_id: String,
    pub object: String,
    pub data: Vec<Embedding>,
    pub model: String,
    pub usage: Option<Usage>,
}

fn default_embedding_response_type() -> String {
    "EmbeddingResponse".to_string()
}

// === Auth ===

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct V1UserProfile {
    pub email: String,
    pub display_name: Option<String>,
    pub handle: Option<String>,
    pub picture: Option<String>,
    pub organization: Option<String>,
    pub role: Option<String>,
    pub external_id: Option<String>,
    pub actor: Option<String>,
    pub organizations: Option<HashMap<String, HashMap<String, String>>>,
    pub created: Option<i64>,
    pub updated: Option<i64>,
    pub token: Option<String>,
}

// === Training ===
#[derive(Deserialize, Serialize, Clone, Default, Debug)]
pub struct LlamaFactoryParams {
    pub model: String,
}

/// Enumeration of valid torch dtypes recognized by TRL.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "lowercase")]
pub enum TorchDType {
    Auto,
    Bfloat16,
    Float16,
    Float32,
}

/// Utility enum for handling either a single string or a list of strings (per the "Union[str, list[str]]" spec).
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum OneOrManyStrings {
    Single(String),
    Multiple(Vec<String>),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TRLParams {
    /// Model checkpoint for weights initialization.
    #[serde(default)]
    pub model_name_or_path: Option<String>,

    /// Specific model version to use.
    #[serde(default = "default_model_revision")]
    pub model_revision: String,

    /// Override the default torch.dtype and load the model under this dtype.
    /// Valid values: "auto", "bfloat16", "float16", "float32".
    #[serde(default)]
    pub torch_dtype: Option<TorchDType>,

    /// Whether to allow for custom models defined on the Hub in their own modeling files.
    #[serde(default)]
    pub trust_remote_code: bool,

    /// Which attention implementation to use (e.g. "flash_attention_2").
    #[serde(default)]
    pub attn_implementation: Option<String>,

    /// Whether to use PEFT for training.
    #[serde(default)]
    pub use_peft: bool,

    /// LoRA R value.
    #[serde(default = "default_lora_r")]
    pub lora_r: i32,

    /// LoRA alpha.
    #[serde(default = "default_lora_alpha")]
    pub lora_alpha: i32,

    /// LoRA dropout.
    #[serde(default = "default_lora_dropout")]
    pub lora_dropout: f32,

    /// LoRA target modules (Union[str, list[str]]).
    #[serde(default)]
    pub lora_target_modules: Option<OneOrManyStrings>,

    /// Model layers to unfreeze & train.
    #[serde(default)]
    pub lora_modules_to_save: Option<Vec<String>>,

    /// Task type to pass for LoRA (use "SEQ_CLS" for reward modeling).
    #[serde(default = "default_lora_task_type")]
    pub lora_task_type: String,

    /// Whether to use Rank-Stabilized LoRA.
    #[serde(default)]
    pub use_rslora: bool,

    /// Enable Weight-Decomposed Low-Rank Adaptation (DoRA).
    #[serde(default)]
    pub use_dora: bool,

    /// Whether to use 8-bit precision for the base model. Works only with LoRA.
    #[serde(default)]
    pub load_in_8bit: bool,

    /// Whether to use 4-bit precision for the base model. Works only with LoRA.
    #[serde(default)]
    pub load_in_4bit: bool,

    /// 4-bit quantization type ("fp4" or "nf4").
    #[serde(default = "default_bnb_4bit_quant_type")]
    pub bnb_4bit_quant_type: String,

    /// Whether to use nested quantization for 4-bit.
    #[serde(default)]
    pub use_bnb_nested_quant: bool,
}

impl Default for TRLParams {
    fn default() -> Self {
        Self {
            model_name_or_path: None,
            model_revision: default_model_revision(),
            torch_dtype: None,
            trust_remote_code: false,
            attn_implementation: None,
            use_peft: false,
            lora_r: default_lora_r(),
            lora_alpha: default_lora_alpha(),
            lora_dropout: default_lora_dropout(),
            lora_target_modules: None,
            lora_modules_to_save: None,
            lora_task_type: default_lora_task_type(),
            use_rslora: false,
            use_dora: false,
            load_in_8bit: false,
            load_in_4bit: false,
            bnb_4bit_quant_type: default_bnb_4bit_quant_type(),
            use_bnb_nested_quant: false,
        }
    }
}

fn default_model_revision() -> String {
    "main".to_string()
}

fn default_lora_r() -> i32 {
    16
}

fn default_lora_alpha() -> i32 {
    32
}

fn default_lora_dropout() -> f32 {
    0.05
}

fn default_lora_task_type() -> String {
    "CAUSAL_LM".to_string()
}

fn default_bnb_4bit_quant_type() -> String {
    "nf4".to_string()
}

#[derive(Deserialize, Serialize, Clone, Default, Debug)]
pub struct MSSwiftParams {
    #[serde(default = "default_ms_swift_model")]
    pub model: String,

    #[serde(default = "default_ms_swift_model_type")]
    pub model_type: String,

    #[serde(default = "default_ms_swift_train_type")]
    pub train_type: String,

    #[serde(default = "default_ms_swift_deepspeed")]
    pub deepspeed: String,

    #[serde(default = "default_ms_swift_dtype")]
    pub torch_dtype: String,

    #[serde(default = "default_ms_swift_max_length")]
    pub max_length: i32,

    #[serde(default)]
    pub dataset: String,

    #[serde(default = "default_ms_swift_val_split_ratio")]
    pub val_split_ratio: f32,

    #[serde(default = "default_ms_swift_num_train_epochs")]
    pub num_train_epochs: i32,

    #[serde(default = "default_ms_swift_eval_strategy")]
    pub eval_strategy: String,

    #[serde(default = "default_ms_swift_save_strategy")]
    pub save_strategy: String,

    #[serde(default)]
    pub save_steps: Option<i32>,

    #[serde(default = "default_ms_swift_save_total_limit")]
    pub save_total_limit: i32,

    #[serde(default)]
    pub lora_rank: Option<i32>,

    #[serde(default)]
    pub lora_alpha: Option<i32>,

    #[serde(default = "default_ms_swift_size_factor")]
    pub size_factor: i32,

    #[serde(default = "default_ms_swift_max_pixels")]
    pub max_pixels: i32,

    #[serde(default)]
    pub resume_from_checkpoint: Option<String>,

    #[serde(default)]
    pub freeze_vit: Option<bool>,

    #[serde(default)]
    pub rlhf_type: Option<String>,

    #[serde(default = "default_ms_swift_gradient_accumulation_steps_total")]
    pub gradient_accumulation_steps_total: i32,

    #[serde(default)]
    pub learning_rate: Option<f32>,
}

// Add these helper functions at the bottom of the file:
fn default_ms_swift_model() -> String {
    "Qwen/Qwen2-VL-7B-Instruct".to_string()
}

fn default_ms_swift_model_type() -> String {
    "qwen2-vl-7b-instruct".to_string()
}

fn default_ms_swift_train_type() -> String {
    "lora".to_string()
}

fn default_ms_swift_deepspeed() -> String {
    "zero3".to_string()
}

fn default_ms_swift_dtype() -> String {
    "bfloat16".to_string()
}

fn default_ms_swift_max_length() -> i32 {
    8192
}

fn default_ms_swift_num_train_epochs() -> i32 {
    3
}

fn default_ms_swift_eval_strategy() -> String {
    "epoch".to_string()
}

fn default_ms_swift_save_strategy() -> String {
    "epoch".to_string()
}

fn default_ms_swift_save_total_limit() -> i32 {
    3
}

fn default_ms_swift_size_factor() -> i32 {
    28
}

fn default_ms_swift_max_pixels() -> i32 {
    1025000
}

// fn default_ms_swift_max_pixels() -> i32 {
//     802816
// }

fn default_ms_swift_val_split_ratio() -> f32 {
    0.90
}

fn default_ms_swift_gradient_accumulation_steps_total() -> i32 {
    16
}

#[derive(Deserialize, Serialize, Clone, Default, Debug)]
pub struct TrainingRequest {
    pub name: Option<String>,
    pub namespace: Option<String>,
    pub framework: String,
    pub vram_request: Option<String>,
    pub accelerators: Option<Vec<String>>,
    pub cpu_request: Option<String>,
    pub trust_remote_code: Option<bool>,
    pub adapter: Option<String>,
    pub buffer: Option<String>,
    pub queue: Option<String>,
    pub ms_swift_params: Option<MSSwiftParams>,
    pub llama_factory_params: Option<LlamaFactoryParams>,
    pub trl_params: Option<TRLParams>,
    pub resume: Option<bool>,
    pub labels: Option<HashMap<String, String>>,
    pub platform: Option<String>,
}

#[derive(Deserialize, Serialize, Clone, Default, Debug)]
pub struct Checkpoint {
    pub step: i32,
    pub trainer_state: Option<String>,
    pub args: Option<String>,
    pub adapter_config: Option<String>,
}

#[derive(Deserialize, Serialize, Clone, Default, Debug)]
pub struct TrainingJob {
    pub id: String,
    pub name: String,
    pub namespace: String,
    pub framework: String,
    pub vram_request: Option<String>,
    pub accelerators: Option<Vec<String>>,
    pub cpu_request: Option<String>,
    pub trust_remote_code: Option<bool>,
    pub adapter: Option<String>,
    pub buffer: Option<String>,
    pub ms_swift_params: Option<MSSwiftParams>,
    pub llama_factory_params: Option<LlamaFactoryParams>,
    pub trl_params: Option<TRLParams>,
    pub resume: Option<bool>,
    pub status: String,
    pub checkpoints: Option<Vec<Checkpoint>>,
    pub queue: Option<String>,
    pub platform: Option<String>,
    pub labels: Option<HashMap<String, String>>,
    pub created: i64,
    pub updated: i64,
}

#[derive(Deserialize, Serialize, Clone, Default, Debug)]
pub struct TRLBufferParams {
    #[serde(default)]
    pub model_name_or_path: Option<String>,

    #[serde(default = "default_model_revision")]
    pub model_revision: String,

    #[serde(default)]
    pub torch_dtype: Option<TorchDType>,

    #[serde(default)]
    pub trust_remote_code: bool,

    #[serde(default)]
    pub attn_implementation: Option<String>,

    #[serde(default)]
    pub use_peft: bool,

    #[serde(default = "default_lora_r")]
    pub lora_r: i32,

    #[serde(default = "default_lora_alpha")]
    pub lora_alpha: i32,

    #[serde(default = "default_lora_dropout")]
    pub lora_dropout: f32,

    #[serde(default)]
    pub lora_target_modules: Option<OneOrManyStrings>,

    #[serde(default)]
    pub lora_modules_to_save: Option<Vec<String>>,

    #[serde(default = "default_lora_task_type")]
    pub lora_task_type: String,

    #[serde(default)]
    pub use_rslora: bool,

    #[serde(default)]
    pub use_dora: bool,

    #[serde(default)]
    pub load_in_8bit: bool,

    #[serde(default)]
    pub load_in_4bit: bool,

    #[serde(default = "default_bnb_4bit_quant_type")]
    pub bnb_4bit_quant_type: String,

    #[serde(default)]
    pub use_bnb_nested_quant: bool,
}

#[derive(Deserialize, Serialize, Clone, Default, Debug)]
pub struct MSSwiftBufferParams {
    #[serde(default = "default_ms_swift_model")]
    pub model: String,

    #[serde(default = "default_ms_swift_model_type")]
    pub model_type: String,

    #[serde(default = "default_ms_swift_train_type")]
    pub train_type: String,

    #[serde(default = "default_ms_swift_deepspeed")]
    pub deepspeed: String,

    #[serde(default = "default_ms_swift_dtype")]
    pub torch_dtype: String,

    #[serde(default = "default_ms_swift_max_length")]
    pub max_length: i32,

    #[serde(default = "default_ms_swift_val_split_ratio")]
    pub val_split_ratio: f32,

    #[serde(default = "default_ms_swift_num_train_epochs")]
    pub num_train_epochs: i32,

    #[serde(default = "default_ms_swift_eval_strategy")]
    pub eval_strategy: String,

    #[serde(default = "default_ms_swift_save_strategy")]
    pub save_strategy: String,

    #[serde(default = "default_ms_swift_save_total_limit")]
    pub save_total_limit: i32,

    #[serde(default)]
    pub save_steps: Option<i32>,

    #[serde(default)]
    pub lora_rank: Option<i32>,

    #[serde(default)]
    pub lora_alpha: Option<i32>,

    #[serde(default = "default_ms_swift_size_factor")]
    pub size_factor: i32,

    #[serde(default = "default_ms_swift_max_pixels")]
    pub max_pixels: i32,

    #[serde(default)]
    pub resume_from_checkpoint: Option<String>,

    #[serde(default)]
    pub freeze_vit: Option<bool>,

    #[serde(default)]
    pub rlhf_type: Option<String>,

    #[serde(default = "default_ms_swift_gradient_accumulation_steps_total")]
    pub gradient_accumulation_steps_total: i32,

    #[serde(default)]
    pub learning_rate: Option<f32>,
}

#[derive(Deserialize, Serialize, Clone, Default)]
pub struct TrainingJobsResponse {
    pub jobs: Vec<TrainingJob>,
}

// #[derive(Deserialize, Serialize, Clone, Default, Debug)]
// pub struct ReplayBufferRequest {
//     pub name: String,
//     pub namespace: Option<String>,
//     pub framework: String,
//     pub vram_request: Option<String>,
//     pub accelerators: Option<Vec<String>>,
//     pub cpu_request: Option<String>,
//     pub trust_remote_code: Option<bool>,
//     pub adapter: Option<String>,
//     pub train_every: Option<i32>,

//     #[serde(default = "default_sample_n")]
//     pub sample_n: i32,

//     #[serde(default = "default_sample_strategy")]
//     pub sample_strategy: String,

//     pub queue: Option<String>,
//     pub ms_swift_params: Option<MSSwiftBufferParams>,
//     pub llama_factory_params: Option<LlamaFactoryParams>,
//     pub trl_params: Option<TRLBufferParams>,
//     pub platform: Option<String>,
//     pub labels: Option<HashMap<String, String>>,
// }

// #[derive(Deserialize, Serialize, Clone, Default, Debug)]
// pub struct UpdateReplayBufferRequest {
//     pub name: Option<String>,
//     pub namespace: Option<String>,
//     pub framework: Option<String>,
//     pub accelerators: Option<Vec<String>>,
//     pub vram_request: Option<String>,
//     pub cpu_request: Option<String>,
//     pub trust_remote_code: Option<bool>,
//     pub adapter: Option<String>,
//     pub train_every: Option<i32>,
//     pub sample_n: Option<i32>,
//     pub sample_strategy: Option<String>,
//     pub queue: Option<String>,
//     pub ms_swift_params: Option<MSSwiftBufferParams>,
//     pub llama_factory_params: Option<LlamaFactoryParams>,
//     pub trl_params: Option<TRLBufferParams>,
//     pub platform: Option<String>,
//     pub labels: Option<HashMap<String, String>>,
// }

// #[derive(Deserialize, Serialize, Clone, Default)]
// pub struct ReplayBuffer {
//     pub id: String,
//     pub name: String,
//     pub namespace: String,
//     pub framework: String,
//     pub vram_request: Option<String>,
//     pub accelerators: Option<Vec<String>>,
//     pub cpu_request: Option<String>,
//     pub trust_remote_code: Option<bool>,
//     pub adapter: Option<String>,
//     pub train_every: Option<i32>,
//     pub sample_n: i32,
//     pub sample_strategy: String,
//     pub ms_swift_params: Option<MSSwiftBufferParams>,
//     pub llama_factory_params: Option<LlamaFactoryParams>,
//     pub trl_params: Option<TRLBufferParams>,
//     pub labels: Option<HashMap<String, String>>,
//     pub queue: Option<String>,
//     pub platform: Option<String>,
//     pub num_records: Option<i32>,
//     pub train_idx: Option<i32>,
// }

// fn default_sample_n() -> i32 {
//     100
// }

// fn default_sample_strategy() -> String {
//     "Random".to_string()
// }

// #[derive(Deserialize, Serialize, Clone, Default)]
// pub struct ReplayBuffersResponse {
//     pub buffers: Vec<ReplayBuffer>,
// }

// #[derive(Deserialize, Serialize, Clone, Default)]
// pub struct ReplayBufferData {
//     pub examples: Vec<serde_json::Value>,
//     pub train: Option<bool>,
// }

#[derive(Deserialize, Serialize, Clone, Default)]
pub struct Adapters {
    pub adapters: Vec<String>,
}

#[derive(Deserialize, Serialize, Clone, Default)]
pub struct Models {
    pub models: Vec<String>,
}

#[derive(Deserialize, Serialize, Clone, Default)]
pub struct Datasets {
    pub datasets: Vec<String>,
}

#[derive(Deserialize, Serialize, Clone, Default)]
pub struct ModelFile {
    pub latest_checkpoint: Option<String>,
}

// === Common ===

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct MessageEnvelope<T> {
    pub user_profile: V1UserProfile,
    pub request_id: String,
    pub output_topic: String,
    pub payload: T,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct ErrorResponse {
    #[serde(rename = "type", default = "default_error_response_type")]
    pub response_type: String,
    pub request_id: String,
    pub error: String,
    pub traceback: Option<String>,
}

fn default_error_response_type() -> String {
    "ErrorResponse".to_string()
}

// Add these helper functions at the bottom of the file:
fn default_sentence_tf_model() -> String {
    "clip-ViT-B-32".to_string()
}

fn default_cuda_device() -> String {
    "cuda".to_string()
}

fn default_cuda_device_option() -> Option<String> {
    // New name
    Some("cuda".to_string())
}

fn default_tensor_parallel_size() -> Option<i32> {
    Some(1)
}

fn default_auto_dtype() -> Option<String> {
    Some("auto".to_string())
}

fn default_max_images() -> Option<i32> {
    Some(1)
}

fn default_det_arch() -> Option<String> {
    Some("fast_base".to_string())
}

fn default_reco_arch() -> Option<String> {
    Some("crnn_vgg16_bn".to_string())
}

fn default_option_true() -> Option<bool> {
    Some(true)
}

fn default_true() -> bool {
    true
}

fn default_option_false() -> Option<bool> {
    Some(false)
}

fn default_false() -> bool {
    false
}

fn default_lang_list() -> Option<Vec<String>> {
    Some(vec!["en".to_string()])
}

fn default_max_model_len() -> Option<i32> {
    Some(8192)
}

fn default_max_num_seqs() -> Option<i32> {
    Some(5)
}

fn default_gpu_memory_utilization() -> Option<f32> {
    Some(0.8)
}

fn default_enforce_eager() -> Option<bool> {
    Some(true)
}

fn default_enable_adapter() -> Option<bool> {
    Some(false)
}

// === LocalQueue ===

use schemars::JsonSchema;

#[derive(CustomResource, Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[kube(
    group = "kueue.x-k8s.io",
    version = "v1beta1",
    kind = "LocalQueue",
    namespaced
)]
#[serde(rename_all = "camelCase")]
pub struct LocalQueueSpec {
    pub cluster_queue: String,
    pub stop_policy: Option<String>,
    pub resource_groups: Option<Vec<ResourceGroup>>,
}

// Example auxiliary structs
#[derive(Serialize, Deserialize, Debug, Default, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ResourceGroup {
    pub covered_resources: Option<Vec<String>>,
    pub flavors: Option<Vec<Flavor>>,
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct Flavor {
    pub name: String,
    pub resources: Option<Vec<FlavorResource>>,
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct FlavorResource {
    pub name: String,
    pub nominal_quota: String,
}

//
// === ClusterQueue ===
//

pub type ResourceName = String;

#[derive(CustomResource, Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[kube(
    group = "kueue.x-k8s.io",
    version = "v1beta1",
    kind = "ClusterQueue",
    namespaced = false
)]
#[serde(rename_all = "camelCase")]
pub struct ClusterQueueSpec {
    /// resourceGroups describes groups of resources.
    pub resource_groups: Vec<ClusterQueueResourceGroup>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub namespace_selector: Option<CustomLabelSelector>,

    /// cohort is now optional (skipped if None).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cohort: Option<String>,

    /// queueingStrategy now optional.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub queueing_strategy: Option<String>,

    /// flavorFungibility now optional.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flavor_fungibility: Option<FlavorFungibility>,

    /// preemption now optional.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub preemption: Option<ClusterQueuePreemption>,

    /// admissionChecks lists the AdmissionChecks required by this ClusterQueue (cannot be used along with admissionChecksStrategy).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub admission_checks: Option<Vec<String>>,

    /// admissionChecksStrategy defines a list of strategies to determine which ResourceFlavors require AdmissionChecks.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub admission_checks_strategy: Option<AdmissionChecksStrategy>,

    /// stopPolicy now optional.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_policy: Option<String>,

    /// fairSharing now optional.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fair_sharing: Option<FairSharing>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct LabelSelectorRequirement {
    pub key: String,
    pub operator: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub values: Option<Vec<String>>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct CustomLabelSelector {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub match_labels: Option<std::collections::BTreeMap<String, String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub match_expressions: Option<Vec<LabelSelectorRequirement>>,
}

/// ClusterQueueStatus defines the observed state of ClusterQueue.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ClusterQueueStatus {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flavors_reservation: Option<Vec<FlavorUsage>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub flavors_usage: Option<Vec<FlavorUsage>>,

    pub pending_workloads: i32,
    pub reserving_workloads: i32,
    pub admitted_workloads: i32,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub pending_workloads_status: Option<ClusterQueuePendingWorkloadsStatus>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub fair_sharing: Option<FairSharingStatus>,
}

/// A single resource group within a ClusterQueue, referencing flavors and their quotas.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ClusterQueueResourceGroup {
    pub covered_resources: Vec<ResourceName>,
    pub flavors: Vec<FlavorQuotas>,
}

/// FlavorQuotas associates a flavor with resource quota definitions.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct FlavorQuotas {
    pub name: String,
    pub resources: Vec<ResourceQuota>,
}

/// ResourceQuota defines nominal, borrowing, and lending quotas for a particular resource.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ResourceQuota {
    pub name: ResourceName,
    pub nominal_quota: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub borrowing_limit: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub lending_limit: Option<String>,
}

/// FlavorUsage tracks usage of a particular flavor and its resources.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct FlavorUsage {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resources: Option<Vec<ResourceUsage>>,
}

/// ResourceUsage captures the total usage and how much was borrowed.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ResourceUsage {
    pub name: ResourceName,
    pub total: String,
    pub borrowed: String,
}

/// ClusterQueuePendingWorkloadsStatus is a listing of pending workloads in the cluster queue.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ClusterQueuePendingWorkloadsStatus {
    #[serde(rename = "clusterQueuePendingWorkload")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cluster_queue_pending_workload: Option<Vec<ClusterQueuePendingWorkload>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_change_time: Option<String>,
}

/// ClusterQueuePendingWorkload identifies a single pending workload by name/namespace.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ClusterQueuePendingWorkload {
    pub name: String,
    pub namespace: String,
}

/// Makes the entire block optional so it won't appear if None.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct FairSharing {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub weight: Option<String>,
}

/// FairSharingStatus contains the current fair sharing usage details.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct FairSharingStatus {
    pub weighted_share: i64,
}

/// Make the entire block optional so it won't appear if None.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct FlavorFungibility {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub when_can_borrow: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub when_can_preempt: Option<String>,
}

/// Make this optional as well if desired.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ClusterQueuePreemption {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reclaim_within_cohort: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub borrow_within_cohort: Option<BorrowWithinCohort>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub within_cluster_queue: Option<String>,
}

/// BorrowWithinCohort defines whether and how to preempt other CQs to reclaim resources while borrowing.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct BorrowWithinCohort {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub policy: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_priority_threshold: Option<i32>,
}

/// AdmissionCheckStrategyRule defines rules for exactly one AdmissionCheck
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct AdmissionCheckStrategyRule {
    pub name: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub on_flavors: Option<Vec<String>>,
}

/// AdmissionChecksStrategy is a list of AdmissionCheck rules.
#[derive(Deserialize, Serialize, Debug, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct AdmissionChecksStrategy {
    pub admission_checks: Vec<AdmissionCheckStrategyRule>,
}