diff --git a/controllers/dnspolicy_dnsrecords.go b/controllers/dnspolicy_dnsrecords.go index 628b8918a..f9017ea5b 100644 --- a/controllers/dnspolicy_dnsrecords.go +++ b/controllers/dnspolicy_dnsrecords.go @@ -57,11 +57,12 @@ func (r *DNSPolicyReconciler) reconcileGatewayDNSRecords(ctx context.Context, ga log.V(3).Info("checking gateway for attached routes ", "gateway", gateway.Name) for _, listener := range gateway.Spec.Listeners { - listenerHost := *listener.Hostname - if listenerHost == "" { - log.Info("skipping listener no hostname assigned", listener.Name, "in ns ", gateway.Namespace) + + if listener.Hostname == nil || *listener.Hostname == "" { + log.Info("skipping listener no hostname assigned", "listener", listener.Name, "in ns ", gateway.Namespace) continue } + hasAttachedRoute := false for _, statusListener := range gateway.Status.Listeners { if string(listener.Name) == string(statusListener.Name) { diff --git a/tests/common/dnspolicy/dnspolicy_controller_test.go b/tests/common/dnspolicy/dnspolicy_controller_test.go index 5b8b688fa..92ec7d3a2 100644 --- a/tests/common/dnspolicy/dnspolicy_controller_test.go +++ b/tests/common/dnspolicy/dnspolicy_controller_test.go @@ -80,6 +80,7 @@ var _ = Describe("DNSPolicy controller", func() { It("should validate loadBalancing field correctly", func(ctx SpecContext) { gateway = tests.NewGatewayBuilder("test-gateway", gatewayClass.Name, testNamespace). + WithHTTPListener(tests.ListenerNameOne, ""). WithHTTPListener(tests.ListenerNameOne, tests.HostTwo(domain)).Gateway // simple should succeed @@ -140,6 +141,7 @@ var _ = Describe("DNSPolicy controller", func() { It("should validate provider ref field correctly", func(ctx SpecContext) { gateway = tests.NewGatewayBuilder("test-gateway", gatewayClass.Name, testNamespace). + WithHTTPListener(tests.ListenerNameOne, ""). WithHTTPListener(tests.ListenerNameOne, tests.HostTwo(domain)).Gateway // should not allow an empty providerRef list diff --git a/tests/common/tlspolicy/tlspolicy_controller_test.go b/tests/common/tlspolicy/tlspolicy_controller_test.go index 847f4dbfe..90036e0c7 100644 --- a/tests/common/tlspolicy/tlspolicy_controller_test.go +++ b/tests/common/tlspolicy/tlspolicy_controller_test.go @@ -115,6 +115,7 @@ var _ = Describe("TLSPolicy controller", func() { By("creating a valid Gateway") gateway = tests.NewGatewayBuilder("test-gateway", gatewayClass.Name, testNamespace). + WithHTTPListener("no-host", ""). WithHTTPListener("test-listener", "test.example.com").Gateway Expect(k8sClient.Create(ctx, gateway)).To(Succeed()) diff --git a/tests/commons.go b/tests/commons.go index 820bd235c..928f5b166 100644 --- a/tests/commons.go +++ b/tests/commons.go @@ -548,14 +548,20 @@ func (t *GatewayBuilder) WithLabels(labels map[string]string) *GatewayBuilder { return t } -func (t *GatewayBuilder) WithHTTPListener(name, hostname string) *GatewayBuilder { - typedHostname := gatewayapiv1.Hostname(hostname) - t.WithListener(gatewayapiv1.Listener{ +func (t *GatewayBuilder) WithHTTPListener(name string, hostname string) *GatewayBuilder { + + var typedHostname gatewayapiv1.Hostname + gl := gatewayapiv1.Listener{ Name: gatewayapiv1.SectionName(name), - Hostname: &typedHostname, Port: gatewayapiv1.PortNumber(80), Protocol: gatewayapiv1.HTTPProtocolType, - }) + } + if hostname != "" { + typedHostname = gatewayapiv1.Hostname(hostname) + gl.Hostname = &typedHostname + } + + t.WithListener(gl) return t }