Skip to content

Commit b285b83

Browse files
authored
Add BackendObjectReference to BackendRef (#47)
BackendRef is a shared type used by routes that may match traffic to an arbitrary backend (other than the `Service` they are attached to). Our current bindings take a simplified approach of representing this by assuming the type is always a `Service`. Several fields are missing (gvk, namespace). This makes it hard to work with the types in code, and it makes it hard to add validation logic since the unserialized objects will be missing the gvk reference. This change removes the fields in favour of adding an inner `BackendObjectReference` type that already exists in the bindings. This will make it so that any serialization takes into account the actual type of the backend the route has. Signed-off-by: Matei David <[email protected]>
1 parent 1b0af25 commit b285b83

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

integration/tests/httproute.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use k8s_gateway_api::{
2-
BackendRef, HttpBackendRef, HttpHeaderMatch, HttpRoute, HttpRouteMatch, HttpRouteRule,
3-
HttpRouteSpec,
2+
BackendObjectReference, BackendRef, HttpBackendRef, HttpHeaderMatch, HttpRoute, HttpRouteMatch,
3+
HttpRouteRule, HttpRouteSpec,
44
};
55
use kube::{api::PostParams, core::ObjectMeta};
66

@@ -26,16 +26,14 @@ async fn round_trip() {
2626
backend_refs: Some(vec![
2727
HttpBackendRef {
2828
backend_ref: Some(BackendRef {
29-
name: "bar-v1".to_string(),
30-
port: 8080,
29+
inner: mk_inner_ref("bar-v1", None, Some(8080)),
3130
weight: Some(90),
3231
}),
3332
filters: None,
3433
},
3534
HttpBackendRef {
3635
backend_ref: Some(BackendRef {
37-
name: "bar-v2".to_string(),
38-
port: 8080,
36+
inner: mk_inner_ref("bar-v2", None, Some(8080)),
3937
weight: Some(10),
4038
}),
4139
filters: None,
@@ -54,8 +52,7 @@ async fn round_trip() {
5452
}]),
5553
backend_refs: Some(vec![HttpBackendRef {
5654
backend_ref: Some(BackendRef {
57-
name: "bar-v2".to_string(),
58-
port: 8080,
55+
inner: mk_inner_ref("bar-v2", None, Some(8080)),
5956
weight: None,
6057
}),
6158
filters: None,
@@ -81,3 +78,17 @@ async fn round_trip() {
8178
.await
8279
.expect("failed to delete resource");
8380
}
81+
82+
fn mk_inner_ref(
83+
name: &str,
84+
namespace: Option<String>,
85+
port: Option<u16>,
86+
) -> BackendObjectReference {
87+
BackendObjectReference {
88+
group: None, // core group inferred
89+
kind: None, // defaults to Service when not specified
90+
name: name.to_string(),
91+
namespace, // defaults to local namespace when unspecified
92+
port,
93+
}
94+
}

src/shared.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1;
22

3+
use crate::BackendObjectReference;
4+
35
/// ParentReference identifies an API object (usually a Gateway) that can be considered
46
/// a parent of this resource (usually a route). The only kind of parent resource
57
/// with "Core" support is Gateway. This API may be extended in the future to
@@ -141,9 +143,9 @@ pub struct BackendRef {
141143
/// Support for this field varies based on the context where used.
142144
pub weight: Option<u16>,
143145

144-
pub name: String,
145-
146-
pub port: PortNumber,
146+
/// BackendObjectReference references a Kubernetes object.
147+
#[serde(flatten)]
148+
pub inner: BackendObjectReference,
147149
}
148150

149151
/// RouteConditionType is a type of condition for a route.

0 commit comments

Comments
 (0)