-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathvstatefulset_e2e.rs
More file actions
273 lines (258 loc) · 9.36 KB
/
Copy pathvstatefulset_e2e.rs
File metadata and controls
273 lines (258 loc) · 9.36 KB
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
#![allow(unused_imports)]
#![allow(unused_variables)]
use k8s_openapi::api::core::v1::{Pod, PersistentVolumeClaim, PersistentVolume};
use std::time::{Duration, Instant};
use tokio::time::sleep;
use verifiable_controllers::crds::VStatefulSet;
use kube::{
api::{Api, ListParams},
Client, discovery::Discovery,
};
use tracing::*;
use crate::common::*;
pub fn v_statefulset() -> String {
"
apiVersion: anvil.dev/v1
kind: VStatefulSet
metadata:
name: pause-statefulset
labels:
app: stateful-demo
spec:
replicas: 3
serviceName: stateful-service
selector:
matchLabels:
app: stateful-demo
template:
metadata:
labels:
app: stateful-demo
spec:
containers:
- name: pause
image: k8s.gcr.io/pause:3.9
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ \"ReadWriteOnce\" ]
resources:
requests:
storage: 1Mi
"
.to_string()
}
async fn wait_for_pods_for_vsts(client: Client, name: &str, expected: usize, timeout: Duration) -> Result<Vec<Pod>, Error> {
let pod_api: Api<Pod> = Api::default_namespaced(client);
let start = Instant::now();
loop {
if start.elapsed() > timeout {
error!("Timeout waiting for pods for {}", name);
return Err(Error::Timeout);
}
sleep(Duration::from_secs(3)).await;
match pod_api.list(&ListParams::default()).await {
Err(e) => {
info!("List pods failed: {}", e);
continue;
}
Ok(list) => {
let pods: Vec<Pod> = list
.items
.into_iter()
.filter(|p| {
if let Some(n) = p.metadata.name.as_ref() {
n.starts_with(&format!("vstatefulset-{}-", name))
} else {
false
}
})
.collect();
if pods.len() < expected {
info!("Have {} pods, want {}; still waiting.", pods.len(), expected);
continue;
} else if pods.len() > expected {
info!("Have {} pods which is more than expected {}.", pods.len(), expected);
continue;
} else {
info!("Found {} pods for VStatefulSet {}.", pods.len(), name);
return Ok(pods);
}
}
}
}
}
async fn check_pv_and_template(client: Client, vsts_name: &str, pods: &[Pod], timeout: Duration) -> Result<(), Error> {
// pick first pod
let first_pod = pods.get(0).ok_or(Error::VStatefulSetFailed)?;
// find a PVC referenced by the pod
let volumes = first_pod.spec.as_ref().map(|s| s.volumes.clone()).flatten().unwrap_or_default();
let mut pvc_names = vec![];
for v in volumes.iter() {
if let Some(pvc) = v.persistent_volume_claim.as_ref() {
pvc_names.push(pvc.claim_name.clone());
}
}
if pvc_names.is_empty() {
info!("No PVCs found for pod {}", first_pod.metadata.name.as_ref().unwrap());
return Err(Error::VStatefulSetFailed);
}
let pvc_api: Api<PersistentVolumeClaim> = Api::default_namespaced(client.clone());
let start = Instant::now();
loop {
if start.elapsed() > timeout {
error!("Timeout waiting for PVCs to be bound for pod {}", first_pod.metadata.name.as_ref().unwrap());
return Err(Error::Timeout);
}
// check if all PVCs are bound
let mut all_bound = true;
for claim in pvc_names.iter() {
match pvc_api.get(claim).await {
Err(e) => {
info!("Get PVC {} failed: {}", claim, e);
all_bound = false;
break;
}
Ok(pvc) => {
if pvc.status.as_ref().and_then(|s| s.phase.as_ref()).map(|s| s == "Bound").unwrap_or(false) {
info!("PVC {} is bound.", claim);
continue;
} else {
info!("PVC {} is not yet bound.", claim);
all_bound = false;
break;
}
}
}
}
if all_bound {
info!("All PVCs are bound for pod {}", first_pod.metadata.name.as_ref().unwrap());
break;
}
sleep(Duration::from_secs(3)).await;
}
// TODO: Add check on PV matching PVC later when we have VPV controller
Ok(())
}
pub async fn vstatefulset_e2e_test() -> Result<(), Error> {
let client = Client::try_default().await?;
let discovery = Discovery::new(client.clone()).run().await?;
let vsts_name = apply(v_statefulset(), client.clone(), &discovery).await?;
let timeout = Duration::from_secs(20);
// wait and check pods matching name against replicas
let vsts_api: Api<VStatefulSet> = Api::default_namespaced(client.clone());
let vsts = vsts_api.get(&vsts_name).await?;
let replicas = vsts.spec.replicas.unwrap_or(1) as usize;
let pods = wait_for_pods_for_vsts(client.clone(), &vsts_name, replicas, timeout).await?;
// check PVs bound to first pod and compare one PV to pvc template
check_pv_and_template(client.clone(), &vsts_name, &pods, timeout).await?;
// scale up
let new_replicas_up = replicas + 2;
run_command(
"kubectl",
vec![
"patch",
"vsts",
vsts_name.as_str(),
"--type=json",
"-p",
&format!("[{{\"op\":\"replace\",\"path\":\"/spec/replicas\",\"value\":{}}}]", new_replicas_up),
],
"failed to scale VStatefulSet up",
);
let _pods_up = wait_for_pods_for_vsts(client.clone(), &vsts_name, new_replicas_up, timeout).await?;
// scale down
let new_replicas_down = (replicas as isize - 1).max(1) as usize;
run_command(
"kubectl",
vec![
"patch",
"vsts",
vsts_name.as_str(),
"--type=json",
"-p",
&format!("[{{\"op\":\"replace\",\"path\":\"/spec/replicas\",\"value\":{}}}]", new_replicas_down),
],
"failed to scale VStatefulSet down",
);
// wait until pods with ordinal >= new_replicas_down are gone
let pod_api: Api<Pod> = Api::default_namespaced(client.clone());
let start = Instant::now();
loop {
if start.elapsed() > timeout {
error!("Timeout waiting for scale down cleanup");
return Err(Error::Timeout);
}
sleep(Duration::from_secs(3)).await;
let list = pod_api.list(&ListParams::default()).await?;
let mut found_bad = false;
for p in list.items.iter() {
if let Some(n) = p.metadata.name.as_ref() {
if n.starts_with(&format!("vstatefulset-{}-", vsts_name)) {
// extract suffix after last '-'
if let Some(pos) = n.rfind('-') {
if let Ok(idx) = n[pos+1..].parse::<usize>() {
if idx >= new_replicas_down {
found_bad = true;
break;
}
}
}
}
}
}
if found_bad {
info!("Found pods with ordinal >= {}; still waiting for deletion.", new_replicas_down);
continue;
}
info!("Scale down cleanup verified.");
break;
}
// TODO: fix spec check or find a workaround for getPodRevision
// patch app version image and wait for pod with new image running
// let new_image = "k8s.gcr.io/pause:3.10";
// run_command(
// "kubectl",
// vec![
// "patch",
// "vsts",
// vsts_name.as_str(),
// "--type=json",
// "-p",
// &format!("[{{\"op\":\"replace\",\"path\":\"/spec/template/spec/containers/0/image\",\"value\":\"{}\"}}]", new_image),
// ],
// "failed to patch VStatefulSet image",
// );
// let start = Instant::now();
// loop {
// if start.elapsed() > timeout {
// error!("Timeout waiting for image update");
// return Err(Error::Timeout);
// }
// sleep(Duration::from_secs(4)).await;
// let list = pod_api.list(&ListParams::default()).await?;
// let mut found = false;
// for p in list.items.iter() {
// if p.status.as_ref().and_then(|s| s.phase.as_ref()).map(|s| s == "Running").unwrap_or(false) {
// if let Some(spec) = p.spec.as_ref() {
// if !spec.containers.is_empty() {
// if let Some(img) = spec.containers.get(0).and_then(|c| c.image.as_ref()) {
// if img == new_image {
// info!("Found running pod {} with new image.", p.metadata.name.as_ref().unwrap());
// found = true;
// break;
// }
// }
// }
// }
// }
// }
// if found {
// info!("Image update verified.");
// break;
// }
// }
info!("VStatefulSet e2e test passed.");
Ok(())
}