diff --git a/admin/src/model/vo/http_route_vo.rs b/admin/src/model/vo/http_route_vo.rs index 7848e303..51bf327c 100644 --- a/admin/src/model/vo/http_route_vo.rs +++ b/admin/src/model/vo/http_route_vo.rs @@ -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>, /// [crate::model::vo::plugin_vo::SgFilterVo]'s id diff --git a/admin/src/model/vo_converter/http_route_conv.rs b/admin/src/model/vo_converter/http_route_conv.rs index 46b4658d..1d36a8ba 100644 --- a/admin/src/model/vo_converter/http_route_conv.rs +++ b/admin/src/model/vo_converter/http_route_conv.rs @@ -19,6 +19,7 @@ impl VoConv 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() { @@ -41,6 +42,7 @@ impl VoConv for SgHttpRouteVo { name: model.name, gateway_name: model.gateway_name, hostnames: model.hostnames, + priority: model.priority, filters, rules, filter_vos, diff --git a/kernel-common/src/constants.rs b/kernel-common/src/constants.rs index 88a62449..87882c33 100644 --- a/kernel-common/src/constants.rs +++ b/kernel-common/src/constants.rs @@ -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"; diff --git a/kernel-common/src/converter/http_route_k8s_conv.rs b/kernel-common/src/converter/http_route_k8s_conv.rs index 77a48552..71d44fe7 100644 --- a/kernel-common/src/converter/http_route_k8s_conv.rs +++ b/kernel-common/src/converter/http_route_k8s_conv.rs @@ -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::().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::>>()).transpose()?, + priority, }) } } diff --git a/kernel/src/config/config_by_k8s.rs b/kernel/src/config/config_by_k8s.rs index b6c40f95..e86c209a 100644 --- a/kernel/src/config/config_by_k8s.rs +++ b/kernel/src/config/config_by_k8s.rs @@ -608,8 +608,8 @@ async fn process_http_route_config(mut http_route_objs: Vec) -> 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::().ok()).unwrap_or(0), - http_route_b.annotations().get(crate::constants::ANNOTATION_RESOURCE_PRIORITY).and_then(|a| a.parse::().ok()).unwrap_or(0), + http_route_a.annotations().get(kernel_common::constants::ANNOTATION_RESOURCE_PRIORITY).and_then(|a| a.parse::().ok()).unwrap_or(0), + http_route_b.annotations().get(kernel_common::constants::ANNOTATION_RESOURCE_PRIORITY).and_then(|a| a.parse::().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), @@ -680,10 +680,12 @@ async fn process_http_route_config(mut http_route_objs: Vec) -> }, 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::().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 diff --git a/kernel/src/constants.rs b/kernel/src/constants.rs index 93116c9b..36b1709c 100644 --- a/kernel/src/constants.rs +++ b/kernel/src/constants.rs @@ -1,3 +1 @@ pub const DOMAIN_CODE: &str = "spacegate_kernel"; - -pub const ANNOTATION_RESOURCE_PRIORITY: &str = "priority";