Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
RWDai committed Dec 11, 2023
1 parent 0d147ce commit 3aaeaac
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 15 deletions.
3 changes: 2 additions & 1 deletion admin/src/api/backend_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ pub struct BackendApi;
impl BackendApi {
/// Get Backend List
#[oai(path = "/", method = "get")]
async fn list(&self, names: Query<Option<String>>, namespace: Query<Option<String>>, session: &Session) -> TardisApiResult<Vec<SgBackendRefVo>> {
async fn list(&self, names: Query<Option<String>>, namespace: Query<Option<String>>, hosts: Query<Option<String>>, session: &Session) -> TardisApiResult<Vec<SgBackendRefVo>> {
let client_name = &super::get_instance_name(session).await?;
let result = BackendRefVoService::list(
client_name,
BackendRefQueryDto {
names: names.0.map(|n| n.split(',').map(|n| n.to_string()).collect()),
namespace: namespace.0,
hosts: hosts.0.map(|n| n.split(',').map(|n| n.to_string()).collect()),
}
.to_instance()?,
)
Expand Down
11 changes: 10 additions & 1 deletion admin/src/api/dashboard_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ impl DashboardApi {
async fn statistics(&self, session: &Session) -> TardisApiResult<Statistics> {
let client_name = &super::get_instance_name(session).await?;
TardisResp::ok(Statistics {
backend_count: BackendRefVoService::list(client_name, BackendRefQueryInst { names: None, namespace: None }).await?.len() as i64,
backend_count: BackendRefVoService::list(
client_name,
BackendRefQueryInst {
names: None,
namespace: None,
hosts: None,
},
)
.await?
.len() as i64,
gateway_count: GatewayVoService::list(
client_name,
GatewayQueryInst {
Expand Down
3 changes: 3 additions & 0 deletions admin/src/model/query_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ pub trait ToInstance<T: Instance> {
pub struct BackendRefQueryDto {
pub(crate) names: Option<Vec<String>>,
pub(crate) namespace: Option<String>,
pub(crate) hosts: Option<Vec<String>>,
}

impl ToInstance<BackendRefQueryInst> for BackendRefQueryDto {
fn to_instance(self) -> TardisResult<BackendRefQueryInst> {
Ok(BackendRefQueryInst {
names: self.names.map(|n| n.into_iter().map(fuzzy_regex).collect::<TardisResult<Vec<_>>>()).transpose()?,
namespace: self.namespace.map(fuzzy_regex).transpose()?,
hosts: self.hosts.map(|n| n.into_iter().map(fuzzy_regex).collect::<TardisResult<Vec<_>>>()).transpose()?,
})
}
}

pub struct BackendRefQueryInst {
pub(crate) names: Option<Vec<Regex>>,
pub(crate) namespace: Option<Regex>,
pub(crate) hosts: Option<Vec<Regex>>,
}

impl Instance for BackendRefQueryInst {}
Expand Down
14 changes: 8 additions & 6 deletions admin/src/model/vo_converter/http_route_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ use tardis::futures_util::future::join_all;
#[async_trait]
impl VoConv<SgHttpRoute, SgHttpRouteVo> for SgHttpRouteVo {
async fn to_model(self, client_name: &str) -> TardisResult<SgHttpRoute> {
Ok(SgHttpRoute {
let result = SgHttpRoute {
name: self.name,
gateway_name: self.gateway_name,
priority: self.priority,
hostnames: self.hostnames,
filters: SgFilterVoConv::ids_to_filter(client_name, self.filters).await?,
rules: if self.rules.is_empty() {
rules: if !self.rules.is_empty() {
Some(SgHttpRouteRuleVo::to_vec_model(client_name, self.rules).await?)
} else {
None
},
})
};
Ok(result)
}

async fn from_model(model: SgHttpRoute) -> TardisResult<SgHttpRouteVo> {
Expand Down Expand Up @@ -55,7 +56,7 @@ struct SgBackendRefVoConv;

impl SgBackendRefVoConv {
async fn ids_to_backends(client_name: &str, ids: Vec<String>) -> TardisResult<Option<Vec<SgBackendRef>>> {
Ok(if ids.is_empty() {
Ok(if !ids.is_empty() {
Some(
join_all(
BackendRefVoService::list(
Expand Down Expand Up @@ -84,12 +85,13 @@ impl SgBackendRefVoConv {
#[async_trait]
impl VoConv<SgHttpRouteRule, SgHttpRouteRuleVo> for SgHttpRouteRuleVo {
async fn to_model(self, client_name: &str) -> TardisResult<SgHttpRouteRule> {
Ok(SgHttpRouteRule {
let result = SgHttpRouteRule {
matches: self.matches,
filters: SgFilterVoConv::ids_to_filter(client_name, self.filters).await?,
backends: SgBackendRefVoConv::ids_to_backends(client_name, self.backends).await?,
timeout_ms: self.timeout_ms,
})
};
Ok(result)
}

async fn from_model(model: SgHttpRouteRule) -> TardisResult<SgHttpRouteRuleVo> {
Expand Down
4 changes: 2 additions & 2 deletions admin/src/model/vo_converter/plugin_vo_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub struct SgFilterVoConv {}
impl SgFilterVoConv {
pub(crate) async fn ids_to_filter(client_name: &str, filters: Vec<String>) -> TardisResult<Option<Vec<SgRouteFilter>>> {
Ok(if filters.is_empty() {
None
} else {
Some(
join_all(
PluginVoService::list(
Expand All @@ -31,8 +33,6 @@ impl SgFilterVoConv {
.into_iter()
.collect::<TardisResult<Vec<_>>>()?,
)
} else {
None
})
}

Expand Down
6 changes: 4 additions & 2 deletions admin/src/service/backend_ref_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl BackendRefVoService {
.into_values()
.filter(|b|
if let Some(q_names) = &query.names {
q_names.iter().any(|q| q.is_match(&b.name_or_host))
q_names.iter().any(|q| q.is_match(&b.id))
} else {
true
} &&
Expand All @@ -27,7 +27,9 @@ impl BackendRefVoService {
else { false }
} else {
true
}
} && query.hosts.as_ref().map_or(true, |hosts| {
hosts.iter().any(|host| host.is_match(&b.name_or_host))
})
)
.collect())
}
Expand Down
15 changes: 15 additions & 0 deletions admin/src/service/route_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use kernel_common::{
};
use kube::api::{DeleteParams, PostParams};
use kube::{Api, ResourceExt};
use tardis::basic::error::TardisError;
use tardis::basic::result::TardisResult;
use tardis::TardisFuns;

Expand Down Expand Up @@ -44,6 +45,7 @@ impl HttpRouteVoService {
}

pub(crate) async fn add(client_name: &str, mut add: SgHttpRouteVo) -> TardisResult<SgHttpRouteVo> {
check_param(&add)?;
let is_kube = SpacegateManageService::client_is_kube(client_name).await?;
if is_kube {
let (namespace, raw_nmae) = parse_k8s_unique_or_default(&add.get_unique_name());
Expand All @@ -69,7 +71,9 @@ impl HttpRouteVoService {
}
Self::add_vo(client_name, add).await
}

pub(crate) async fn update(client_name: &str, update: SgHttpRouteVo) -> TardisResult<SgHttpRouteVo> {
check_param(&update)?;
let update_un = &update.get_unique_name();

let update_sg_httproute = update.clone().to_model(client_name).await?;
Expand Down Expand Up @@ -128,3 +132,14 @@ impl HttpRouteVoService {
))
}
}

#[inline]
fn check_param(param: &SgHttpRouteVo) -> TardisResult<()> {
if param.gateway_name.is_empty() {
return Err(TardisError::bad_request("[Admin] gateway_name is empty", ""));
}
if param.name.is_empty() {
return Err(TardisError::bad_request("[Admin] name is empty", ""));
}
Ok(())
}
10 changes: 7 additions & 3 deletions kernel-common/src/converter/http_route_k8s_conv.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::constants::{self, BANCKEND_KIND_EXTERNAL, BANCKEND_KIND_EXTERNAL_HTTP, BANCKEND_KIND_EXTERNAL_HTTPS};
use crate::converter::plugin_k8s_conv::SgSingeFilter;
use crate::helper::k8s_helper::{get_k8s_obj_unique, parse_k8s_obj_unique};
use crate::helper::k8s_helper::{format_k8s_obj_unique, get_k8s_obj_unique, parse_k8s_obj_unique};
use crate::inner_model::gateway::SgProtocol;
use crate::inner_model::http_route::{
SgBackendRef, SgHttpHeaderMatch, SgHttpHeaderMatchType, SgHttpPathMatch, SgHttpPathMatchType, SgHttpQueryMatch, SgHttpQueryMatchType, SgHttpRoute, SgHttpRouteMatch,
Expand Down Expand Up @@ -132,10 +132,14 @@ impl SgHttpRoute {
} else {
constants::RAW_HTTP_ROUTE_KIND_SPACEROUTE
};
let priority=httproute.annotations().get(crate::constants::ANNOTATION_RESOURCE_PRIORITY).and_then(|a| a.parse::<i64>().ok()).unwrap_or(0);
let priority = httproute.annotations().get(crate::constants::ANNOTATION_RESOURCE_PRIORITY).and_then(|a| a.parse::<i64>().ok()).unwrap_or(0);
let gateway_refs = httproute.spec.inner.parent_refs.clone().unwrap_or_default();
Ok(SgHttpRoute {
name: get_k8s_obj_unique(&httproute),
gateway_name: httproute.spec.inner.parent_refs.clone().unwrap_or_default().get(0).map(|x| x.name.clone()).unwrap_or_default(),
gateway_name: format_k8s_obj_unique(
gateway_refs.get(0).and_then(|x| x.namespace.clone()).as_ref(),
&gateway_refs.get(0).map(|x| x.name.clone()).unwrap_or_default(),
),
hostnames: httproute.spec.hostnames.clone(),
filters: SgRouteFilter::from_crd_filters(client_name, kind, &httproute.metadata.name, &httproute.metadata.namespace).await?,
rules: httproute.spec.rules.map(|r_vec| r_vec.into_iter().map(|r| SgHttpRouteRule::from_kube_httproute(r)).collect::<TardisResult<Vec<_>>>()).transpose()?,
Expand Down
2 changes: 2 additions & 0 deletions kernel-common/src/inner_model/http_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct SgHttpRoute {
pub name: String,
/// Associated gateway name.
pub gateway_name: String,
/// Priority is used to sort HTTPRoutes based on priority. Routes with higher priority are tried first.
pub priority: i64,
/// Hostnames defines a set of hostname that should match against the HTTP Host header to select a HTTPRoute to process the request.
pub hostnames: Option<Vec<String>>,
/// Filters define the filters that are applied to requests that match this hostnames.
Expand Down

0 comments on commit 3aaeaac

Please sign in to comment.