Skip to content

Commit

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

use crate::{
Expand Down Expand Up @@ -406,11 +406,11 @@ impl CommandManager {
pub fn activate_listener(
&mut self,
address: String,
proxy: ListenerType,
listener_type: ListenerType,
) -> anyhow::Result<()> {
self.order_request(Request::ActivateListener(ActivateListener {
address: address.parse().with_context(|| "wrong socket address")?,
proxy,
proxy: listener_type.into(),
from_scm: false,
}))
}
Expand Down
6 changes: 6 additions & 0 deletions command/src/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ message TcpListenerConfig {
required bool active = 7 [default = false];
}

message ActivateListener {
required string address = 1;
required ListenerType proxy = 2;
required bool from_scm = 3;
}

message RemoveListener {
required string address = 1;
required ListenerType proxy = 2;
Expand Down
16 changes: 8 additions & 8 deletions command/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ use toml;
use crate::{
certificate::split_certificate_chain,
proto::command::{
AddBackend, AddCertificate, CertificateAndKey, Cluster, HttpListenerConfig,
HttpsListenerConfig, ListenerType, LoadBalancingAlgorithms, LoadBalancingParams,
LoadMetric, PathRule, ProxyProtocolConfig, RequestHttpFrontend, RequestTcpFrontend,
RulePosition, TcpListenerConfig, TlsVersion,
ActivateListener, AddBackend, AddCertificate, CertificateAndKey, Cluster,
HttpListenerConfig, HttpsListenerConfig, ListenerType, LoadBalancingAlgorithms,
LoadBalancingParams, LoadMetric, PathRule, ProxyProtocolConfig, RequestHttpFrontend,
RequestTcpFrontend, RulePosition, TcpListenerConfig, TlsVersion,
},
request::{ActivateListener, Request, WorkerRequest},
request::{Request, WorkerRequest},
};

/// [`DEFAULT_RUSTLS_CIPHER_LIST`] provides all supported cipher suites exported by Rustls TLS
Expand Down Expand Up @@ -1360,7 +1360,7 @@ impl Config {
id: format!("CONFIG-{count}"),
content: Request::ActivateListener(ActivateListener {
address: listener.address.clone(),
proxy: ListenerType::Http,
proxy: ListenerType::Http.into(),
from_scm: false,
}),
});
Expand All @@ -1372,7 +1372,7 @@ impl Config {
id: format!("CONFIG-{count}"),
content: Request::ActivateListener(ActivateListener {
address: listener.address.clone(),
proxy: ListenerType::Https,
proxy: ListenerType::Https.into(),
from_scm: false,
}),
});
Expand All @@ -1384,7 +1384,7 @@ impl Config {
id: format!("CONFIG-{count}"),
content: Request::ActivateListener(ActivateListener {
address: listener.address.clone(),
proxy: ListenerType::Tcp,
proxy: ListenerType::Tcp.into(),
from_scm: false,
}),
});
Expand Down
9 changes: 1 addition & 8 deletions command/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anyhow::Context;
use crate::{
certificate::Fingerprint,
proto::command::{
AddBackend, AddCertificate, Cluster, FrontendFilters, HttpListenerConfig,
ActivateListener, AddBackend, AddCertificate, Cluster, FrontendFilters, HttpListenerConfig,
HttpsListenerConfig, ListenerType, LoadBalancingAlgorithms, MetricsConfiguration,
PathRuleKind, QueryClusterByDomain, QueryMetricsOptions, RemoveBackend, RemoveCertificate,
RemoveListener, ReplaceCertificate, RequestHttpFrontend, RequestTcpFrontend, RulePosition,
Expand Down Expand Up @@ -212,13 +212,6 @@ pub fn default_sticky_name() -> String {
String::from("SOZUBALANCEID")
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ActivateListener {
pub address: String,
pub proxy: ListenerType,
pub from_scm: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DeactivateListener {
pub address: String,
Expand Down
55 changes: 28 additions & 27 deletions command/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use anyhow::{bail, Context};
use crate::{
certificate::{calculate_fingerprint, Fingerprint},
proto::command::{
AddBackend, AddCertificate, CertificateAndKey, Cluster, HttpListenerConfig,
HttpsListenerConfig, ListenerType, PathRule, RemoveBackend, RemoveCertificate,
RemoveListener, ReplaceCertificate, RequestHttpFrontend, RequestTcpFrontend,
TcpListenerConfig,
ActivateListener, AddBackend, AddCertificate, CertificateAndKey, Cluster,
HttpListenerConfig, HttpsListenerConfig, ListenerType, PathRule, RemoveBackend,
RemoveCertificate, RemoveListener, ReplaceCertificate, RequestHttpFrontend,
RequestTcpFrontend, TcpListenerConfig,
},
request::{ActivateListener, DeactivateListener, Request},
request::{DeactivateListener, Request},
response::{Backend, ClusterInformation, HttpFrontend, TcpFrontend},
};

Expand Down Expand Up @@ -213,8 +213,8 @@ impl ConfigState {
}

fn activate_listener(&mut self, activate: &ActivateListener) -> anyhow::Result<()> {
match activate.proxy {
ListenerType::Http => {
match ListenerType::from_i32(activate.proxy) {
Some(ListenerType::Http) => {
if self
.http_listeners
.get_mut(&activate.address)
Expand All @@ -224,7 +224,7 @@ impl ConfigState {
bail!("No http listener found with address {}", activate.address)
}
}
ListenerType::Https => {
Some(ListenerType::Https) => {
if self
.https_listeners
.get_mut(&activate.address)
Expand All @@ -234,7 +234,7 @@ impl ConfigState {
bail!("No https listener found with address {}", activate.address)
}
}
ListenerType::Tcp => {
Some(ListenerType::Tcp) => {
if self
.tcp_listeners
.get_mut(&activate.address)
Expand All @@ -244,6 +244,7 @@ impl ConfigState {
bail!("No tcp listener found with address {}", activate.address)
}
}
None => bail!("Wrong variant for ListenerType on request"),
}
Ok(())
}
Expand Down Expand Up @@ -520,7 +521,7 @@ impl ConfigState {
if listener.active {
v.push(Request::ActivateListener(ActivateListener {
address: listener.address.clone(),
proxy: ListenerType::Http,
proxy: ListenerType::Http.into(),
from_scm: false,
}));
}
Expand All @@ -531,7 +532,7 @@ impl ConfigState {
if listener.active {
v.push(Request::ActivateListener(ActivateListener {
address: listener.address.clone(),
proxy: ListenerType::Https,
proxy: ListenerType::Https.into(),
from_scm: false,
}));
}
Expand All @@ -542,7 +543,7 @@ impl ConfigState {
if listener.active {
v.push(Request::ActivateListener(ActivateListener {
address: listener.address.clone(),
proxy: ListenerType::Tcp,
proxy: ListenerType::Tcp.into(),
from_scm: false,
}));
}
Expand Down Expand Up @@ -595,7 +596,7 @@ impl ConfigState {
{
v.push(Request::ActivateListener(ActivateListener {
address: front.to_string(),
proxy: ListenerType::Http,
proxy: ListenerType::Http.into(),
from_scm: false,
}));
}
Expand All @@ -608,7 +609,7 @@ impl ConfigState {
{
v.push(Request::ActivateListener(ActivateListener {
address: front.to_string(),
proxy: ListenerType::Https,
proxy: ListenerType::Https.into(),
from_scm: false,
}));
}
Expand All @@ -620,7 +621,7 @@ impl ConfigState {
{
v.push(Request::ActivateListener(ActivateListener {
address: front.to_string(),
proxy: ListenerType::Tcp,
proxy: ListenerType::Tcp.into(),
from_scm: false,
}));
}
Expand Down Expand Up @@ -670,7 +671,7 @@ impl ConfigState {
if other.tcp_listeners[*address].active {
v.push(Request::ActivateListener(ActivateListener {
address: address.to_string(),
proxy: ListenerType::Tcp,
proxy: ListenerType::Tcp.into(),
from_scm: false,
}));
}
Expand Down Expand Up @@ -699,7 +700,7 @@ impl ConfigState {
if other.http_listeners[*address].active {
v.push(Request::ActivateListener(ActivateListener {
address: address.to_string(),
proxy: ListenerType::Http,
proxy: ListenerType::Http.into(),
from_scm: false,
}));
}
Expand Down Expand Up @@ -728,7 +729,7 @@ impl ConfigState {
if other.https_listeners[*address].active {
v.push(Request::ActivateListener(ActivateListener {
address: address.to_string(),
proxy: ListenerType::Https,
proxy: ListenerType::Https.into(),
from_scm: false,
}));
}
Expand Down Expand Up @@ -760,7 +761,7 @@ impl ConfigState {
if !my_listener.active && their_listener.active {
v.push(Request::ActivateListener(ActivateListener {
address: addr.to_string(),
proxy: ListenerType::Tcp,
proxy: ListenerType::Tcp.into(),
from_scm: false,
}));
}
Expand Down Expand Up @@ -792,7 +793,7 @@ impl ConfigState {
if !my_listener.active && their_listener.active {
v.push(Request::ActivateListener(ActivateListener {
address: addr.to_string(),
proxy: ListenerType::Http,
proxy: ListenerType::Http.into(),
from_scm: false,
}));
}
Expand Down Expand Up @@ -824,7 +825,7 @@ impl ConfigState {
if !my_listener.active && their_listener.active {
v.push(Request::ActivateListener(ActivateListener {
address: addr.to_string(),
proxy: ListenerType::Https,
proxy: ListenerType::Https.into(),
from_scm: false,
}));
}
Expand Down Expand Up @@ -1001,7 +1002,7 @@ impl ConfigState {
if listener.active {
v.push(Request::ActivateListener(ActivateListener {
address: listener.address.clone(),
proxy: ListenerType::Tcp,
proxy: ListenerType::Tcp.into(),
from_scm: false,
}));
}
Expand Down Expand Up @@ -1606,7 +1607,7 @@ mod tests {
state
.dispatch(&Request::ActivateListener(ActivateListener {
address: "0.0.0.0:1234".parse().unwrap(),
proxy: ListenerType::Tcp,
proxy: ListenerType::Tcp.into(),
from_scm: false,
}))
.expect("Could not execute request");
Expand All @@ -1628,7 +1629,7 @@ mod tests {
state
.dispatch(&Request::ActivateListener(ActivateListener {
address: "0.0.0.0:8443".parse().unwrap(),
proxy: ListenerType::Https,
proxy: ListenerType::Https.into(),
from_scm: false,
}))
.expect("Could not execute request");
Expand All @@ -1653,7 +1654,7 @@ mod tests {
state2
.dispatch(&Request::ActivateListener(ActivateListener {
address: "0.0.0.0:8080".parse().unwrap(),
proxy: ListenerType::Http,
proxy: ListenerType::Http.into(),
from_scm: false,
}))
.expect("Could not execute request");
Expand All @@ -1667,7 +1668,7 @@ mod tests {
state2
.dispatch(&Request::ActivateListener(ActivateListener {
address: "0.0.0.0:8443".parse().unwrap(),
proxy: ListenerType::Https,
proxy: ListenerType::Https.into(),
from_scm: false,
}))
.expect("Could not execute request");
Expand Down Expand Up @@ -1700,7 +1701,7 @@ mod tests {
}),
Request::ActivateListener(ActivateListener {
address: "0.0.0.0:8080".parse().unwrap(),
proxy: ListenerType::Http,
proxy: ListenerType::Http.into(),
from_scm: false,
}),
Request::RemoveListener(RemoveListener {
Expand Down
6 changes: 3 additions & 3 deletions e2e/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::{io::stdin, net::SocketAddr};

use sozu_command_lib::{
config::{Config, ListenerBuilder},
proto::command::ListenerType,
request::{ActivateListener, Request},
proto::command::{ActivateListener, ListenerType},
request::Request,
scm_socket::Listeners,
state::ConfigState,
};
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn setup_test<S: Into<String>>(
));
worker.send_proxy_request(Request::ActivateListener(ActivateListener {
address: front_address.to_string(),
proxy: ListenerType::Http,
proxy: ListenerType::Http.into(),
from_scm: false,
}));
worker.send_proxy_request(Request::AddCluster(Worker::default_cluster("cluster_0")));
Expand Down
11 changes: 6 additions & 5 deletions e2e/src/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use sozu_command_lib::{
info,
logging::{Logger, LoggerBackend},
proto::command::{
AddCertificate, CertificateAndKey, ListenerType, RemoveBackend, RequestHttpFrontend,
ActivateListener, AddCertificate, CertificateAndKey, ListenerType, RemoveBackend,
RequestHttpFrontend,
},
request::{ActivateListener, Request},
request::Request,
state::ConfigState,
};

Expand Down Expand Up @@ -289,7 +290,7 @@ pub fn try_issue_810_panic(part2: bool) -> State {
));
worker.send_proxy_request(Request::ActivateListener(ActivateListener {
address: front_address.to_string(),
proxy: ListenerType::Tcp,
proxy: ListenerType::Tcp.into(),
from_scm: false,
}));
worker.send_proxy_request(Request::AddCluster(Worker::default_cluster("cluster_0")));
Expand Down Expand Up @@ -355,7 +356,7 @@ pub fn try_tls_endpoint() -> State {

worker.send_proxy_request(Request::ActivateListener(ActivateListener {
address: front_address.to_string(),
proxy: ListenerType::Https,
proxy: ListenerType::Https.into(),
from_scm: false,
}));

Expand Down Expand Up @@ -634,7 +635,7 @@ fn try_http_behaviors() -> State {
));
worker.send_proxy_request(Request::ActivateListener(ActivateListener {
address: front_address.to_string(),
proxy: ListenerType::Http,
proxy: ListenerType::Http.into(),
from_scm: false,
}));
worker.read_to_last();
Expand Down

0 comments on commit c2df4b0

Please sign in to comment.