Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
RWDai committed Nov 14, 2023
1 parent a6af2f3 commit bee1a1b
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 2 deletions.
1 change: 1 addition & 0 deletions admin/src/model/vo_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use tardis::async_trait::async_trait;
use tardis::basic::result::TardisResult;

pub mod gateway_vo_conv;
pub mod http_route_conv;
pub mod plugin_vo_conv;

#[async_trait]
Expand Down
16 changes: 16 additions & 0 deletions admin/src/model/vo_converter/http_route_conv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::model::vo::http_route_vo::SgHttpRouteVo;
use crate::model::vo_converter::VoConv;
use kernel_common::inner_model::http_route::SgHttpRoute;
use tardis::async_trait::async_trait;
use tardis::basic::result::TardisResult;

#[async_trait]
impl VoConv<SgHttpRoute, SgHttpRouteVo> for SgHttpRouteVo {
async fn to_model(self) -> TardisResult<SgHttpRoute> {
todo!()
}

async fn from_model(model: SgHttpRoute) -> TardisResult<SgHttpRouteVo> {
todo!()
}
}
2 changes: 1 addition & 1 deletion admin/src/service/gateway_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl GatewayVoService {
let (namespace, raw_nmae) = parse_k8s_unique_or_default(&add.get_unique_name());
add.name = format_k8s_obj_unique(Some(&namespace), &raw_nmae);
}
let mut add_model = add.clone().to_model().await?;
let add_model = add.clone().to_model().await?;
#[cfg(feature = "k8s")]
{
let (namespace, _) = parse_k8s_unique_or_default(&add.get_unique_name());
Expand Down
6 changes: 6 additions & 0 deletions admin/src/service/route_service.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::model::query_dto::HttpRouteQueryInst;
use crate::model::vo::http_route_vo::SgHttpRouteVo;
use crate::model::vo::Vo;
use crate::model::vo_converter::VoConv;
use crate::service::base_service::VoBaseService;
use kernel_common::helper::k8s_helper::{format_k8s_obj_unique, parse_k8s_unique_or_default};
use tardis::basic::result::TardisResult;
Expand Down Expand Up @@ -39,6 +40,11 @@ impl HttpRouteVoService {
let (namespace, raw_nmae) = parse_k8s_unique_or_default(&add.get_unique_name());
add.name = format_k8s_obj_unique(Some(&namespace), &raw_nmae);
}
let add_model = add.clone().to_model().await?;
#[cfg(feature = "k8s")]
{
add_model
}
Self::add_vo(add).await?;
Ok(())
}
Expand Down
209 changes: 209 additions & 0 deletions kernel-common/src/converter/http_route_k8s_conv.rs
Original file line number Diff line number Diff line change
@@ -1 +1,210 @@
use crate::converter::plugin_k8s_conv::SgSingeFilter;
use crate::helper::k8s_helper::{get_k8s_obj_unique, parse_k8s_obj_unique};
use crate::inner_model::gateway::SgGateway;
use crate::inner_model::http_route::{
SgHttpHeaderMatch, SgHttpHeaderMatchType, SgHttpPathMatch, SgHttpPathMatchType, SgHttpQueryMatch, SgHttpQueryMatchType, SgHttpRoute, SgHttpRouteMatch, SgHttpRouteRule,
};
use crate::k8s_crd::http_spaceroute::{HttpRouteRule, HttpSpaceroute, HttpSpacerouteSpec};
use crate::k8s_crd::sg_filter::{K8sSgFilterSpecFilter, K8sSgFilterSpecTargetRef};
use k8s_gateway_api::{CommonRouteSpec, Gateway, HttpHeaderMatch, HttpPathMatch, HttpQueryParamMatch, HttpRouteMatch, ParentReference};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
// use schemars::schema::SingleOrVec::Vec;
use tardis::basic::result::TardisResult;
use tardis::web::poem::EndpointExt;

