Skip to content

Commit

Permalink
fix listener address
Browse files Browse the repository at this point in the history
Signed-off-by: zirain <zirain2009@gmail.com>
  • Loading branch information
zirain committed Oct 30, 2024
1 parent e44b1f9 commit c1b335b
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 = "::"
}

Check warning on line 108 in internal/gatewayapi/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/listener.go#L107-L108

Added lines #L107 - L108 were not covered by tests
// 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"))

Check warning on line 24 in internal/utils/net/ip.go

View check run for this annotation

Codecov / codecov/patch

internal/utils/net/ip.go#L23-L24

Added lines #L23 - L24 were not covered by tests
}
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
}

Check warning on line 55 in internal/xds/bootstrap/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/bootstrap/bootstrap.go#L54-L55

Added lines #L54 - L55 were not covered by tests
return envoyAdminAddress
}

func readinessAddress() string {
podIP := os.Getenv("POD_IP")
if netutils.IsIPv6(podIP) {
if netutils.IsIPv6Pod() {
return envoyReadinessAddressIPv6
}

Check warning on line 62 in internal/xds/bootstrap/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/bootstrap/bootstrap.go#L61-L62

Added lines #L61 - L62 were not covered by tests
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 c1b335b

Please sign in to comment.