Merge pull request #27630 from ticosax/include-container-id-docker-mine

Expose container_id in mine.get_docker
This commit is contained in:
Colton Myers 2015-10-05 15:56:53 -06:00
commit 77516912fa
2 changed files with 75 additions and 10 deletions

View File

@ -294,13 +294,19 @@ def flush():
return _mine_send(load, __opts__)
def get_docker(interfaces=None, cidrs=None):
def get_docker(interfaces=None, cidrs=None, with_container_id=False):
'''
Get all mine data for 'docker.get_containers' and run an aggregation
routine. The "interfaces" parameter allows for specifying which network
interfaces to select ip addresses from. The "cidrs" parameter allows for
specifying a list of cidrs which the ip address must match.
with_container_id
Boolean, to expose container_id in the list of results
.. versionadded:: 2015.8.2
CLI Example:
.. code-block:: bash
@ -358,6 +364,7 @@ def get_docker(interfaces=None, cidrs=None):
# Process each container
for container in six.itervalues(containers):
container_id = container['Info']['Id']
if container['Image'] not in proxy_lists:
proxy_lists[container['Image']] = {}
for dock_port in container['Ports']:
@ -366,12 +373,22 @@ def get_docker(interfaces=None, cidrs=None):
# If port is 0.0.0.0, then we must get the docker host IP
if ip_address == '0.0.0.0':
for ip_ in host_ips:
proxy_lists[container['Image']].setdefault('ipv4', {}).setdefault(dock_port['PrivatePort'], []).append(
'{0}:{1}'.format(ip_, dock_port['PublicPort']))
proxy_lists[container['Image']]['ipv4'][dock_port['PrivatePort']] = list(set(proxy_lists[container['Image']]['ipv4'][dock_port['PrivatePort']]))
containers = proxy_lists[container['Image']].setdefault('ipv4', {}).setdefault(dock_port['PrivatePort'], [])
container_network_footprint = '{0}:{1}'.format(ip_, dock_port['PublicPort'])
if with_container_id:
value = (container_network_footprint, container_id)
else:
value = container_network_footprint
if value not in containers:
containers.append(value)
elif ip_address:
proxy_lists[container['Image']].setdefault('ipv4', {}).setdefault(dock_port['PrivatePort'], []).append(
'{0}:{1}'.format(dock_port['IP'], dock_port['PublicPort']))
proxy_lists[container['Image']]['ipv4'][dock_port['PrivatePort']] = list(set(proxy_lists[container['Image']]['ipv4'][dock_port['PrivatePort']]))
containers = proxy_lists[container['Image']].setdefault('ipv4', {}).setdefault(dock_port['PrivatePort'], [])
container_network_footprint = '{0}:{1}'.format(dock_port['IP'], dock_port['PublicPort'])
if with_container_id:
value = (container_network_footprint, container_id)
else:
value = container_network_footprint
if value not in containers:
containers.append(value)
return proxy_lists

View File

@ -190,14 +190,62 @@ class MineTestCase(TestCase):
'PrivatePort': 80,
'PublicPort': 80,
'Type': 'tcp'}],
'Image': 'image:latest'
'Image': 'image:latest',
'Info': {'Id': 'abcdefhjhi1234567899'},
},
}}
with patch.object(mine, 'get', return_value=ps_response):
self.assertEqual(mine.get_docker(),
{'image:latest': {
'ipv4': {80: ['192.168.0.1:80',
'172.17.42.1:80']}}})
'ipv4': {80: [
'172.17.42.1:80',
'192.168.0.1:80',
]}}})
def test_get_docker_with_container_id(self):
'''
Test for Get all mine data for 'dockerng.ps' and run an
aggregation.
'''
ps_response = {
'localhost': {
'host': {
'interfaces': {
'docker0': {
'hwaddr': '88:99:00:00:99:99',
'inet': [{'address': '172.17.42.1',
'broadcast': None,
'label': 'docker0',
'netmask': '255.255.0.0'}],
'inet6': [{'address': 'ffff::eeee:aaaa:bbbb:8888',
'prefixlen': '64'}],
'up': True},
'eth0': {'hwaddr': '88:99:00:99:99:99',
'inet': [{'address': '192.168.0.1',
'broadcast': '192.168.0.255',
'label': 'eth0',
'netmask': '255.255.255.0'}],
'inet6': [{'address':
'ffff::aaaa:aaaa:bbbb:8888',
'prefixlen': '64'}],
'up': True},
}},
'abcdefhjhi1234567899': { # container Id
'Ports': [{'IP': '0.0.0.0', # we bind on every interfaces
'PrivatePort': 80,
'PublicPort': 80,
'Type': 'tcp'}],
'Image': 'image:latest',
'Info': {'Id': 'abcdefhjhi1234567899'},
},
}}
with patch.object(mine, 'get', return_value=ps_response):
self.assertEqual(mine.get_docker(with_container_id=True),
{'image:latest': {
'ipv4': {80: [
('172.17.42.1:80', 'abcdefhjhi1234567899'),
('192.168.0.1:80', 'abcdefhjhi1234567899'),
]}}})
if __name__ == '__main__':