Skip to content

Commit 66009e7

Browse files
authored
Merge pull request #5241 from davidwin93/Fix-GW-Gen
fix(Gateway API): ensure generation match
2 parents 5372915 + f27da2b commit 66009e7

11 files changed

+351
-29
lines changed

source/gateway.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type gatewayRoute interface {
5454
Metadata() *metav1.ObjectMeta
5555
// Hostnames returns the route's specified hostnames.
5656
Hostnames() []v1.Hostname
57+
// ParentRefs returns the route's parent references as defined in the route spec.
58+
ParentRefs() []v1.ParentReference
5759
// Protocol returns the route's protocol type.
5860
Protocol() v1.ProtocolType
5961
// RouteStatus returns the route's common status.
@@ -294,18 +296,31 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar
294296
}
295297
hostTargets := make(map[string]endpoint.Targets)
296298

299+
routeParentRefs := rt.ParentRefs()
300+
301+
if len(routeParentRefs) == 0 {
302+
log.Debugf("No parent references found for %s %s/%s", c.src.rtKind, rt.Metadata().Namespace, rt.Metadata().Name)
303+
return hostTargets, nil
304+
}
305+
297306
meta := rt.Metadata()
298307
for _, rps := range rt.RouteStatus().Parents {
299308
// Confirm the Parent is the standard Gateway kind.
300309
ref := rps.ParentRef
310+
namespace := strVal((*string)(ref.Namespace), meta.Namespace)
311+
// Ensure that the parent reference is in the routeParentRefs list
312+
if !gwRouteHasParentRef(routeParentRefs, ref, meta) {
313+
log.Debugf("Parent reference %s/%s not found in routeParentRefs for %s %s/%s", namespace, string(ref.Name), c.src.rtKind, meta.Namespace, meta.Name)
314+
continue
315+
}
316+
301317
group := strVal((*string)(ref.Group), gatewayGroup)
302318
kind := strVal((*string)(ref.Kind), gatewayKind)
303319
if group != gatewayGroup || kind != gatewayKind {
304320
log.Debugf("Unsupported parent %s/%s for %s %s/%s", group, kind, c.src.rtKind, meta.Namespace, meta.Name)
305321
continue
306322
}
307323
// Lookup the Gateway and its Listeners.
308-
namespace := strVal((*string)(ref.Namespace), meta.Namespace)
309324
gw, ok := c.gws[namespacedName(namespace, string(ref.Name))]
310325
if !ok {
311326
log.Debugf("Gateway %s/%s not found for %s %s/%s", namespace, ref.Name, c.src.rtKind, meta.Namespace, meta.Name)
@@ -316,6 +331,7 @@ func (c *gatewayRouteResolver) resolve(rt gatewayRoute) (map[string]endpoint.Tar
316331
log.Debugf("Gateway %s/%s does not match %s %s/%s", namespace, ref.Name, c.src.gwName, meta.Namespace, meta.Name)
317332
continue
318333
}
334+
319335
// Confirm the Gateway has accepted the Route.
320336
if !gwRouteIsAccepted(rps.Conditions) {
321337
log.Debugf("Gateway %s/%s has not accepted %s %s/%s", namespace, ref.Name, c.src.rtKind, meta.Namespace, meta.Name)
@@ -458,6 +474,26 @@ func (c *gatewayRouteResolver) routeIsAllowed(gw *v1beta1.Gateway, lis *v1.Liste
458474
return false
459475
}
460476

477+
func gwRouteHasParentRef(routeParentRefs []v1.ParentReference, ref v1.ParentReference, meta *metav1.ObjectMeta) bool {
478+
// Ensure that the parent reference is in the routeParentRefs list
479+
namespace := strVal((*string)(ref.Namespace), meta.Namespace)
480+
group := strVal((*string)(ref.Group), gatewayGroup)
481+
kind := strVal((*string)(ref.Kind), gatewayKind)
482+
for _, rpr := range routeParentRefs {
483+
rprGroup := strVal((*string)(rpr.Group), gatewayGroup)
484+
rprKind := strVal((*string)(rpr.Kind), gatewayKind)
485+
if rprGroup != group || rprKind != kind {
486+
continue
487+
}
488+
rprNamespace := strVal((*string)(rpr.Namespace), meta.Namespace)
489+
if string(rpr.Name) != string(ref.Name) || rprNamespace != namespace {
490+
continue
491+
}
492+
return true
493+
}
494+
return false
495+
}
496+
461497
func gwRouteIsAccepted(conds []metav1.Condition) bool {
462498
for _, c := range conds {
463499
if v1.RouteConditionType(c.Type) == v1.RouteConditionAccepted {

source/gateway_grpcroute.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ func NewGatewayGRPCRouteSource(clients ClientGenerator, config *Config) (Source,
3333

3434
type gatewayGRPCRoute struct{ route v1.GRPCRoute } // NOTE: Must update TypeMeta in List when changing the APIVersion.
3535

36-
func (rt *gatewayGRPCRoute) Object() kubeObject { return &rt.route }
37-
func (rt *gatewayGRPCRoute) Metadata() *metav1.ObjectMeta { return &rt.route.ObjectMeta }
38-
func (rt *gatewayGRPCRoute) Hostnames() []v1.Hostname { return rt.route.Spec.Hostnames }
39-
func (rt *gatewayGRPCRoute) Protocol() v1.ProtocolType { return v1.HTTPSProtocolType }
40-
func (rt *gatewayGRPCRoute) RouteStatus() v1.RouteStatus { return rt.route.Status.RouteStatus }
36+
func (rt *gatewayGRPCRoute) Object() kubeObject { return &rt.route }
37+
func (rt *gatewayGRPCRoute) Metadata() *metav1.ObjectMeta { return &rt.route.ObjectMeta }
38+
func (rt *gatewayGRPCRoute) Hostnames() []v1.Hostname { return rt.route.Spec.Hostnames }
39+
func (rt *gatewayGRPCRoute) ParentRefs() []v1.ParentReference { return rt.route.Spec.ParentRefs }
40+
func (rt *gatewayGRPCRoute) Protocol() v1.ProtocolType { return v1.HTTPSProtocolType }
41+
func (rt *gatewayGRPCRoute) RouteStatus() v1.RouteStatus { return rt.route.Status.RouteStatus }
4142

4243
type gatewayGRPCRouteInformer struct {
4344
informers_v1.GRPCRouteInformer

source/gateway_grpcroute_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ func TestGatewayGRPCRouteSourceEndpoints(t *testing.T) {
7474
},
7575
Spec: v1.GRPCRouteSpec{
7676
Hostnames: []v1.Hostname{"api-hostnames.foobar.internal"},
77+
CommonRouteSpec: v1.CommonRouteSpec{
78+
ParentRefs: []v1.ParentReference{
79+
gwParentRef("default", "internal"),
80+
},
81+
},
7782
},
7883
Status: v1.GRPCRouteStatus{
7984
RouteStatus: gwRouteStatus(gwParentRef("default", "internal")),

source/gateway_httproute.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ func NewGatewayHTTPRouteSource(clients ClientGenerator, config *Config) (Source,
3434

3535
type gatewayHTTPRoute struct{ route v1.HTTPRoute } // NOTE: Must update TypeMeta in List when changing the APIVersion.
3636

37-
func (rt *gatewayHTTPRoute) Object() kubeObject { return &rt.route }
38-
func (rt *gatewayHTTPRoute) Metadata() *metav1.ObjectMeta { return &rt.route.ObjectMeta }
39-
func (rt *gatewayHTTPRoute) Hostnames() []v1.Hostname { return rt.route.Spec.Hostnames }
40-
func (rt *gatewayHTTPRoute) Protocol() v1.ProtocolType { return v1.HTTPProtocolType }
41-
func (rt *gatewayHTTPRoute) RouteStatus() v1.RouteStatus { return rt.route.Status.RouteStatus }
37+
func (rt *gatewayHTTPRoute) Object() kubeObject { return &rt.route }
38+
func (rt *gatewayHTTPRoute) Metadata() *metav1.ObjectMeta { return &rt.route.ObjectMeta }
39+
func (rt *gatewayHTTPRoute) Hostnames() []v1.Hostname { return rt.route.Spec.Hostnames }
40+
func (rt *gatewayHTTPRoute) ParentRefs() []v1.ParentReference { return rt.route.Spec.ParentRefs }
41+
func (rt *gatewayHTTPRoute) Protocol() v1.ProtocolType { return v1.HTTPProtocolType }
42+
func (rt *gatewayHTTPRoute) RouteStatus() v1.RouteStatus { return rt.route.Status.RouteStatus }
4243

4344
type gatewayHTTPRouteInformer struct {
4445
informers_v1beta1.HTTPRouteInformer

0 commit comments

Comments
 (0)