Skip to content

Commit

Permalink
fix(sd): support ipv6 for consul and eureka
Browse files Browse the repository at this point in the history
  • Loading branch information
tony612 committed Sep 14, 2023
1 parent dfe43fa commit 23eb35b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
4 changes: 3 additions & 1 deletion sd/consul/instancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package consul
import (
"errors"
"fmt"
"net"
"strconv"
"time"

consul "github.com/hashicorp/consul/api"
Expand Down Expand Up @@ -181,7 +183,7 @@ func makeInstances(entries []*consul.ServiceEntry) []string {
if entry.Service.Address != "" {
addr = entry.Service.Address
}
instances[i] = fmt.Sprintf("%s:%d", addr, entry.Service.Port)
instances[i] = net.JoinHostPort(addr, strconv.Itoa(entry.Service.Port))
}
return instances
}
42 changes: 42 additions & 0 deletions sd/consul/instancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ var consulState = []*consul.ServiceEntry{
},
},
},
{
Node: &consul.Node{
Address: "10.0.0.1",
Node: "app01.local",
},
Service: &consul.AgentService{
Address: "2001:db8:1::ab9:C0A8:102",
ID: "search-db-1",
Port: 9000,
Service: "search",
Tags: []string{
"ipv6",
},
},
},
}

func TestInstancer(t *testing.T) {
Expand Down Expand Up @@ -135,6 +150,33 @@ func TestInstancerAddressOverride(t *testing.T) {
}
}

func TestInstancerAddressIpv6(t *testing.T) {
s := NewInstancer(newTestClient(consulState), log.NewNopLogger(), "search", []string{"ipv6"}, true)
defer s.Stop()

state := s.cache.State()
if want, have := 1, len(state.Instances); want != have {
t.Fatalf("want %d, have %d", want, have)
}

endpoint, closer, err := testFactory(state.Instances[0])
if err != nil {
t.Fatal(err)
}
if closer != nil {
defer closer.Close()
}

response, err := endpoint(context.Background(), struct{}{})
if err != nil {
t.Fatal(err)
}

if want, have := "[2001:db8:1::ab9:C0A8:102]:9000", response.(string); want != have {
t.Errorf("want %q, have %q", want, have)
}
}

type eofTestClient struct {
client *testClient
eofSig chan bool
Expand Down
5 changes: 3 additions & 2 deletions sd/eureka/instancer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package eureka

import (
"fmt"
"net"
"strconv"

"github.com/hudl/fargo"

Expand Down Expand Up @@ -84,7 +85,7 @@ func (s *Instancer) getInstances() ([]string, error) {
func convertFargoAppToInstances(app *fargo.Application) []string {
instances := make([]string, len(app.Instances))
for i, inst := range app.Instances {
instances[i] = fmt.Sprintf("%s:%d", inst.IPAddr, inst.Port)
instances[i] = net.JoinHostPort(inst.IPAddr, strconv.Itoa(inst.Port))
}
return instances
}
Expand Down
16 changes: 16 additions & 0 deletions sd/eureka/instancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,19 @@ func TestBadInstancerScheduleUpdates(t *testing.T) {
t.Errorf("want %d, have %d", want, have)
}
}

func TestConvertIpv6Instance(t *testing.T) {
app := &fargo.Application{
Instances: []*fargo.Instance{
{IPAddr: "10.0.0.10", Port: 8000},
{IPAddr: "2001:db8:1::ab9:C0A8:102", Port: 8000},
},
}
expect := []string{"10.0.0.10:8000", "[2001:db8:1::ab9:C0A8:102]:8000"}
for i, inst := range convertFargoAppToInstances(app) {
if inst != expect[i] {
t.Fatalf("instance %s converting is wrong", expect[i])
}
}

}
4 changes: 3 additions & 1 deletion sd/eureka/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package eureka

import (
"fmt"
"net"
"net/http"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -51,7 +53,7 @@ func NewRegistrar(conn fargoConnection, instance *fargo.Instance, logger log.Log
return &Registrar{
conn: conn,
instance: instance,
logger: log.With(logger, "service", instance.App, "address", fmt.Sprintf("%s:%d", instance.IPAddr, instance.Port)),
logger: log.With(logger, "service", instance.App, "address", net.JoinHostPort(instance.IPAddr, strconv.Itoa(instance.Port))),
}
}

Expand Down

0 comments on commit 23eb35b

Please sign in to comment.