Skip to content

Commit

Permalink
add httproute priority
Browse files Browse the repository at this point in the history
  • Loading branch information
RWDai committed Dec 10, 2023
1 parent 0d69a8f commit 0d147ce
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions admin/src/model/vo/http_route_vo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct SgHttpRouteVo {
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>>,
/// [crate::model::vo::plugin_vo::SgFilterVo]'s id
Expand Down
2 changes: 2 additions & 0 deletions admin/src/model/vo_converter/http_route_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl VoConv<SgHttpRoute, SgHttpRouteVo> for SgHttpRouteVo {
Ok(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() {
Expand All @@ -41,6 +42,7 @@ impl VoConv<SgHttpRoute, SgHttpRouteVo> for SgHttpRouteVo {
name: model.name,
gateway_name: model.gateway_name,
hostnames: model.hostnames,
priority: model.priority,
filters,
rules,
filter_vos,
Expand Down
2 changes: 2 additions & 0 deletions kernel-common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub const RAW_HTTP_ROUTE_KIND: &str = "raw.http.route.kind";
pub const RAW_HTTP_ROUTE_KIND_DEFAULT: &str = "HTTPRoute";
pub const RAW_HTTP_ROUTE_KIND_SPACEROUTE: &str = "HTTPSpaceroute";

pub const ANNOTATION_RESOURCE_PRIORITY: &str = "priority";

pub const BANCKEND_KIND_EXTERNAL: &str = "External";
pub const BANCKEND_KIND_EXTERNAL_HTTP: &str = "ExternalHttp";
pub const BANCKEND_KIND_EXTERNAL_HTTPS: &str = "ExternalHttps";
2 changes: 2 additions & 0 deletions kernel-common/src/converter/http_route_k8s_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +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);
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(),
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()?,
priority,
})
}
}
Expand Down
6 changes: 4 additions & 2 deletions kernel/src/config/config_by_k8s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,8 @@ async fn process_http_route_config(mut http_route_objs: Vec<HttpSpaceroute>) ->
let mut http_route_configs = Vec::new();
http_route_objs.sort_by(|http_route_a, http_route_b| {
let (a_priority, b_priority) = (
http_route_a.annotations().get(crate::constants::ANNOTATION_RESOURCE_PRIORITY).and_then(|a| a.parse::<i64>().ok()).unwrap_or(0),
http_route_b.annotations().get(crate::constants::ANNOTATION_RESOURCE_PRIORITY).and_then(|a| a.parse::<i64>().ok()).unwrap_or(0),
http_route_a.annotations().get(kernel_common::constants::ANNOTATION_RESOURCE_PRIORITY).and_then(|a| a.parse::<i64>().ok()).unwrap_or(0),
http_route_b.annotations().get(kernel_common::constants::ANNOTATION_RESOURCE_PRIORITY).and_then(|a| a.parse::<i64>().ok()).unwrap_or(0),
);
match b_priority.cmp(&a_priority) {
Ordering::Equal => http_route_a.metadata.creation_timestamp.cmp(&http_route_b.metadata.creation_timestamp),
Expand Down Expand Up @@ -680,10 +680,12 @@ 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 priority=http_route_obj.annotations().get(kernel_common::constants::ANNOTATION_RESOURCE_PRIORITY).and_then(|a| a.parse::<i64>().ok()).unwrap_or(0);
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(),
priority,
filters: if let Some(name) = &http_route_obj.metadata.name {
let kind = if let Some(kind) = http_route_obj.annotations().get(kernel_common::constants::RAW_HTTP_ROUTE_KIND) {
kind
Expand Down
2 changes: 0 additions & 2 deletions kernel/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub const DOMAIN_CODE: &str = "spacegate_kernel";

pub const ANNOTATION_RESOURCE_PRIORITY: &str = "priority";

0 comments on commit 0d147ce

Please sign in to comment.