impl SgHttpRoute {
pub fn to_kube_httproute(self) -> (HttpSpaceroute, Vec<SgSingeFilter>) {
let (namespace, raw_name) = parse_k8s_obj_unique(&self.name);

let (gateway_namespace, gateway_name) = parse_k8s_obj_unique(&self.gateway_name);

let httproute = HttpSpaceroute {
metadata: ObjectMeta {
labels: None,
name: Some(raw_name),
owner_references: None,
self_link: None,
..Default::default()
},
spec: HttpSpacerouteSpec {
inner: CommonRouteSpec {
parent_refs: Some(vec![ParentReference {
group: None,
kind: Some("Gateway".to_string()),
namespace: Some(gateway_namespace),
name: gateway_name,
section_name: None,
port: None,
}]),
},
hostnames: self.hostnames,
rules: self.rules.map(|r_vec| r_vec.into_iter().map(|r| r.to_kube_httproute()).collect::<Vec<_>>()),
},
status: None,
};

let mut sgfilters: Vec<SgSingeFilter> = self
.rules
.map(|r_vec| {
r_vec
.iter()
.map(|r| {
let mut route_filters_vec = r
.filters
.clone()
.map(|filters| {
filters
.into_iter()
.map(|f| {
f.to_singe_filter(K8sSgFilterSpecTargetRef {
kind: "HTTPSpaceroute".to_string(),
name: self.name.clone(),
namespace: Some(namespace.to_string()),
})
})
.collect::<Vec<_>>()
})
.unwrap_or_default();

let mut b_singe_f_vec = r
.backends
.iter()
.map(|b_vec| {
b_vec
.iter()
.map(|b| {
b.filters
.map(|b_f_vec| {
b_f_vec
.iter()
.map(|b_f| {
b_f.to_singe_filter(K8sSgFilterSpecTargetRef {
kind: "HTTPSpaceroute".to_string(),
name: self.name.clone(),
namespace: Some(namespace.to_string()),
})
})
.collect::<Vec<_>>()
})
.unwrap_or_default()
})
.flatten()
.collect::<Vec<_>>()
})
.flatten()
.collect::<Vec<_>>();

route_filters_vec.append(&mut b_singe_f_vec);
route_filters_vec
})
.flatten()
.collect::<Vec<SgSingeFilter>>()
})
.unwrap_or_default();

sgfilters.append(
&mut self
.filters
.map(|filters| {
filters
.into_iter()
.map(|f| {
f.to_singe_filter(K8sSgFilterSpecTargetRef {
kind: "HTTPSpaceroute".to_string(),
name: self.name.clone(),
namespace: Some(namespace.to_string()),
})
})
.collect::<Vec<_>>()
})
.unwrap_or_default(),
);
(httproute, sgfilters)
}

pub async fn from_kube_gateway(httproute: HttpSpaceroute) -> TardisResult<SgHttpRoute> {
//todo
Ok(SgHttpRoute {
name: get_k8s_obj_unique(&httproute),
gateway_name: "".to_string(),
hostnames: None,
filters: None,
rules: None,
})
}
}

impl SgHttpRouteRule {
//todo 不包括filters
pub(crate) fn to_kube_httproute(self) -> HttpRouteRule {
HttpRouteRule {
matches: self.matches.map(|m_vec| m_vec.into_iter().map(|m| m.to_kube_httproute()).flatten().collect::<Vec<_>>()),
filters: None,
backend_refs: self.backends,
timeout_ms: self.timeout_ms,
}
}
}

impl SgHttpRouteMatch {
pub(crate) fn to_kube_httproute(self) -> Vec<HttpRouteMatch> {
if let Some(method_vec) = self.method {
method_vec
.into_iter()
.map(|m| HttpRouteMatch {
path: self.path.clone().map(|p| p.to_kube_httproute()),
headers: self.header.clone().map(|h_vec| h_vec.into_iter().map(|h| h.to_kube_httproute()).collect::<Vec<_>>()),
query_params: self.query.clone().map(|q_vec| q_vec.into_iter().map(|q| q.to_kube_httproute()).collect::<Vec<_>>()),
method: Some(m),
})
.collect::<Vec<_>>()
} else {
vec![HttpRouteMatch {
path: self.path.map(|p| p.to_kube_httproute()),
headers: self.header.map(|h_vec| h_vec.into_iter().map(|h| h.to_kube_httproute()).collect::<Vec<_>>()),
query_params: self.query.map(|q_vec| q_vec.into_iter().map(|q| q.to_kube_httproute()).collect::<Vec<_>>()),
method: None,
}]
}
}
}

