// SPDX-License-Identifier: Apache-2.0 // Copyright (c) 2026 Shane Utt //! Routing configuration generation for Praxis proxy. use std::collections::{BTreeMap, HashMap, HashSet}; use gateway_api::{ httproutes::{HTTPRoute, HttpRouteRulesBackendRefs, HttpRouteRulesMatches, HttpRouteRulesMatchesPathType}, referencegrants::ReferenceGrant, }; use serde::Serialize; use tracing::warn; use super::cluster::cluster_name as backend_cluster_name; use crate::gateway_api::{ attachment::AttachedRoute, hostname::intersect_hostnames, reference_grant::is_reference_allowed, route_validation::validate_route, }; // ----------------------------------------------------------------------------- // PraxisRoute // ----------------------------------------------------------------------------- /// Praxis route configuration. /// /// Represents a routing rule in the Praxis proxy config. Uses either `path` /// (exact match) or `path_prefix` (prefix match). When `path` is set it /// takes precedence over `path_prefix`. #[derive(Debug, Clone, Serialize, PartialEq)] pub struct PraxisRoute { /// Target cluster name. pub cluster: String, /// Request headers to match (exact match only). #[serde(skip_serializing_if = "Option::is_none")] pub headers: Option>, /// Hostname match. #[serde(skip_serializing_if = "Option::is_none")] pub host: Option, /// Listener names this route targets. `None` means all listeners. /// /// Used for per-listener route partitioning; not serialized. #[serde(skip)] pub listener_names: Vec>, /// Exact path match. Takes precedence over `path_prefix`. #[serde(skip_serializing_if = "Option::is_none")] pub path: Option, /// Path prefix match. Must end with '/'. #[serde(default, skip_serializing_if = "String::is_empty")] pub path_prefix: String, } // ----------------------------------------------------------------------------- // PraxisFilterEntry // ----------------------------------------------------------------------------- /// Praxis filter entry. /// /// Represents a filter in a filter chain with its configuration. #[derive(Debug, Clone, Serialize, PartialEq)] pub struct PraxisFilterEntry { /// Filter name. pub filter: String, /// Filter configuration (flattened into parent). #[serde(flatten)] pub config: yaml_serde::Value, } // ----------------------------------------------------------------------------- // BackendRef // ----------------------------------------------------------------------------- /// Resolved backend reference from an `HTTPRoute` rule. /// /// Carries enough metadata for the gateway controller to resolve Kubernetes /// `Service` endpoints into cluster `IP:port` pairs. #[derive(Debug, Clone, PartialEq)] pub struct BackendRef { /// Cluster name (format: `namespace_service_port`). pub cluster_name: String, /// Kubernetes namespace of the backend Service. pub namespace: String, /// Backend Service port number. pub port: i32, /// Backend Service name. pub service: String, /// Traffic weight for weighted routing (Gateway API `backendRef.weight`). pub weight: Option, } // ----------------------------------------------------------------------------- // Route Conversion // ----------------------------------------------------------------------------- /// Converts `HTTPRoutes` to Praxis route configurations. /// /// Returns `(routes, backend_refs)`. Routes carry either an exact `path` or /// a `path_prefix` (defaulting to `"/"`). `Backend` refs carry structured /// metadata for endpoint resolution. /// /// `listener_hostnames` maps listener section names to their hostname /// constraints. Route hostnames are intersected with listener hostnames /// per the Gateway API spec: only hostnames matching at least one /// targeted listener are retained. /// /// [`intersect_hostnames`]: crate::gateway_api::hostname::intersect_hostnames pub fn convert_routes( routes: &[AttachedRoute<'_>], listener_hostnames: &HashMap>, grants: &[ReferenceGrant], ) -> (Vec, Vec) { let mut praxis_routes = Vec::new(); let mut seen_clusters = HashSet::new(); let mut backend_refs = Vec::new(); for attached in routes { let (route, section_names) = (attached.route, &attached.section_names); let route_ns = route.metadata.namespace.as_deref().unwrap_or("default"); let raw_hostnames = route.spec.hostnames.as_deref().unwrap_or(&[]); let effective = effective_hostnames(raw_hostnames, section_names, listener_hostnames); if !raw_hostnames.is_empty() && effective.is_empty() { continue; } process_supported_rules( route, route_ns, &effective, section_names, grants, &mut seen_clusters, &mut praxis_routes, &mut backend_refs, ); } (praxis_routes, backend_refs) } /// Converts the rules of one route, skipping any this operator cannot /// express. #[expect( clippy::too_many_arguments, reason = "rule processing needs access to shared accumulators" )] fn process_supported_rules( route: &HTTPRoute, route_ns: &str, hostnames: &[String], section_names: &[Option], grants: &[ReferenceGrant], seen_clusters: &mut HashSet, praxis_routes: &mut Vec, backend_refs: &mut Vec, ) { let validation = validate_route(route); let rules = route.spec.rules.as_deref().unwrap_or(&[]); for (index, rule) in rules.iter().enumerate() { if validation.is_rejected(index) { tracing::debug!( route = route.metadata.name.as_deref().unwrap_or_default(), rule = index, "skipping rule this operator cannot express" ); continue; } process_rule( rule, route_ns, hostnames, section_names, grants, seen_clusters, praxis_routes, backend_refs, ); } } /// Computes the effective hostnames for a route after listener intersection. /// /// Collects listener hostnames for each section name the route targets, /// then intersects route hostnames with that set. Routes without /// hostnames pass through unchanged (empty vec). When a route targets /// all listeners (section name is `None`), all listener hostnames are /// used for intersection. fn effective_hostnames( route_hostnames: &[String], section_names: &[Option], listener_hostnames: &HashMap>, ) -> Vec { if route_hostnames.is_empty() { return Vec::new(); } let listener_hosts: Vec> = collect_listener_hosts(section_names, listener_hostnames); intersect_hostnames(route_hostnames, &listener_hosts) } /// Gathers listener hostnames for the targeted sections. /// /// Named sections resolve to their specific listener hostname. /// `None` sections (no `sectionName`) expand to all listener hostnames. fn collect_listener_hosts( section_names: &[Option], listener_hostnames: &HashMap>, ) -> Vec> { let targets_all = section_names.iter().any(Option::is_none); if targets_all { return listener_hostnames.values().cloned().collect(); } section_names .iter() .filter_map(|sn| sn.as_ref().and_then(|name| listener_hostnames.get(name)).cloned()) .collect() } /// Processes a single rule: creates routes and collects backend refs. #[expect( clippy::too_many_arguments, reason = "rule processing needs access to shared accumulators" )] fn process_rule( rule: &gateway_api::httproutes::HttpRouteRules, route_ns: &str, route_hostnames: &[String], section_names: &[Option], grants: &[ReferenceGrant], seen_clusters: &mut HashSet, praxis_routes: &mut Vec, backend_refs: &mut Vec, ) { let matches = rule.matches.as_deref().unwrap_or(&[]); let raw_backends = rule.backend_refs.as_deref().unwrap_or(&[]); if raw_backends.is_empty() { return; } let authorized: Vec<_> = raw_backends .iter() .filter(|backend| check_backend_authorized(backend, route_ns, grants)) .collect(); if authorized.is_empty() { tracing::debug!("rule has no authorized backends; emitting no route"); return; } let cluster_name = merged_cluster_name_from_refs(&authorized, route_ns); create_routes_for_backend(&cluster_name, matches, route_hostnames, section_names, praxis_routes); backend_refs.extend( authorized .iter() .filter_map(|backend| process_backend_ref(backend, route_ns, &cluster_name, seen_clusters)), ); } /// Returns `true` when a rule has a backend the gateway may forward to /// right now. /// /// A rule whose every `backendRef` was refused is not the same as a /// rule that named none: it still has to answer, with the 500 the /// Gateway API prescribes for an unresolvable reference, so the caller /// needs to tell the two apart. A rule whose backends are all /// authorized but share a cluster with no ready endpoints is in the /// same position — the reference resolves, but no upstream can take /// the traffic yet — so it counts as not serving too. /// /// A rule mixing a ready backend with an endpointless one serves: its /// backends merge into one cluster, and the ready service's endpoints /// keep it populated. pub fn rule_has_serving_backend( rule: &gateway_api::httproutes::HttpRouteRules, route_ns: &str, grants: &[ReferenceGrant], pending: &HashSet, ) -> bool { let authorized: Vec<_> = rule .backend_refs .as_deref() .unwrap_or(&[]) .iter() .filter(|backend| is_backend_authorized(backend, route_ns, grants)) .collect(); !authorized.is_empty() && !pending.contains(&merged_cluster_name_from_refs(&authorized, route_ns)) } /// Returns `true` if authorized; logs and returns `false` otherwise. fn check_backend_authorized(backend: &HttpRouteRulesBackendRefs, route_ns: &str, grants: &[ReferenceGrant]) -> bool { if is_backend_authorized(backend, route_ns, grants) { return true; } tracing::debug!(backend = backend.name, "skipping unauthorized cross-namespace backend"); false } /// Checks if a backend ref is authorized (same namespace or [`ReferenceGrant`]). fn is_backend_authorized(backend: &HttpRouteRulesBackendRefs, route_ns: &str, grants: &[ReferenceGrant]) -> bool { let backend_ns = backend.namespace.as_deref().unwrap_or(route_ns); let group = backend.group.as_deref().unwrap_or(""); let kind = backend.kind.as_deref().unwrap_or("Service"); is_reference_allowed( route_ns, "gateway.networking.k8s.io", "HTTPRoute", backend_ns, group, kind, Some(&backend.name), grants, ) } /// Builds a merged cluster name from a pre-filtered list of backend refs. fn merged_cluster_name_from_refs(backends: &[&HttpRouteRulesBackendRefs], route_ns: &str) -> String { if let [only] = backends { return single_cluster_name(only, route_ns); } backends .iter() .map(|backend| { format!( "{}_w{}", single_cluster_name(backend, route_ns), backend.weight.unwrap_or(1) ) }) .collect::>() .join("__") } /// Builds a cluster name for a single backend ref. fn single_cluster_name(backend: &HttpRouteRulesBackendRefs, route_ns: &str) -> String { let namespace = backend.namespace.as_deref().unwrap_or(route_ns); let service = backend.name.as_str(); let port = backend.port.unwrap_or(80); backend_cluster_name(namespace, service, port) } /// Resolves a single backend ref into a [`BackendRef`]. /// /// Returns `None` for unsupported backend kinds (non-empty group or /// non-Service kind). Deduplicates by the combination of /// `cluster_name` and service identity using `seen`. fn process_backend_ref( backend: &HttpRouteRulesBackendRefs, route_ns: &str, cluster_name: &str, seen: &mut HashSet, ) -> Option { let group = backend.group.as_deref().unwrap_or(""); let kind = backend.kind.as_deref().unwrap_or("Service"); if !group.is_empty() || kind != "Service" { tracing::debug!(group, kind, "skipping unsupported backend ref kind"); return None; } let namespace = backend.namespace.as_deref().unwrap_or(route_ns); let service = backend.name.as_str(); let port = backend.port.unwrap_or(80); let dedup_key = format!("{cluster_name}:{}", backend_cluster_name(namespace, service, port)); if !seen.insert(dedup_key) { return None; } Some(BackendRef { cluster_name: cluster_name.to_owned(), namespace: namespace.to_owned(), service: service.to_owned(), port, weight: backend.weight, }) } /// Creates [`PraxisRoute`] entries for a single backend cluster. /// /// When `matches` is empty a catch-all route (`/`) is created. Each match /// is expanded across all `hostnames`; if no hostnames are present a single /// host-less route is emitted. fn create_routes_for_backend( cluster: &str, matches: &[HttpRouteRulesMatches], hostnames: &[String], section_names: &[Option], out: &mut Vec, ) { if matches.is_empty() { emit_catchall_routes(cluster, hostnames, section_names, out); } else { for rule_match in matches { emit_match_routes(cluster, rule_match, hostnames, section_names, out); } } } /// Emits catch-all routes (prefix `/`) for a backend cluster. fn emit_catchall_routes( cluster: &str, hostnames: &[String], section_names: &[Option], out: &mut Vec, ) { let base = PraxisRoute { cluster: cluster.to_owned(), headers: None, host: None, listener_names: section_names.to_vec(), path: None, path_prefix: "/".to_owned(), }; push_per_hostname(base, hostnames, out); } /// Emits routes for a single match entry, expanding across hostnames. /// /// A single match may produce multiple path entries (for element-wise /// prefix matching), each expanded across all hostnames. fn emit_match_routes( cluster: &str, rule_match: &HttpRouteRulesMatches, hostnames: &[String], section_names: &[Option], out: &mut Vec, ) { let headers = extract_headers(rule_match.headers.as_ref()); for (path, path_prefix) in extract_path_match(rule_match.path.as_ref()) { let base = PraxisRoute { cluster: cluster.to_owned(), headers: headers.clone(), host: None, listener_names: section_names.to_vec(), path, path_prefix, }; push_per_hostname(base, hostnames, out); } } /// Pushes one copy of `base` per hostname. /// /// A route with no hostname constraint is emitted once, unscoped. fn push_per_hostname(base: PraxisRoute, hostnames: &[String], out: &mut Vec) { if hostnames.is_empty() { out.push(base); return; } for hostname in hostnames { let mut scoped = base.clone(); scoped.host = Some(hostname.clone()); out.push(scoped); } } // ----------------------------------------------------------------------------- // Utility Functions // ----------------------------------------------------------------------------- /// Extracts path match entries from an `HTTPRoute` path match. /// /// Returns a vec of `(path, path_prefix)` pairs: /// - `PathPrefix` without trailing `/`: emits two entries for element-wise matching (exact `"/foo"` + prefix /// `"/foo/"`), preventing `/foobar` from matching while allowing `/foo` and `/foo/anything`. /// - `PathPrefix` with trailing `/` or `"/"`: single prefix entry. /// - `Exact`: single entry with `path` set, empty `path_prefix`. /// - Default / unsupported: single entry with prefix `"/"`. fn extract_path_match( path: Option<&gateway_api::httproutes::HttpRouteRulesMatchesPath>, ) -> Vec<(Option, String)> { let Some(path_match) = path else { return vec![(None, "/".to_owned())]; }; match &path_match.r#type { Some(HttpRouteRulesMatchesPathType::PathPrefix) => { let value = path_match.value.as_deref().unwrap_or("/"); if value == "/" || value.ends_with('/') { vec![(None, value.to_owned())] } else { vec![(Some(value.to_owned()), String::new()), (None, format!("{value}/"))] } }, Some(HttpRouteRulesMatchesPathType::Exact) => { let value = path_match.value.clone().unwrap_or_else(|| "/".to_owned()); vec![(Some(value), String::new())] }, Some(HttpRouteRulesMatchesPathType::RegularExpression) => { warn!("RegularExpression path match reached conversion; emitting no route"); Vec::new() }, _ => vec![(None, "/".to_owned())], } } /// Extracts header match pairs from `HTTPRoute` header matches. /// /// Only `Exact` match type is supported. `RegularExpression` headers are /// skipped with a warning. Per Gateway API spec, first entry wins for /// duplicate header names. fn extract_headers( headers: Option<&Vec>, ) -> Option> { let hs = headers.filter(|hs| !hs.is_empty())?; let map = collect_exact_headers(hs); if map.is_empty() { None } else { Some(map) } } /// Collects exact-match headers into a map, skipping regex matches. fn collect_exact_headers(hs: &[gateway_api::httproutes::HttpRouteRulesMatchesHeaders]) -> BTreeMap { let mut map = BTreeMap::new(); for header in hs { if is_regex_header(header) { warn!(header = %header.name, "RegularExpression header match not supported, skipping"); continue; } map.entry(header.name.clone()).or_insert_with(|| header.value.clone()); } map } /// Returns `true` if the header match uses `RegularExpression` type. fn is_regex_header(header: &gateway_api::httproutes::HttpRouteRulesMatchesHeaders) -> bool { header.r#type.as_ref().is_some_and(|match_type| { *match_type == gateway_api::httproutes::HttpRouteRulesMatchesHeadersType::RegularExpression }) } // ----------------------------------------------------------------------------- // Tests // ----------------------------------------------------------------------------- #[cfg(test)] #[expect(clippy::too_many_lines, reason = "tests")] mod tests { use gateway_api::httproutes::{HttpRouteRules, HttpRouteRulesMatchesPath, HttpRouteSpec}; use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta; use super::*; #[test] fn test_convert_routes_path_prefix() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/api".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "my-svc".to_owned(), port: Some(8080), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, status: None, }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, backend_refs) = convert_routes(&routes, &HashMap::new(), &[]); assert_eq!( praxis_routes.len(), 2, "element-wise prefix should produce exact + prefix routes" ); assert_eq!( praxis_routes[0].path, Some("/api".to_owned()), "first route should be exact match for bare path" ); assert_eq!(praxis_routes[0].path_prefix, "", "exact route should have empty prefix"); assert_eq!(praxis_routes[1].path, None, "second route should be prefix match"); assert_eq!( praxis_routes[1].path_prefix, "/api/", "prefix route should have trailing slash" ); for r in &praxis_routes { assert_eq!( r.cluster, "default_my-svc_8080", "cluster should match namespace-service-port" ); } assert_eq!(backend_refs.len(), 1, "should have one backend ref"); assert_eq!( backend_refs[0].cluster_name, "default_my-svc_8080", "backend ref cluster name should match" ); assert_eq!(backend_refs[0].service, "my-svc", "backend ref service should match"); assert_eq!(backend_refs[0].port, 8080, "backend ref port should match"); } #[test] fn test_convert_routes_exact_path() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::Exact), value: Some("/exact".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "my-svc".to_owned(), port: Some(8080), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, status: None, }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, _) = convert_routes(&routes, &HashMap::new(), &[]); assert_eq!(praxis_routes.len(), 1, "should produce one route"); assert_eq!( praxis_routes[0].path, Some("/exact".to_owned()), "exact match should use path field" ); assert_eq!( praxis_routes[0].path_prefix, "", "exact match should have empty path_prefix" ); } #[test] fn test_convert_routes_multiple_rules() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![ HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/api".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "api-svc".to_owned(), port: Some(8080), ..Default::default() }]), ..Default::default() }, HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/web".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "web-svc".to_owned(), port: Some(80), ..Default::default() }]), ..Default::default() }, ]), ..Default::default() }, status: None, }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, backend_refs) = convert_routes(&routes, &HashMap::new(), &[]); assert_eq!( praxis_routes.len(), 4, "two prefix rules produce two element-wise pairs each" ); assert!( backend_refs.iter().any(|b| b.cluster_name == "default_api-svc_8080"), "should have api-svc backend ref" ); assert!( backend_refs.iter().any(|b| b.cluster_name == "default_web-svc_80"), "should have web-svc backend ref" ); } #[test] fn test_convert_routes_with_hostname() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { hostnames: Some(vec!["example.com".to_owned()]), rules: Some(vec![HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "my-svc".to_owned(), port: Some(80), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, status: None, }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, _) = convert_routes(&routes, &HashMap::new(), &[]); assert_eq!(praxis_routes.len(), 1, "should produce one route"); assert_eq!( praxis_routes[0].host, Some("example.com".to_owned()), "host should match" ); } #[test] fn test_convert_routes_cross_namespace() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("app-ns".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "svc".to_owned(), namespace: Some("other-ns".to_owned()), port: Some(80), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, status: None, }; let grant = make_reference_grant("other-ns", "app-ns", "svc"); let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, backend_refs) = convert_routes(&routes, &HashMap::new(), &[grant]); assert_eq!(praxis_routes.len(), 1, "should produce one route"); assert_eq!( praxis_routes[0].cluster, "other-ns_svc_80", "cluster should use backend namespace" ); assert!( backend_refs.iter().any(|b| b.cluster_name == "other-ns_svc_80"), "backend refs should use backend namespace" ); assert_eq!( backend_refs[0].namespace, "other-ns", "backend ref namespace should be other-ns" ); } #[test] fn test_convert_routes_with_header_matches() { use gateway_api::httproutes::HttpRouteRulesMatchesHeaders; let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/api".to_owned()), }), headers: Some(vec![ HttpRouteRulesMatchesHeaders { name: "x-version".to_owned(), value: "v1".to_owned(), ..Default::default() }, HttpRouteRulesMatchesHeaders { name: "x-region".to_owned(), value: "us-east-1".to_owned(), ..Default::default() }, ]), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "api-svc".to_owned(), port: Some(8080), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, status: None, }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, _) = convert_routes(&routes, &HashMap::new(), &[]); assert_eq!( praxis_routes.len(), 2, "element-wise prefix should produce exact + prefix routes" ); for r in &praxis_routes { assert!(r.headers.is_some(), "both routes should have header matches"); let headers = r.headers.as_ref().unwrap(); assert_eq!(headers.len(), 2, "should have two header matches"); assert_eq!(headers.get("x-version").unwrap(), "v1", "x-version header should match"); assert_eq!( headers.get("x-region").unwrap(), "us-east-1", "x-region header should match" ); } } #[test] fn test_convert_routes_missing_path_value() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: None, }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "svc".to_owned(), port: Some(80), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, status: None, }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, _) = convert_routes(&routes, &HashMap::new(), &[]); assert_eq!(praxis_routes.len(), 1, "should produce one route"); assert_eq!( praxis_routes[0].path, None, "missing path value should have no exact path" ); assert_eq!( praxis_routes[0].path_prefix, "/", "missing path value should default to /" ); } #[test] fn test_convert_routes_rejects_method_match() { let mut rule = rule_with_backend(); rule.matches = Some(vec![HttpRouteRulesMatches { method: Some(gateway_api::httproutes::HttpRouteRulesMatchesMethod::Post), ..Default::default() }]); let route = route_with_rules(vec![rule]); let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, _) = convert_routes(&routes, &HashMap::new(), &[]); assert!( praxis_routes.is_empty(), "the Praxis route schema has no method field, so emitting the route would serve \ every method instead of only POST" ); } #[test] fn test_convert_routes_rejects_query_param_match() { let mut rule = rule_with_backend(); rule.matches = Some(vec![HttpRouteRulesMatches { query_params: Some(vec![gateway_api::httproutes::HttpRouteRulesMatchesQueryParams { name: "version".to_owned(), value: "v2".to_owned(), r#type: Some(gateway_api::httproutes::HttpRouteRulesMatchesQueryParamsType::Exact), }]), ..Default::default() }]); let route = route_with_rules(vec![rule]); let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, _) = convert_routes(&routes, &HashMap::new(), &[]); assert!( praxis_routes.is_empty(), "the Praxis route schema has no query parameter field, so the constraint cannot be honoured" ); } #[test] fn test_convert_routes_rejects_regular_expression_path() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::RegularExpression), value: Some("/api/.*".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "svc".to_owned(), port: Some(80), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, status: None, }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, backend_refs) = convert_routes(&routes, &HashMap::new(), &[]); assert!( praxis_routes.is_empty(), "a RegularExpression path must produce no route: widening it to the catch-all prefix / \ would silently hijack every request on the listener" ); assert!( backend_refs.is_empty(), "a rejected rule must not contribute a backend cluster either" ); } #[test] fn test_convert_routes_keeps_supported_rules_alongside_rejected_ones() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("mixed-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![ HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::Exact), value: Some("/ok".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "svc".to_owned(), port: Some(80), ..Default::default() }]), ..Default::default() }, HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::RegularExpression), value: Some("/bad/.*".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "other".to_owned(), port: Some(80), ..Default::default() }]), ..Default::default() }, ]), ..Default::default() }, status: None, }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, _) = convert_routes(&routes, &HashMap::new(), &[]); assert_eq!( praxis_routes.len(), 1, "the supported rule must survive while only the regex rule is dropped" ); assert_eq!( praxis_routes[0].path, Some("/ok".to_owned()), "the surviving route should be the exact-match rule" ); } #[test] fn test_convert_routes_hostname_intersection_filters_non_matching() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { hostnames: Some(vec!["non.matching.com".to_owned(), "very.specific.com".to_owned()]), rules: Some(vec![HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/s1".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "backend".to_owned(), port: Some(8080), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, ..Default::default() }; let mut listener_map = HashMap::new(); listener_map.insert("listener-1".to_owned(), Some("very.specific.com".to_owned())); let routes = vec![AttachedRoute { route: &route, section_names: vec![Some("listener-1".to_owned())], }]; let (praxis_routes, _) = convert_routes(&routes, &listener_map, &[]); let hosts: Vec<_> = praxis_routes.iter().filter_map(|r| r.host.as_deref()).collect(); assert!( hosts.iter().all(|h| *h == "very.specific.com"), "should only have matching hostname, got: {hosts:?}" ); assert!( !hosts.contains(&"non.matching.com"), "non-matching hostname should be filtered out" ); } #[test] fn test_convert_routes_hostname_intersection_wildcard_listener() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { hostnames: Some(vec![ "non.matching.com".to_owned(), "foo.wildcard.io".to_owned(), "bar.wildcard.io".to_owned(), ]), rules: Some(vec![HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/s2".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "backend".to_owned(), port: Some(8080), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, ..Default::default() }; let mut listener_map = HashMap::new(); listener_map.insert("listener-2".to_owned(), Some("*.wildcard.io".to_owned())); let routes = vec![AttachedRoute { route: &route, section_names: vec![Some("listener-2".to_owned())], }]; let (praxis_routes, _) = convert_routes(&routes, &listener_map, &[]); let hosts: Vec<_> = praxis_routes.iter().filter_map(|r| r.host.as_deref()).collect(); assert!( hosts.contains(&"foo.wildcard.io"), "foo.wildcard.io should match *.wildcard.io" ); assert!( hosts.contains(&"bar.wildcard.io"), "bar.wildcard.io should match *.wildcard.io" ); assert!( !hosts.contains(&"non.matching.com"), "non.matching.com should be filtered out" ); } #[test] fn test_convert_routes_no_hostnames_unaffected() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![HttpRouteRules { backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "backend".to_owned(), port: Some(8080), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, ..Default::default() }; let mut listener_map = HashMap::new(); listener_map.insert("listener-1".to_owned(), Some("specific.com".to_owned())); let routes = vec![AttachedRoute { route: &route, section_names: vec![Some("listener-1".to_owned())], }]; let (praxis_routes, _) = convert_routes(&routes, &listener_map, &[]); assert_eq!(praxis_routes.len(), 1, "should produce one catch-all route"); assert_eq!( praxis_routes[0].host, None, "route without hostnames should stay host-less" ); } #[test] fn test_convert_routes_all_hostnames_filtered_produces_no_routes() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { hostnames: Some(vec!["wrong.com".to_owned(), "also.wrong.com".to_owned()]), rules: Some(vec![HttpRouteRules { matches: Some(vec![HttpRouteRulesMatches { path: Some(HttpRouteRulesMatchesPath { r#type: Some(HttpRouteRulesMatchesPathType::PathPrefix), value: Some("/s5".to_owned()), }), ..Default::default() }]), backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "backend".to_owned(), port: Some(8080), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, ..Default::default() }; let mut listener_map = HashMap::new(); listener_map.insert("listener-1".to_owned(), Some("very.specific.com".to_owned())); let routes = vec![AttachedRoute { route: &route, section_names: vec![Some("listener-1".to_owned())], }]; let (praxis_routes, _) = convert_routes(&routes, &listener_map, &[]); assert!( praxis_routes.is_empty(), "routes with no intersecting hostnames should produce no PraxisRoutes" ); } fn make_reference_grant(target_ns: &str, from_ns: &str, service_name: &str) -> ReferenceGrant { use gateway_api::referencegrants::{ReferenceGrantFrom, ReferenceGrantSpec, ReferenceGrantTo}; ReferenceGrant { metadata: ObjectMeta { namespace: Some(target_ns.to_owned()), ..Default::default() }, spec: ReferenceGrantSpec { from: vec![ReferenceGrantFrom { group: "gateway.networking.k8s.io".to_owned(), kind: "HTTPRoute".to_owned(), namespace: from_ns.to_owned(), }], to: vec![ReferenceGrantTo { group: String::new(), kind: "Service".to_owned(), name: Some(service_name.to_owned()), }], }, } } #[test] fn test_convert_routes_cross_ns_backend_without_grant_excluded() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("xns-route".to_owned()), namespace: Some("infra".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![HttpRouteRules { backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "web-backend".to_owned(), namespace: Some("other-ns".to_owned()), port: Some(8080), ..Default::default() }]), ..Default::default() }]), ..Default::default() }, ..Default::default() }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, backend_refs) = convert_routes(&routes, &HashMap::new(), &[]); assert!( praxis_routes.is_empty(), "a rule whose only backend is unauthorized must emit no route: pointing one at a \ cluster that is never created leaves the data plane with a dangling reference" ); assert!( backend_refs.is_empty(), "unauthorized cross-ns backend should be excluded from cluster" ); } #[test] fn test_convert_routes_cluster_name_covers_only_authorized_backends() { let route = HTTPRoute { metadata: ObjectMeta { name: Some("mixed-xns".to_owned()), namespace: Some("infra".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(vec![HttpRouteRules { backend_refs: Some(vec![ HttpRouteRulesBackendRefs { name: "local".to_owned(), port: Some(80), ..Default::default() }, HttpRouteRulesBackendRefs { name: "denied".to_owned(), namespace: Some("other-ns".to_owned()), port: Some(80), ..Default::default() }, ]), ..Default::default() }]), ..Default::default() }, ..Default::default() }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, backend_refs) = convert_routes(&routes, &HashMap::new(), &[]); assert_eq!( praxis_routes[0].cluster, "infra_local_80", "the cluster name must describe only the backends that actually resolve, or the \ generated name promises endpoints the cluster will never contain" ); assert_eq!( backend_refs.len(), 1, "only the authorized backend contributes endpoints" ); } #[test] fn test_config_generation_is_deterministic_across_runs() { let mut yamls = std::collections::BTreeSet::new(); for _ in 0..50 { let mut listener_hostnames = HashMap::new(); listener_hostnames.insert("l1".to_owned(), Some("*.example.com".to_owned())); listener_hostnames.insert("l2".to_owned(), Some("a.example.com".to_owned())); listener_hostnames.insert("l3".to_owned(), Some("*.other.com".to_owned())); listener_hostnames.insert("l4".to_owned(), Some("b.example.com".to_owned())); let route = HTTPRoute { metadata: ObjectMeta { name: Some("r".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { hostnames: Some(vec!["a.example.com".to_owned(), "b.example.com".to_owned()]), rules: Some(vec![rule_with_backend()]), ..Default::default() }, status: None, }; let routes = vec![AttachedRoute { route: &route, section_names: vec![None], }]; let (praxis_routes, _) = convert_routes(&routes, &listener_hostnames, &[]); yamls.insert(yaml_serde::to_string(&praxis_routes).expect("serializes")); } assert_eq!( yamls.len(), 1, "config generation must be deterministic; unstable output churns the config hash and \ rolls the data plane forever. distinct outputs: {yamls:?}" ); } // ----------------------------------------------------------------------------- // Test Utilities // ----------------------------------------------------------------------------- /// Builds a rule with a single backend and no matches. fn rule_with_backend() -> HttpRouteRules { HttpRouteRules { backend_refs: Some(vec![HttpRouteRulesBackendRefs { name: "svc".to_owned(), port: Some(80), ..Default::default() }]), ..Default::default() } } /// Builds an `HTTPRoute` in the default namespace carrying `rules`. fn route_with_rules(rules: Vec) -> HTTPRoute { HTTPRoute { metadata: ObjectMeta { name: Some("test-route".to_owned()), namespace: Some("default".to_owned()), ..Default::default() }, spec: HttpRouteSpec { rules: Some(rules), ..Default::default() }, status: None, } } }