Skip to content

Commit

Permalink
support ipv6 registration, add address validation (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang committed Nov 21, 2018
1 parent fbf4149 commit 6b4c5d8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 42 deletions.
5 changes: 4 additions & 1 deletion core/registry/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ func RegisterMicroserviceInstances() error {
service.ServiceDescription.Version, err)
return err
}
eps := MakeEndpointMap(config.GlobalDefinition.Cse.Protocols)
eps, err := MakeEndpointMap(config.GlobalDefinition.Cse.Protocols)
if err != nil {
return err
}
lager.Logger.Infof("service support protocols %s", config.GlobalDefinition.Cse.Protocols)
if InstanceEndpoints != nil {
eps = InstanceEndpoints
Expand Down
5 changes: 4 additions & 1 deletion core/registry/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ func (s *HeartbeatService) ReRegisterSelfMSandMSI() error {

// reRegisterSelfMSI 只重新注册实例
func reRegisterSelfMSI(sid, iid string) error {
eps := MakeEndpointMap(config.GlobalDefinition.Cse.Protocols)
eps, err := MakeEndpointMap(config.GlobalDefinition.Cse.Protocols)
if err != nil {
return err
}
if InstanceEndpoints != nil {
eps = InstanceEndpoints
}
Expand Down
44 changes: 12 additions & 32 deletions core/registry/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,31 @@ func MakeEndpoints(m map[string]model.Protocol) []string {
}

//MakeEndpointMap returns the endpoints map
func MakeEndpointMap(m map[string]model.Protocol) map[string]string {
func MakeEndpointMap(m map[string]model.Protocol) (map[string]string, error) {
eps := make(map[string]string, 0)
for name, protocol := range m {

if len(protocol.Advertise) == 0 {
host, port, err := net.SplitHostPort(protocol.Listen)
if err != nil {
lager.Logger.Warnf("get port from listen addr failed.", err)
port = iputil.DefaultPort4Protocol(name)
host = iputil.Localhost()
return nil, err
}

if host != "" {
if host == "0.0.0.0" {
host = iputil.GetLocalIP()
}
eps[name] = strings.Join([]string{host, port}, ":")

} else {
eps[name] = iputil.DefaultEndpoint4Protocol(name)
if host == "" || port == "" {
return nil, fmt.Errorf("listen address is invalid [%s]", protocol.Listen)
}
eps[name] = protocol.Listen
} else {
var (
ip net.IP
err error
)

// check the provided Advertise ip is IPV4 or IPV6
ipWithoutPort := strings.Split(protocol.Advertise, ":")
if len(ipWithoutPort) > 2 {
ip, _, err = net.ParseCIDR(protocol.Advertise + "/0")
} else {
ip, _, err = net.ParseCIDR(ipWithoutPort[0] + "/0")
}

host, port, err := net.SplitHostPort(protocol.Advertise)
if err != nil {
lager.Logger.Errorf("failed to parse ip address: %s", err)
} else {
if ip != nil && ip.To4() != nil {
eps[name] = ip.String() + ":" + ipWithoutPort[1]
}
return nil, err
}
if host == "" || port == "" {
return nil, fmt.Errorf("advertise address is invalid [%s]", protocol.Advertise)
}
eps[name] = protocol.Advertise
}
}
return eps
return eps, nil
}

//Microservice2ServiceKeyStr prepares a microservice key
Expand Down
34 changes: 26 additions & 8 deletions core/registry/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ import (
"github.com/stretchr/testify/assert"
)

func TestMakeEndpointMap(t *testing.T) {
protocols := make(map[string]model.Protocol)
protocols[common.ProtocolRest] = model.Protocol{
Listen: "0.0.0.0:1",
Advertise: "[2407:c080:17ff:ffff::7274:83a]:8080",
}
m, err := registry.MakeEndpointMap(protocols)
assert.NoError(t, err)
assert.Equal(t, "[2407:c080:17ff:ffff::7274:83a]:8080", m[common.ProtocolRest])

protocols2 := make(map[string]model.Protocol)
protocols2[common.ProtocolRest] = model.Protocol{
Listen: "[2407:c080:17ff:ffff::7274:83a]:8080",
}
m, err = registry.MakeEndpointMap(protocols2)
assert.NoError(t, err)
assert.Equal(t, "[2407:c080:17ff:ffff::7274:83a]:8080", m[common.ProtocolRest])
}
func TestUtil(t *testing.T) {
lager.Initialize("", "INFO", "", "size", true, 1, 10, 7)
var eps = []string{"https://127.0.0.1", "http://0.0.0.0"}
Expand All @@ -32,7 +50,7 @@ func TestUtil(t *testing.T) {
assert.Equal(t, common.ProtocolHighway+"://"+mapproto[common.ProtocolHighway].Advertise, strArr[0])

//Advertise address are given in the protocol map for highway
protocolArr := registry.MakeEndpointMap(mapproto)
protocolArr, _ := registry.MakeEndpointMap(mapproto)
t.Log("making endpoints with listen and advertise addr, endpoint : ", protocolArr)
assert.NotNil(t, protocolArr)
assert.Equal(t, common.ProtocolHighway+":"+mapproto[common.ProtocolHighway].Advertise, common.ProtocolHighway+":"+protocolArr[common.ProtocolHighway])
Expand All @@ -43,7 +61,7 @@ func TestUtil(t *testing.T) {
Advertise: "0.0.0.1:1",
}

protocolArrRest := registry.MakeEndpointMap(mapprotoRest)
protocolArrRest, _ := registry.MakeEndpointMap(mapprotoRest)
t.Log("making endpoints with listen and advertise addr, endpoint : ", protocolArrRest)
assert.NotNil(t, protocolArrRest)
assert.Equal(t, common.ProtocolRest+":"+mapprotoRest[common.ProtocolRest].Advertise, common.ProtocolRest+":"+protocolArrRest[common.ProtocolRest])
Expand All @@ -55,7 +73,7 @@ func TestUtil(t *testing.T) {
Advertise: "127.0.0.1:1",
}

protocolArrRest = registry.MakeEndpointMap(mapprotoRest)
protocolArrRest, _ = registry.MakeEndpointMap(mapprotoRest)
t.Log("making endpoints with listen and advertise addr, endpoint : ", protocolArrRest)
assert.NotNil(t, protocolArrRest)
assert.Equal(t, "127.0.0.1:1", protocolArrRest[common.ProtocolRest])
Expand All @@ -64,20 +82,20 @@ func TestUtil(t *testing.T) {
// and addr is IPV6 ip. so it should return empty response
mapprotoRest[common.ProtocolRest] = model.Protocol{
Listen: "0.0.0.2:1",
Advertise: "fe80::3436:b05c:350a:1ccd:1",
Advertise: "[fe80::3436:b05c:350a:1ccd]:1",
}

protocolArrRest = registry.MakeEndpointMap(mapprotoRest)
protocolArrRest, _ = registry.MakeEndpointMap(mapprotoRest)
t.Log("making endpoints with listen and advertise addr, endpoint : ", protocolArrRest)
assert.NotNil(t, protocolArrRest)
assert.Equal(t, "", protocolArrRest[common.ProtocolRest])
assert.Equal(t, "[fe80::3436:b05c:350a:1ccd]:1", protocolArrRest[common.ProtocolRest])

// Advertise address is not given so based on the listen address it should choose the advertise addr.
mapprotoRest[common.ProtocolRest] = model.Protocol{
Listen: "0.0.0.2:1",
}

protocolArrRest = registry.MakeEndpointMap(mapprotoRest)
protocolArrRest, _ = registry.MakeEndpointMap(mapprotoRest)
t.Log("making endpoints with listen and advertise addr, endpoint : ", protocolArrRest)
assert.NotNil(t, protocolArrRest)
assert.Equal(t, common.ProtocolRest+":"+mapprotoRest[common.ProtocolRest].Listen, common.ProtocolRest+":"+protocolArrRest[common.ProtocolRest])
Expand All @@ -87,7 +105,7 @@ func TestUtil(t *testing.T) {
Listen: "0.0.0.0:1",
}

protocolArrRest = registry.MakeEndpointMap(mapprotoRest)
protocolArrRest, _ = registry.MakeEndpointMap(mapprotoRest)
t.Log("making endpoints with listen and advertise addr, endpoint : ", protocolArrRest)
assert.NotNil(t, protocolArrRest)

Expand Down
8 changes: 8 additions & 0 deletions docs/user-guides/protocols.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ cse:
grpc:
listenAddress: 0.0.0.0:6000
```

for ipv6, need quotation marks. because [] is object list in yaml format
```
cse:
protocols:
rest:
listenAddress: "[2407:c080:17ff:ffff::7274:83a]:5000"
```
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ require (
github.com/stretchr/testify v1.2.2
go.uber.org/ratelimit v0.0.0-20180316092928-c15da0234277
golang.org/x/net v0.0.0-20180824152047-4bcd98cce591
golang.org/x/text v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 // indirect
google.golang.org/grpc v1.14.0
gopkg.in/yaml.v2 v2.2.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pO
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/sys v0.0.0-20180824143301-4910a1d54f87 h1:Kfu+SJhcKm8P3zmLBxmYj63edaBttRcelyx5CXsdu5A=
github.com/golang/sys v0.0.0-20180824143301-4910a1d54f87/go.mod h1:5JyrLPvD/ZdaYkT7IqKhsP5xt7aLjA99KXRtk4EIYDk=
github.com/golang/text v0.3.0 h1:uI5zIUA9cg047ctlTptnVc0Ghjfurf2eZMFrod8R7v8=
github.com/golang/text v0.3.0/go.mod h1:GUiq9pdJKRKKAZXiVgWFEvocYuREvC14NhI4OPgEjeE=
github.com/golang/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:Goyxmr1dEyuE8J10MyNptB/4WJaypDxCpNr2pf27wjI=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-genproto v0.0.0-20180817151627-c66870c02cf8 h1:I9PuChzQA31gMw88WmVPJaAwE0nZNHpMrLDUnTyzFAI=
github.com/google/go-genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:3Rcd9jSoLVkV/osPrt5CogLvLiarfI8U9/x78NwhuDU=
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
github.com/googleapis/gnostic v0.2.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
Expand Down

0 comments on commit 6b4c5d8

Please sign in to comment.