Skip to content

Commit 6f691d0

Browse files
committed
Merge pull request #18 from cnf/internal
Adding support for internal ip/port usage.
2 parents 1461fc7 + ca15d85 commit 6f691d0

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Registrator assumes the default Docker socket at `file:///var/run/docker.sock` o
1414

1515
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.
1616

17+
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)
18+
1719
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).
1820

1921
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:

bridge.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@ import (
1515
type PublishedPort struct {
1616
HostPort string
1717
HostIP string
18+
HostName string
1819
ExposedPort string
20+
ExposedIP string
1921
PortType string
2022
Container *dockerapi.Container
2123
}
2224

2325
type Service struct {
24-
ID string
25-
Name string
26-
Port int
27-
IP string
28-
Tags []string
29-
Attrs map[string]string
30-
TTL int
26+
ID string
27+
Name string
28+
// HostName string
29+
Port int
30+
IP string
31+
Tags []string
32+
Attrs map[string]string
33+
TTL int
3134

3235
pp PublishedPort
3336
}
@@ -66,9 +69,16 @@ func NewService(port PublishedPort, isgroup bool) *Service {
6669
service.pp = port
6770
service.ID = hostname + ":" + container.Name[1:] + ":" + port.ExposedPort
6871
service.Name = mapdefault(metadata, "name", defaultName)
69-
p, _ := strconv.Atoi(port.HostPort)
72+
var p int
73+
if *internal == true {
74+
service.IP = port.ExposedIP
75+
p, _ = strconv.Atoi(port.ExposedPort)
76+
// service.HostName = port.HostName
77+
} else {
78+
service.IP = port.HostIP
79+
p, _ = strconv.Atoi(port.HostPort)
80+
}
7081
service.Port = p
71-
service.IP = port.HostIP
7282

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

135145
ports := make([]PublishedPort, 0)
136146
for port, published := range container.NetworkSettings.Ports {
147+
var hp, hip string
137148
if len(published) > 0 {
149+
hp = published[0].HostPort
150+
hip = published[0].HostIp
151+
}
138152
p := strings.Split(string(port), "/")
139153
ports = append(ports, PublishedPort{
140-
HostPort: published[0].HostPort,
141-
HostIP: published[0].HostIp,
154+
HostPort: hp,
155+
HostIP: hip,
156+
HostName: container.Config.Hostname,
142157
ExposedPort: p[0],
158+
ExposedIP: container.NetworkSettings.IPAddress,
143159
PortType: p[1],
144160
Container: container,
145161
})
146-
}
147-
}
148-
149-
if len(ports) == 0 {
150-
log.Println("registrator: ignored:", container.ID[:12], "no published ports")
151-
return
162+
// }
152163
}
153164

154165
for _, port := range ports {
166+
if *internal != true && port.HostPort == "" {
167+
log.Println("registrator: ignored", container.ID[:12], "port", port.ExposedPort, "not published on host")
168+
continue
169+
}
155170
service := NewService(port, len(ports) > 1)
156171
if service == nil {
157172
log.Println("registrator: ignored:", container.ID[:12], "service on port", port.ExposedPort)
@@ -167,6 +182,10 @@ func (b *RegistryBridge) Add(containerId string) {
167182
b.services[container.ID] = append(b.services[container.ID], service)
168183
log.Println("registrator: added:", container.ID[:12], service.ID)
169184
}
185+
186+
if len(b.services) == 0 {
187+
log.Println("registrator: ignored:", container.ID[:12], "no published ports")
188+
}
170189
}
171190

172191
func (b *RegistryBridge) Remove(containerId string) {

registrator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

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

stage/registrator

-41.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)