Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service aliases #660

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 87 additions & 69 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,26 @@ func (b *Bridge) add(containerId string, quiet bool) {

isGroup := len(servicePorts) > 1
for _, port := range servicePorts {
service := b.newService(port, isGroup)
if service == nil {
services := b.newService(port, isGroup)
if services == nil {
if !quiet {
log.Println("ignored:", container.ID[:12], "service on port", port.ExposedPort)
}
continue
}
err := b.registry.Register(service)
if err != nil {
log.Println("register failed:", service, err)
continue
for _, service := range services {
err := b.registry.Register(service)
if err != nil {
log.Println("register failed:", service, err)
continue
}
b.services[container.ID] = append(b.services[container.ID], service)
log.Println("added:", container.ID[:12], service.ID)
}
b.services[container.ID] = append(b.services[container.ID], service)
log.Println("added:", container.ID[:12], service.ID)
}
}

func (b *Bridge) newService(port ServicePort, isgroup bool) *Service {
func (b *Bridge) newService(port ServicePort, isgroup bool) []*Service {
container := port.container
defaultName := strings.Split(path.Base(container.Config.Image), ":")[0]

Expand All @@ -273,86 +275,102 @@ func (b *Bridge) newService(port ServicePort, isgroup bool) *Service {
return nil
}

serviceName := mapDefault(metadata, "name", "")
if serviceName == "" {
mainServiceName := mapDefault(metadata, "name", "")
if mainServiceName == "" {
if b.config.Explicit {
return nil
}
serviceName = defaultName
mainServiceName = defaultName
}

service := new(Service)
service.Origin = port
service.ID = hostname + ":" + container.Name[1:] + ":" + port.ExposedPort
service.Name = serviceName
if isgroup && !metadataFromPort["name"] {
service.Name += "-" + port.ExposedPort
}
var p int

if b.config.Internal == true {
service.IP = port.ExposedIP
p, _ = strconv.Atoi(port.ExposedPort)
} else {
service.IP = port.HostIP
p, _ = strconv.Atoi(port.HostPort)
serviceNames := []string{mainServiceName}

aliasesValue := mapDefault(metadata, "aliases", "")
aliases := strings.Split(aliasesValue, ",")
for _, alias := range aliases {
if len(alias) > 0 {
serviceNames = append(serviceNames, alias)
}
}
service.Port = p

if b.config.UseIpFromLabel != "" {
containerIp := container.Config.Labels[b.config.UseIpFromLabel]
if containerIp != "" {
slashIndex := strings.LastIndex(containerIp, "/")
if slashIndex > -1 {
service.IP = containerIp[:slashIndex]

services := make([]*Service, len(serviceNames))

for i, serviceName := range serviceNames {
service := new(Service)
service.Origin = port
service.ID = hostname + ":" + container.Name[1:] + "-" + serviceName + ":" + port.ExposedPort
service.Name = serviceName
if isgroup && !metadataFromPort["name"] {
service.Name += "-" + port.ExposedPort
}
var p int

if b.config.Internal == true {
service.IP = port.ExposedIP
p, _ = strconv.Atoi(port.ExposedPort)
} else {
service.IP = port.HostIP
p, _ = strconv.Atoi(port.HostPort)
}
service.Port = p

if b.config.UseIpFromLabel != "" {
containerIp := container.Config.Labels[b.config.UseIpFromLabel]
if containerIp != "" {
slashIndex := strings.LastIndex(containerIp, "/")
if slashIndex > -1 {
service.IP = containerIp[:slashIndex]
} else {
service.IP = containerIp
}
log.Println("using container IP " + service.IP + " from label '" +
b.config.UseIpFromLabel + "'")
} else {
service.IP = containerIp
log.Println("Label '" + b.config.UseIpFromLabel +
"' not found in container configuration")
}
log.Println("using container IP " + service.IP + " from label '" +
b.config.UseIpFromLabel + "'")
} else {
log.Println("Label '" + b.config.UseIpFromLabel +
"' not found in container configuration")
}
}

// NetworkMode can point to another container (kuberenetes pods)
networkMode := container.HostConfig.NetworkMode
if networkMode != "" {
if strings.HasPrefix(networkMode, "container:") {
networkContainerId := strings.Split(networkMode, ":")[1]
log.Println(service.Name + ": detected container NetworkMode, linked to: " + networkContainerId[:12])
networkContainer, err := b.docker.InspectContainer(networkContainerId)
if err != nil {
log.Println("unable to inspect network container:", networkContainerId[:12], err)
} else {
service.IP = networkContainer.NetworkSettings.IPAddress
log.Println(service.Name + ": using network container IP " + service.IP)
// NetworkMode can point to another container (kuberenetes pods)
networkMode := container.HostConfig.NetworkMode
if networkMode != "" {
if strings.HasPrefix(networkMode, "container:") {
networkContainerId := strings.Split(networkMode, ":")[1]
log.Println(service.Name + ": detected container NetworkMode, linked to: " + networkContainerId[:12])
networkContainer, err := b.docker.InspectContainer(networkContainerId)
if err != nil {
log.Println("unable to inspect network container:", networkContainerId[:12], err)
} else {
service.IP = networkContainer.NetworkSettings.IPAddress
log.Println(service.Name + ": using network container IP " + service.IP)
}
}
}
}

if port.PortType == "udp" {
service.Tags = combineTags(
mapDefault(metadata, "tags", ""), b.config.ForceTags, "udp")
service.ID = service.ID + ":udp"
} else {
service.Tags = combineTags(
mapDefault(metadata, "tags", ""), b.config.ForceTags)
}
if port.PortType == "udp" {
service.Tags = combineTags(
mapDefault(metadata, "tags", ""), b.config.ForceTags, "udp")
service.ID = service.ID + ":udp"
} else {
service.Tags = combineTags(
mapDefault(metadata, "tags", ""), b.config.ForceTags)
}

id := mapDefault(metadata, "id", "")
if id != "" {
service.ID = id
}

id := mapDefault(metadata, "id", "")
if id != "" {
service.ID = id
service.Attrs = metadata
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a subtle difference, by assigning the service.Attrs = metadata here means that the metadata still contains the id, tags, name fields? Note that in the original code, there are three calls to delete(metadata, ...) to remove those fields before assigning service.Attrs = metadata. Is this any cause for concern or change in registration behavior?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I realize it's been almost two years since this PR was posted, and I doubt there will be any further activity on this project, but this PR solves a problem I was just researching in how to coordinate a service name transition period!)

service.TTL = b.config.RefreshTtl
services[i] = service
}

delete(metadata, "id")
delete(metadata, "tags")
delete(metadata, "name")
service.Attrs = metadata
service.TTL = b.config.RefreshTtl

return service
return services
}

func (b *Bridge) remove(containerId string, deregister bool) {
Expand Down