Skip to content

Commit

Permalink
fix: Set default Namespace in ParentRef from UDPRoute.Namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Jul 6, 2023
1 parent 7a41047 commit e770d05
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/controllers/udproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
12 changes: 10 additions & 2 deletions internal/renderer/udproute_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions internal/renderer/udproute_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -482,13 +484,15 @@ 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")
udp2 := testutils.TestUDPRoute.DeepCopy()
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}
Expand Down Expand Up @@ -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,
},
}
Expand All @@ -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,
},
}
Expand Down

0 comments on commit e770d05

Please sign in to comment.