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
#![allow(missing_docs)]
use kube_derive::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[kube(group = "clux.dev", version = "v1", kind = "FooEnum")]
#[serde(rename_all = "camelCase")]
#[allow(clippy::enum_variant_names)]
enum FooEnumSpec {
/// 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 {},
}
#[test]
fn test_crd_name() {
use kube::core::CustomResourceExt;
assert_eq!("fooenums.clux.dev", FooEnum::crd_name());
}
#[test]
fn test_serialized_matches_expected() {
assert_eq!(
serde_json::to_value(FooEnum::new("bar", FooEnumSpec::VariantOne { int: 42 })).unwrap(),
serde_json::json!({
"apiVersion": "clux.dev/v1",
"kind": "FooEnum",
"metadata": {
"name": "bar",
},
"spec": {
"variantOne": {
"int": 42
}
}
})
);
assert_eq!(
serde_json::to_value(FooEnum::new("bar", FooEnumSpec::VariantThree {})).unwrap(),
serde_json::json!({
"apiVersion": "clux.dev/v1",
"kind": "FooEnum",
"metadata": {
"name": "bar",
},
"spec": {
"variantThree": {}
}
})
);
}
#[test]
fn test_crd_schema_matches_expected() {
use kube::core::CustomResourceExt;
assert_eq!(
FooEnum::crd(),
serde_json::from_value(serde_json::json!({
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "CustomResourceDefinition",
"metadata": {
"name": "fooenums.clux.dev"
},
"spec": {
"group": "clux.dev",
"names": {
"categories": [],
"kind": "FooEnum",
"plural": "fooenums",
"shortNames": [],
"singular": "fooenum"
},
"scope": "Cluster",
"versions": [
{
"additionalPrinterColumns": [],
"name": "v1",
"schema": {
"openAPIV3Schema": {
"description": "Auto-generated derived type for FooEnumSpec via `CustomResource`",
"properties": {
"spec": {
"oneOf": [
{
"required": [
"variantOne"
]
},
{
"required": [
"variantTwo"
]
},
{
"required": [
"variantThree"
]
}
],
"properties": {
"variantOne": {
"description": "First variant with an int",
"properties": {
"int": {
"format": "int32",
"type": "integer"
}
},
"required": [
"int"
],
"type": "object"
},
"variantThree": {
"description": "Third variant which doesn't has an attribute",
"type": "object"
},
"variantTwo": {
"description": "Second variant with an String",
"properties": {
"str": {
"type": "string"
}
},
"required": [
"str"
],
"type": "object"
}
},
"type": "object"
}
},
"required": [
"spec"
],
"title": "FooEnum",
"type": "object"
}
},
"served": true,
"storage": true,
"subresources": {}
}
]
}
}
))
.unwrap()
);
}