Skip to content

Commit 23eb35b

Browse files
committed
fix(sd): support ipv6 for consul and eureka
1 parent dfe43fa commit 23eb35b

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

sd/consul/instancer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package consul
33
import (
44
"errors"
55
"fmt"
6+
"net"
7+
"strconv"
68
"time"
79

810
consul "github.com/hashicorp/consul/api"
@@ -181,7 +183,7 @@ func makeInstances(entries []*consul.ServiceEntry) []string {
181183
if entry.Service.Address != "" {
182184
addr = entry.Service.Address
183185
}
184-
instances[i] = fmt.Sprintf("%s:%d", addr, entry.Service.Port)
186+
instances[i] = net.JoinHostPort(addr, strconv.Itoa(entry.Service.Port))
185187
}
186188
return instances
187189
}

sd/consul/instancer_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ var consulState = []*consul.ServiceEntry{
6161
},
6262
},
6363
},
64+
{
65+
Node: &consul.Node{
66+
Address: "10.0.0.1",
67+
Node: "app01.local",
68+
},
69+
Service: &consul.AgentService{
70+
Address: "2001:db8:1::ab9:C0A8:102",
71+
ID: "search-db-1",
72+
Port: 9000,
73+
Service: "search",
74+
Tags: []string{
75+
"ipv6",
76+
},
77+
},
78+
},
6479
}
6580

6681
func TestInstancer(t *testing.T) {
@@ -135,6 +150,33 @@ func TestInstancerAddressOverride(t *testing.T) {
135150
}
136151
}
137152

153+
func TestInstancerAddressIpv6(t *testing.T) {
154+
s := NewInstancer(newTestClient(consulState), log.NewNopLogger(), "search", []string{"ipv6"}, true)
155+
defer s.Stop()
156+
157+
state := s.cache.State()
158+
if want, have := 1, len(state.Instances); want != have {
159+
t.Fatalf("want %d, have %d", want, have)
160+
}
161+
162+
endpoint, closer, err := testFactory(state.Instances[0])
163+
if err != nil {
164+
t.Fatal(err)
165+
}
166+
if closer != nil {
167+
defer closer.Close()
168+
}
169+
170+
response, err := endpoint(context.Background(), struct{}{})
171+
if err != nil {
172+
t.Fatal(err)
173+
}
174+
175+
if want, have := "[2001:db8:1::ab9:C0A8:102]:9000", response.(string); want != have {
176+
t.Errorf("want %q, have %q", want, have)
177+
}
178+
}
179+
138180
type eofTestClient struct {
139181
client *testClient
140182
eofSig chan bool

sd/eureka/instancer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package eureka
22

33
import (
4-
"fmt"
4+
"net"
5+
"strconv"
56

67
"github.com/hudl/fargo"
78

@@ -84,7 +85,7 @@ func (s *Instancer) getInstances() ([]string, error) {
8485
func convertFargoAppToInstances(app *fargo.Application) []string {
8586
instances := make([]string, len(app.Instances))
8687
for i, inst := range app.Instances {
87-
instances[i] = fmt.Sprintf("%s:%d", inst.IPAddr, inst.Port)
88+
instances[i] = net.JoinHostPort(inst.IPAddr, strconv.Itoa(inst.Port))
8889
}
8990
return instances
9091
}

sd/eureka/instancer_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,19 @@ func TestBadInstancerScheduleUpdates(t *testing.T) {
9090
t.Errorf("want %d, have %d", want, have)
9191
}
9292
}
93+
94+
func TestConvertIpv6Instance(t *testing.T) {
95+
app := &fargo.Application{
96+
Instances: []*fargo.Instance{
97+
{IPAddr: "10.0.0.10", Port: 8000},
98+
{IPAddr: "2001:db8:1::ab9:C0A8:102", Port: 8000},
99+
},
100+
}
101+
expect := []string{"10.0.0.10:8000", "[2001:db8:1::ab9:C0A8:102]:8000"}
102+
for i, inst := range convertFargoAppToInstances(app) {
103+
if inst != expect[i] {
104+
t.Fatalf("instance %s converting is wrong", expect[i])
105+
}
106+
}
107+
108+
}

sd/eureka/registrar.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package eureka
22

33
import (
44
"fmt"
5+
"net"
56
"net/http"
7+
"strconv"
68
"sync"
79
"time"
810

@@ -51,7 +53,7 @@ func NewRegistrar(conn fargoConnection, instance *fargo.Instance, logger log.Log
5153
return &Registrar{
5254
conn: conn,
5355
instance: instance,
54-
logger: log.With(logger, "service", instance.App, "address", fmt.Sprintf("%s:%d", instance.IPAddr, instance.Port)),
56+
logger: log.With(logger, "service", instance.App, "address", net.JoinHostPort(instance.IPAddr, strconv.Itoa(instance.Port))),
5557
}
5658
}
5759

0 commit comments

Comments
 (0)