diff --git a/nameresolution/consul/consul.go b/nameresolution/consul/consul.go index 63e0b10f23..e37e4db7c4 100644 --- a/nameresolution/consul/consul.go +++ b/nameresolution/consul/consul.go @@ -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. diff --git a/nameresolution/consul/consul_test.go b/nameresolution/consul/consul_test.go index 1b378178c3..3efe3c17a5 100644 --- a/nameresolution/consul/consul_test.go +++ b/nameresolution/consul/consul_test.go @@ -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, }, },