forked from ideal-world/spacegate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
259 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod backend_vo; | ||
pub mod gateway_vo; | ||
pub mod http_route_vo; | ||
pub mod plugin_vo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use kernel_common::inner_model::gateway::SgProtocol; | ||
use serde::{Deserialize, Serialize}; | ||
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 { | ||
/// Name is the kubernetes service name OR url host. | ||
pub name_or_host: String, | ||
/// Namespace is the kubernetes namespace | ||
pub namespace: Option<String>, | ||
/// Port specifies the destination port number to use for this resource. | ||
pub port: u16, | ||
/// Timeout specifies the timeout for requests forwarded to the referenced backend. | ||
pub timeout_ms: Option<u64>, | ||
// Protocol specifies the protocol used to talk to the referenced backend. | ||
pub protocol: Option<SgProtocol>, | ||
/// Weight specifies the proportion of requests forwarded to the referenced backend. | ||
/// This is computed as weight/(sum of all weights in this BackendRefs list). | ||
/// For non-zero values, there may be some epsilon from the exact proportion defined here depending on the precision an implementation supports. | ||
/// Weight is not a percentage and the sum of weights does not need to equal 100. | ||
pub weight: Option<u16>, | ||
/// [crate::model::vo::plugin_vo::SgFilterVO]'s id | ||
pub filters: Option<Vec<String>>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use kernel_common::inner_model::gateway::{SgParameters, SgProtocol, SgTlsMode}; | ||
use serde::{Deserialize, Serialize}; | ||
use tardis::web::poem_openapi; | ||
|
||
/// Gateway represents an instance of a service-traffic handling infrastructure | ||
/// by binding Listeners to a set of IP addresses. | ||
/// | ||
/// Reference: [Kubernetes Gateway](https://gateway-api.sigs.k8s.io/references/spec/#gateway.networking.k8s.io/v1beta1.Gateway) | ||
#[derive(Default, Debug, Serialize, Deserialize, Clone, poem_openapi::Object)] | ||
pub struct SgGatewayVO { | ||
/// Name of the Gateway. Global Unique. | ||
/// | ||
/// In k8s mode, this name MUST be unique within a namespace. | ||
/// format see [k8s_helper::format_k8s_obj_unique] | ||
pub name: String, | ||
/// Some parameters necessary for the gateway. | ||
pub parameters: SgParameters, | ||
/// Listeners associated with this Gateway. Listeners define logical endpoints | ||
/// that are bound on this Gateway’s addresses. | ||
pub listeners: Vec<SgListenerVO>, | ||
/// [crate::model::vo::plugin_vo::SgFilterVO]'s id | ||
pub filters: Option<Vec<String>>, | ||
} | ||
|
||
/// Listener embodies the concept of a logical endpoint where a Gateway accepts network connections. | ||
#[derive(Default, Debug, Serialize, Deserialize, Clone, poem_openapi::Object)] | ||
pub struct SgListenerVO { | ||
/// Name is the name of the Listener. This name MUST be unique within a Gateway. | ||
pub name: String, | ||
/// Ip bound to the Listener. Default is 0.0.0.0 | ||
pub ip: Option<String>, | ||
/// Port is the network port. Multiple listeners may use the same port, subject | ||
/// to the Listener compatibility rules. | ||
pub port: u16, | ||
/// Protocol specifies the network protocol this listener expects to receive. | ||
pub protocol: SgProtocol, | ||
/// SgTlsConfig's id refers to the TLS configuration. | ||
pub tls: Option<String>, | ||
/// `HostName` is used to define the host on which the listener accepts requests. | ||
pub hostname: Option<String>, | ||
} | ||
|
||
/// GatewayTLSConfig describes a TLS configuration. | ||
/// unique by id | ||
#[derive(Debug, Serialize, Deserialize, Clone, poem_openapi::Object)] | ||
pub struct SgTlsConfigVO { | ||
pub id: String, | ||
pub mode: SgTlsMode, | ||
pub key: String, | ||
pub cert: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use kernel_common::inner_model::http_route::SgHttpRouteMatch; | ||
use serde::{Deserialize, Serialize}; | ||
use tardis::web::poem_openapi; | ||
|
||
/// HTTPRoute provides a way to route HTTP requests. | ||
/// | ||
/// 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 { | ||
/// 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. | ||
pub hostnames: Option<Vec<String>>, | ||
/// [crate::model::vo::plugin_vo::SgFilterVO]'s id | ||
pub filters: Option<Vec<String>>, | ||
/// Rules are a list of HTTP matchers, filters and actions. | ||
pub rules: Option<Vec<SgHttpRouteRuleVO>>, | ||
} | ||
|
||
/// HTTPRouteRule defines semantics for matching an HTTP request based on conditions (matches), processing it (filters), and forwarding the request to an API object | ||
#[derive(Default, Debug, Serialize, Deserialize, Clone, poem_openapi::Object)] | ||
pub struct SgHttpRouteRuleVO { | ||
/// Matches define conditions used for matching the rule against incoming HTTP requests. Each match is independent, i.e. this rule will be matched if any one of the matches is satisfied. | ||
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 | ||
pub backends: Option<Vec<String>>, | ||
/// Timeout define the timeout for requests that match this rule. | ||
pub timeout_ms: Option<u64>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
use tardis::web::poem_openapi; | ||
|
||
/// RouteFilter defines processing steps that must be completed during the request or response lifecycle. | ||
/// | ||
/// There are four levels of filters | ||
/// 1. Global level, which works on all requests under the same gateway service | ||
/// 2. Routing level, which works on all requests under the same gateway route | ||
/// 3. Rule level, which works on all requests under the same gateway routing rule | ||
/// 4. Backend level, which works on all requests under the same backend | ||
#[derive(Default, Debug, Serialize, Deserialize, Clone, poem_openapi::Object)] | ||
pub struct SgFilterVO { | ||
pub id: String, | ||
/// Filter code, Used to match the corresponding filter. | ||
pub code: String, | ||
/// Filter name. If the name of the same filter exists at different levels of configuration, | ||
/// only the child nodes take effect(Backend Level > Rule Level > Routing Level > Global Level) | ||
pub name: Option<String>, | ||
/// filter parameters. | ||
pub spec: Value, | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
pub(crate) mod backend_ref_service; | ||
pub(crate) mod base_service; | ||
pub(crate) mod gateway_service; | ||
pub(crate) mod plugin_service; | ||
pub(crate) mod route_service; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::helper::get_k8s_client; | ||
use k8s_openapi::api::core::v1::ConfigMap; | ||
use k8s_openapi::merge_strategies::list; | ||
use kube::api::ListParams; | ||
use kube::Api; | ||
use serde::Deserialize; | ||
use serde_json::Value; | ||
use std::collections::HashMap; | ||
use tardis::basic::error::TardisError; | ||
use tardis::basic::result::TardisResult; | ||
|
||
pub const GATEWAY_CONFIG_NAME: &str = "gateway_config"; | ||
pub const PLUGIN_CONFIG_NAME: &str = "plugin_config"; | ||
pub const ROUTE_CONFIG_NAME: &str = "route_config"; | ||
pub const BACKEND_REF_CONFIG_NAME: &str = "backend_ref_config"; | ||
|
||
pub trait GetConfigMapName { | ||
fn get_config_map_name() -> String; | ||
} | ||
|
||
pub struct BaseService; | ||
|
||
impl BaseService { | ||
#[cfg(feature = "k8s")] | ||
pub async fn list<'a, T>() -> TardisResult<HashMap<String, String>> | ||
where | ||
T: GetConfigMapName + Deserialize<'a>, | ||
{ | ||
let mut items = get_config_map_api() | ||
.await? | ||
.list(&ListParams::default().fields(&format!("metadata.name={}", T::get_config_map_name()))) | ||
.await | ||
.map_err(|e| TardisError::io_error(&format!("err:{e}"), ""))? | ||
.items; | ||
if items.is_empty() { | ||
Ok(HashMap::new()) | ||
} else { | ||
if let Some(b_map) = items.remove(0).data { | ||
Ok(b_map.into_iter().collect()) | ||
} else { | ||
Ok(HashMap::new()) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "k8s")] | ||
pub async fn add<'a, T>(config: T) -> TardisResult<()> | ||
where | ||
T: GetConfigMapName + Deserialize<'a>, | ||
{ | ||
config | ||
} | ||
} | ||
|
||
#[cfg(feature = "k8s")] | ||
pub async fn get_config_map_api() -> TardisResult<Api<ConfigMap>> { | ||
Ok(Api::namespaced(get_k8s_client().await?, "spacegate")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.