diff --git a/bridge/bridge.go b/bridge/bridge.go index f02ca99c9..d7102e278 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -317,6 +317,7 @@ func (b *Bridge) newService(port ServicePort, isgroup bool) *Service { } // NetworkMode can point to another container (kuberenetes pods) + var ipFromNetworkContainer string networkMode := container.HostConfig.NetworkMode if networkMode != "" { if strings.HasPrefix(networkMode, "container:") { @@ -326,12 +327,37 @@ func (b *Bridge) newService(port ServicePort, isgroup bool) *Service { 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) + ipFromNetworkContainer = networkContainer.NetworkSettings.IPAddress + + if ipFromNetworkContainer != "" { + service.IP = networkContainer.NetworkSettings.IPAddress + log.Println(service.Name + ": using network container IP " + service.IP) + } else { + log.Println(service.Name + ": network container IP address is empty") + } } } } + // Grab the container IP address from docker or kubernetes env + log.Println("Fetch ip using "+ b.config.UseIpFromEnv + " :" + ipFromNetworkContainer+ "'") + if b.config.UseIpFromEnv != "" && ipFromNetworkContainer == "" { + log.Println("Going to reset the ip using "+b.config.UseIpFromEnv + "'") + var isIpFound bool = false + for _, requiredEnv := range container.Config.Env { + if strings.Contains(requiredEnv, b.config.UseIpFromEnv) { + service.IP = requiredEnv[len(b.config.UseIpFromEnv)+1:] + log.Println(service.Name + ": using container IP '" + service.IP + "' from env '" + b.config.UseIpFromEnv + "'") + isIpFound = true + break + } + } + if isIpFound == false { + log.Println(service.Name + ": could not found env '" + b.config.UseIpFromEnv + + "' from docker env") + } + } + if port.PortType == "udp" { service.Tags = combineTags( mapDefault(metadata, "tags", ""), b.config.ForceTags, "udp") diff --git a/bridge/types.go b/bridge/types.go index e643ed3f5..75137d3d8 100644 --- a/bridge/types.go +++ b/bridge/types.go @@ -24,6 +24,7 @@ type Config struct { Internal bool Explicit bool UseIpFromLabel string + UseIpFromEnv string ForceTags string RefreshTtl int RefreshInterval int diff --git a/docs/user/run.md b/docs/user/run.md index 5c084474b..e5369bc9b 100644 --- a/docs/user/run.md +++ b/docs/user/run.md @@ -44,6 +44,7 @@ Option | Since | Description `-ttl ` | | TTL for services. Default: 0, no expiry (supported backends only) `-ttl-refresh ` | | Frequency service TTLs are refreshed (supported backends only) `-useIpFromLabel