Skip to content

Commit

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

Expand Down Expand Up @@ -160,8 +159,8 @@ impl CommandManager {
cluster_id: id,
sticky_session,
https_redirect,
proxy_protocol,
load_balancing: load_balancing_policy,
proxy_protocol: proxy_protocol.and_then(|pp| Some(pp as i32)),
load_balancing: load_balancing_policy as i32,
load_metric: None,
answer_503: None,
}))
Expand Down
5 changes: 4 additions & 1 deletion command/assets/add_cluster.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"cluster_id": "xxx",
"sticky_session": true,
"https_redirect": true,
"proxy_protocol": "EXPECT_HEADER"
"proxy_protocol": 0,
"load_balancing": 0,
"answer_503": null,
"load_metric": null
}
}
11 changes: 10 additions & 1 deletion command/src/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ enum TlsVersion {
TLS_V1_3 = 5;
}

// TODO: check that default is ROUND_ROBIN
message Cluster {
required string cluster_id = 1;
required bool sticky_session = 2;
required bool https_redirect = 3;
optional ProxyProtocolConfig proxy_protocol = 4;
required LoadBalancingAlgorithms load_balancing = 5 [default = ROUND_ROBIN];
optional string answer_503 = 6;
optional LoadMetric load_metric = 7;
}

enum LoadBalancingAlgorithms {
ROUND_ROBIN = 0;
RANDOM = 1;
Expand Down
14 changes: 7 additions & 7 deletions command/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use toml;
use crate::{
certificate::split_certificate_chain,
proto::command::{
AddCertificate, CertificateAndKey, LoadBalancingAlgorithms, LoadMetric, PathRule,
AddCertificate, CertificateAndKey, Cluster, LoadBalancingAlgorithms, LoadMetric, PathRule,
ProxyProtocolConfig, RequestHttpFrontend, RulePosition, TlsVersion,
},
request::{
ActivateListener, AddBackend, Cluster, ListenerType, LoadBalancingParams, Request,
ActivateListener, AddBackend, ListenerType, LoadBalancingParams, Request,
RequestTcpFrontend, WorkerRequest,
},
response::{HttpListenerConfig, HttpsListenerConfig, TcpListenerConfig},
Expand Down Expand Up @@ -806,9 +806,9 @@ impl HttpClusterConfig {
sticky_session: self.sticky_session,
https_redirect: self.https_redirect,
proxy_protocol: None,
load_balancing: self.load_balancing,
load_balancing: self.load_balancing as i32,
answer_503: self.answer_503.clone(),
load_metric: self.load_metric,
load_metric: self.load_metric.and_then(|s| Some(s as i32)),
})];

for frontend in &self.frontends {
Expand Down Expand Up @@ -860,9 +860,9 @@ impl TcpClusterConfig {
cluster_id: self.cluster_id.clone(),
sticky_session: false,
https_redirect: false,
proxy_protocol: self.proxy_protocol.clone(),
load_balancing: self.load_balancing,
load_metric: self.load_metric,
proxy_protocol: self.proxy_protocol.and_then(|s| Some(s as i32)),
load_balancing: self.load_balancing as i32,
load_metric: self.load_metric.and_then(|s| Some(s as i32)),
answer_503: None,
})];