impl SgHttpPathMatch {
pub(crate) fn to_kube_httproute(self) -> HttpPathMatch {
match self.kind {
SgHttpPathMatchType::Exact => HttpPathMatch::Exact { value: self.value },
SgHttpPathMatchType::Prefix => HttpPathMatch::PathPrefix { value: self.value },
SgHttpPathMatchType::Regular => HttpPathMatch::RegularExpression { value: self.value },
}
}
}

impl SgHttpHeaderMatch {
pub(crate) fn to_kube_httproute(self) -> HttpHeaderMatch {
match self.kind {
SgHttpHeaderMatchType::Exact => HttpHeaderMatch::Exact {
name: self.name,
value: self.value,
},
SgHttpHeaderMatchType::Regular => HttpHeaderMatch::RegularExpression {
name: self.name,
value: self.value,
},
}
}
}

impl SgHttpQueryMatch {
pub(crate) fn to_kube_httproute(self) -> HttpQueryParamMatch {
match self.kind {
SgHttpQueryMatchType::Exact => HttpQueryParamMatch::Exact {
name: self.name,
value: self.value,
},
SgHttpQueryMatchType::Regular => HttpQueryParamMatch::RegularExpression {
name: self.name,
value: self.value,
},
}
}
}
18 changes: 18 additions & 0 deletions kernel-common/src/converter/plugin_k8s_conv.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
use crate::constants::DEFAULT_NAMESPACE;
use crate::inner_model::plugin_filter::SgRouteFilter;
use crate::k8s_crd::sg_filter::{K8sSgFilterSpecFilter, K8sSgFilterSpecTargetRef};
use std::hash::{Hash, Hasher};

impl SgRouteFilter {
pub fn to_singe_filter(self, target: K8sSgFilterSpecTargetRef) -> SgSingeFilter {
SgSingeFilter {
name: self.name,
namespace: target.namespace.unwrap_or(DEFAULT_NAMESPACE.to_string()),
filter: K8sSgFilterSpecFilter {
code: self.code,
name: None,
enable: true,
config: self.spec,
},
target_ref: target,
}
}
}

#[cfg(feature = "k8s")]
#[derive(Clone)]
pub struct SgSingeFilter {
Expand Down
5 changes: 5 additions & 0 deletions kernel-common/src/inner_model/http_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ use super::{gateway::SgProtocol, plugin_filter::SgRouteFilter};
/// Reference: [Kubernetes Gateway](https://gateway-api.sigs.k8s.io/references/spec/#gateway.networking.k8s.io%2fv1beta1.HTTPRoute)
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct SgHttpRoute {
/// Unique Name
///
/// In k8s mode, this name MUST be unique within a namespace.
/// format see [k8s_helper::format_k8s_obj_unique]
pub name: String,
/// Associated gateway name.
pub gateway_name: String,
/// Hostnames defines a set of hostname that should match against the HTTP Host header to select a HTTPRoute to process the request.
Expand Down
3 changes: 2 additions & 1 deletion kernel/src/config/config_by_k8s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::helpers::k8s_helper;
use crate::plugins::filters::header_modifier::SgFilterHeaderModifierKind;
use kernel_common::constants::{DEFAULT_NAMESPACE, GATEWAY_CLASS_NAME};
use kernel_common::helper;
use kernel_common::helper::k8s_helper::WarpKubeResult;
use kernel_common::helper::k8s_helper::{get_k8s_obj_unique, WarpKubeResult};
use kernel_common::inner_model::plugin_filter::SgHttpPathModifierType;
use kernel_common::inner_model::{
gateway::{SgGateway, SgListener, SgParameters, SgProtocol, SgTlsConfig, SgTlsMode},
Expand Down Expand Up @@ -669,6 +669,7 @@ async fn process_http_route_config(mut http_route_objs: Vec<HttpSpaceroute>) ->
http_route_obj.spec.inner.parent_refs.as_ref().ok_or_else(|| TardisError::format_error("[SG.Config] HttpRoute [spec.parentRefs] is required", ""))?[0].name
);
let http_route_config = SgHttpRoute {
name: get_k8s_obj_unique(&http_route_obj),
gateway_name: rel_gateway_name,
hostnames: http_route_obj.spec.hostnames.clone(),
filters: if let Some(name) = &http_route_obj.metadata.name {
Expand Down

0 comments on commit bee1a1b

Please sign in to comment.