-
Notifications
You must be signed in to change notification settings - Fork 95
/
xds.rs
445 lines (406 loc) · 16 KB
/
xds.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#[cfg(test)]
mod tests {
use std::net::SocketAddr;
use std::{collections::BTreeMap, time::Duration};
use k8s_openapi::{
api::{
apps::v1::{Deployment, DeploymentSpec},
core::v1::{
ConfigMap, ContainerPort, Node, Pod, PodSpec, PodTemplateSpec, Service,
ServiceAccount, ServicePort, ServiceSpec,
},
rbac::v1::{ClusterRole, PolicyRule, RoleBinding, RoleRef, Subject},
},
apimachinery::pkg::{
apis::meta::v1::{LabelSelector, ObjectMeta},
util::intstr::IntOrString,
},
};
use kube::{
api::{DeleteParams, ListParams, PostParams},
runtime::wait::await_condition,
Api, ResourceExt,
};
use tokio::time::timeout;
use quilkin::{
config::providers::k8s::agones::{Fleet, GameServer},
test_utils::TestHelper,
};
use crate::{
debug_pods, fleet, is_deployment_ready, is_fleet_ready, quilkin_config_map,
quilkin_container, Client,
};
const PROXY_DEPLOYMENT: &str = "quilkin-proxies";
#[tokio::test]
/// Test for Agones integration. Since this will look at all GameServers in the namespace
/// for this test, we should only have single Agones integration test in this suite, since they
/// could easily collide with each other.
async fn agones_token_router() {
quilkin::test_utils::enable_log("agones=debug");
let client = Client::new().await;
let deployments: Api<Deployment> = client.namespaced_api();
let fleets: Api<Fleet> = client.namespaced_api();
let gameservers: Api<GameServer> = client.namespaced_api();
let config_maps: Api<ConfigMap> = client.namespaced_api();
let pp = PostParams::default();
let config = r#"
version: v1alpha1
filters:
- name: quilkin.filters.capture.v1alpha1.Capture # Capture and remove the authentication token
config:
suffix:
size: 3
remove: true
- name: quilkin.filters.token_router.v1alpha1.TokenRouter
"#;
let mut config_map = quilkin_config_map(config);
config_map
.metadata
.labels
.get_or_insert(Default::default())
.insert("quilkin.dev/configmap".into(), "true".into());
config_maps.create(&pp, &config_map).await.unwrap();
agones_control_plane(&client, deployments.clone()).await;
let proxy_address = quilkin_proxy_deployment(&client, deployments.clone()).await;
// create a fleet so we can ensure that a packet is going to the GameServer we expect, and not
// any other.
let fleet = fleet();
let fleet = fleets.create(&pp, &fleet).await.unwrap();
let name = fleet.name_unchecked();
timeout(
Duration::from_secs(30),
await_condition(fleets.clone(), name.as_str(), is_fleet_ready()),
)
.await
.expect("Fleet should be ready")
.unwrap();
let lp = ListParams {
label_selector: Some(format!("agones.dev/fleet={}", fleet.name_unchecked())),
..Default::default()
};
let list = gameservers.list(&lp).await.unwrap();
// let's allocate this specific game server
let mut t = TestHelper::default();
let (mut rx, socket) = t.open_socket_and_recv_multiple_packets().await;
let mut gs = list.items[0].clone();
let gs_address = crate::gameserver_address(&gs);
// add routing label to the GameServer
let token = "456"; // NDU2
assert_eq!(3, token.as_bytes().len());
let token_key = "quilkin.dev/tokens";
gs.metadata
.annotations
.get_or_insert(Default::default())
.insert(
token_key.into(),
base64::Engine::encode(&base64::engine::general_purpose::STANDARD, token),
);
gameservers
.replace(gs.name_unchecked().as_str(), &pp, &gs)
.await
.unwrap();
// and allocate it such that we have an endpoint.
socket
.send_to("ALLOCATE".as_bytes(), gs_address)
.await
.unwrap();
let response = timeout(Duration::from_secs(30), rx.recv())
.await
.expect("should receive packet from GameServer")
.unwrap();
assert_eq!("ACK: ALLOCATE\n", response);
// Proxy Deployment should be ready, since there is now an endpoint
if timeout(
Duration::from_secs(30),
await_condition(deployments.clone(), PROXY_DEPLOYMENT, is_deployment_ready()),
)
.await
.is_err()
{
debug_pods(&client, "role=proxy".into()).await;
panic!("Quilkin proxy deployment should be ready");
}
// keep trying to send the packet to the proxy until it works, since distributed systems are eventually consistent.
let mut response: String = "not-found".into();
for i in 0..30 {
println!("Connection Attempt: {i}");
// returns the nae of the GameServer. This proves we are routing the the allocated
// GameServer with the correct token attached.
socket
.send_to(format!("GAMESERVER{token}").as_bytes(), proxy_address)
.await
.unwrap();
let result = timeout(Duration::from_secs(1), rx.recv()).await;
if let Ok(Some(value)) = result {
response = value;
break;
}
}
assert_eq!(format!("NAME: {}\n", gs.name_unchecked()), response);
// let's remove the token from the gameserver, which should remove access.
let mut gs = gameservers.get(gs.name_unchecked().as_str()).await.unwrap();
let name = gs.name_unchecked();
gs.metadata
.annotations
.as_mut()
.map(|annotations| annotations.remove(token_key).unwrap());
gameservers.replace(name.as_str(), &pp, &gs).await.unwrap();
// now we should send a packet, and not get a response.
let mut failed = false;
for i in 0..30 {
println!("Disconnection Attempt: {i}");
socket
.send_to(format!("GAMESERVER{token}").as_bytes(), proxy_address)
.await
.unwrap();
let result = timeout(Duration::from_secs(1), rx.recv()).await;
if result.is_err() {
failed = true;
break;
}
}
assert!(failed, "Packet should have failed");
}
/// Creates Quilkin xDS management instance that is in the mode to watch Agones GameServers
/// in this test namespace
async fn agones_control_plane(client: &Client, deployments: Api<Deployment>) {
let services: Api<Service> = client.namespaced_api();
let service_accounts: Api<ServiceAccount> = client.namespaced_api();
let cluster_roles: Api<ClusterRole> = Api::all(client.kubernetes.clone());
let role_bindings: Api<RoleBinding> = client.namespaced_api();
let pp = PostParams::default();
// create all the rbac rules
let rbac_name = "quilkin-agones";
let rbac_meta = ObjectMeta {
name: Some(rbac_name.into()),
..Default::default()
};
let service_account = ServiceAccount {
metadata: rbac_meta.clone(),
..Default::default()
};
service_accounts
.create(&pp, &service_account)
.await
.unwrap();
// Delete the cluster role if it already exists, since it's cluster wide.
match cluster_roles
.delete(rbac_name, &DeleteParams::default())
.await
{
Ok(_) => {}
Err(err) => println!("Cluster role not found: {err}"),
};
let cluster_role = ClusterRole {
metadata: rbac_meta.clone(),
rules: Some(vec![
PolicyRule {
api_groups: Some(vec!["agones.dev".into()]),
resources: Some(vec!["gameservers".into()]),
verbs: ["get", "list", "watch"].map(String::from).to_vec(),
..Default::default()
},
PolicyRule {
api_groups: Some(vec!["".into()]),
resources: Some(vec!["configmaps".into()]),
verbs: ["get", "list", "watch"].map(String::from).to_vec(),
..Default::default()
},
]),
..Default::default()
};
cluster_roles.create(&pp, &cluster_role).await.unwrap();
let binding = RoleBinding {
metadata: rbac_meta,
subjects: Some(vec![Subject {
kind: "User".into(),
name: format!("system:serviceaccount:{}:{rbac_name}", client.namespace),
api_group: Some("rbac.authorization.k8s.io".into()),
..Default::default()
}]),
role_ref: RoleRef {
api_group: "rbac.authorization.k8s.io".into(),
kind: "ClusterRole".into(),
name: rbac_name.into(),
},
};
role_bindings.create(&pp, &binding).await.unwrap();
// Setup the xDS Agones provider server
let args = [
"manage",
"agones",
"--config-namespace",
client.namespace.as_str(),
"--gameservers-namespace",
client.namespace.as_str(),
]
.map(String::from)
.to_vec();
let mut container = quilkin_container(client, Some(args), None);
container.ports = Some(vec![ContainerPort {
container_port: 7777,
..Default::default()
}]);
let labels = BTreeMap::from([("role".to_string(), "xds".to_string())]);
let deployment = Deployment {
metadata: ObjectMeta {
name: Some("quilkin-manage-agones".into()),
labels: Some(labels.clone()),
..Default::default()
},
spec: Some(DeploymentSpec {
replicas: Some(1),
selector: LabelSelector {
match_expressions: None,
match_labels: Some(labels.clone()),
},
template: PodTemplateSpec {
metadata: Some(ObjectMeta {
labels: Some(labels.clone()),
..Default::default()
}),
spec: Some(PodSpec {
containers: vec![container],
service_account_name: Some(rbac_name.into()),
..Default::default()
}),
},
..Default::default()
}),
..Default::default()
};
let deployment = deployments.create(&pp, &deployment).await.unwrap();
let service = Service {
metadata: ObjectMeta {
name: Some("quilkin-manage-agones".into()),
..Default::default()
},
spec: Some(ServiceSpec {
selector: Some(labels),
ports: Some(vec![ServicePort {
protocol: Some("TCP".into()),
port: 7800,
target_port: Some(IntOrString::Int(7800)),
..Default::default()
}]),
..Default::default()
}),
..Default::default()
};
services.create(&pp, &service).await.unwrap();
// make sure the deployment and service are ready
let name = deployment.name_unchecked();
timeout(
Duration::from_secs(30),
await_condition(deployments.clone(), name.as_str(), is_deployment_ready()),
)
.await
.expect("xDS provider deployment should be ready")
.unwrap();
}
/// create a Deployment with a singular Quilkin proxy, that is configured
/// to be attached to the Quilkin Agones xDS server in `agones_control_plane()`.
async fn quilkin_proxy_deployment(client: &Client, deployments: Api<Deployment>) -> SocketAddr {
let pp = PostParams::default();
let mut container = quilkin_container(
client,
Some(vec![
"proxy".into(),
"--management-server=http://quilkin-manage-agones:7800".into(),
]),
None,
);
// we'll use a host port, since spinning up a load balancer takes a long time.
// we know that port 7777 is open because this is an Agones cluster and it has associated
// firewall rules , and even if we conflict with a GameServer
// the k8s scheduler will move us to another node.
let host_port: u16 = 7005;
container.ports = Some(vec![ContainerPort {
container_port: 7777,
host_port: Some(host_port as i32),
protocol: Some("UDP".into()),
..Default::default()
}]);
let labels = BTreeMap::from([("role".to_string(), "proxy".to_string())]);
let deployment = Deployment {
metadata: ObjectMeta {
name: Some(PROXY_DEPLOYMENT.into()),
labels: Some(labels.clone()),
..Default::default()
},
spec: Some(DeploymentSpec {
replicas: Some(1),
selector: LabelSelector {
match_expressions: None,
match_labels: Some(labels.clone()),
},
template: PodTemplateSpec {
metadata: Some(ObjectMeta {
labels: Some(labels.clone()),
..Default::default()
}),
spec: Some(PodSpec {
containers: vec![container],
..Default::default()
}),
},
..Default::default()
}),
..Default::default()
};
let deployment = deployments.create(&pp, &deployment).await.unwrap();
let name = deployment.name_unchecked();
// should not be ready, since there are no endpoints, but let's wait 3 seconds, make sure it doesn't do something we don't expect
let result = timeout(
Duration::from_secs(3),
await_condition(deployments.clone(), name.as_str(), is_deployment_ready()),
)
.await;
assert!(result.is_err());
// get the address to send data to
let pods = client.namespaced_api::<Pod>();
let list = pods
.list(&ListParams {
label_selector: Some("role=proxy".into()),
..Default::default()
})
.await
.unwrap();
assert_eq!(1, list.items.len());
let nodes: Api<Node> = Api::all(client.kubernetes.clone());
let name = list.items[0]
.spec
.as_ref()
.unwrap()
.node_name
.as_ref()
.unwrap();
let node = nodes.get(name.as_str()).await.unwrap();
let external_ip = node
.status
.unwrap()
.addresses
.unwrap()
.iter()
.find(|addr| addr.type_ == "ExternalIP")
.unwrap()
.address
.clone();
SocketAddr::new(external_ip.parse().unwrap(), host_port)
}
}