Expand Down
37 changes: 5 additions & 32 deletions command/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use anyhow::Context;
use crate::{
certificate::Fingerprint,
proto::command::{
AddCertificate, FrontendFilters, LoadBalancingAlgorithms, LoadMetric, PathRuleKind,
ProxyProtocolConfig, RemoveCertificate, ReplaceCertificate, RequestHttpFrontend,
RulePosition,
AddCertificate, Cluster, FrontendFilters, LoadBalancingAlgorithms, LoadMetric,
PathRuleKind, ProxyProtocolConfig, RemoveCertificate, ReplaceCertificate,
RequestHttpFrontend, RulePosition,
},
response::{
HttpFrontend, HttpListenerConfig, HttpsListenerConfig, MessageId, TcpListenerConfig,
Expand Down Expand Up @@ -209,29 +209,6 @@ pub struct ProxyDestinations {
pub to_tcp_proxy: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Cluster {
pub cluster_id: ClusterId,
#[serde(default)]
#[serde(skip_serializing_if = "is_false")]
pub sticky_session: bool,
#[serde(default)]
#[serde(skip_serializing_if = "is_false")]
pub https_redirect: bool,
#[serde(default)]
#[serde(skip_serializing_if = "is_default")]
pub proxy_protocol: Option<ProxyProtocolConfig>,
#[serde(rename = "load_balancing")]
#[serde(default)]
#[serde(skip_serializing_if = "is_default")]
pub load_balancing: LoadBalancingAlgorithms,
#[serde(default)]
#[serde(skip_serializing_if = "is_default")]
pub answer_503: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub load_metric: Option<LoadMetric>,
}

pub fn default_sticky_name() -> String {
String::from("SOZUBALANCEID")
}
Expand Down Expand Up @@ -395,10 +372,6 @@ pub fn is_false(b: &bool) -> bool {
!*b
}

fn is_default<T: Default + PartialEq>(t: &T) -> bool {
t == &T::default()
}

#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
Expand Down Expand Up @@ -454,8 +427,8 @@ mod tests {
cluster_id: String::from("xxx"),
sticky_session: true,
https_redirect: true,
proxy_protocol: Some(ProxyProtocolConfig::ExpectHeader),
load_balancing: LoadBalancingAlgorithms::RoundRobin,
proxy_protocol: Some(ProxyProtocolConfig::ExpectHeader as i32),
load_balancing: LoadBalancingAlgorithms::RoundRobin as i32,
load_metric: None,
answer_503: None,
})
Expand Down
7 changes: 4 additions & 3 deletions command/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use std::{

use crate::{
proto::command::{
CertificateSummary, PathRule, PathRuleKind, RequestHttpFrontend, RulePosition, TlsVersion,
CertificateSummary, Cluster, PathRule, PathRuleKind, RequestHttpFrontend, RulePosition,
TlsVersion,
},
request::{
default_sticky_name, is_false, AddBackend, Cluster, LoadBalancingParams,
RequestTcpFrontend, PROTOCOL_VERSION,
default_sticky_name, is_false, AddBackend, LoadBalancingParams, RequestTcpFrontend,
PROTOCOL_VERSION,
},
state::{ClusterId, ConfigState},
};
Expand Down
12 changes: 6 additions & 6 deletions command/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use anyhow::{bail, Context};
use crate::{
certificate::{calculate_fingerprint, Fingerprint},
proto::command::{
AddCertificate, CertificateAndKey, PathRule, RemoveCertificate, ReplaceCertificate,
RequestHttpFrontend,
AddCertificate, CertificateAndKey, Cluster, PathRule, RemoveCertificate,
ReplaceCertificate, RequestHttpFrontend,
},
request::{
ActivateListener, AddBackend, Cluster, DeactivateListener, ListenerType, RemoveBackend,
ActivateListener, AddBackend, DeactivateListener, ListenerType, RemoveBackend,
RemoveListener, Request, RequestTcpFrontend,
},
response::{
Expand Down Expand Up @@ -1394,7 +1394,7 @@ mod tests {
sticky_session: true,
https_redirect: true,
proxy_protocol: None,
load_balancing: LoadBalancingAlgorithms::RoundRobin,
load_balancing: LoadBalancingAlgorithms::RoundRobin as i32,
load_metric: None,
answer_503: None,
}))
Expand Down Expand Up @@ -1448,7 +1448,7 @@ mod tests {
sticky_session: false,
https_redirect: false,
proxy_protocol: None,
load_balancing: LoadBalancingAlgorithms::RoundRobin,
load_balancing: LoadBalancingAlgorithms::RoundRobin as i32,
load_metric: None,
answer_503: None,
}))
Expand Down Expand Up @@ -1485,7 +1485,7 @@ mod tests {
sticky_session: false,
https_redirect: false,
proxy_protocol: None,
load_balancing: LoadBalancingAlgorithms::RoundRobin,
load_balancing: LoadBalancingAlgorithms::RoundRobin as i32,
load_metric: None,
answer_503: None,
}),
Expand Down
8 changes: 4 additions & 4 deletions e2e/src/sozu/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use sozu_command::{
channel::Channel,
config::{Config, ConfigBuilder, FileConfig},
logging::{Logger, LoggerBackend},
proto::command::{LoadBalancingAlgorithms, PathRule, RequestHttpFrontend, RulePosition},
request::{
AddBackend, Cluster, LoadBalancingParams, Request, RequestTcpFrontend, WorkerRequest,
proto::command::{
Cluster, LoadBalancingAlgorithms, PathRule, RequestHttpFrontend, RulePosition,
},
request::{AddBackend, LoadBalancingParams, Request, RequestTcpFrontend, WorkerRequest},
response::WorkerResponse,
scm_socket::{Listeners, ScmSocket},
state::ConfigState,
Expand Down Expand Up @@ -272,7 +272,7 @@ impl Worker {
sticky_session: false,
https_redirect: false,
proxy_protocol: None,
load_balancing: LoadBalancingAlgorithms::default(),
load_balancing: LoadBalancingAlgorithms::default() as i32,
load_metric: None,
answer_503: None,
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use time::{Duration, Instant};

use sozu_command::{
logging,
proto::command::RequestHttpFrontend,
proto::command::{Cluster, RequestHttpFrontend},
ready::Ready,
request::{Cluster, RemoveListener, Request, WorkerRequest},
request::{RemoveListener, Request, WorkerRequest},
response::{HttpFrontend, HttpListenerConfig, WorkerResponse},
scm_socket::{Listeners, ScmSocket},
};
Expand Down Expand Up @@ -1357,7 +1357,7 @@ mod tests {
answer_503: None,
cluster_id: String::from("cluster_1"),
https_redirect: true,
load_balancing: LoadBalancingAlgorithms::default(),
load_balancing: LoadBalancingAlgorithms::default() as i32,
load_metric: None,
proxy_protocol: None,
sticky_session: false,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ use sozu_command::{
config::DEFAULT_CIPHER_SUITES,
logging,
proto::command::{
AddCertificate, CertificateSummary, RemoveCertificate, ReplaceCertificate,
AddCertificate, CertificateSummary, Cluster, RemoveCertificate, ReplaceCertificate,
RequestHttpFrontend, TlsVersion,
},
ready::Ready,
request::{Cluster, RemoveListener, Request, WorkerRequest},
request::{RemoveListener, Request, WorkerRequest},
response::{HttpFrontend, HttpsListenerConfig, ResponseContent, WorkerResponse},
scm_socket::ScmSocket,
state::ClusterId,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ use sozu_command::state::ClusterId;
use time::{Duration, Instant};

use crate::sozu_command::{
proto::command::Cluster,
ready::Ready,
request::{Cluster, LoadBalancingParams, WorkerRequest},
request::{LoadBalancingParams, WorkerRequest},
response::{Event, WorkerResponse},
};

Expand Down
9 changes: 5 additions & 4 deletions lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ use slab::Slab;
use sozu_command::{
channel::Channel,
config::Config,
proto::command::{Cluster, LoadBalancingAlgorithms, LoadMetric},
ready::Ready,
request::{
ActivateListener, AddBackend, Cluster, DeactivateListener, ListenerType, RemoveBackend,
Request, WorkerRequest,
ActivateListener, AddBackend, DeactivateListener, ListenerType, RemoveBackend, Request,
WorkerRequest,
},
response::{
Event, HttpListenerConfig, HttpsListenerConfig, MessageId, ResponseContent, ResponseStatus,
Expand Down Expand Up @@ -1004,8 +1005,8 @@ impl Server {
.borrow_mut()
.set_load_balancing_policy_for_cluster(
&cluster.cluster_id,
cluster.load_balancing,
cluster.load_metric,
LoadBalancingAlgorithms::from_i32(cluster.load_balancing).unwrap_or_default(),
cluster.load_metric.and_then(|lm| LoadMetric::from_i32(lm)),
);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,9 @@ impl ProxyConfiguration for TcpProxy {
}
Request::AddCluster(cluster) => {
let config = ClusterConfiguration {
proxy_protocol: cluster.proxy_protocol,
proxy_protocol: cluster
.proxy_protocol
.and_then(|pp| ProxyProtocolConfig::from_i32(pp)),
//load_balancing: cluster.load_balancing,
};
self.configs.insert(cluster.cluster_id, config);
Expand Down

0 comments on commit 25f6018

Please sign in to comment.