Skip to content

Commit

Permalink
Bair
Browse files Browse the repository at this point in the history
  • Loading branch information
RWDai committed Oct 26, 2023
1 parent f7b2fc1 commit 4ae96b2
Show file tree
Hide file tree
Showing 20 changed files with 454 additions and 222 deletions.
2 changes: 1 addition & 1 deletion admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ readme = "./README.md"
k8s = ["kube", "k8s-openapi", "k8s-gateway-api","kernel-common/k8s"]

[dependencies]
tardis = { workspace = true ,features = ["web-server"]}
tardis = { workspace = true ,features = ["web-server","crypto"]}
serde.workspace = true
serde_json.workspace = true

Expand Down
18 changes: 9 additions & 9 deletions admin/src/api/backend_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::model::query_dto::BackendRefQueryDto;
use crate::model::vo::backend_vo::BackendRefVO;
use crate::service::backend_ref_service::BackendRefService;
use crate::model::vo::backend_vo::SgBackendRefVO;
use crate::service::backend_ref_service::BackendRefServiceVo;
use tardis::web::poem::web::Query;
use tardis::web::poem_openapi;
use tardis::web::poem_openapi::param::Path;
Expand All @@ -15,8 +15,8 @@ pub struct BackendApi;
impl BackendApi {
/// Get Backend List
#[oai(path = "/", method = "get")]
async fn list(&self, name: Query<Option<String>>, namespace: Query<Option<String>>) -> TardisApiResult<Vec<BackendRefVO>> {
let result = BackendRefService::list(
async fn list(&self, name: Query<Option<String>>, namespace: Query<Option<String>>) -> TardisApiResult<Vec<SgBackendRefVO>> {
let result = BackendRefServiceVo::list(
namespace.0.clone(),
BackendRefQueryDto {
name: name.0,
Expand All @@ -29,22 +29,22 @@ impl BackendApi {

/// Add Backend
#[oai(path = "/", method = "post")]
async fn add(&self, backend: Json<BackendRefVO>) -> TardisApiResult<Void> {
BackendRefService::add(backend.0).await?;
async fn add(&self, backend: Json<SgBackendRefVO>) -> TardisApiResult<Void> {
BackendRefServiceVo::add(backend.0).await?;
TardisResp::ok(Void {})
}

/// update Backend
#[oai(path = "/", method = "put")]
async fn update(&self, backend: Json<BackendRefVO>) -> TardisApiResult<Void> {
BackendRefService::update(backend.0).await?;
async fn update(&self, backend: Json<SgBackendRefVO>) -> TardisApiResult<Void> {
BackendRefServiceVo::update(backend.0).await?;
TardisResp::ok(Void {})
}

/// delete Backend
#[oai(path = "/:backend_id", method = "put")]
async fn delete(&self, backend_id: Path<String>) -> TardisApiResult<Void> {
BackendRefService::delete(&backend_id.0).await?;
BackendRefServiceVo::delete(&backend_id.0).await?;
TardisResp::ok(Void {})
}
}
16 changes: 6 additions & 10 deletions admin/src/api/gateway_api.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::model::query_dto::GatewayQueryDto;
use crate::model::vo::backend_vo::BackendRefVO;
use crate::model::vo::gateway_vo::SgGatewayVO;
use crate::service::gateway_service::GatewayService;
use crate::service::plugin_service::PluginService;
use kernel_common::inner_model::gateway::SgGateway;
use crate::service::gateway_service::GatewayServiceVo;
use tardis::web::poem_openapi;
use tardis::web::poem_openapi::param::Query;
use tardis::web::poem_openapi::payload::Json;
Expand All @@ -17,22 +13,22 @@ pub struct GatewayApi;
impl GatewayApi {
/// Add Gateway
#[oai(path = "/", method = "post")]
async fn add(&self, backend: Json<BackendRefVO>) -> TardisApiResult<Void> {
GatewayService::add(backend.0).await?;
async fn add(&self, add: Json<SgGatewayVO>) -> TardisApiResult<Void> {
GatewayServiceVo::add(add.0).await?;
TardisResp::ok(Void {})
}

/// Update Gateway
#[oai(path = "/", method = "put")]
async fn update(&self, backend: Json<SgGatewayVO>) -> TardisApiResult<Void> {
GatewayService::update(backend.0).await?;
GatewayServiceVo::update(backend.0).await?;
TardisResp::ok(Void {})
}

/// Delete Gateway
#[oai(path = "/", method = "delete")]
async fn delete(&self, namespace: Query<Option<String>>, name: Query<String>) -> TardisApiResult<Void> {
GatewayService::delete(namespace.0, &name.0).await?;
async fn delete(&self, name: Query<String>) -> TardisApiResult<Void> {
GatewayServiceVo::delete(&name.0).await?;
TardisResp::ok(Void {})
}
}
19 changes: 10 additions & 9 deletions admin/src/api/plugin_api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::model::query_dto::PluginQueryDto;
use crate::model::vo::backend_vo::BackendRefVO;
use crate::service::backend_ref_service::BackendRefService;
use crate::service::plugin_service::PluginService;
use crate::model::vo::backend_vo::SgBackendRefVO;
use crate::model::vo::plugin_vo::SgFilterVO;
use crate::service::backend_ref_service::BackendRefServiceVo;
use crate::service::plugin_service::PluginServiceVo;
use tardis::web::poem_openapi;
use tardis::web::poem_openapi::param::{Path, Query};
use tardis::web::poem_openapi::payload::Json;
Expand All @@ -15,7 +16,7 @@ impl PluginApi {
/// Get Plugin List
#[oai(path = "/", method = "get")]
async fn list(&self, ids: Query<Option<String>>, name: Query<Option<String>>, namespace: Query<Option<String>>, code: Query<Option<String>>) -> TardisApiResult<Void> {
let _ = PluginService::list(PluginQueryDto {
let _ = PluginServiceVo::list(PluginQueryDto {
ids: ids.0.map(|s| s.split(',').map(|s| s.to_string()).collect::<Vec<String>>()),
name: name.0,
namespace: namespace.0,
Expand All @@ -28,22 +29,22 @@ impl PluginApi {

/// Add Plugin
#[oai(path = "/", method = "post")]
async fn add(&self, backend: Json<BackendRefVO>) -> TardisApiResult<Void> {
PluginService::add(backend.0).await?;
async fn add(&self, add: Json<SgFilterVO>) -> TardisApiResult<Void> {
PluginServiceVo::add(add.0).await?;
TardisResp::ok(Void {})
}

/// Update Plugin
#[oai(path = "/", method = "put")]
async fn update(&self, backend: Json<BackendRefVO>) -> TardisApiResult<Void> {
PluginService::update(backend.0).await?;
async fn update(&self, update: Json<SgFilterVO>) -> TardisApiResult<Void> {
PluginServiceVo::update(update.0).await?;
TardisResp::ok(Void {})
}

/// Delete Plugin
#[oai(path = "/:backend_id", method = "put")]
async fn delete(&self, backend_id: Path<String>) -> TardisApiResult<Void> {
PluginService::delete(&backend_id.0).await?;
PluginServiceVo::delete(&backend_id.0).await?;
TardisResp::ok(Void {})
}
}
36 changes: 18 additions & 18 deletions admin/src/api/tls_config_api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::model::query_dto::PluginQueryDto;
use crate::model::vo::backend_vo::BackendRefVO;
use crate::service::plugin_service::PluginService;
use crate::model::vo::gateway_vo::SgTlsConfigVO;
use crate::service::tls_config_service::TlsConfigServiceVo;
use tardis::web::poem_openapi;
use tardis::web::poem_openapi::param::Path;
use tardis::web::poem_openapi::param::{Path, Query};
use tardis::web::poem_openapi::payload::Json;
use tardis::web::web_resp::{TardisApiResult, TardisResp, Void};

#[derive(Clone, Default)]
Expand All @@ -14,35 +14,35 @@ impl TlsConfigApi {
/// Get TlsConfig List
#[oai(path = "/", method = "get")]
async fn list(&self, ids: Query<Option<String>>, name: Query<Option<String>>, namespace: Query<Option<String>>, code: Query<Option<String>>) -> TardisApiResult<Void> {
let _ = PluginService::list(PluginQueryDto {
ids: ids.0.map(|s| s.split(',').map(|s| s.to_string()).collect::<Vec<String>>()),
name: name.0,
namespace: namespace.0,
code: code.0,
target: None,
})
.await;
// let _ = PluginService::list(PluginQueryDto {
// ids: ids.0.map(|s| s.split(',').map(|s| s.to_string()).collect::<Vec<String>>()),
// name: name.0,
// namespace: namespace.0,
// code: code.0,
// target: None,
// })
// .await;
TardisResp::ok(Void {})
}

/// Add TlsConfig
#[oai(path = "/", method = "post")]
async fn add(&self, backend: Json<BackendRefVO>) -> TardisApiResult<Void> {
PluginService::add(backend.0).await?;
async fn add(&self, tls_config: Json<SgTlsConfigVO>) -> TardisApiResult<Void> {
TlsConfigServiceVo::add(tls_config.0).await?;
TardisResp::ok(Void {})
}

/// Update TlsConfig
#[oai(path = "/", method = "put")]
async fn update(&self, backend: Json<BackendRefVO>) -> TardisApiResult<Void> {
PluginService::update(backend.0).await?;
async fn update(&self, tls_config: Json<SgTlsConfigVO>) -> TardisApiResult<Void> {
TlsConfigServiceVo::update(tls_config.0).await?;
TardisResp::ok(Void {})
}

/// Delete TlsConfig
#[oai(path = "/:backend_id", method = "put")]
async fn delete(&self, backend_id: Path<String>) -> TardisApiResult<Void> {
PluginService::delete(&backend_id.0).await?;
async fn delete(&self, tls_config_id: Path<String>) -> TardisApiResult<Void> {
TlsConfigServiceVo::delete(&tls_config_id.0).await?;
TardisResp::ok(Void {})
}
}
1 change: 1 addition & 0 deletions admin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod service;

#[tokio::main]
async fn main() -> TardisResult<()> {
// todo 根据现有的k8s资源初始化成VO
TardisFuns::init(Some("config")).await?;
let web_server = TardisFuns::web_server();
initializer::init(&web_server).await?;
Expand Down
1 change: 1 addition & 0 deletions admin/src/model/query_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl ToFields for GatewayQueryDto {
}
}

#[derive(Default)]
pub struct PluginQueryDto {
pub ids: Option<Vec<String>>,
pub name: Option<String>,
Expand Down
4 changes: 2 additions & 2 deletions admin/src/model/vo/backend_vo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tardis::web::poem_openapi;

/// BackendRef defines how a HTTPRoute should forward an HTTP request.
#[derive(Default, Debug, Serialize, Deserialize, Clone, poem_openapi::Object)]
pub struct BackendRefVO {
pub struct SgBackendRefVO {
/// unique by id
pub id: String,
/// Name is the kubernetes service name OR url host.
Expand All @@ -28,7 +28,7 @@ pub struct BackendRefVO {
pub filters: Option<Vec<String>>,
}

impl Vo for BackendRefVO {
impl Vo for SgBackendRefVO {
fn get_vo_type() -> String {
constants::BACKEND_REF_TYPE.to_string()
}
Expand Down
4 changes: 2 additions & 2 deletions admin/src/model/vo/http_route_vo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tardis::web::poem_openapi;
///
/// Reference: [Kubernetes Gateway](https://gateway-api.sigs.k8s.io/references/spec/#gateway.networking.k8s.io%2fv1beta1.HTTPRoute)
#[derive(Default, Debug, Serialize, Deserialize, Clone, poem_openapi::Object)]
pub struct SgHttpRoute {
pub struct SgHttpRouteVo {
/// Name of the HttpRoute. Global Unique.
///
/// In k8s mode, this name MUST be unique within a namespace.
Expand All @@ -29,7 +29,7 @@ pub struct SgHttpRouteRuleVO {
pub matches: Option<Vec<SgHttpRouteMatch>>,
/// [crate::model::vo::plugin_vo::SgFilterVO]'s id
pub filters: Option<Vec<String>>,
/// [crate::model::vo::backend_vo::BackendRefVO]'s id
/// [crate::model::vo::backend_vo::SgBackendRefVO]'s id
pub backends: Option<Vec<String>>,
/// Timeout define the timeout for requests that match this rule.
pub timeout_ms: Option<u64>,
Expand Down
13 changes: 13 additions & 0 deletions admin/src/model/vo_converter.rs
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
use tardis::async_trait::async_trait;
use tardis::basic::result::TardisResult;

pub mod gateway_vo_conv;
pub mod plugin_vo_conv;

#[async_trait]
pub trait VoConv<M, S>
where
S: VoConv<M, S>,
{
async fn to_model(self) -> TardisResult<M>;
async fn from_model(model: M) -> TardisResult<S>;
}
92 changes: 92 additions & 0 deletions admin/src/model/vo_converter/gateway_vo_conv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use crate::model::query_dto::PluginQueryDto;
use crate::model::vo::gateway_vo::{SgGatewayVO, SgListenerVO, SgTlsConfigVO};
use crate::model::vo_converter::VoConv;
use crate::service::base_service::VoBaseService;
use crate::service::plugin_service::PluginServiceVo;
use crate::service::tls_config_service::TlsConfigServiceVo;
use kernel_common::inner_model::gateway::{SgGateway, SgListener, SgTlsConfig};
use kernel_common::inner_model::plugin_filter::SgRouteFilter;
use tardis::async_trait::async_trait;
use tardis::basic::result::TardisResult;
use tardis::futures_util::future::join_all;
use tardis::TardisFuns;

#[async_trait]
impl VoConv<SgGateway, SgGatewayVO> for SgGatewayVO {
async fn to_model(self) -> TardisResult<SgGateway> {
let filters = if let Some(filter_strs) = self.filters {
Some(
join_all(
PluginServiceVo::list(PluginQueryDto {
ids: Some(filter_strs),
..Default::default()
})
.await?
.into_iter()
.map(|f| f.to_model())
.collect::<Vec<_>>(),
)
.await
.into_iter()
.collect::<TardisResult<Vec<_>>>()?,
)
} else {
None
};
Ok(SgGateway {
name: self.name,
parameters: self.parameters,
listeners: join_all(self.listeners.into_iter().map(|l| l.to_model()).collect::<Vec<_>>()).await.into_iter().collect::<TardisResult<Vec<_>>>()?,
filters,
})
}

async fn from_model(model: SgGateway) -> TardisResult<SgGatewayVO> {
todo!()
}
}

#[async_trait]
impl VoConv<SgListener, SgListenerVO> for SgListenerVO {
async fn to_model(self) -> TardisResult<SgListener> {
let tls = if let Some(tls_id) = self.tls {
Some(TlsConfigServiceVo::get_by_id(&tls_id).await?.to_model().await?)
} else {
None
};

Ok(SgListener {
name: self.name,
ip: self.ip,
port: self.port,
protocol: self.protocol,
tls,
hostname: self.hostname,
})
}

async fn from_model(model: SgListener) -> TardisResult<SgListenerVO> {
todo!()
}
}

#[async_trait]
impl VoConv<SgTlsConfig, SgTlsConfigVO> for SgTlsConfigVO {
async fn to_model(self) -> TardisResult<SgTlsConfig> {
Ok(SgTlsConfig {
mode: self.mode,
key: self.key,
cert: self.cert,
})
}

async fn from_model(model: SgTlsConfig) -> TardisResult<SgTlsConfigVO> {
Ok(SgTlsConfigVO {
id: TardisFuns::crypto.digest.md5(&format!("{}{}{}", model.mode.clone().to_kube_tls_mode_type().to_string(), model.key, model.cert))?,
mode: model.mode,
key: model.key,
cert: model.cert,
ref_ids: None,
})
}
}
20 changes: 20 additions & 0 deletions admin/src/model/vo_converter/plugin_vo_conv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::model::vo::plugin_vo::SgFilterVO;
use crate::model::vo_converter::VoConv;
use kernel_common::inner_model::plugin_filter::SgRouteFilter;
use tardis::async_trait::async_trait;
use tardis::basic::result::TardisResult;

#[async_trait]
impl VoConv<SgRouteFilter, SgFilterVO> for SgFilterVO {
async fn to_model(self) -> TardisResult<SgRouteFilter> {
Ok(SgRouteFilter {
code: self.code,
name: self.name,
spec: self.spec,
})
}

async fn from_model(model: SgRouteFilter) -> TardisResult<SgFilterVO> {
todo!()
}
}
Loading

0 comments on commit 4ae96b2

Please sign in to comment.