Skip to content

Commit

Permalink
fix listener address
Browse files Browse the repository at this point in the history
Signed-off-by: zirain <[email protected]>
  • Loading branch information
zirain committed Oct 30, 2024
1 parent c92d106 commit 1498ab7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
13 changes: 10 additions & 3 deletions internal/gatewayapi/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/utils"
"github.com/envoyproxy/gateway/internal/utils/naming"
"github.com/envoyproxy/gateway/internal/utils/net"
)

var _ ListenersTranslator = (*Translator)(nil)
Expand Down Expand Up @@ -99,6 +100,12 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource
if !isReady {
continue
}

// TODO: find a better way to this
address := "0.0.0.0"
if net.IsIPv6Pod() {
address = "::"
}
// Add the listener to the Xds IR
servicePort := &protocolPort{protocol: listener.Protocol, port: int32(listener.Port)}
containerPort := servicePortToContainerPort(int32(listener.Port), gateway.envoyProxy)
Expand All @@ -107,7 +114,7 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource
irListener := &ir.HTTPListener{
CoreListenerDetails: ir.CoreListenerDetails{
Name: irListenerName(listener),
Address: "0.0.0.0",
Address: address,
Port: uint32(containerPort),
Metadata: buildListenerMetadata(listener, gateway),
IPFamily: getIPFamily(gateway.envoyProxy),
Expand All @@ -134,7 +141,7 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource
irListener := &ir.TCPListener{
CoreListenerDetails: ir.CoreListenerDetails{
Name: irListenerName(listener),
Address: "0.0.0.0",
Address: address,
Port: uint32(containerPort),
IPFamily: getIPFamily(gateway.envoyProxy),
},
Expand All @@ -150,7 +157,7 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource
irListener := &ir.UDPListener{
CoreListenerDetails: ir.CoreListenerDetails{
Name: irListenerName(listener),
Address: "0.0.0.0",
Address: address,
Port: uint32(containerPort),
},
}
Expand Down
11 changes: 10 additions & 1 deletion internal/utils/net/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package net

import "net"
import (
"net"
"os"
)

func IsIPv6(s string) bool {
ip := net.ParseIP(s)
Expand All @@ -14,3 +17,9 @@ func IsIPv6(s string) bool {
}
return ip.To4() == nil
}

// IsIPv6Pod returns true if the POD_IP environment variable is an IPv6 address.
// WARNING: This function is only intended to be used in the context of Kubernetes.
func IsIPv6Pod() bool {
return IsIPv6(os.Getenv("POD_IP"))
}
7 changes: 2 additions & 5 deletions internal/xds/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
// Register embed
_ "embed"
"fmt"
"os"
"strings"
"text/template"

Expand Down Expand Up @@ -51,16 +50,14 @@ const (
)

func AdminAddress() string {
podIP := os.Getenv("POD_IP")
if netutils.IsIPv6(podIP) {
if netutils.IsIPv6Pod() {
return envoyAdminAddressIPv6
}
return envoyAdminAddress
}

func readinessAddress() string {
podIP := os.Getenv("POD_IP")
if netutils.IsIPv6(podIP) {
if netutils.IsIPv6Pod() {
return envoyReadinessAddressIPv6
}
return envoyReadinessAddress
Expand Down
13 changes: 6 additions & 7 deletions internal/xds/translator/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,12 @@ func setAddressByIPFamily(socketAddress *corev3.SocketAddress, ipFamily *ir.IPFa
if ipFamily == nil {
return nil
}
switch *ipFamily {
case ir.IPv4:
socketAddress.Address = "0.0.0.0"
case ir.IPv6:
socketAddress.Address = "::"
case ir.Dualstack:
socketAddress.Address = "0.0.0.0"
if *ipFamily == ir.Dualstack {
// case ir.IPv4:
// socketAddress.Address = "0.0.0.0"
// case ir.IPv6:
// socketAddress.Address = "::"
// socketAddress.Address = "0.0.0.0"
return []*listenerv3.AdditionalAddress{
{
Address: &corev3.Address{
Expand Down

0 comments on commit 1498ab7

Please sign in to comment.