Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ip from env #674

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 9 additions & 0 deletions registrator.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>