Skip to content

Commit

Permalink
Add HttpRoute round-trip test (#11)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Leong <alex@buoyant.io>
  • Loading branch information
adleong authored Jun 14, 2022
1 parent ca97314 commit 569271c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
83 changes: 83 additions & 0 deletions integration/tests/httproute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use k8s_gateway_api::v1alpha2::{
BackendRef, HttpBackendRef, HttpHeaderMatch, HttpRoute, HttpRouteMatch, HttpRouteRule,
HttpRouteSpec,
};
use kube::{api::PostParams, core::ObjectMeta};

#[tokio::test(flavor = "current_thread")]
async fn round_trip() {
tracing::trace!("Initializing client");
let client = kube::Client::try_default()
.await
.expect("failed to initialize k8s client");
let api = kube::Api::<HttpRoute>::default_namespaced(client);

let route = HttpRoute {
metadata: ObjectMeta {
name: Some("bar-route".to_string()),
namespace: None,
labels: Some([("gateway".to_string(), "external-https-prod".to_string())].into()),
..Default::default()
},
spec: HttpRouteSpec {
hostnames: Some(vec!["bar.example.com".to_string()]),
rules: Some(vec![
HttpRouteRule {
backend_refs: Some(vec![
HttpBackendRef {
backend_ref: Some(BackendRef {
name: "bar-v1".to_string(),
port: 8080,
weight: Some(90),
}),
filters: None,
},
HttpBackendRef {
backend_ref: Some(BackendRef {
name: "bar-v2".to_string(),
port: 8080,
weight: Some(10),
}),
filters: None,
},
]),
filters: None,
matches: None,
},
HttpRouteRule {
matches: Some(vec![HttpRouteMatch {
headers: Some(vec![HttpHeaderMatch::Exact {
name: "env".to_string(),
value: "canary".to_string(),
}]),
..HttpRouteMatch::default()
}]),
backend_refs: Some(vec![HttpBackendRef {
backend_ref: Some(BackendRef {
name: "bar-v2".to_string(),
port: 8080,
weight: None,
}),
filters: None,
}]),
filters: None,
},
]),
..HttpRouteSpec::default()
},
status: None,
};
let post_params = PostParams {
field_manager: Some("gateway-api-test".to_string()),
..Default::default()
};
api.create(&post_params, &route)
.await
.expect("failed to create resource");

api.get("bar-route").await.expect("failed to get resource");

api.delete("bar-route", &Default::default())
.await
.expect("failed to delete resource");
}
14 changes: 11 additions & 3 deletions src/v1alpha2/httproute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ use super::*;
/// Filters can be used to specify additional processing steps. Backends specify
/// where matching requests should be routed.
#[derive(
Clone, Debug, kube::CustomResource, serde::Deserialize, serde::Serialize, schemars::JsonSchema,
Clone,
Debug,
Default,
kube::CustomResource,
serde::Deserialize,
serde::Serialize,
schemars::JsonSchema,
)]
#[kube(
group = "gateway.networking.k8s.io",
Expand Down Expand Up @@ -119,7 +125,7 @@ pub struct HttpRouteRule {
///
/// When no rules matching a request have been successfully attached to the
/// parent a request is coming from, a HTTP 404 status code MUST be returned.
pub matches: Option<HttpRouteMatch>,
pub matches: Option<Vec<HttpRouteMatch>>,

/// Filters define the filters that are applied to requests that match this
/// rule.
Expand Down Expand Up @@ -185,7 +191,9 @@ pub struct HttpRouteRule {
/// - name: "version"
/// value "v1"
/// ```
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
#[derive(
Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize, schemars::JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct HttpRouteMatch {
/// Path specifies a HTTP request path matcher. If this field is not
Expand Down
8 changes: 7 additions & 1 deletion src/v1alpha2/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ pub struct ParentReference {

/// CommonRouteSpec defines the common attributes that all Routes MUST include
/// within their spec.
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
#[derive(
Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize, schemars::JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct CommonRouteSpec {
/// ParentRefs references the resources (usually Gateways) that a Route
Expand Down Expand Up @@ -134,6 +136,10 @@ pub struct BackendRef {
///
/// Support for this field varies based on the context where used.
pub weight: Option<u16>,

pub name: String,

pub port: PortNumber,
}

/// RouteConditionType is a type of condition for a route.
Expand Down

0 comments on commit 569271c

Please sign in to comment.