forked from kube-rs/kube
/
crd_schema_test.rs
340 lines (317 loc) · 13.7 KB
/
crd_schema_test.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
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
#![recursion_limit = "256"]
use assert_json_diff::assert_json_eq;
use chrono::{DateTime, NaiveDateTime, Utc};
use kube_derive::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
// See `crd_derive_schema` example for how the schema generated from this struct affects defaulting and validation.
#[derive(CustomResource, Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
#[kube(
group = "clux.dev",
version = "v1",
kind = "Foo",
category = "clux",
namespaced,
derive = "PartialEq",
shortname = "fo",
shortname = "f"
)]
#[serde(rename_all = "camelCase")]
struct FooSpec {
non_nullable: String,
#[serde(default = "default_value")]
non_nullable_with_default: String,
#[serde(skip_serializing_if = "Option::is_none")]
nullable_skipped: Option<String>,
nullable: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default = "default_nullable")]
nullable_skipped_with_default: Option<String>,
#[serde(default = "default_nullable")]
nullable_with_default: Option<String>,
// Using feature `chrono`
timestamp: DateTime<Utc>,
/// This is a complex enum with a description
complex_enum: ComplexEnum,
/// This is a untagged enum with a description
untagged_enum_person: UntaggedEnumPerson,
}
fn default_value() -> String {
"default_value".into()
}
fn default_nullable() -> Option<String> {
Some("default_nullable".into())
}
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
#[kube(group = "clux.dev", version = "v1", kind = "Flattening")]
pub struct FlatteningSpec {
foo: String,
#[serde(flatten)]
arbitrary: HashMap<String, serde_json::Value>,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[allow(clippy::enum_variant_names)]
enum ComplexEnum {
/// First variant with an int
VariantOne { int: i32 },
/// Second variant with an String
VariantTwo { str: String },
/// Third variant which doesn't has an attribute
VariantThree {},
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
enum UntaggedEnumPerson {
GenderAndAge(GenderAndAge),
GenderAndDateOfBirth(GenderAndDateOfBirth),
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
struct GenderAndAge {
/// Gender of the person
gender: Gender,
/// Age of the person in years
age: i32,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
#[serde(rename_all = "camelCase")]
struct GenderAndDateOfBirth {
/// Gender of the person
gender: Gender,
/// Date of birth of the person as ISO 8601 date
date_of_birth: String,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, JsonSchema)]
#[serde(rename_all = "PascalCase")]
enum Gender {
Female,
Male,
/// This variant has a comment!
Other,
}
#[test]
fn test_crd_name() {
use kube::core::CustomResourceExt;
assert_eq!("foos.clux.dev", Foo::crd_name());
}
#[test]
fn test_shortnames() {
use kube::core::CustomResourceExt;
assert_eq!(&["fo", "f"], Foo::shortnames());
}
#[test]
fn test_serialized_matches_expected() {
assert_json_eq!(
serde_json::to_value(Foo::new("bar", FooSpec {
non_nullable: "asdf".to_string(),
non_nullable_with_default: "asdf".to_string(),
nullable_skipped: None,
nullable: None,
nullable_skipped_with_default: None,
nullable_with_default: None,
timestamp: DateTime::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc),
complex_enum: ComplexEnum::VariantOne { int: 23 },
untagged_enum_person: UntaggedEnumPerson::GenderAndAge(GenderAndAge {
age: 42,
gender: Gender::Male,
})
}))
.unwrap(),
serde_json::json!({
"apiVersion": "clux.dev/v1",
"kind": "Foo",
"metadata": {
"name": "bar",
},
"spec": {
"nonNullable": "asdf",
"nonNullableWithDefault": "asdf",
"nullable": null,
"nullableWithDefault": null,
"timestamp": "1970-01-01T00:00:00Z",
"complexEnum": {
"variantOne": {
"int": 23
}
},
"untaggedEnumPerson": {
"age": 42,
"gender": "Male"
}
}
})
)
}
#[test]
fn test_crd_schema_matches_expected() {
use kube::core::CustomResourceExt;
assert_json_eq!(
Foo::crd(),
serde_json::json!({
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "CustomResourceDefinition",
"metadata": {
"name": "foos.clux.dev"
},
"spec": {
"group": "clux.dev",
"names": {
"categories": ["clux"],
"kind": "Foo",
"plural": "foos",
"shortNames": ["fo", "f"],
"singular": "foo"
},
"scope": "Namespaced",
"versions": [
{
"name": "v1",
"served": true,
"storage": true,
"additionalPrinterColumns": [],
"schema": {
"openAPIV3Schema": {
"description": "Auto-generated derived type for FooSpec via `CustomResource`",
"properties": {
"spec": {
"properties": {
"nonNullable": {
"type": "string"
},
"nonNullableWithDefault": {
"default": "default_value",
"type": "string"
},
"nullableSkipped": {
"nullable": true,
"type": "string"
},
"nullable": {
"nullable": true,
"type": "string"
},
"nullableSkippedWithDefault": {
"default": "default_nullable",
"nullable": true,
"type": "string"
},
"nullableWithDefault": {
"default": "default_nullable",
"nullable": true,
"type": "string"
},
"timestamp": {
"type": "string",
"format": "date-time"
},
"complexEnum": {
"type": "object",
"properties": {
"variantOne": {
"type": "object",
"properties": {
"int": {
"type": "integer",
"format": "int32"
}
},
"required": ["int"],
"description": "First variant with an int"
},
"variantTwo": {
"type": "object",
"properties": {
"str": {
"type": "string"
}
},
"required": ["str"],
"description": "Second variant with an String"
},
"variantThree": {
"type": "object",
"description": "Third variant which doesn't has an attribute"
}
},
"oneOf": [
{
"required": ["variantOne"]
},
{
"required": ["variantTwo"]
},
{
"required": ["variantThree"]
}
],
"description": "This is a complex enum with a description"
},
"untaggedEnumPerson": {
"type": "object",
"properties": {
"age": {
"type": "integer",
"format": "int32",
"description": "Age of the person in years"
},
"dateOfBirth": {
"type": "string",
"description": "Date of birth of the person as ISO 8601 date"
},
"gender": {
"type": "string",
"enum": ["Female", "Male", "Other"],
"description": "Gender of the person"
}
},
"anyOf": [
{
"required": ["age", "gender"]
},
{
"required": ["dateOfBirth", "gender"]
}
],
"description": "This is a untagged enum with a description"
}
},
"required": [
"complexEnum",
"nonNullable",
"timestamp",
"untaggedEnumPerson"
],
"type": "object"
}
},
"required": [
"spec"
],
"title": "Foo",
"type": "object"
}
},
"subresources": {},
}
]
}
})
);
}
#[test]
fn flattening() {
use kube::core::CustomResourceExt;
let spec = &Flattening::crd().spec.versions[0]
.schema
.clone()
.unwrap()
.open_api_v3_schema
.unwrap()
.properties
.unwrap()["spec"];
assert_eq!(spec.x_kubernetes_preserve_unknown_fields, Some(true));
assert_eq!(spec.additional_properties, None);
}