Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
RWDai committed Nov 15, 2023
1 parent 1814dee commit b175991
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 47 deletions.
47 changes: 40 additions & 7 deletions kernel-common/src/converter/plugin_k8s_conv.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::constants::k8s_constants::DEFAULT_NAMESPACE;
use crate::gatewayapi_support_filter::{SgFilterHeaderModifier, SgFilterHeaderModifierKind};
use crate::inner_model::plugin_filter::SgRouteFilter;
use crate::gatewayapi_support_filter::{
SgFilterHeaderModifier, SgFilterHeaderModifierKind, SgFilterRedirect, SgFilterRewrite, SG_FILTER_HEADER_MODIFIER_CODE, SG_FILTER_REDIRECT_CODE, SG_FILTER_REWRITE_CODE,
};
use crate::inner_model::plugin_filter::{SgHttpPathModifier, SgHttpPathModifierType, SgRouteFilter};
use crate::k8s_crd::sg_filter::{K8sSgFilterSpecFilter, K8sSgFilterSpecTargetRef};
use k8s_gateway_api::{HttpHeader, HttpRequestHeaderFilter, HttpRouteFilter};
use k8s_gateway_api::{HttpHeader, HttpPathModifier, HttpRequestHeaderFilter, HttpRequestRedirectFilter, HttpRouteFilter, HttpUrlRewriteFilter};
use std::hash::{Hash, Hasher};
use tardis::TardisFuns;

Expand All @@ -22,7 +24,7 @@ impl SgRouteFilter {
}

