From 8d904c60949e310893a25c8af3636b0151334dd4 Mon Sep 17 00:00:00 2001 From: ChrisMcKenzie Date: Fri, 6 Oct 2017 15:24:50 -0700 Subject: [PATCH] added support for useIpFromNetwork --- bridge/bridge.go | 16 ++++++++++++++-- bridge/types.go | 19 ++++++++++--------- registrator.go | 20 +++++++++++--------- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/bridge/bridge.go b/bridge/bridge.go index f02ca99c9..42ca030a8 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -202,7 +202,7 @@ func (b *Bridge) add(containerId string, quiet bool) { // Extract configured host port mappings, relevant when using --net=host for port, _ := range container.Config.ExposedPorts { - published := []dockerapi.PortBinding{ {"0.0.0.0", port.Port()}, } + published := []dockerapi.PortBinding{{"0.0.0.0", port.Port()}} ports[string(port)] = servicePort(container, port, published) } @@ -309,13 +309,25 @@ func (b *Bridge) newService(port ServicePort, isgroup bool) *Service { service.IP = containerIp } log.Println("using container IP " + service.IP + " from label '" + - b.config.UseIpFromLabel + "'") + b.config.UseIpFromLabel + "'") } else { log.Println("Label '" + b.config.UseIpFromLabel + "' not found in container configuration") } } + if b.config.UseIpFromNetwork != "" { + containerNetwork, ok := container.NetworkSettings.Networks[b.config.UseIpFromNetwork] + if ok { + service.IP = containerNetwork.IPAddress + log.Printf("using container IP %s from network '%s'", + service.IP, b.config.UseIpFromLabel) + } else { + log.Printf("Network '%s' not found in container configuration.", + b.config.UseIpFromNetwork) + } + } + // NetworkMode can point to another container (kuberenetes pods) networkMode := container.HostConfig.NetworkMode if networkMode != "" { diff --git a/bridge/types.go b/bridge/types.go index e643ed3f5..e87ca391a 100644 --- a/bridge/types.go +++ b/bridge/types.go @@ -20,15 +20,16 @@ type RegistryAdapter interface { } type Config struct { - HostIp string - Internal bool - Explicit bool - UseIpFromLabel string - ForceTags string - RefreshTtl int - RefreshInterval int - DeregisterCheck string - Cleanup bool + HostIp string + Internal bool + Explicit bool + UseIpFromNetwork string + UseIpFromLabel string + ForceTags string + RefreshTtl int + RefreshInterval int + DeregisterCheck string + Cleanup bool } type Service struct { diff --git a/registrator.go b/registrator.go index 0b449b41b..686a6b663 100644 --- a/registrator.go +++ b/registrator.go @@ -22,6 +22,7 @@ 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 explicit = flag.Bool("explicit", false, "Only register containers which have SERVICE_NAME label set") var useIpFromLabel = flag.String("useIpFromLabel", "", "Use IP which is stored in a label assigned to the container") +var useIpFromNetwork = flag.String("useIpFromNetwork", "", "Use IP that is assigned on to a containers network") 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)") var forceTags = flag.String("tags", "", "Append tags for all registered services") @@ -98,15 +99,16 @@ func main() { } b, err := bridge.New(docker, flag.Arg(0), bridge.Config{ - HostIp: *hostIp, - Internal: *internal, - Explicit: *explicit, - UseIpFromLabel: *useIpFromLabel, - ForceTags: *forceTags, - RefreshTtl: *refreshTtl, - RefreshInterval: *refreshInterval, - DeregisterCheck: *deregister, - Cleanup: *cleanup, + HostIp: *hostIp, + Internal: *internal, + Explicit: *explicit, + UseIpFromNetwork: *useIpFromNetwork, + UseIpFromLabel: *useIpFromLabel, + ForceTags: *forceTags, + RefreshTtl: *refreshTtl, + RefreshInterval: *refreshInterval, + DeregisterCheck: *deregister, + Cleanup: *cleanup, }) assert(err)