Skip to content

Commit

Permalink
[refactor] Renaming RateLimitPolicy to Policy
Browse files Browse the repository at this point in the history
* Missing configuration change, TODO alongside a PR to Kuadrant Operator

Signed-off-by: dd di cesare <[email protected]>
  • Loading branch information
didierofrivia committed Aug 7, 2024
1 parent ea446f5 commit 16405b6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
29 changes: 15 additions & 14 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,15 +485,15 @@ pub struct Rule {

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RateLimitPolicy {
pub struct Policy {
pub name: String,
pub domain: String,
pub service: String,
pub hostnames: Vec<String>,
pub rules: Vec<Rule>,
}

impl RateLimitPolicy {
impl Policy {
#[cfg(test)]
pub fn new(
name: String,
Expand All @@ -502,7 +502,7 @@ impl RateLimitPolicy {
hostnames: Vec<String>,
rules: Vec<Rule>,
) -> Self {
RateLimitPolicy {
Policy {
name,
domain,
service,
Expand Down Expand Up @@ -533,7 +533,7 @@ impl TryFrom<PluginConfiguration> for FilterConfig {
fn try_from(config: PluginConfiguration) -> Result<Self, Self::Error> {
let mut index = PolicyIndex::new();

for rlp in config.rate_limit_policies.iter() {
for rlp in config.policies.iter() {
for rule in &rlp.rules {
for datum in &rule.data {
let result = datum.item.compile();
Expand Down Expand Up @@ -572,7 +572,8 @@ pub enum FailureMode {
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PluginConfiguration {
pub rate_limit_policies: Vec<RateLimitPolicy>,
#[serde(rename = "rateLimitPolicies")]
pub policies: Vec<Policy>,
// Deny/Allow request when faced with an irrecoverable failure.
pub failure_mode: FailureMode,
}
Expand Down Expand Up @@ -635,9 +636,9 @@ mod test {
assert!(res.is_ok());

let filter_config = res.unwrap();
assert_eq!(filter_config.rate_limit_policies.len(), 1);
assert_eq!(filter_config.policies.len(), 1);

let rules = &filter_config.rate_limit_policies[0].rules;
let rules = &filter_config.policies[0].rules;
assert_eq!(rules.len(), 1);

let conditions = &rules[0].conditions;
Expand Down Expand Up @@ -689,7 +690,7 @@ mod test {
assert!(res.is_ok());

let filter_config = res.unwrap();
assert_eq!(filter_config.rate_limit_policies.len(), 0);
assert_eq!(filter_config.policies.len(), 0);
}

#[test]
Expand Down Expand Up @@ -722,9 +723,9 @@ mod test {
assert!(res.is_ok());

let filter_config = res.unwrap();
assert_eq!(filter_config.rate_limit_policies.len(), 1);
assert_eq!(filter_config.policies.len(), 1);

let rules = &filter_config.rate_limit_policies[0].rules;
let rules = &filter_config.policies[0].rules;
assert_eq!(rules.len(), 1);

let data_items = &rules[0].data;
Expand Down Expand Up @@ -794,9 +795,9 @@ mod test {
assert!(res.is_ok());

let filter_config = res.unwrap();
assert_eq!(filter_config.rate_limit_policies.len(), 1);
assert_eq!(filter_config.policies.len(), 1);

let rules = &filter_config.rate_limit_policies[0].rules;
let rules = &filter_config.policies[0].rules;
assert_eq!(rules.len(), 1);

let conditions = &rules[0].conditions;
Expand Down Expand Up @@ -855,9 +856,9 @@ mod test {
assert!(res.is_ok());

let filter_config = res.unwrap();
assert_eq!(filter_config.rate_limit_policies.len(), 1);
assert_eq!(filter_config.policies.len(), 1);

let rules = &filter_config.rate_limit_policies[0].rules;
let rules = &filter_config.policies[0].rules;
assert_eq!(rules.len(), 1);

let conditions = &rules[0].conditions;
Expand Down
10 changes: 3 additions & 7 deletions src/filter/http_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::configuration::{
Condition, DataItem, DataType, FailureMode, FilterConfig, PatternExpression, RateLimitPolicy,
Rule,
Condition, DataItem, DataType, FailureMode, FilterConfig, PatternExpression, Policy, Rule,
};
use crate::envoy::{
RateLimitDescriptor, RateLimitDescriptor_Entry, RateLimitRequest, RateLimitResponse,
Expand Down Expand Up @@ -59,7 +58,7 @@ impl Filter {
}
}

fn process_rate_limit_policy(&self, rlp: &RateLimitPolicy) -> Action {
fn process_rate_limit_policy(&self, rlp: &Policy) -> Action {
let descriptors = self.build_descriptors(rlp);
if descriptors.is_empty() {
debug!(
Expand Down Expand Up @@ -107,10 +106,7 @@ impl Filter {
}
}

fn build_descriptors(
&self,
rlp: &RateLimitPolicy,
) -> protobuf::RepeatedField<RateLimitDescriptor> {
fn build_descriptors(&self, rlp: &Policy) -> protobuf::RepeatedField<RateLimitDescriptor> {
rlp.rules
.iter()
.filter(|rule: &&Rule| self.filter_rule_by_conditions(&rule.conditions))
Expand Down
14 changes: 7 additions & 7 deletions src/policy_index.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use radix_trie::Trie;

use crate::configuration::RateLimitPolicy;
use crate::configuration::Policy;

pub struct PolicyIndex {
raw_tree: Trie<String, RateLimitPolicy>,
raw_tree: Trie<String, Policy>,
}

impl PolicyIndex {
Expand All @@ -13,12 +13,12 @@ impl PolicyIndex {
}
}

pub fn insert(&mut self, subdomain: &str, policy: RateLimitPolicy) {
pub fn insert(&mut self, subdomain: &str, policy: Policy) {
let rev = Self::reverse_subdomain(subdomain);
self.raw_tree.insert(rev, policy);
}

pub fn get_longest_match_policy(&self, subdomain: &str) -> Option<&RateLimitPolicy> {
pub fn get_longest_match_policy(&self, subdomain: &str) -> Option<&Policy> {
let rev = Self::reverse_subdomain(subdomain);
self.raw_tree.get_ancestor_value(&rev)
}
Expand All @@ -37,11 +37,11 @@ impl PolicyIndex {

#[cfg(test)]
mod tests {
use crate::configuration::RateLimitPolicy;
use crate::configuration::Policy;
use crate::policy_index::PolicyIndex;

fn build_ratelimit_policy(name: &str) -> RateLimitPolicy {
RateLimitPolicy::new(
fn build_ratelimit_policy(name: &str) -> Policy {
Policy::new(
name.to_owned(),
"".to_owned(),
"".to_owned(),
Expand Down

0 comments on commit 16405b6

Please sign in to comment.