Skip to content

Commit

Permalink
write RequestTcpFrontend in protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Apr 5, 2023
1 parent 25f6018 commit 82bb6c5
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 43 deletions.
9 changes: 5 additions & 4 deletions bin/src/ctl/request_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use sozu_command_lib::{
config::{Config, ListenerBuilder},
proto::command::{
AddCertificate, CertificateAndKey, Cluster, FrontendFilters, PathRule, ProxyProtocolConfig,
RemoveCertificate, ReplaceCertificate, RequestHttpFrontend, RulePosition, TlsVersion,
RemoveCertificate, ReplaceCertificate, RequestHttpFrontend, RequestTcpFrontend,
RulePosition, TlsVersion,
},
request::{
ActivateListener, AddBackend, DeactivateListener, ListenerType, LoadBalancingParams,
MetricsConfiguration, RemoveBackend, RemoveListener, Request, RequestTcpFrontend,
MetricsConfiguration, RemoveBackend, RemoveListener, Request,
},
};

Expand Down Expand Up @@ -177,14 +178,14 @@ impl CommandManager {
self.order_request(Request::AddTcpFrontend(RequestTcpFrontend {
cluster_id: id,
address: address.to_string(),
tags,
tags: tags.unwrap_or(BTreeMap::new()),
}))
}
TcpFrontendCmd::Remove { id, address } => {
self.order_request(Request::RemoveTcpFrontend(RequestTcpFrontend {
cluster_id: id,
address: address.to_string(),
tags: None,
..Default::default()
}))
}
}
Expand Down
9 changes: 9 additions & 0 deletions command/src/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ message RequestHttpFrontend {
required PathRule path = 4;
optional string method = 5;
required RulePosition position = 6 [default = TREE];
// custom tags to identify the frontend in the access logs
map<string, string> tags = 7;
}

message RequestTcpFrontend {
required string cluster_id = 1;
// the socket address on which to listen for incoming traffic
required string address = 2;
// custom tags to identify the frontend in the access logs
map<string, string> tags = 3;
}

