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
use std::{pin::Pin, sync::Arc};
use cached::Cached;
use futures::Stream;
use tokio_stream::StreamExt;
use tracing_futures::Instrument;
use crate::{
config::Config,
xds::{
metrics,
service::discovery::v3::{
aggregated_discovery_service_server::{
AggregatedDiscoveryService, AggregatedDiscoveryServiceServer,
},
DeltaDiscoveryRequest, DeltaDiscoveryResponse, DiscoveryRequest, DiscoveryResponse,
},
ResourceType,
},
};
#[tracing::instrument(skip_all)]
pub async fn spawn(config: std::sync::Arc<crate::Config>) -> crate::Result<()> {
let port = *config.port.load();
let server = AggregatedDiscoveryServiceServer::new(ControlPlane::from_arc(config));
let server = tonic::transport::Server::builder().add_service(server);
tracing::info!("Serving management server at {}", port);
Ok(server
.serve((std::net::Ipv4Addr::UNSPECIFIED, port).into())
.await?)
}
#[derive(Clone)]
pub struct ControlPlane {
config: Arc<Config>,
watchers: Arc<crate::xds::resource::ResourceMap<Watchers>>,
}
struct Watchers {
sender: tokio::sync::watch::Sender<()>,
receiver: tokio::sync::watch::Receiver<()>,
version: std::sync::atomic::AtomicU64,
}
impl Default for Watchers {
fn default() -> Self {
let (sender, receiver) = tokio::sync::watch::channel(());
Self {
sender,
receiver,
version: <_>::default(),
}
}
}
impl ControlPlane {
pub fn new(config: Config) -> Self {
Self::from_arc(Arc::new(config))
}
pub fn from_arc(config: Arc<Config>) -> Self {
let this = Self {
config,
watchers: <_>::default(),
};
this.config.clusters.watch({
let this = this.clone();
move |_| {
this.push_update(ResourceType::Endpoint);
this.push_update(ResourceType::Cluster);
}
});
this.config.filters.watch({
let this = this.clone();
move |_| {
this.push_update(ResourceType::Listener);
}
});
this
}
fn push_update(&self, resource_type: ResourceType) {
let watchers = &self.watchers[resource_type];
watchers
.version
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
tracing::trace!(%resource_type, watchers=watchers.sender.receiver_count(), "pushing update");
let _ = watchers.sender.send(());
}
fn discovery_response(
&self,
id: &str,
resource_type: ResourceType,
names: &[String],
) -> Result<DiscoveryResponse, tonic::Status> {
let mut response = self
.config
.discovery_request(id, resource_type, names)
.map_err(|error| tonic::Status::internal(error.to_string()))?;
let watchers = &self.watchers[resource_type];
let nonce = uuid::Uuid::new_v4();
response.version_info = watchers
.version
.load(std::sync::atomic::Ordering::Relaxed)
.to_string();
response.control_plane = Some(crate::xds::config::core::v3::ControlPlane {
identifier: (*self.config.id.load()).clone(),
});
response.nonce = nonce.to_string();
tracing::trace!(
id = &*response.version_info,
r#type = &*response.type_url,
nonce = &*response.nonce,
"discovery response"
);
Ok(response)
}
pub async fn stream_aggregated_resources(
&self,
mut streaming: Pin<Box<dyn Stream<Item = Result<DiscoveryRequest, tonic::Status>> + Send>>,
) -> Result<
Pin<Box<dyn Stream<Item = Result<DiscoveryResponse, tonic::Status>> + Send>>,
tonic::Status,
> {
tracing::trace!("starting stream");
let message = streaming.next().await.ok_or_else(|| {
tracing::error!("No message found");
tonic::Status::invalid_argument("No message found")
})??;
if message.node.is_none() {
tracing::error!("Node identifier was not found");
return Err(tonic::Status::invalid_argument("Node identifier required"));
}
let node = message.node.clone().unwrap();
let resource_type: ResourceType = message.type_url.parse()?;
tracing::trace!(id = %node.id, %resource_type, "initial request");
metrics::DISCOVERY_REQUESTS
.with_label_values(&[&*node.id, resource_type.type_url()])
.inc();
let mut rx = self.watchers[resource_type].receiver.clone();
let mut pending_acks = cached::TimedSizedCache::with_size_and_lifespan(50, 1);
let this = Self::clone(self);
let response = this.discovery_response(&node.id, resource_type, &message.resource_names)?;
pending_acks.cache_set(response.nonce.clone(), ());
let id = node.id.clone();
Ok(Box::pin(async_stream::try_stream! {
yield response;
loop {
tracing::trace!("stream loop");
tokio::select! {
_ = rx.changed() => {
tracing::trace!("sending new discovery response");
yield this.discovery_response(&id, resource_type, &message.resource_names).map(|response| {
pending_acks.cache_set(response.nonce.clone(), ());
response
})?;
}
new_message = streaming.next() => {
let new_message = match new_message.transpose() {
Ok(Some(value)) => value,
Ok(None) => break,
Err(error) => {
tracing::error!(%error, "unknown resource type");
continue;
}
};
let id = new_message.node.as_ref().map(|node| &*node.id).unwrap_or(&*id);
let resource_type = match new_message.type_url.parse::<ResourceType>() {
Ok(value) => value,
Err(error) => {
tracing::error!(%error, "unknown resource type");
continue;
}
};
tracing::trace!("new request");
metrics::DISCOVERY_REQUESTS.with_label_values(&[id, resource_type.type_url()]).inc();
if let Some(error) = &new_message.error_detail {
metrics::NACKS.with_label_values(&[id, resource_type.type_url()]).inc();
tracing::error!(nonce = %new_message.response_nonce, ?error, "NACK");
} else if uuid::Uuid::parse_str(&new_message.response_nonce).is_ok() {
if pending_acks.cache_get(&new_message.response_nonce).is_some() {
tracing::info!(nonce = %new_message.response_nonce, "ACK");
continue
} else {
tracing::trace!(nonce = %new_message.response_nonce, "Unknown nonce: could not be found in cache");
continue
}
}
yield this.discovery_response(id, resource_type, &message.resource_names).map(|response| {
pending_acks.cache_set(response.nonce.clone(), ());
response
}).unwrap();
}
}
}
tracing::info!("terminating stream");
}.instrument(tracing::info_span!("xds_stream", %node.id, %resource_type))))
}
}
#[tonic::async_trait]
impl AggregatedDiscoveryService for ControlPlane {
type StreamAggregatedResourcesStream =
std::pin::Pin<Box<dyn Stream<Item = Result<DiscoveryResponse, tonic::Status>> + Send>>;
type DeltaAggregatedResourcesStream =
tokio_stream::wrappers::ReceiverStream<Result<DeltaDiscoveryResponse, tonic::Status>>;
#[tracing::instrument(skip_all)]
async fn stream_aggregated_resources(
&self,
request: tonic::Request<tonic::Streaming<DiscoveryRequest>>,
) -> Result<tonic::Response<Self::StreamAggregatedResourcesStream>, tonic::Status> {
Ok(tonic::Response::new(
self.stream_aggregated_resources(Box::pin(request.into_inner()))
.in_current_span()
.await?,
))
}
async fn delta_aggregated_resources(
&self,
_request: tonic::Request<tonic::Streaming<DeltaDiscoveryRequest>>,
) -> Result<tonic::Response<Self::DeltaAggregatedResourcesStream>, tonic::Status> {
Err(tonic::Status::unimplemented(
"Quilkin doesn't currently support Delta xDS",
))
}
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use tokio::time::timeout;
use super::*;
use crate::xds::{
config::{
core::v3::Node,
listener::v3::{FilterChain, Listener},
},
service::discovery::v3::DiscoveryResponse,
ResourceType,
};
const TIMEOUT_DURATION: std::time::Duration = std::time::Duration::from_secs(10);
#[tokio::test]
async fn valid_response() {
const RESOURCE: ResourceType = ResourceType::Endpoint;
const LISTENER_TYPE: ResourceType = ResourceType::Listener;
let mut response = DiscoveryResponse {
version_info: String::new(),
resources: vec![],
type_url: RESOURCE.type_url().into(),
..<_>::default()
};
let mut listener_response = DiscoveryResponse {
version_info: String::new(),
resources: vec![prost_types::Any {
type_url: LISTENER_TYPE.type_url().into(),
value: crate::prost::encode(&Listener {
filter_chains: vec![FilterChain {
filters: vec![],
..<_>::default()
}],
..<_>::default()
})
.unwrap(),
}],
type_url: LISTENER_TYPE.type_url().into(),
..<_>::default()
};
let config = Arc::new(Config::default());
let client = ControlPlane::from_arc(config.clone());
let (tx, rx) = tokio::sync::mpsc::channel(256);
let mut request = DiscoveryRequest {
node: Some(Node {
id: "quilkin".into(),
user_agent_name: "quilkin".into(),
..Node::default()
}),
resource_names: vec![],
type_url: RESOURCE.type_url().into(),
..DiscoveryRequest::default()
};
let mut listener_request = DiscoveryRequest {
node: Some(Node {
id: "quilkin".into(),
user_agent_name: "quilkin".into(),
..Node::default()
}),
resource_names: vec![],
type_url: LISTENER_TYPE.type_url().into(),
..DiscoveryRequest::default()
};
timeout(TIMEOUT_DURATION, tx.send(Ok(request.clone())))
.await
.unwrap()
.unwrap();
let mut stream = timeout(
TIMEOUT_DURATION,
client.stream_aggregated_resources(Box::pin(
tokio_stream::wrappers::ReceiverStream::new(rx),
)),
)
.await
.unwrap()
.unwrap();
let message = timeout(TIMEOUT_DURATION, stream.next())
.await
.unwrap()
.unwrap()
.unwrap();
response.version_info = message.version_info.clone();
response.nonce = message.nonce.clone();
response.control_plane = message.control_plane.clone();
request.response_nonce = message.nonce.clone();
assert_eq!(response, message);
timeout(TIMEOUT_DURATION, tx.send(Ok(request.clone())))
.await
.unwrap()
.unwrap();
timeout(TIMEOUT_DURATION, tx.send(Ok(listener_request.clone())))
.await
.unwrap()
.unwrap();
let message = timeout(TIMEOUT_DURATION, stream.next())
.await
.unwrap()
.unwrap()
.unwrap();
listener_response.control_plane = message.control_plane.clone();
listener_response.version_info = message.version_info.clone();
listener_response.nonce = message.nonce.clone();
listener_request.response_nonce = message.nonce.clone();
assert_eq!(listener_response, message);
timeout(TIMEOUT_DURATION, tx.send(Ok(listener_request.clone())))
.await
.unwrap()
.unwrap();
}
}