Have the zookeeper backend use the host port for the service paths, allow publishing services if the base service path already exists, and allow publishing into the root of zookeeper. (#367)

This commit is contained in:
jmeichle 2016-04-19 18:59:53 -04:00 committed by Matt Aitchison
parent 55a38b9135
commit 04f52e0a9e

View File

@ -49,30 +49,33 @@ type ZnodeBody struct {
func (r *ZkAdapter) Register(service *bridge.Service) error { func (r *ZkAdapter) Register(service *bridge.Service) error {
privatePort, _ := strconv.Atoi(service.Origin.ExposedPort) privatePort, _ := strconv.Atoi(service.Origin.ExposedPort)
publicPortString := strconv.Itoa(service.Port)
acl := zk.WorldACL(zk.PermAll) acl := zk.WorldACL(zk.PermAll)
basePath := r.path + "/" + service.Name
exists, _, err := r.client.Exists(r.path + "/" + service.Name) if (r.path == "/") {
basePath = r.path + service.Name
}
exists, _, err := r.client.Exists(basePath)
if err != nil { if err != nil {
log.Println("zookeeper: error checking if exists: ", err) log.Println("zookeeper: error checking if exists: ", err)
} else { } else {
if !exists { if !exists {
_, err := r.client.Create(r.path+"/"+service.Name, []byte{}, 0, acl) _, err := r.client.Create(basePath, []byte{}, 0, acl)
if err != nil { if err != nil {
log.Println("zookeeper: failed to create base service node: ", err) log.Println("zookeeper: failed to create base service node at path '" + basePath + "': ", err)
} else { }
zbody := &ZnodeBody{Name: service.Name, IP: service.IP, PublicPort: service.Port, PrivatePort: privatePort, Tags: service.Tags, Attrs: service.Attrs, ContainerID: service.Origin.ContainerHostname} } // create base path for the service name if it missing
body, err := json.Marshal(zbody) zbody := &ZnodeBody{Name: service.Name, IP: service.IP, PublicPort: service.Port, PrivatePort: privatePort, Tags: service.Tags, Attrs: service.Attrs, ContainerID: service.Origin.ContainerHostname}
if err != nil { body, err := json.Marshal(zbody)
log.Println("zookeeper: failed to json encode service body: ", err) if err != nil {
} else { log.Println("zookeeper: failed to json encode service body: ", err)
path := r.path + "/" + service.Name + "/" + service.Origin.ExposedPort } else {
_, err = r.client.Create(path, body, 1, acl) path := basePath + "/" + service.IP + ":" + publicPortString
if err != nil { _, err = r.client.Create(path, body, 1, acl)
log.Println("zookeeper: failed to register service: ", err) if err != nil {
} log.Println("zookeeper: failed to register service at path '" + path + "': ", err)
} // json encode error check
} // create service path error check } // create service path error check
} // service path exists } // json znode body creation check
} // service path exists error check } // service path exists error check
return err return err
} }
@ -88,8 +91,12 @@ func (r *ZkAdapter) Ping() error {
func (r *ZkAdapter) Deregister(service *bridge.Service) error { func (r *ZkAdapter) Deregister(service *bridge.Service) error {
basePath := r.path + "/" + service.Name basePath := r.path + "/" + service.Name
if (r.path == "/") {
basePath = r.path + service.Name
}
publicPortString := strconv.Itoa(service.Port)
servicePortPath := basePath + "/" + service.IP + ":" + publicPortString
// Delete the service-port znode // Delete the service-port znode
servicePortPath := basePath + "/" + service.Origin.ExposedPort
err := r.client.Delete(servicePortPath, -1) // -1 means latest version number err := r.client.Delete(servicePortPath, -1) // -1 means latest version number
if err != nil { if err != nil {
log.Println("zookeeper: failed to deregister service port entry: ", err) log.Println("zookeeper: failed to deregister service port entry: ", err)