Skip to content

Commit

Permalink
[service discovery] fix fallback IP addr extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaj committed Sep 19, 2016
1 parent c71e878 commit 97982d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions tests/core/test_service_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def test_get_host_address(self, mock_check_yaml, mock_get):
'Networks': {
'bridge': {'IPAddress': '172.17.0.2'},
'foo': {'IPAddress': '192.168.0.2'}}}},
'host', '127.0.0.1'),
'host', '172.17.0.2'),

({'NetworkSettings': {'Networks': {}}}, 'host', None),
({'NetworkSettings': {'Networks': {}}}, 'host_bridge', None),
Expand Down Expand Up @@ -349,7 +349,7 @@ def test_fill_tpl(self):
'Networks': {'bridge': {'IPAddress': '172.17.0.2'}}}
},
{'host': '%%host%%', 'port': 1337}, ['host'], ['foo', 'bar:baz']),
({'host': '%%host%%', 'port': 1337, 'tags': ['foo', 'bar:baz']}, {'host': '127.0.0.1'}),
({'host': '%%host%%', 'port': 1337, 'tags': ['foo', 'bar:baz']}, {'host': '172.17.0.2'}),
),
(
({'NetworkSettings': {
Expand Down
32 changes: 15 additions & 17 deletions utils/service_discovery/sd_docker_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,20 @@ def __init__(self, agentConfig):
def _get_host_address(self, c_inspect, tpl_var):
"""Extract the container IP from a docker inspect object, or the kubelet API."""
c_id, c_img = c_inspect.get('Id', ''), c_inspect.get('Config', {}).get('Image', '')
tpl_parts = tpl_var.split('_')

# a specifier was given
if len(tpl_parts) > 1:
networks = c_inspect.get('NetworkSettings', {}).get('Networks') or {}
ip_dict = {}
for net_name, net_desc in networks.iteritems():
ip = net_desc.get('IPAddress')
if ip:
ip_dict[net_name] = ip
ip_addr = self._extract_ip_from_networks(ip_dict, tpl_var)
if ip_addr:
return ip_addr

networks = c_inspect.get('NetworkSettings', {}).get('Networks') or {}
ip_dict = {}
for net_name, net_desc in networks.iteritems():
ip = net_desc.get('IPAddress')
if ip:
ip_dict[net_name] = ip
ip_addr = self._extract_ip_from_networks(ip_dict, tpl_var)
if ip_addr:
return ip_addr

# try to get the bridge (default) IP address
log.debug("No network was specified for container %s (%s), trying with IPAddress field" % (c_id[:12], c_img))
log.debug("No IP address was found in container %s (%s) "
"networks, trying with the IPAddress field" % (c_id[:12], c_img))
ip_addr = c_inspect.get('NetworkSettings', {}).get('IPAddress')
if ip_addr:
return ip_addr
Expand Down Expand Up @@ -87,7 +85,7 @@ def _extract_ip_from_networks(self, ip_dict, tpl_var):
"""Extract a single IP from a dictionary made of network names and IPs."""
if not ip_dict:
return None
tpl_parts = tpl_var.split('_')
tpl_parts = tpl_var.split('_', 1)

# no specifier
if len(tpl_parts) < 2:
Expand All @@ -108,7 +106,7 @@ def _get_fallback_ip(self, ip_dict):
return ip_dict['bridge']
else:
last_key = sorted(ip_dict.iterkeys())[-1]
log.warning("Trying with the last key: '%s'." % last_key)
log.warning("Trying with the last (sorted) network: '%s'." % last_key)
return ip_dict[last_key]

def _get_port(self, container_inspect, tpl_var):
Expand Down Expand Up @@ -142,7 +140,7 @@ def _extract_port_from_list(self, ports, tpl_var):
if not ports:
return None

tpl_parts = tpl_var.split('_')
tpl_parts = tpl_var.split('_', 1)

if len(tpl_parts) == 1:
log.debug("No index was passed for template variable %s. "
Expand Down

0 comments on commit 97982d1

Please sign in to comment.