Skip to content

Commit

Permalink
Merge pull request #18 from cnf/internal
Browse files Browse the repository at this point in the history
Adding support for internal ip/port usage.
  • Loading branch information
progrium committed Oct 23, 2014
2 parents 1461fc7 + ca15d85 commit 6f691d0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Registrator assumes the default Docker socket at `file:///var/run/docker.sock` o

By default, when registering a service, registrator will assign the service address by attempting to resolve the current hostname. If you would like to force the service address to be a specific address, you can specify the `-ip` argument.

If the argument `-internal` is passed, registrator will register the docker0 internal ip and port instead of the host mapped ones. (etcd only for now)

The consul backend does not support automatic expiry of stale registrations after some TTL. Instead, TTL checks must be configured (see below). For backends that do support TTL expiry, registrator can be started with the `-ttl` and `-ttl-refresh` arguments (both disabled by default).

Registrator was designed to just be run as a container. You must pass the Docker socket file as a mount to `/tmp/docker.sock`, and it's a good idea to set the hostname to the machine host:
Expand Down
53 changes: 36 additions & 17 deletions bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ import (
type PublishedPort struct {
HostPort string
HostIP string
HostName string
ExposedPort string
ExposedIP string
PortType string
Container *dockerapi.Container
}

type Service struct {
ID string
Name string
Port int
IP string
Tags []string
Attrs map[string]string
TTL int
ID string
Name string
// HostName string
Port int
IP string
Tags []string
Attrs map[string]string
TTL int

pp PublishedPort
}
Expand Down Expand Up @@ -66,9 +69,16 @@ func NewService(port PublishedPort, isgroup bool) *Service {
service.pp = port
service.ID = hostname + ":" + container.Name[1:] + ":" + port.ExposedPort
service.Name = mapdefault(metadata, "name", defaultName)
p, _ := strconv.Atoi(port.HostPort)
var p int
if *internal == true {
service.IP = port.ExposedIP
p, _ = strconv.Atoi(port.ExposedPort)
// service.HostName = port.HostName
} else {
service.IP = port.HostIP
p, _ = strconv.Atoi(port.HostPort)
}
service.Port = p
service.IP = port.HostIP

service.Tags = make([]string, 0)
tags := mapdefault(metadata, "tags", "")
Expand Down Expand Up @@ -134,24 +144,29 @@ func (b *RegistryBridge) Add(containerId string) {

ports := make([]PublishedPort, 0)
for port, published := range container.NetworkSettings.Ports {
var hp, hip string
if len(published) > 0 {
hp = published[0].HostPort
hip = published[0].HostIp
}
p := strings.Split(string(port), "/")
ports = append(ports, PublishedPort{
HostPort: published[0].HostPort,
HostIP: published[0].HostIp,
HostPort: hp,
HostIP: hip,
HostName: container.Config.Hostname,
ExposedPort: p[0],
ExposedIP: container.NetworkSettings.IPAddress,
PortType: p[1],
Container: container,
})
}
}

if len(ports) == 0 {
log.Println("registrator: ignored:", container.ID[:12], "no published ports")
return
// }
}

for _, port := range ports {
if *internal != true && port.HostPort == "" {
log.Println("registrator: ignored", container.ID[:12], "port", port.ExposedPort, "not published on host")
continue
}
service := NewService(port, len(ports) > 1)
if service == nil {
log.Println("registrator: ignored:", container.ID[:12], "service on port", port.ExposedPort)
Expand All @@ -167,6 +182,10 @@ func (b *RegistryBridge) Add(containerId string) {
b.services[container.ID] = append(b.services[container.ID], service)
log.Println("registrator: added:", container.ID[:12], service.ID)
}

if len(b.services) == 0 {
log.Println("registrator: ignored:", container.ID[:12], "no published ports")
}
}

func (b *RegistryBridge) Remove(containerId string) {
Expand Down
1 change: 1 addition & 0 deletions registrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

var hostIp = flag.String("ip", "", "IP for ports mapped to the host")
var internal = flag.Bool("internal", false, "Use internal ports instead of published ones")
var refreshInterval = flag.Int("ttl-refresh", 0, "Frequency with which service TTLs are refreshed")
var refreshTtl = flag.Int("ttl", 0, "TTL for services (default is no expiry)")

Expand Down
Binary file modified stage/registrator
Binary file not shown.

0 comments on commit 6f691d0

Please sign in to comment.