pub fn to_http_route_filter(self) -> Option<HttpRouteFilter> {
if &self.code == "header_modifier" {
if &self.code == SG_FILTER_HEADER_MODIFIER_CODE {
if let Ok(header) = TardisFuns::json.json_to_obj::<SgFilterHeaderModifier>(self.spec) {
let header_filter = HttpRequestHeaderFilter {
set: header.sets.map(|header_map| header_map.into_iter().map(|(k, v)| HttpHeader { name: k, value: v }).collect()),
Expand All @@ -40,15 +42,46 @@ impl SgRouteFilter {
} else {
None
}
//todo
// } else if &self.code == "redirect" {
// if let Ok(header) = TardisFuns::json.json_to_obj::<SgFilterHeaderModifier>(self.spec) {}
} else if &self.code == SG_FILTER_REDIRECT_CODE {
if let Ok(redirect) = TardisFuns::json.json_to_obj::<SgFilterRedirect>(self.spec) {
Some(HttpRouteFilter::RequestRedirect {
request_redirect: HttpRequestRedirectFilter {
scheme: redirect.scheme,
hostname: redirect.hostname,
path: redirect.path.map(|p| p.to_http_path_modifier()),
port: redirect.port,
status_code: redirect.status_code,
},
})
} else {
None
}
} else if &self.code == SG_FILTER_REWRITE_CODE {
if let Ok(rewrite) = TardisFuns::json.json_to_obj::<SgFilterRewrite>(self.spec) {
Some(HttpRouteFilter::URLRewrite {
url_rewrite: HttpUrlRewriteFilter {
hostname: rewrite.hostname,
path: rewrite.path.map(|p| p.to_http_path_modifier()),
},
})
} else {
None
}
} else {
None
}
}
}

impl SgHttpPathModifier {
pub fn to_http_path_modifier(self) -> HttpPathModifier {
match self.kind {
SgHttpPathModifierType::ReplaceFullPath => HttpPathModifier::ReplaceFullPath { replace_full_path: self.value },
SgHttpPathModifierType::ReplacePrefixMatch => HttpPathModifier::ReplacePrefixMatch { replace_prefix_match: self.value },
}
}
}

#[cfg(feature = "k8s")]
#[derive(Clone)]
pub struct SgSingeFilter {
Expand Down
35 changes: 35 additions & 0 deletions kernel-common/src/gatewayapi_support_filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::inner_model::plugin_filter::SgHttpPathModifier;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

pub const SG_FILTER_HEADER_MODIFIER_CODE: &str = "header_modifier";

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct SgFilterHeaderModifier {
pub kind: SgFilterHeaderModifierKind,
Expand All @@ -14,3 +17,35 @@ pub enum SgFilterHeaderModifierKind {
Request,
Response,
}

pub const SG_FILTER_REDIRECT_CODE: &str = "redirect";

/// RedirectFilter defines a filter that redirects a request.
///
/// https://gateway-api.sigs.k8s.io/geps/gep-726/
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct SgFilterRedirect {
/// Scheme is the scheme to be used in the value of the Location header in the response. When empty, the scheme of the request is used.
pub scheme: Option<String>,
/// Hostname is the hostname to be used in the value of the Location header in the response. When empty, the hostname in the Host header of the request is used.
pub hostname: Option<String>,
/// Path defines parameters used to modify the path of the incoming request. The modified path is then used to construct the Location header. When empty, the request path is used as-is.
pub path: Option<SgHttpPathModifier>,
/// Port is the port to be used in the value of the Location header in the response.
pub port: Option<u16>,
/// StatusCode is the HTTP status code to be used in response.
pub status_code: Option<u16>,
}

pub const SG_FILTER_REWRITE_CODE: &str = "rewrite";

/// RewriteFilter defines a filter that modifies a request during forwarding.
///
/// https://gateway-api.sigs.k8s.io/geps/gep-726/
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct SgFilterRewrite {
/// Hostname is the value to be used to replace the Host header value during forwarding.
pub hostname: Option<String>,
/// Path defines parameters used to modify the path of the incoming request. The modified path is then used to construct the Location header. When empty, the request path is used as-is.
pub path: Option<SgHttpPathModifier>,
}
8 changes: 4 additions & 4 deletions kernel/src/config/config_by_k8s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ fn convert_filters(filters: Option<Vec<HttpRouteFilter>>) -> Option<Vec<SgRouteF
k8s_gateway_api::HttpRouteFilter::RequestRedirect { request_redirect } => SgRouteFilter {
code: crate::plugins::filters::redirect::CODE.to_string(),
name: None,
spec: TardisFuns::json.obj_to_json(&crate::plugins::filters::redirect::SgFilterRedirect {
spec: TardisFuns::json.obj_to_json(&kernel_common::gatewayapi_support_filter::SgFilterRedirect {
scheme: request_redirect.scheme,
hostname: request_redirect.hostname,
path: request_redirect.path.map(|path| match path {
Expand All @@ -905,7 +905,7 @@ fn convert_filters(filters: Option<Vec<HttpRouteFilter>>) -> Option<Vec<SgRouteF
k8s_gateway_api::HttpRouteFilter::URLRewrite { url_rewrite } => SgRouteFilter {
code: crate::plugins::filters::rewrite::CODE.to_string(),
name: None,
spec: TardisFuns::json.obj_to_json(&crate::plugins::filters::rewrite::SgFilterRewrite {
spec: TardisFuns::json.obj_to_json(&kernel_common::gatewayapi_support_filter::SgFilterRewrite {
hostname: url_rewrite.hostname,
path: url_rewrite.path.map(|path| match path {
k8s_gateway_api::HttpPathModifier::ReplaceFullPath { replace_full_path } => plugin_filter::SgHttpPathModifier {
Expand Down Expand Up @@ -970,7 +970,7 @@ fn convert_to_kube_filters(filters: Option<Vec<SgRouteFilter>>) -> TardisResult<
}
crate::plugins::filters::rewrite::CODE => {
let rewrite = TardisFuns::json
.json_to_obj::<crate::plugins::filters::rewrite::SgFilterRewrite>(filter.spec)
.json_to_obj::<kernel_common::gatewayapi_support_filter::SgFilterRewrite>(filter.spec)
.map_err(|error| TardisError::bad_request(&format!("[SG.Config] HttpRouteFilter [code={}] config parsing error: {error} ", filter.code), ""))?;
HttpRouteFilter::URLRewrite {
url_rewrite: k8s_gateway_api::HttpUrlRewriteFilter {
Expand All @@ -984,7 +984,7 @@ fn convert_to_kube_filters(filters: Option<Vec<SgRouteFilter>>) -> TardisResult<
}
crate::plugins::filters::redirect::CODE => {
let redirect = TardisFuns::json
.json_to_obj::<crate::plugins::filters::redirect::SgFilterRedirect>(filter.spec)
.json_to_obj::<kernel_common::gatewayapi_support_filter::SgFilterRedirect>(filter.spec)
.map_err(|error| TardisError::bad_request(&format!("[SG.Config] HttpRouteFilter [code={}] config parsing error: {error} ", filter.code), ""))?;
HttpRouteFilter::RequestRedirect {
request_redirect: k8s_gateway_api::HttpRequestRedirectFilter {
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/plugins/filters/header_modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use http::HeaderName;

use super::{SgPluginFilter, SgPluginFilterAccept, SgPluginFilterInitDto, SgPluginFilterKind, SgRoutePluginContext};
use crate::def_filter;
use kernel_common::gatewayapi_support_filter::{SgFilterHeaderModifier, SgFilterHeaderModifierKind};
use kernel_common::gatewayapi_support_filter::{SgFilterHeaderModifier, SgFilterHeaderModifierKind, SG_FILTER_HEADER_MODIFIER_CODE};

use tardis::basic::error::TardisError;
use tardis::basic::result::TardisResult;

def_filter!("header_modifier", SgFilterHeaderModifierDef, SgFilterHeaderModifier);
def_filter!(SG_FILTER_HEADER_MODIFIER_CODE, SgFilterHeaderModifierDef, SgFilterHeaderModifier);

#[async_trait]
impl SgPluginFilter for SgFilterHeaderModifier {
Expand Down
25 changes: 4 additions & 21 deletions kernel/src/plugins/filters/redirect.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
use async_trait::async_trait;
use http::StatusCode;
use serde::{Deserialize, Serialize};
use kernel_common::gatewayapi_support_filter::SgFilterRedirect;

use tardis::basic::{error::TardisError, result::TardisResult};
use tardis::url::Url;

use crate::def_filter;
use crate::helpers::url_helper::UrlToUri;
use crate::plugins::context::SgRouteFilterRequestAction;
use kernel_common::inner_model::plugin_filter::SgHttpPathModifier;

use super::{http_common_modify_path, SgPluginFilter, SgPluginFilterInitDto, SgRoutePluginContext};

def_filter!("redirect", SgFilterRedirectDef, SgFilterRedirect);

/// RedirectFilter defines a filter that redirects a request.
///
/// https://gateway-api.sigs.k8s.io/geps/gep-726/
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct SgFilterRedirect {
/// Scheme is the scheme to be used in the value of the Location header in the response. When empty, the scheme of the request is used.
pub scheme: Option<String>,
/// Hostname is the hostname to be used in the value of the Location header in the response. When empty, the hostname in the Host header of the request is used.
pub hostname: Option<String>,
/// Path defines parameters used to modify the path of the incoming request. The modified path is then used to construct the Location header. When empty, the request path is used as-is.
pub path: Option<SgHttpPathModifier>,
/// Port is the port to be used in the value of the Location header in the response.
pub port: Option<u16>,
/// StatusCode is the HTTP status code to be used in response.
pub status_code: Option<u16>,
}
def_filter!(SG_FILTER_REDIRECT_CODE, SgFilterRedirectDef, SgFilterRedirect);

#[async_trait]
impl SgPluginFilter for SgFilterRedirect {
Expand Down Expand Up @@ -93,7 +76,7 @@ mod tests {
use http::{HeaderMap, Method, StatusCode, Uri, Version};
use hyper::Body;
use kernel_common::inner_model::http_route::SgHttpPathMatchType;
use kernel_common::inner_model::plugin_filter::SgHttpPathModifierType;
use kernel_common::inner_model::plugin_filter::{SgHttpPathModifier, SgHttpPathModifierType};
use tardis::tokio;

#[tokio::test]
Expand Down
14 changes: 2 additions & 12 deletions kernel/src/plugins/filters/rewrite.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
use crate::def_filter;
use crate::helpers::url_helper::UrlToUri;
use async_trait::async_trait;
use kernel_common::gatewayapi_support_filter::SgFilterRewrite;
use kernel_common::inner_model::plugin_filter::SgHttpPathModifier;
use serde::{Deserialize, Serialize};
use tardis::basic::{error::TardisError, result::TardisResult};
use tardis::url::Url;

use super::{http_common_modify_path, SgPluginFilter, SgPluginFilterInitDto, SgRoutePluginContext};

def_filter!("rewrite", SgFilterRewriteDef, SgFilterRewrite);

/// RewriteFilter defines a filter that modifies a request during forwarding.
///
/// https://gateway-api.sigs.k8s.io/geps/gep-726/
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct SgFilterRewrite {
/// Hostname is the value to be used to replace the Host header value during forwarding.
pub hostname: Option<String>,
/// Path defines parameters used to modify the path of the incoming request. The modified path is then used to construct the Location header. When empty, the request path is used as-is.
pub path: Option<SgHttpPathModifier>,
}
def_filter!(SG_FILTER_REWRITE_CODE, SgFilterRewriteDef, SgFilterRewrite);

#[async_trait]
impl SgPluginFilter for SgFilterRewrite {
Expand Down
2 changes: 1 addition & 1 deletion kernel/tests/test_websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async fn test_webscoket() -> TardisResult<()> {
filters: Some(vec![SgRouteFilter {
code: "rewrite".to_string(),
name: None,
spec: TardisFuns::json.obj_to_json(&filters::rewrite::SgFilterRewrite {
spec: TardisFuns::json.obj_to_json(&kernel_common::gatewayapi_support_filter::SgFilterRewrite {
hostname: None,
path: Some(plugin_filter::SgHttpPathModifier {
kind: plugin_filter::SgHttpPathModifierType::ReplacePrefixMatch,
Expand Down

0 comments on commit b175991

Please sign in to comment.