Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core+grpc): preliminary name changes to make route types generic #12662

6 changes: 3 additions & 3 deletions policy-controller/core/src/inbound.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{
http_route::{
identity_match::IdentityMatch,
network_match::NetworkMatch,
routes::{
FailureInjectorFilter, GroupKindName, HeaderModifierFilter, HostMatch, HttpRouteMatch,
PathMatch, RequestRedirectFilter,
},
identity_match::IdentityMatch,
network_match::NetworkMatch,
};
use ahash::AHashMap as HashMap;
use anyhow::Result;
Expand Down
2 changes: 1 addition & 1 deletion policy-controller/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![deny(warnings, rust_2018_idioms)]
#![forbid(unsafe_code)]

pub mod http_route;
mod identity_match;
pub mod inbound;
mod network_match;
pub mod outbound;
pub mod routes;

pub use self::{identity_match::IdentityMatch, network_match::NetworkMatch};
pub use ipnet::{IpNet, Ipv4Net, Ipv6Net};
Expand Down
2 changes: 1 addition & 1 deletion policy-controller/core/src/outbound.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::http_route::{
use crate::routes::{
GroupKindNamespaceName, HeaderModifierFilter, HostMatch, HttpRouteMatch, RequestRedirectFilter,
};
use ahash::AHashMap as HashMap;
Expand Down
15 changes: 9 additions & 6 deletions policy-controller/grpc/src/inbound.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{http_route, workload::Workload};
use crate::{routes, workload::Workload};
use futures::prelude::*;
use linkerd2_proxy_api::{
self as api,
Expand Down Expand Up @@ -382,14 +382,17 @@ fn to_http_route(

let hosts = hostnames
.into_iter()
.map(http_route::convert_host_match)
.map(routes::convert_host_match)
.collect();

let rules = rules
.into_iter()
.map(
|HttpRouteRule { matches, filters }| proto::http_route::Rule {
matches: matches.into_iter().map(http_route::convert_match).collect(),
matches: matches
.into_iter()
.map(routes::http::convert_match)
.collect(),
filters: filters.into_iter().filter_map(convert_filter).collect(),
},
)
Expand All @@ -413,13 +416,13 @@ fn convert_filter(filter: Filter) -> Option<proto::http_route::Filter> {

let kind = match filter {
Filter::FailureInjector(f) => Some(Kind::FailureInjector(
http_route::convert_failure_injector_filter(f),
routes::http::convert_failure_injector_filter(f),
)),
Filter::RequestHeaderModifier(f) => Some(Kind::RequestHeaderModifier(
http_route::convert_request_header_modifier_filter(f),
routes::convert_request_header_modifier_filter(f),
)),
Filter::ResponseHeaderModifier(_) => None,
Filter::RequestRedirect(f) => Some(Kind::Redirect(http_route::convert_redirect_filter(f))),
Filter::RequestRedirect(f) => Some(Kind::Redirect(routes::convert_redirect_filter(f))),
};

kind.map(|kind| proto::http_route::Filter { kind: Some(kind) })
Expand Down
2 changes: 1 addition & 1 deletion policy-controller/grpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(warnings, rust_2018_idioms)]
#![forbid(unsafe_code)]

mod http_route;
mod routes;

pub mod inbound;
pub mod outbound;
Expand Down
17 changes: 10 additions & 7 deletions policy-controller/grpc/src/outbound.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{http_route, workload};
use crate::{routes, workload};
use futures::prelude::*;
use linkerd2_proxy_api::{
self as api, destination,
Expand All @@ -9,11 +9,11 @@ use linkerd2_proxy_api::{
},
};
use linkerd_policy_controller_core::{
http_route::GroupKindNamespaceName,
outbound::{
Backend, DiscoverOutboundPolicy, Filter, HttpRoute, HttpRouteRule, OutboundDiscoverTarget,
OutboundPolicy, OutboundPolicyStream,
},
routes::GroupKindNamespaceName,
};
use std::{net::SocketAddr, num::NonZeroU16, str::FromStr, sync::Arc, time};

Expand Down Expand Up @@ -316,7 +316,7 @@ fn convert_outbound_http_route(

let hosts = hostnames
.into_iter()
.map(http_route::convert_host_match)
.map(routes::convert_host_match)
.collect();

let rules = rules
Expand Down Expand Up @@ -351,7 +351,10 @@ fn convert_outbound_http_route(
)
};
outbound::http_route::Rule {
matches: matches.into_iter().map(http_route::convert_match).collect(),
matches: matches
.into_iter()
.map(routes::http::convert_match)
.collect(),
backends: Some(outbound::http_route::Distribution { kind: Some(dist) }),
filters: filters.into_iter().map(convert_filter).collect(),
request_timeout: request_timeout
Expand Down Expand Up @@ -598,12 +601,12 @@ fn convert_filter(filter: Filter) -> outbound::http_route::Filter {
outbound::http_route::Filter {
kind: Some(match filter {
Filter::RequestHeaderModifier(f) => {
Kind::RequestHeaderModifier(http_route::convert_request_header_modifier_filter(f))
Kind::RequestHeaderModifier(routes::convert_request_header_modifier_filter(f))
}
Filter::ResponseHeaderModifier(f) => {
Kind::ResponseHeaderModifier(http_route::convert_response_header_modifier_filter(f))
Kind::ResponseHeaderModifier(routes::convert_response_header_modifier_filter(f))
}
Filter::RequestRedirect(f) => Kind::Redirect(http_route::convert_redirect_filter(f)),
Filter::RequestRedirect(f) => Kind::Redirect(routes::convert_redirect_filter(f)),
}),
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use linkerd2_proxy_api::{http_route as proto, http_types};
use linkerd_policy_controller_core::http_route::{
use linkerd_policy_controller_core::routes::{
FailureInjectorFilter, HeaderMatch, HeaderModifierFilter, HostMatch, HttpRouteMatch, PathMatch,
PathModifier, QueryParamMatch, RequestRedirectFilter,
};
Expand All @@ -17,75 +17,6 @@ pub(crate) fn convert_host_match(h: HostMatch) -> proto::HostMatch {
}
}

pub(crate) fn convert_match(
HttpRouteMatch {
headers,
path,
query_params,
method,
}: HttpRouteMatch,
) -> proto::HttpRouteMatch {
let headers = headers
.into_iter()
.map(|hm| match hm {
HeaderMatch::Exact(name, value) => proto::HeaderMatch {
name: name.to_string(),
value: Some(proto::header_match::Value::Exact(value.as_bytes().to_vec())),
},
HeaderMatch::Regex(name, re) => proto::HeaderMatch {
name: name.to_string(),
value: Some(proto::header_match::Value::Regex(re.to_string())),
},
})
.collect();

let path = path.map(|path| proto::PathMatch {
kind: Some(match path {
PathMatch::Exact(path) => proto::path_match::Kind::Exact(path),
PathMatch::Prefix(prefix) => proto::path_match::Kind::Prefix(prefix),
PathMatch::Regex(regex) => proto::path_match::Kind::Regex(regex.to_string()),
}),
});

let query_params = query_params
.into_iter()
.map(|qpm| match qpm {
QueryParamMatch::Exact(name, value) => proto::QueryParamMatch {
name,
value: Some(proto::query_param_match::Value::Exact(value)),
},
QueryParamMatch::Regex(name, re) => proto::QueryParamMatch {
name,
value: Some(proto::query_param_match::Value::Regex(re.to_string())),
},
})
.collect();

proto::HttpRouteMatch {
headers,
path,
query_params,
method: method.map(Into::into),
}
}

pub(crate) fn convert_failure_injector_filter(
FailureInjectorFilter {
status,
message,
ratio,
}: FailureInjectorFilter,
) -> proto::HttpFailureInjector {
proto::HttpFailureInjector {
status: u32::from(status.as_u16()),
message,
ratio: Some(proto::Ratio {
numerator: ratio.numerator,
denominator: ratio.denominator,
}),
}
}

pub(crate) fn convert_request_header_modifier_filter(
HeaderModifierFilter { add, set, remove }: HeaderModifierFilter,
) -> proto::RequestHeaderModifier {
Expand Down Expand Up @@ -160,3 +91,78 @@ pub(crate) fn convert_redirect_filter(
status: u32::from(status.unwrap_or_default().as_u16()),
}
}

pub(crate) mod http {
use super::{
proto, FailureInjectorFilter, HeaderMatch, HttpRouteMatch, PathMatch, QueryParamMatch,
};

pub(crate) fn convert_match(
HttpRouteMatch {
headers,
path,
query_params,
method,
}: HttpRouteMatch,
) -> proto::HttpRouteMatch {
let headers = headers
.into_iter()
.map(|hm| match hm {
HeaderMatch::Exact(name, value) => proto::HeaderMatch {
name: name.to_string(),
value: Some(proto::header_match::Value::Exact(value.as_bytes().to_vec())),
},
HeaderMatch::Regex(name, re) => proto::HeaderMatch {
name: name.to_string(),
value: Some(proto::header_match::Value::Regex(re.to_string())),
},
})
.collect();

let path = path.map(|path| proto::PathMatch {
kind: Some(match path {
PathMatch::Exact(path) => proto::path_match::Kind::Exact(path),
PathMatch::Prefix(prefix) => proto::path_match::Kind::Prefix(prefix),
PathMatch::Regex(regex) => proto::path_match::Kind::Regex(regex.to_string()),
}),
});

let query_params = query_params
.into_iter()
.map(|qpm| match qpm {
QueryParamMatch::Exact(name, value) => proto::QueryParamMatch {
name,
value: Some(proto::query_param_match::Value::Exact(value)),
},
QueryParamMatch::Regex(name, re) => proto::QueryParamMatch {
name,
value: Some(proto::query_param_match::Value::Regex(re.to_string())),
},
})
.collect();

proto::HttpRouteMatch {
headers,
path,
query_params,
method: method.map(Into::into),
}
}

pub(crate) fn convert_failure_injector_filter(
FailureInjectorFilter {
status,
message,
ratio,
}: FailureInjectorFilter,
) -> proto::HttpFailureInjector {
proto::HttpFailureInjector {
status: u32::from(status.as_u16()),
message,
ratio: Some(proto::Ratio {
numerator: ratio.numerator,
denominator: ratio.denominator,
}),
}
}
}
Loading