Skip to content

Commit 8c45c77

Browse files
committed
Validate k8s_gateway_api::HttpRouteSpec
Signed-off-by: Takumi Sue <[email protected]>
1 parent 49eb879 commit 8c45c77

File tree

1 file changed

+76
-27
lines changed

1 file changed

+76
-27
lines changed

policy-controller/src/admission.rs

+76-27
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ impl Admission {
123123
return self.admit_spec::<HttpRouteSpec>(req).await;
124124
}
125125

126+
if is_kind::<k8s_gateway_api::HttpRoute>(&req) {
127+
return self.admit_spec::<k8s_gateway_api::HttpRouteSpec>(req).await;
128+
}
129+
126130
AdmissionResponse::invalid(format_args!(
127131
"unsupported resource type: {}.{}.{}",
128132
req.kind.group, req.kind.version, req.kind.kind
@@ -422,36 +426,35 @@ impl Validate<ServerAuthorizationSpec> for Admission {
422426
}
423427
}
424428

425-
#[async_trait::async_trait]
426-
impl Validate<HttpRouteSpec> for Admission {
427-
async fn validate(self, _ns: &str, _name: &str, spec: HttpRouteSpec) -> Result<()> {
428-
use index::http_route;
429-
430-
fn validate_match(
431-
httproute::HttpRouteMatch {
432-
path,
433-
headers,
434-
query_params,
435-
method,
436-
}: httproute::HttpRouteMatch,
437-
) -> Result<()> {
438-
let _ = path.map(http_route::path_match).transpose()?;
439-
let _ = method
440-
.as_deref()
441-
.map(core::http_route::Method::try_from)
442-
.transpose()?;
443-
444-
for q in query_params.into_iter().flatten() {
445-
http_route::query_param_match(q)?;
446-
}
429+
use index::http_route;
430+
fn validate_match(
431+
httproute::HttpRouteMatch {
432+
path,
433+
headers,
434+
query_params,
435+
method,
436+
}: httproute::HttpRouteMatch,
437+
) -> Result<()> {
438+
let _ = path.map(http_route::path_match).transpose()?;
439+
let _ = method
440+
.as_deref()
441+
.map(core::http_route::Method::try_from)
442+
.transpose()?;
443+
444+
for q in query_params.into_iter().flatten() {
445+
http_route::query_param_match(q)?;
446+
}
447447

448-
for h in headers.into_iter().flatten() {
449-
http_route::header_match(h)?;
450-
}
448+
for h in headers.into_iter().flatten() {
449+
http_route::header_match(h)?;
450+
}
451451

452-
Ok(())
453-
}
452+
Ok(())
453+
}
454454

455+
#[async_trait::async_trait]
456+
impl Validate<HttpRouteSpec> for Admission {
457+
async fn validate(self, _ns: &str, _name: &str, spec: HttpRouteSpec) -> Result<()> {
455458
fn validate_filter(filter: httproute::HttpRouteFilter) -> Result<()> {
456459
match filter {
457460
httproute::HttpRouteFilter::RequestHeaderModifier {
@@ -516,3 +519,49 @@ impl Validate<HttpRouteSpec> for Admission {
516519
Ok(())
517520
}
518521
}
522+
523+
#[async_trait::async_trait]
524+
impl Validate<k8s_gateway_api::HttpRouteSpec> for Admission {
525+
async fn validate(
526+
self,
527+
_ns: &str,
528+
_name: &str,
529+
spec: k8s_gateway_api::HttpRouteSpec,
530+
) -> Result<()> {
531+
fn validate_filter(filter: k8s_gateway_api::HttpRouteFilter) -> Result<()> {
532+
match filter {
533+
k8s_gateway_api::HttpRouteFilter::RequestHeaderModifier {
534+
request_header_modifier,
535+
} => http_route::header_modifier(request_header_modifier).map(|_| ()),
536+
k8s_gateway_api::HttpRouteFilter::ResponseHeaderModifier {
537+
response_header_modifier,
538+
} => http_route::header_modifier(response_header_modifier).map(|_| ()),
539+
k8s_gateway_api::HttpRouteFilter::RequestRedirect { request_redirect } => {
540+
http_route::req_redirect(request_redirect).map(|_| ())
541+
}
542+
k8s_gateway_api::HttpRouteFilter::RequestMirror { .. } => Ok(()),
543+
k8s_gateway_api::HttpRouteFilter::URLRewrite { .. } => Ok(()),
544+
k8s_gateway_api::HttpRouteFilter::ExtensionRef { .. } => Ok(()),
545+
}
546+
}
547+
548+
// Validate the rules in this spec.
549+
// This is essentially equivalent to the indexer's conversion function
550+
// from `HttpRouteSpec` to `InboundRouteBinding`, except that we don't
551+
// actually allocate stuff in order to return an `InboundRouteBinding`.
552+
for k8s_gateway_api::HttpRouteRule {
553+
filters, matches, ..
554+
} in spec.rules.into_iter().flatten()
555+
{
556+
for m in matches.into_iter().flatten() {
557+
validate_match(m)?;
558+
}
559+
560+
for f in filters.into_iter().flatten() {
561+
validate_filter(f)?;
562+
}
563+
}
564+
565+
Ok(())
566+
}
567+
}

0 commit comments

Comments
 (0)