Skip to content

Commit 26abd3e

Browse files
czeslavorandmonkey
andauthored
fix: remove check of parentRef group and kind in validating HTTPRoutes (#5919) (#5939)
* fix: remove check of parentRef group and kind in validating HTTPRoutes * add CHANGELOG (cherry picked from commit 14e51ab) Co-authored-by: Tao Yi <tao.yi@konghq.com>
1 parent 2f432bc commit 26abd3e

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ Adding a new version? You'll need three changes:
9898
- Bump `golang.org/x/net` to `0.23.0` and `google.golang.org/protobuf` to `1.33.0`
9999
To fix [GO-2024-2687](https://pkg.go.dev/vuln/GO-2024-2687) and [GO-2024-2611](https://pkg.go.dev/vuln/GO-2024-2611).
100100
[#5938](https://github.com/Kong/kubernetes-ingress-controller/pull/5938)
101+
- Remove the constraint of items of `parentRefs` can only be empty or
102+
`gateway.network.k8s.io/Gateway` in validating `HTTPRoute`s. If an item in
103+
`parentRefs`'s group/kind is not `gateway.network.k8s.io/Gateway`, the item
104+
is seen as a parent other than the controller and ignored in parentRef check.
105+
[#5919](https://github.com/Kong/kubernetes-ingress-controller/pull/5919)
106+
101107

102108
## [3.1.2]
103109

internal/admission/validation/gateway/httproute.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ func ValidateHTTPRoute(
3636
httproute *gatewayapi.HTTPRoute,
3737
managerClient client.Client,
3838
) (bool, string, error) {
39-
// Validate that the route has valid parentRefs.
40-
if err := ValidateHTTPRouteParentRefs(httproute); err != nil {
41-
return false, fmt.Sprintf("HTTPRoute has invalid parentRefs: %s", err), nil
42-
}
43-
4439
// Check if route is managed by this controller. If not, we don't need to validate it.
4540
routeIsManaged, err := ensureHTTPRouteIsManagedByController(ctx, httproute, managerClient)
4641
if err != nil {
@@ -74,30 +69,25 @@ func ValidateHTTPRoute(
7469
// Validation - HTTPRoute - Private Functions
7570
// -----------------------------------------------------------------------------
7671

77-
// ValidateHTTPRouteParentRefs checks the group/kind of each parentRef in spec and allows only
78-
// empty or `gateway.networking.k8s.io.Gateway`.
79-
func ValidateHTTPRouteParentRefs(httproute *gatewayapi.HTTPRoute) error {
72+
// parentRefIsGateway returns true if the group/kind of ParentReference is empty or gateway.networking.k8s.io/Gateway.
73+
func parentRefIsGateway(parentRef gatewayapi.ParentReference) bool {
8074
const KindGateway = gatewayapi.Kind("Gateway")
8175

82-
for parentRefIndex, parentRef := range httproute.Spec.ParentRefs {
83-
if parentRef.Group != nil && *parentRef.Group != "" && *parentRef.Group != gatewayapi.V1Group {
84-
return fmt.Errorf("parentRefs[%d]: %s is not a supported group for httproute parentRefs, only %s is supported",
85-
parentRefIndex, *parentRef.Group, gatewayapi.V1Group)
86-
}
87-
if parentRef.Kind != nil && *parentRef.Kind != "" && *parentRef.Kind != KindGateway {
88-
return fmt.Errorf("parentRefs[%d]: %s is not a supported kind for httproute parentRefs, only kind %s is supported",
89-
parentRefIndex, *parentRef.Kind, KindGateway)
90-
}
91-
}
92-
93-
return nil
76+
return (parentRef.Group == nil || (*parentRef.Group == "" || *parentRef.Group == gatewayapi.V1Group)) &&
77+
(parentRef.Kind == nil || (*parentRef.Kind == "" || *parentRef.Kind == KindGateway))
9478
}
9579

9680
// ensureHTTPRouteIsManagedByController checks whether the provided HTTPRoute is managed by this controller implementation.
9781
func ensureHTTPRouteIsManagedByController(ctx context.Context, httproute *gatewayapi.HTTPRoute, managerClient client.Client) (bool, error) {
9882
// In order to be sure whether an HTTPRoute resource is managed by this
9983
// controller we ignore references to Gateway resources that do not exist.
10084
for _, parentRef := range httproute.Spec.ParentRefs {
85+
// Skip the parentRefs that are not Gateways because they cannot refer to the controller.
86+
// https://github.com/Kong/kubernetes-ingress-controller/issues/5912
87+
if !parentRefIsGateway(parentRef) {
88+
continue
89+
}
90+
10191
// Determine the namespace of the gateway referenced via parentRef. If no
10292
// explicit namespace is provided, assume the namespace of the route.
10393
namespace := httproute.Namespace

internal/admission/validation/gateway/httproute_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,50 @@ func TestValidateHTTPRoute(t *testing.T) {
7777
},
7878
valid: true,
7979
},
80+
{
81+
msg: "route with parentRef to non-gateway object is accepted with no vlaidation",
82+
route: &gatewayapi.HTTPRoute{
83+
ObjectMeta: metav1.ObjectMeta{
84+
Namespace: corev1.NamespaceDefault,
85+
Name: "testing-httproute",
86+
},
87+
Spec: gatewayapi.HTTPRouteSpec{
88+
CommonRouteSpec: gatewayapi.CommonRouteSpec{
89+
ParentRefs: []gatewayapi.ParentReference{
90+
{
91+
Kind: lo.ToPtr(gatewayapi.Kind("Service")),
92+
Namespace: lo.ToPtr(gatewayapi.Namespace(corev1.NamespaceDefault)),
93+
Name: gatewayapi.ObjectName("kuma-cp"),
94+
},
95+
},
96+
},
97+
},
98+
}, // parentRef to a Service
99+
cachedObjects: []client.Object{
100+
gatewayClass,
101+
&gatewayapi.Gateway{
102+
ObjectMeta: metav1.ObjectMeta{
103+
Namespace: corev1.NamespaceDefault,
104+
Name: "testing-gateway",
105+
},
106+
Spec: gatewayapi.GatewaySpec{
107+
GatewayClassName: gatewayClassName,
108+
Listeners: []gatewayapi.Listener{{
109+
Name: "http",
110+
Port: 80,
111+
Protocol: (gatewayapi.HTTPProtocolType),
112+
AllowedRoutes: &gatewayapi.AllowedRoutes{
113+
Kinds: []gatewayapi.RouteGroupKind{{
114+
Group: &group,
115+
Kind: "HTTPRoute",
116+
}},
117+
},
118+
}},
119+
},
120+
},
121+
},
122+
valid: true,
123+
},
80124
{
81125
msg: "parentRefs which omit the namespace pass validation in the same namespace",
82126
route: &gatewayapi.HTTPRoute{

0 commit comments

Comments
 (0)