Skip to content

Commit

Permalink
[WIP] ipv6 formatting fixed (#3075)
Browse files Browse the repository at this point in the history
Signed-off-by: Jhoysbou <neeroll21@gmail.com>
Co-authored-by: Bernd Verst <github@bernd.dev>
  • Loading branch information
Jhoysbou and berndverst authored Sep 1, 2023
1 parent 75ac751 commit fe5ce08
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
16 changes: 13 additions & 3 deletions nameresolution/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
50 changes: 40 additions & 10 deletions nameresolution/consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
},
},
{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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: "",
Expand All @@ -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: "",
Expand All @@ -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)
},
},
{
Expand Down Expand Up @@ -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,
},
},
Expand Down

0 comments on commit fe5ce08

Please sign in to comment.