// list the frontends, filtered by protocol and/or domain
message FrontendFilters {
required bool http = 1;
Expand Down
7 changes: 3 additions & 4 deletions command/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ use crate::{
certificate::split_certificate_chain,
proto::command::{
AddCertificate, CertificateAndKey, Cluster, LoadBalancingAlgorithms, LoadMetric, PathRule,
ProxyProtocolConfig, RequestHttpFrontend, RulePosition, TlsVersion,
ProxyProtocolConfig, RequestHttpFrontend, RequestTcpFrontend, RulePosition, TlsVersion,
},
request::{
ActivateListener, AddBackend, ListenerType, LoadBalancingParams, Request,
RequestTcpFrontend, WorkerRequest,
ActivateListener, AddBackend, ListenerType, LoadBalancingParams, Request, WorkerRequest,
},
response::{HttpListenerConfig, HttpsListenerConfig, TcpListenerConfig},
};
Expand Down Expand Up @@ -870,7 +869,7 @@ impl TcpClusterConfig {
v.push(Request::AddTcpFrontend(RequestTcpFrontend {
cluster_id: self.cluster_id.clone(),
address: frontend.address.to_string(),
tags: frontend.tags.clone(),
tags: frontend.tags.clone().unwrap_or(BTreeMap::new()),
}));
}

Expand Down
15 changes: 3 additions & 12 deletions command/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
collections::BTreeMap,
error,
fmt::{self, Display},
net::SocketAddr,
Expand All @@ -11,9 +10,9 @@ use anyhow::Context;
use crate::{
certificate::Fingerprint,
proto::command::{
AddCertificate, Cluster, FrontendFilters, LoadBalancingAlgorithms, LoadMetric,
PathRuleKind, ProxyProtocolConfig, RemoveCertificate, ReplaceCertificate,
RequestHttpFrontend, RulePosition,
AddCertificate, Cluster, FrontendFilters, LoadBalancingAlgorithms, PathRuleKind,
RemoveCertificate, ReplaceCertificate, RequestHttpFrontend, RequestTcpFrontend,
RulePosition,
},
response::{
HttpFrontend, HttpListenerConfig, HttpsListenerConfig, MessageId, TcpListenerConfig,
Expand Down Expand Up @@ -241,14 +240,6 @@ pub struct DeactivateListener {
pub to_scm: bool,
}

/// Meant for outside users, contains a String instead of a SocketAddr
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RequestTcpFrontend {
pub cluster_id: String,
pub address: String,
pub tags: Option<BTreeMap<String, String>>,
}

impl RequestHttpFrontend {
/// convert a requested frontend to a usable one by parsing its address
pub fn to_frontend(self) -> anyhow::Result<HttpFrontend> {
Expand Down
12 changes: 5 additions & 7 deletions command/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ use std::{

use crate::{
proto::command::{
CertificateSummary, Cluster, PathRule, PathRuleKind, RequestHttpFrontend, RulePosition,
TlsVersion,
},
request::{
default_sticky_name, is_false, AddBackend, LoadBalancingParams, RequestTcpFrontend,
PROTOCOL_VERSION,
CertificateSummary, Cluster, PathRule, PathRuleKind, RequestHttpFrontend,
RequestTcpFrontend, RulePosition, TlsVersion,
},
request::{default_sticky_name, is_false, AddBackend, LoadBalancingParams, PROTOCOL_VERSION},
state::{ClusterId, ConfigState},
};

Expand Down Expand Up @@ -216,6 +213,7 @@ impl std::fmt::Display for PathRule {
pub struct TcpFrontend {
pub cluster_id: String,
pub address: SocketAddr,
// TODO: remove the Option here, the map may as well be empty
pub tags: Option<BTreeMap<String, String>>,
}

Expand All @@ -224,7 +222,7 @@ impl Into<RequestTcpFrontend> for TcpFrontend {
RequestTcpFrontend {
cluster_id: self.cluster_id,
address: self.address.to_string(),
tags: self.tags,
tags: self.tags.unwrap_or(BTreeMap::new()),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions command/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use crate::{
certificate::{calculate_fingerprint, Fingerprint},
proto::command::{
AddCertificate, CertificateAndKey, Cluster, PathRule, RemoveCertificate,
ReplaceCertificate, RequestHttpFrontend,
ReplaceCertificate, RequestHttpFrontend, RequestTcpFrontend,
},
request::{
ActivateListener, AddBackend, DeactivateListener, ListenerType, RemoveBackend,
RemoveListener, Request, RequestTcpFrontend,
RemoveListener, Request,
},
response::{
Backend, ClusterInformation, HttpFrontend, HttpListenerConfig, HttpsListenerConfig,
Expand Down Expand Up @@ -438,7 +438,7 @@ impl ConfigState {
.address
.parse()
.with_context(|| "wrong socket address")?,
tags: front.tags.clone(),
tags: Some(front.tags.clone()),
};
if tcp_frontends.contains(&tcp_frontend) {
bail!("This tcp frontend is already present: {:?}", tcp_frontend);
Expand Down
11 changes: 5 additions & 6 deletions e2e/src/sozu/worker.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
collections::BTreeMap,
env,
io::stdout,
net::SocketAddr,
Expand All @@ -18,9 +17,10 @@ use sozu_command::{
config::{Config, ConfigBuilder, FileConfig},
logging::{Logger, LoggerBackend},
proto::command::{
Cluster, LoadBalancingAlgorithms, PathRule, RequestHttpFrontend, RulePosition,
Cluster, LoadBalancingAlgorithms, PathRule, RequestHttpFrontend, RequestTcpFrontend,
RulePosition,
},
request::{AddBackend, LoadBalancingParams, Request, RequestTcpFrontend, WorkerRequest},
request::{AddBackend, LoadBalancingParams, Request, WorkerRequest},
response::WorkerResponse,
scm_socket::{Listeners, ScmSocket},
state::ConfigState,
Expand Down Expand Up @@ -285,7 +285,7 @@ impl Worker {
RequestTcpFrontend {
cluster_id: cluster_id.into(),
address,
tags: None,
..Default::default()
}
}

Expand All @@ -298,9 +298,8 @@ impl Worker {
address: address.to_string(),
hostname: String::from("localhost"),
path: PathRule::prefix(String::from("/")),
method: None,
position: RulePosition::Tree.into(),
tags: BTreeMap::new(),
..Default::default()
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/examples/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use anyhow::Context;
use sozu_command::{
channel::Channel,
logging::{Logger, LoggerBackend},
request::{AddBackend, LoadBalancingParams, Request, RequestTcpFrontend, WorkerRequest},
proto::command::RequestTcpFrontend,
request::{AddBackend, LoadBalancingParams, Request, WorkerRequest},
response::TcpListenerConfig,
};

Expand Down Expand Up @@ -59,7 +60,7 @@ fn main() -> anyhow::Result<()> {
let tcp_front = RequestTcpFrontend {
cluster_id: String::from("test"),
address: "127.0.0.1:8080".to_string(),
tags: None,
..Default::default()
};
let tcp_backend = AddBackend {
cluster_id: String::from("test"),
Expand Down
10 changes: 5 additions & 5 deletions lib/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ use crate::{
socket::server_bind,
sozu_command::{
logging,
proto::command::ProxyProtocolConfig,
proto::command::{ProxyProtocolConfig, RequestTcpFrontend},
ready::Ready,
request::{Request, RequestTcpFrontend, WorkerRequest},
request::{Request, WorkerRequest},
response::{Event, TcpListenerConfig, WorkerResponse},
scm_socket::ScmSocket,
state::ClusterId,
Expand Down Expand Up @@ -1298,7 +1298,7 @@ impl TcpProxy {

self.fronts
.insert(front.cluster_id.to_string(), listener.token);
listener.set_tags(front.address.to_string(), front.tags);
listener.set_tags(front.address.to_string(), Some(front.tags));
listener.cluster_id = Some(front.cluster_id.to_string());
Ok(())
}
Expand Down Expand Up @@ -1864,7 +1864,7 @@ mod tests {
let front = RequestTcpFrontend {
cluster_id: String::from("yolo"),
address: "127.0.0.1:1234".to_string(),
tags: None,
..Default::default()
};
let backend = sozu_command_lib::response::Backend {
cluster_id: String::from("yolo"),
Expand Down Expand Up @@ -1894,7 +1894,7 @@ mod tests {
let front = RequestTcpFrontend {
cluster_id: String::from("yolo"),
address: "127.0.0.1:1235".to_string(),
tags: None,
..Default::default()
};
let backend = sozu_command::response::Backend {
cluster_id: String::from("yolo"),
Expand Down

0 comments on commit 82bb6c5

Please sign in to comment.