diff --git a/internal/gatewayapi/listener.go b/internal/gatewayapi/listener.go index 90d817668f7a..cc1561d4c146 100644 --- a/internal/gatewayapi/listener.go +++ b/internal/gatewayapi/listener.go @@ -25,11 +25,6 @@ import ( "github.com/envoyproxy/gateway/internal/utils/net" ) -const ( - ipv4ListenerAddress = "0.0.0.0" - ipv6ListenerAddress = "::1" -) - var _ ListenersTranslator = (*Translator)(nil) type ListenersTranslator interface { @@ -106,9 +101,9 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource continue } - address := ipv4ListenerAddress + address := net.IPv4ListenerAddress if net.PreferIPFamily(t.IPv6First, gateway.envoyProxy) == egv1a1.IPv6 { - address = ipv6ListenerAddress + address = net.IPv6ListenerAddress } // Add the listener to the Xds IR diff --git a/internal/utils/net/ip.go b/internal/utils/net/ip.go index be130938c36c..99bde677062a 100644 --- a/internal/utils/net/ip.go +++ b/internal/utils/net/ip.go @@ -12,6 +12,11 @@ import ( egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" ) +const ( + IPv4ListenerAddress = "0.0.0.0" + IPv6ListenerAddress = "::" +) + func IsIPv6(s string) bool { ip := net.ParseIP(s) if ip == nil { diff --git a/internal/xds/translator/listener.go b/internal/xds/translator/listener.go index b938f52cefa6..c4722c7e6deb 100644 --- a/internal/xds/translator/listener.go +++ b/internal/xds/translator/listener.go @@ -35,6 +35,7 @@ import ( egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" "github.com/envoyproxy/gateway/internal/ir" + "github.com/envoyproxy/gateway/internal/utils/net" "github.com/envoyproxy/gateway/internal/utils/protocov" xdsfilters "github.com/envoyproxy/gateway/internal/xds/filters" ) @@ -190,10 +191,10 @@ func additionalAddressByIPFamily(currentAddress string, ipFamily *ir.IPFamily, p } if *ipFamily == ir.Dualstack { - additionalAddress := "::1" + additionalAddress := net.IPv6ListenerAddress // If the current address is already IPv6, use the IPv4 equivalent - if currentAddress == "::1" { - additionalAddress = "0.0.0.0" + if currentAddress == net.IPv6ListenerAddress { + additionalAddress = net.IPv4ListenerAddress } return []*listenerv3.AdditionalAddress{ { diff --git a/internal/xds/translator/testdata/out/xds-ir/tcp-listener-ipfamily.listeners.yaml b/internal/xds/translator/testdata/out/xds-ir/tcp-listener-ipfamily.listeners.yaml index 20373d4501b6..0615ffcff8ad 100644 --- a/internal/xds/translator/testdata/out/xds-ir/tcp-listener-ipfamily.listeners.yaml +++ b/internal/xds/translator/testdata/out/xds-ir/tcp-listener-ipfamily.listeners.yaml @@ -1,7 +1,7 @@ - additionalAddresses: - address: socketAddress: - address: ::1 + address: '::' portValue: 8082 address: socketAddress: