From e770d05b083fd174470f53dec16577b0d7e64046 Mon Sep 17 00:00:00 2001 From: Gabor Retvari Date: Thu, 6 Jul 2023 21:50:47 +0200 Subject: [PATCH] fix: Set default Namespace in ParentRef from UDPRoute.Namespace --- internal/controllers/udproute.go | 2 +- internal/renderer/udproute_util.go | 12 ++++++++++-- internal/renderer/udproute_util_test.go | 8 ++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/internal/controllers/udproute.go b/internal/controllers/udproute.go index 8ca996e..82c7b25 100644 --- a/internal/controllers/udproute.go +++ b/internal/controllers/udproute.go @@ -114,7 +114,7 @@ func RegisterUDPRouteController(mgr manager.Manager, ch chan event.Event, log lo // Reconcile handles an update to a UDPRoute or a Service/Endpoints referenced by an UDPRoute. func (r *udpRouteReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { - log := r.log.WithValues("udproute", req.String()) + log := r.log.WithValues("object", req.String()) log.Info("reconciling") routeList := []client.Object{} diff --git a/internal/renderer/udproute_util.go b/internal/renderer/udproute_util.go index cecabf9..50aabe6 100644 --- a/internal/renderer/udproute_util.go +++ b/internal/renderer/udproute_util.go @@ -62,14 +62,21 @@ func resolveParentRef(ro *gwapiv1a2.UDPRoute, p *gwapiv1a2.ParentReference, gw * return false, fmt.Sprintf("parent group %q does not match gateway group %q", string(*p.Group), gwapiv1a2.GroupVersion.Group) } + if p.Kind != nil && *p.Kind != "Gateway" { return false, fmt.Sprintf("parent kind %q does not match gateway kind %q", string(*p.Kind), "Gateway") } - if p.Namespace != nil && *p.Namespace != gwapiv1a2.Namespace(gw.GetNamespace()) { + + namespace := gwapiv1a2.Namespace(ro.GetNamespace()) + if p.Namespace != nil { + namespace = *p.Namespace + } + if namespace != gwapiv1a2.Namespace(gw.GetNamespace()) { return false, fmt.Sprintf("parent namespace %q does not match gateway namespace %q", - string(*p.Namespace), gw.GetNamespace()) + string(namespace), gw.GetNamespace()) } + if p.Name != gwapiv1a2.ObjectName(gw.GetName()) { return false, fmt.Sprintf("parent name %q does not match gateway name %q", string(p.Name), gw.GetName()) @@ -78,6 +85,7 @@ func resolveParentRef(ro *gwapiv1a2.UDPRoute, p *gwapiv1a2.ParentReference, gw * if !allowed { return false, msg } + if p.SectionName != nil && *p.SectionName != l.Name { return false, fmt.Sprintf("parent SectionName %q does not match listener name %q", string(*p.SectionName), l.Name) diff --git a/internal/renderer/udproute_util_test.go b/internal/renderer/udproute_util_test.go index 069ee08..422f011 100644 --- a/internal/renderer/udproute_util_test.go +++ b/internal/renderer/udproute_util_test.go @@ -145,6 +145,7 @@ func TestRenderUDPRouteUtil(t *testing.T) { udp2.SetName("udproute-dummy-namespace") udp2.SetNamespace("dummy-namespace") udp2.Spec.CommonRouteSpec.ParentRefs[0].Name = "gateway-1" + udp2.Spec.CommonRouteSpec.ParentRefs[0].Namespace = &testutils.TestNsName udp2.Spec.CommonRouteSpec.ParentRefs[0].SectionName = nil c.rs = []gwapiv1a2.UDPRoute{*udp1, *udp2} @@ -239,6 +240,7 @@ func TestRenderUDPRouteUtil(t *testing.T) { udp2.SetName("udproute-dummy-namespace") udp2.SetNamespace("dummy-namespace") udp2.Spec.CommonRouteSpec.ParentRefs[0].Name = "gateway-1" + udp2.Spec.CommonRouteSpec.ParentRefs[0].Namespace = &testutils.TestNsName udp2.Spec.CommonRouteSpec.ParentRefs[0].SectionName = nil c.rs = []gwapiv1a2.UDPRoute{*udp1, *udp2} @@ -482,6 +484,7 @@ func TestRenderUDPRouteUtil(t *testing.T) { udp1.SetName("udproute-dummy-namespace-listener-udp") udp1.SetNamespace("dummy-namespace") udp1.Spec.CommonRouteSpec.ParentRefs[0].Name = "gateway-1" + udp1.Spec.CommonRouteSpec.ParentRefs[0].Namespace = &testutils.TestNsName udp1.Spec.CommonRouteSpec.ParentRefs[0].SectionName = &sn1 sn2 := gwapiv1a2.SectionName("gateway-1-listener-tcp") @@ -489,6 +492,7 @@ func TestRenderUDPRouteUtil(t *testing.T) { udp2.SetName("udproute-dummy-namespace-listener-tcp") udp2.SetNamespace("dummy-namespace") udp2.Spec.CommonRouteSpec.ParentRefs[0].Name = "gateway-1" + udp2.Spec.CommonRouteSpec.ParentRefs[0].Namespace = &testutils.TestNsName udp2.Spec.CommonRouteSpec.ParentRefs[0].SectionName = &sn2 c.rs = []gwapiv1a2.UDPRoute{*udp1, *udp2} @@ -565,10 +569,12 @@ func TestRenderUDPRouteUtil(t *testing.T) { udp1.Spec.CommonRouteSpec.ParentRefs = []gwapiv1a2.ParentReference{ { Name: "gateway-1", + Namespace: &testutils.TestNsName, SectionName: &testutils.TestSectionName, }, { Name: "gateway-1", + Namespace: &testutils.TestNsName, SectionName: &sn, }, } @@ -579,10 +585,12 @@ func TestRenderUDPRouteUtil(t *testing.T) { udp2.Spec.CommonRouteSpec.ParentRefs = []gwapiv1a2.ParentReference{ { Name: "gateway-1", + Namespace: &testutils.TestNsName, SectionName: &testutils.TestSectionName, }, { Name: "gateway-1", + Namespace: &testutils.TestNsName, SectionName: &sn, }, }