Repository navigation
Expand file tree
/
Copy pathcurl.rs
More file actions
241 lines (224 loc) · 8.28 KB
/
Copy pathcurl.rs
File metadata and controls
241 lines (224 loc) · 8.28 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
use super::{create, LinkerdInject};
use linkerd_policy_controller_k8s_api::{self as k8s};
use maplit::{btreemap, convert_args};
use tokio::time;
#[derive(Clone)]
#[must_use]
pub struct Runner {
namespace: String,
client: kube::Client,
}
#[derive(Clone)]
pub struct Running {
namespace: String,
name: String,
client: kube::Client,
}
impl Runner {
pub async fn init(client: &kube::Client, ns: &str) -> Runner {
let runner = Runner {
namespace: ns.to_string(),
client: client.clone(),
};
runner.create_rbac().await;
runner
}
/// Creates a configmap that prevents curl pods from executing.
pub async fn create_lock(&self) {
create(
&self.client,
k8s::api::core::v1::ConfigMap {
metadata: k8s::ObjectMeta {
namespace: Some(self.namespace.clone()),
name: Some("curl-lock".to_string()),
..Default::default()
},
..Default::default()
},
)
.await;
}
/// Deletes the lock configmap, allowing curl pods to execute.
pub async fn delete_lock(&self) {
tracing::trace!(ns = %self.namespace, "Deleting curl-lock");
kube::Api::<k8s::api::core::v1::ConfigMap>::namespaced(
self.client.clone(),
&self.namespace,
)
.delete("curl-lock", &kube::api::DeleteParams::foreground())
.await
.expect("curl-lock must be deleted");
tracing::debug!(ns = %self.namespace, "Deleted curl-lock");
}
/// Runs a [`k8s::Pod`] that runs curl against the provided target URL.
///
/// The pod:
/// - has the `linkerd.io/inject` annotation set, based on the
/// `linkerd_inject` parameter;
/// - runs under the service account `curl`;
/// - does not actually execute curl until the `curl-lock` configmap is not
/// present
pub async fn run(&self, name: &str, target_url: &str, inject: LinkerdInject) -> Running {
create(
&self.client,
Self::gen_pod(&self.namespace, name, target_url, inject),
)
.await;
Running {
client: self.client.clone(),
namespace: self.namespace.clone(),
name: name.to_string(),
}
}
/// Creates a service account and RBAC to allow curl pods to watch the
/// curl-lock configmap.
async fn create_rbac(&self) {
create(
&self.client,
k8s::api::core::v1::ServiceAccount {
metadata: k8s::ObjectMeta {
namespace: Some(self.namespace.clone()),
name: Some("curl".to_string()),
..Default::default()
},
..Default::default()
},
)
.await;
create(
&self.client,
k8s::api::rbac::v1::Role {
metadata: k8s::ObjectMeta {
namespace: Some(self.namespace.clone()),
name: Some("curl-lock".to_string()),
..Default::default()
},
rules: Some(vec![k8s::api::rbac::v1::PolicyRule {
api_groups: Some(vec!["".to_string()]),
resources: Some(vec!["configmaps".to_string()]),
verbs: vec!["get".to_string(), "list".to_string(), "watch".to_string()],
..Default::default()
}]),
},
)
.await;
create(
&self.client,
k8s::api::rbac::v1::RoleBinding {
metadata: k8s::ObjectMeta {
namespace: Some(self.namespace.clone()),
name: Some("curl-lock".to_string()),
..Default::default()
},
role_ref: k8s::api::rbac::v1::RoleRef {
api_group: "rbac.authorization.k8s.io".to_string(),
kind: "Role".to_string(),
name: "curl-lock".to_string(),
},
subjects: Some(vec![k8s::api::rbac::v1::Subject {
kind: "ServiceAccount".to_string(),
name: "curl".to_string(),
namespace: Some(self.namespace.clone()),
..Default::default()
}]),
},
)
.await;
}
fn gen_pod(ns: &str, name: &str, target_url: &str, inject: LinkerdInject) -> k8s::Pod {
k8s::Pod {
metadata: k8s::ObjectMeta {
namespace: Some(ns.to_string()),
name: Some(name.to_string()),
annotations: Some(convert_args!(btreemap!(
"linkerd.io/inject" => inject.to_string(),
"config.linkerd.io/proxy-log-level" => "linkerd=trace,info",
))),
..Default::default()
},
spec: Some(k8s::PodSpec {
service_account: Some("curl".to_string()),
init_containers: Some(vec![k8s::api::core::v1::Container {
name: "wait-for-nginx".to_string(),
image: Some("docker.io/bitnami/kubectl:latest".to_string()),
args: Some(
vec![
"wait",
"--timeout=120s",
"--for=delete",
"--namespace",
ns,
"cm",
"curl-lock",
]
.into_iter()
.map(Into::into)
.collect(),
),
..Default::default()
}]),
containers: vec![k8s::api::core::v1::Container {
name: "curl".to_string(),
image: Some("docker.io/curlimages/curl:latest".to_string()),
args: Some(
vec!["curl", "-sSfv", target_url]
.into_iter()
.map(Into::into)
.collect(),
),
..Default::default()
}],
restart_policy: Some("Never".to_string()),
..Default::default()
}),
..k8s::Pod::default()
}
}
}
impl Running {
pub fn name(&self) -> &str {
&self.name
}
/// Waits for the pod to have an IP address and returns it.
pub async fn ip(&self) -> std::net::IpAddr {
super::await_pod_ip(&self.client, &self.namespace, &self.name).await
}
/// Waits for the curl container to complete and returns its exit code.
pub async fn exit_code(self) -> i32 {
fn get_exit_code(pod: &k8s::Pod) -> Option<i32> {
let c = pod
.status
.as_ref()?
.container_statuses
.as_ref()?
.iter()
.find(|c| c.name == "curl")?;
let code = c.state.as_ref()?.terminated.as_ref()?.exit_code;
Some(code)
}
tracing::debug!(ns = %self.namespace, pod = %self.name, "Waiting for exit code");
let api = kube::Api::namespaced(self.client.clone(), &self.namespace);
let finished = kube::runtime::wait::await_condition(
api.clone(),
&self.name,
|obj: Option<&k8s::Pod>| -> bool { obj.and_then(get_exit_code).is_some() },
);
match time::timeout(time::Duration::from_secs(120), finished).await {
Ok(Ok(())) => {}
Ok(Err(error)) => panic!("Failed to wait for exit code: {}: {}", self.name, error),
Err(_timeout) => {
panic!("Timeout waiting for exit code: {}", self.name);
}
};
let curl_pod = api.get(&self.name).await.expect("pod must exist");
let code = get_exit_code(&curl_pod).expect("curl pod must have an exit code");
tracing::debug!(pod = %self.name, %code, "Curl exited");
if let Err(error) = api
.delete(&self.name, &kube::api::DeleteParams::foreground())
.await
{
tracing::trace!(%error, name = %self.name, "Failed to delete pod");
}
code
}
}