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

Feature/addressable ips #91

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
53 changes: 45 additions & 8 deletions lib/synapse/service_watcher/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@ def rewrite_container_ports(ports)
[ pair[1].rpartition("/").first, pair[0].rpartition(":").last ]
end
elsif ports.is_a?(Array)
# New style API, ports is an array of hashes, with numeric values (or nil if no ports forwarded)
# New style API, ports is an array of hashes, with numeric values (or nil if no ports forwarded)
# Check to see if we are using addressable IP addresses. If so, we don't need the 'PublicPort' as the 'PrivatePort' is available
pairs = ports.collect do |v|
[v['PrivatePort'].to_s, v['PublicPort'].to_s]
addressable = @discovery['addressable_ip']
# If addressable is true, just use the 'private port'
if addressable == "true"
[v['PrivatePort'].to_s,v['PrivatePort'].to_s]
# otherwise, we will want to use the public port on the docker host
else
[v['PrivatePort'].to_s, v['PublicPort'].to_s]
end
end
end
Hash[pairs]
Expand All @@ -85,12 +93,41 @@ def containers
cnt["Image"].rpartition(":").first == @discovery["image_name"] \
and cnt["Ports"].has_key?(@discovery["container_port"].to_s())
end
cnts.map do |cnt|
{
'name' => server['name'],
'host' => server['host'],
'port' => cnt["Ports"][@discovery["container_port"].to_s()]
}

# Check to see if we are using an addressable IP
addressable = @discovery['addressable_ip']
# If so, then we need to inspect the containers and get the IP Address and Port
if addressable == "true"
cnts.map do |cnt|
id = cnt['Id']
request = '/containers/' + id + '/json'
begin
Docker.url = "http://#{server['host']}:#{server['port'] || 4243}"
response = Docker::Util.parse_json('[' + Docker.connection.get(request, {})+ ']')
rescue => an_error
log.warn "synapse: error inspecting container : #{an_error.inspect}"
next []
end

# Nothing to loop through here, so just look at the response and get what we want
{
'name' => response[0]['Config']['Hostname'],
'host' => response[0]['NetworkSettings']['IPAddress'],
'port' => cnt['Ports'][@discovery['container_port'].to_s()]
}

end

# If the IP isn't addressable, then we pick our host and server from the config and use the Private Port.
else
cnts.map do |cnt|
id = cnt['Id']
{
'name' => server['name'],
'host' => server['host'],
'port' => cnt["Ports"][@discovery["container_port"].to_s()]
}
end
end
end
backends.flatten
Expand Down