Skip to content

Commit

Permalink
useIpFromEnv changes to support PR gliderlabs#544 by ganeshkaila:master
Browse files Browse the repository at this point in the history
  • Loading branch information
skumarsi committed Jan 14, 2020
1 parent 6614f7a commit 4fe9e21
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
30 changes: 28 additions & 2 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:") {
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions bridge/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
Internal bool
Explicit bool
UseIpFromLabel string
UseIpFromEnv string
ForceTags string
RefreshTtl int
RefreshInterval int
Expand Down
1 change: 1 addition & 0 deletions docs/user/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Option | Since | Description
`-ttl <seconds>` | | TTL for services. Default: 0, no expiry (supported backends only)
`-ttl-refresh <seconds>` | | Frequency service TTLs are refreshed (supported backends only)
`-useIpFromLabel <label>` | | Uses the IP address stored in the given label, which is assigned to a container, for registration with Consul
`-useIpFromEnv <env>` | | Uses the IP address from the given environment variable, which is assigned to a container, for registration with Consul

If the `-internal` option is used, Registrator will register the docker0
internal IP and port instead of the host mapped ones.
Expand Down
2 changes: 2 additions & 0 deletions registrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 useIpFromEnv = flag.String("useIpFromEnv", "", "Use IP from the container environment variable")
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")
Expand Down Expand Up @@ -107,6 +108,7 @@ func main() {
Internal: *internal,
Explicit: *explicit,
UseIpFromLabel: *useIpFromLabel,
UseIpFromEnv: *useIpFromEnv,
ForceTags: *forceTags,
RefreshTtl: *refreshTtl,
RefreshInterval: *refreshInterval,
Expand Down

0 comments on commit 4fe9e21

Please sign in to comment.