-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcustom_resource_definition.rs
294 lines (271 loc) · 12.5 KB
/
custom_resource_definition.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
use k8s_openapi::{http, serde_json};
#[test]
fn create() {
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1beta1 as apiextensions;
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as meta;
#[derive(Debug, Default, serde_derive::Deserialize, serde_derive::Serialize)]
struct FooBar {
#[serde(rename = "apiVersion")]
pub api_version: Option<String>,
pub kind: Option<String>,
pub metadata: Option<meta::ObjectMeta>,
pub spec: Option<FooBarSpec>,
}
#[derive(Debug, Default, serde_derive::Deserialize, serde_derive::Serialize)]
struct FooBarSpec {
prop1: String,
prop2: Vec<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
prop3: Option<i32>,
}
#[derive(Debug)]
enum CreateFooBarResponse {
Created(FooBar),
UnprocessableEntity(meta::Status),
Other,
}
impl k8s_openapi::Response for CreateFooBarResponse {
fn try_from_parts(status_code: http::StatusCode, buf: &[u8]) -> Result<(Self, usize), k8s_openapi::ResponseError> {
match status_code {
http::StatusCode::CREATED => {
let result = match serde_json::from_slice(buf) {
Ok(value) => value,
Err(ref err) if err.is_eof() => return Err(k8s_openapi::ResponseError::NeedMoreData),
Err(err) => return Err(k8s_openapi::ResponseError::Json(err)),
};
Ok((CreateFooBarResponse::Created(result), buf.len()))
},
http::StatusCode::UNPROCESSABLE_ENTITY => {
let result = match serde_json::from_slice(buf) {
Ok(value) => value,
Err(ref err) if err.is_eof() => return Err(k8s_openapi::ResponseError::NeedMoreData),
Err(err) => return Err(k8s_openapi::ResponseError::Json(err)),
};
Ok((CreateFooBarResponse::UnprocessableEntity(result), buf.len()))
},
_ => Ok((CreateFooBarResponse::Other, 0)),
}
}
}
#[derive(Debug)]
enum DeleteFooBarResponse {
Ok,
Other,
}
impl k8s_openapi::Response for DeleteFooBarResponse {
fn try_from_parts(status_code: http::StatusCode, _: &[u8]) -> Result<(Self, usize), k8s_openapi::ResponseError> {
match status_code {
http::StatusCode::OK => Ok((DeleteFooBarResponse::Ok, 0)),
_ => Ok((DeleteFooBarResponse::Other, 0)),
}
}
}
crate::Client::with("custom_resource_definition-create", |client| {
let custom_resource_definition_spec = apiextensions::CustomResourceDefinitionSpec {
group: "k8s-openapi-tests-custom-resource-definition.com".to_string(),
names: apiextensions::CustomResourceDefinitionNames {
kind: "FooBar".to_string(),
plural: "foobars".to_string(),
short_names: Some(vec!["fb".to_string()]),
singular: Some("foobar".to_string()),
..Default::default()
},
scope: "Namespaced".to_string(),
version: "v1".to_string().into(),
..Default::default()
};
k8s_if_ge_1_9! {
// CRD validation entered beta in v1.9
let custom_resource_definition_spec = apiextensions::CustomResourceDefinitionSpec {
validation: Some(apiextensions::CustomResourceValidation {
open_api_v3_schema: Some(apiextensions::JSONSchemaProps {
properties: Some(vec![
("spec".to_string(), apiextensions::JSONSchemaProps {
properties: Some(vec![
("prop1".to_string(), apiextensions::JSONSchemaProps {
type_: Some("string".to_string()),
..Default::default()
}),
("prop2".to_string(), apiextensions::JSONSchemaProps {
type_: Some("array".to_string()),
items: Some(apiextensions::JSONSchemaPropsOrArray::Schema(Box::new(apiextensions::JSONSchemaProps {
type_: Some("boolean".to_string()),
..Default::default()
}))),
..Default::default()
}),
("prop3".to_string(), apiextensions::JSONSchemaProps {
format: Some("int32".to_string()),
type_: Some("integer".to_string()),
..Default::default()
}),
].into_iter().collect()),
required: Some(vec![
"prop1".to_string(),
"prop2".to_string(),
]),
..Default::default()
}),
].into_iter().collect()),
..Default::default()
}),
}),
..custom_resource_definition_spec
};
}
let custom_resource_definition = apiextensions::CustomResourceDefinition {
metadata: Some(meta::ObjectMeta {
name: Some("foobars.k8s-openapi-tests-custom-resource-definition.com".to_string()),
..Default::default()
}),
spec: custom_resource_definition_spec.into(),
..Default::default()
};
loop {
enum Result {
Ok(apiextensions::CustomResourceDefinition),
Conflict,
Retry,
}
let (request, response_body) =
apiextensions::CustomResourceDefinition::create_custom_resource_definition(&custom_resource_definition, Default::default())
.expect("couldn't create custom resource definition");
let response = client.execute(request).expect("couldn't create custom resource definition");
let custom_resource_definition =
crate::get_single_value(response, response_body, |response, status_code, _response_body| k8s_match!(response, {
k8s_if_1_8!(apiextensions::CreateCustomResourceDefinitionResponse::Other if status_code == http::StatusCode::CREATED =>
match serde_json::from_slice(_response_body) {
Ok(custom_resource_definition) => Ok(crate::ValueResult::GotValue(Result::Ok(custom_resource_definition))),
Err(ref err) if err.is_eof() => Ok(crate::ValueResult::NeedMoreData),
Err(err) => Err(err.into()),
}),
k8s_if_ge_1_9!(apiextensions::CreateCustomResourceDefinitionResponse::Created(custom_resource_definition) =>
Ok(crate::ValueResult::GotValue(Result::Ok(custom_resource_definition)))),
apiextensions::CreateCustomResourceDefinitionResponse::Other if status_code == http::StatusCode::CONFLICT =>
Ok(crate::ValueResult::GotValue(Result::Conflict)),
apiextensions::CreateCustomResourceDefinitionResponse::Other if status_code == http::StatusCode::INTERNAL_SERVER_ERROR =>
Ok(crate::ValueResult::GotValue(Result::Retry)),
other => Err(format!("{:?} {}", other, status_code).into()),
})).expect("couldn't create custom resource definition");
match custom_resource_definition {
Result::Ok(_) | Result::Conflict => break,
Result::Retry => (),
}
}
// Wait for CRD to be registered
let custom_resource_definition = loop {
let (request, response_body) =
apiextensions::CustomResourceDefinition::read_custom_resource_definition(
"foobars.k8s-openapi-tests-custom-resource-definition.com", Default::default())
.expect("couldn't get custom resource definition");
let custom_resource_definition = {
let response = client.execute(request).expect("couldn't get custom resource definition");
crate::get_single_value(response, response_body, |response, status_code, _| match response {
apiextensions::ReadCustomResourceDefinitionResponse::Ok(custom_resource_definition) => Ok(crate::ValueResult::GotValue(custom_resource_definition)),
other => Err(format!("{:?} {}", other, status_code).into()),
}).expect("couldn't get custom resource definition")
};
if custom_resource_definition.status.as_ref().map_or(false, |status| status.accepted_names.kind == "FooBar") {
break custom_resource_definition;
}
client.sleep(std::time::Duration::from_secs(1));
};
let fb1 = FooBar {
api_version: Some("k8s-openapi-tests-custom-resource-definition.com/v1".to_string()),
kind: Some("FooBar".to_string()),
metadata: Some(meta::ObjectMeta {
name: Some("fb1".to_string()),
..Default::default()
}),
spec: Some(FooBarSpec {
prop1: "value1".to_string(),
prop2: vec![true, false, true],
..Default::default()
}),
..Default::default()
};
let request =
http::Request::post("/apis/k8s-openapi-tests-custom-resource-definition.com/v1/namespaces/default/foobars")
.body(serde_json::to_vec(&fb1).expect("couldn't create custom resource definition"))
.expect("couldn't create custom resource");
let fb1 = {
let response = client.execute(request).expect("couldn't create custom resource");
crate::get_single_value(response, k8s_openapi::ResponseBody::new, |response, status_code, _| match response {
CreateFooBarResponse::Created(fb) => Ok(crate::ValueResult::GotValue(fb)),
other => Err(format!("{:?} {}", other, status_code).into()),
}).expect("couldn't create custom resource")
};
let fb1_self_link = {
let metadata = fb1.metadata.expect("couldn't get custom resource metadata");
metadata.self_link.expect("couldn't get custom resource self link")
};
let request = http::Request::delete(fb1_self_link).body(vec![]).expect("couldn't delete custom resource");
{
let response = client.execute(request).expect("couldn't delete custom resource");
crate::get_single_value(response, k8s_openapi::ResponseBody::new, |response, status_code, _| match response {
DeleteFooBarResponse::Ok => Ok(crate::ValueResult::GotValue(())),
other => Err(format!("{:?} {}", other, status_code).into()),
}).expect("couldn't delete custom resource");
}
k8s_if_ge_1_9! {
let fb2 = serde_json::Value::Object(vec![
("apiVersion".to_string(), serde_json::Value::String("k8s-openapi-tests-custom-resource-definition.com/v1".to_string())),
("kind".to_string(), serde_json::Value::String("FooBar".to_string())),
("metadata".to_string(), serde_json::Value::Object(vec![
("name".to_string(), serde_json::Value::String("fb1".to_string())),
].into_iter().collect())),
("spec".to_string(), serde_json::Value::Object(vec![
("prop1".to_string(), serde_json::Value::String("value1".to_string())),
].into_iter().collect())),
].into_iter().collect());
let request =
http::Request::post("/apis/k8s-openapi-tests-custom-resource-definition.com/v1/namespaces/default/foobars")
.body(serde_json::to_vec(&fb2).expect("couldn't create custom resource definition"))
.expect("couldn't create custom resource");
{
let response = client.execute(request).expect("couldn't create custom resource");
crate::get_single_value(response, k8s_openapi::ResponseBody::new, |response, status_code, _| match response {
CreateFooBarResponse::UnprocessableEntity(_) => Ok(crate::ValueResult::GotValue(())),
other => Err(format!("{:?} {}", other, status_code).into()),
}).expect("expected custom resource creation to fail validation");
}
}
k8s_if_ge_1_9! {
let fb3 = serde_json::Value::Object(vec![
("apiVersion".to_string(), serde_json::Value::String("k8s-openapi-tests-custom-resource-definition.com/v1".to_string())),
("kind".to_string(), serde_json::Value::String("FooBar".to_string())),
("metadata".to_string(), serde_json::Value::Object(vec![
("name".to_string(), serde_json::Value::String("fb1".to_string())),
].into_iter().collect())),
("spec".to_string(), serde_json::Value::Object(vec![
("prop1".to_string(), serde_json::Value::String("value1".to_string())),
("prop2".to_string(), serde_json::Value::Bool(true)),
].into_iter().collect())),
].into_iter().collect());
let request =
http::Request::post("/apis/k8s-openapi-tests-custom-resource-definition.com/v1/namespaces/default/foobars")
.body(serde_json::to_vec(&fb3).expect("couldn't create custom resource definition"))
.expect("couldn't create custom resource");
{
let response = client.execute(request).expect("couldn't create custom resource");
crate::get_single_value(response, k8s_openapi::ResponseBody::new, |response, status_code, _| match response {
CreateFooBarResponse::UnprocessableEntity(_) => Ok(crate::ValueResult::GotValue(())),
other => Err(format!("{:?} {}", other, status_code).into()),
}).expect("expected custom resource creation to fail validation");
}
}
let custom_resource_definition_self_link = {
let metadata = custom_resource_definition.metadata.expect("couldn't get custom resource definition metadata");
metadata.self_link.expect("couldn't get custom resource definition self link")
};
let request = http::Request::delete(custom_resource_definition_self_link).body(vec![]).expect("couldn't delete custom resource definition");
{
let response = client.execute(request).expect("couldn't delete custom resource definition");
crate::get_single_value(response, k8s_openapi::ResponseBody::new, |response, status_code, _| match response {
apiextensions::DeleteCollectionCustomResourceDefinitionResponse::OkStatus(_) |
apiextensions::DeleteCollectionCustomResourceDefinitionResponse::OkValue(_) => Ok(crate::ValueResult::GotValue(())),
other => Err(format!("{:?} {}", other, status_code).into()),
}).expect("couldn't delete custom resource definition");
}
});
}