[WIP] ipv6 formatting fixed (#3075)

Signed-off-by: Jhoysbou <neeroll21@gmail.com>
Co-authored-by: Bernd Verst <github@bernd.dev>
This commit is contained in:
Artem Bakuta 2023-09-01 23:46:29 +02:00 committed by GitHub
parent 75ac751803
commit fe5ce08349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 13 deletions

View File

@ -149,14 +149,24 @@ func (r *resolver) ResolveID(req nr.ResolveRequest) (addr string, err error) {
}
if svc.Service.Address != "" {
addr = svc.Service.Address + ":" + port
addr = svc.Service.Address
} else if svc.Node.Address != "" {
addr = svc.Node.Address + ":" + port
addr = svc.Node.Address
} else {
return "", fmt.Errorf("no healthy services found with AppID '%s'", req.ID)
}
return addr, nil
return formatAddress(addr, port)
}
func formatAddress(address string, port string) (addr string, err error) {
if net.ParseIP(address).To4() != nil {
return address + ":" + port, nil
} else if net.ParseIP(address).To16() != nil {
return fmt.Sprintf("[%s]:%s", address, port), nil
}
return "", fmt.Errorf("invalid ip address %s", address)
}
// getConfig configuration from metadata, defaults are best suited for self-hosted mode.

View File

@ -207,7 +207,7 @@ func TestResolveID(t *testing.T) {
serviceResult: []*consul.ServiceEntry{
{
Service: &consul.AgentService{
Address: "123.234.345.456",
Address: "123.234.245.255",
Port: 8600,
Meta: map[string]string{
"DAPR_PORT": "50005",
@ -222,7 +222,37 @@ func TestResolveID(t *testing.T) {
addr, _ := resolver.ResolveID(req)
assert.Equal(t, "123.234.345.456:50005", addr)
assert.Equal(t, "123.234.245.255:50005", addr)
},
},
{
"should get ipv6 address from service",
nr.ResolveRequest{
ID: "test-app",
},
func(t *testing.T, req nr.ResolveRequest) {
t.Helper()
mock := mockClient{
mockHealth: mockHealth{
serviceResult: []*consul.ServiceEntry{
{
Service: &consul.AgentService{
Address: "2001:db8:3333:4444:5555:6666:7777:8888",
Port: 8600,
Meta: map[string]string{
"DAPR_PORT": "50005",
},
},
},
},
},
}
resolver := newResolver(logger.NewLogger("test"), &mock)
resolver.config = testConfig
addr, _ := resolver.ResolveID(req)
assert.Equal(t, "[2001:db8:3333:4444:5555:6666:7777:8888]:50005", addr)
},
},
{
@ -237,7 +267,7 @@ func TestResolveID(t *testing.T) {
serviceResult: []*consul.ServiceEntry{
{
Service: &consul.AgentService{
Address: "123.234.345.456",
Address: "123.234.245.255",
Port: 8600,
Meta: map[string]string{
"DAPR_PORT": "50005",
@ -246,7 +276,7 @@ func TestResolveID(t *testing.T) {
},
{
Service: &consul.AgentService{
Address: "234.345.456.678",
Address: "234.245.255.228",
Port: 8600,
Meta: map[string]string{
"DAPR_PORT": "50005",
@ -264,9 +294,9 @@ func TestResolveID(t *testing.T) {
for i := 0; i < 100; i++ {
addr, _ := resolver.ResolveID(req)
if addr == "123.234.345.456:50005" {
if addr == "123.234.245.255:50005" {
total1++
} else if addr == "234.345.456.678:50005" {
} else if addr == "234.245.255.228:50005" {
total2++
} else {
t.Fatalf("Received unexpected address: %s", addr)
@ -291,7 +321,7 @@ func TestResolveID(t *testing.T) {
serviceResult: []*consul.ServiceEntry{
{
Node: &consul.Node{
Address: "999.888.777",
Address: "123.234.245.255",
},
Service: &consul.AgentService{
Address: "",
@ -303,7 +333,7 @@ func TestResolveID(t *testing.T) {
},
{
Node: &consul.Node{
Address: "999.888.777",
Address: "123.234.245.255",
},
Service: &consul.AgentService{
Address: "",
@ -321,7 +351,7 @@ func TestResolveID(t *testing.T) {
addr, _ := resolver.ResolveID(req)
assert.Equal(t, "999.888.777:50005", addr)
assert.Equal(t, "123.234.245.255:50005", addr)
},
},
{
@ -366,7 +396,7 @@ func TestResolveID(t *testing.T) {
serviceResult: []*consul.ServiceEntry{
{
Service: &consul.AgentService{
Address: "123.234.345.456",
Address: "123.234.145.155",
Port: 8600,
},
},