diff options
| author | Roman Krček <roman.krcek@tietoevry.com> | 2025-02-14 14:12:44 +0000 |
|---|---|---|
| committer | Dincer Celik <hello@dincercelik.com> | 2025-10-01 14:28:40 +0300 |
| commit | 8d5cb518d66115515ef185e28562fd5e9a60de2b (patch) | |
| tree | bbe366241a000afd8de62316ae40068df917625d | |
| parent | 2b591fb097caa24e577714f6bd8c6b168fa652f1 (diff) | |
| download | kolla-ansible-8d5cb518d66115515ef185e28562fd5e9a60de2b.tar.gz kolla-ansible-8d5cb518d66115515ef185e28562fd5e9a60de2b.zip | |
Add ability to get stopped container's facts
kolla_container_facts could only get running containers,
this patch adds the ability to specify, that user wants
even stopped containers.
Also updates module's docs and corrects few typos.
Change-Id: I9ace3c1d802bc00ce692b3ca755384c6b62bd72d
Signed-off-by: Roman Krček <roman.krcek@tietoevry.com>
| -rw-r--r-- | ansible/library/kolla_container_facts.py | 59 | ||||
| -rw-r--r-- | tests/test_kolla_container_facts.py | 31 |
2 files changed, 74 insertions, 16 deletions
diff --git a/ansible/library/kolla_container_facts.py b/ansible/library/kolla_container_facts.py index 053b2ef5e..9886d5252 100644 --- a/ansible/library/kolla_container_facts.py +++ b/ansible/library/kolla_container_facts.py @@ -20,10 +20,10 @@ from traceback import format_exc DOCUMENTATION = ''' --- module: kolla_container_facts -short_description: Module for collecting Docker container facts +short_description: Module for collecting container facts description: - - A module targeting at collecting Docker container facts. It is used for - detecting whether the container is running on host in Kolla. + - A module targeted at collecting container facts. It is used for + retrieving data about containers like their environment or state. options: container_engine: description: @@ -32,7 +32,7 @@ options: type: str api_version: description: - - The version of the api for docker-py to use when contacting docker + - The version of the API for container SDK to use required: False type: str default: auto @@ -44,18 +44,45 @@ options: action: description: - The action to perform + - The action "get_containers" only returns running containers, unless + argument get_all_containers is True required: True type: str -author: Jeffrey Zhang, Michal Nasiadka, Ivan Halomi + choices: + - get_containers + - get_container_env + - get_container_state + args: + description: + - Additional arguments for actions + required: False + type: dict + elements: dict + suboptions: + get_all_containers: + description: + - Get all containers, even stopped containers when + performing action "get_containers" + type: bool + required: False + default: False +author: Jeffrey Zhang, Michal Nasiadka, Roman Krček, Ivan Halomi ''' EXAMPLES = ''' - hosts: all tasks: - - name: Gather docker facts + - name: Gather docker facts for running containers + kolla_container_facts: + container_engine: docker + action: get_containers + + - name: Gather docker facts for all containers kolla_container_facts: container_engine: docker - action: get_containers + action: get_containers + args: + get_all_containers: true - name: Gather glance container facts kolla_container_facts: @@ -136,9 +163,11 @@ class ContainerFactsWorker(): def get_containers(self): """Handle when module is called with action get_containers""" names = self.params.get('name') + args = self.params.get('args', {}) + get_all_containers = args.get('get_all_containers', False) self.result['containers'] = dict() - containers = self.client.containers.list() + containers = self.client.containers.list(all=get_all_containers) for container in containers: container.reload() container_name = container.name @@ -226,9 +255,19 @@ def main(): action=dict(required=True, type='str', choices=['get_containers', 'get_containers_env', - 'get_containers_state', + 'get_volumes', 'get_containers_names', - 'get_volumes']), + 'get_containers_state']), + args=dict( + type='dict', + required=False, + default={}, + options=dict( + get_all_containers=dict(required=False, + type='bool', + default=False) + ) + ) ) required_if = [ diff --git a/tests/test_kolla_container_facts.py b/tests/test_kolla_container_facts.py index e58b058c5..b6a3d8000 100644 --- a/tests/test_kolla_container_facts.py +++ b/tests/test_kolla_container_facts.py @@ -109,7 +109,7 @@ def contruct_volume(vol_dict: dict) -> mock.Mock: return volume -def get_containers(override=None): +def get_containers(override=None, all: bool = False): if override: cont_dicts = override else: @@ -117,9 +117,11 @@ def get_containers(override=None): containers = [] for c in cont_dicts: - # Only running containers should be returned by the container APIs - if c['State']['Status'] == 'running': - containers.append(construct_container(c)) + # With the option "all", only running containers are returned + # by the container API + if not all and c['State']['Status'] != 'running': + continue + containers.append(construct_container(c)) return containers @@ -152,8 +154,9 @@ class TestContainerFacts(base.BaseTestCase): self.assertDictEqual( self.fake_data['containers'][0], self.dfw.result['containers']['my_container']) + self.dfw.client.containers.list.assert_called_once_with(all=False) - def test_get_container_multi(self): + def test_get_containers_multi(self): self.dfw = get_DockerFactsWorker( {'name': ['my_container', 'exited_container'], 'action': 'get_containers'}) @@ -165,8 +168,9 @@ class TestContainerFacts(base.BaseTestCase): self.assertIn('my_container', self.dfw.result['containers']) self.assertNotIn('my_container', self.dfw.result) self.assertNotIn('exited_container', self.dfw.result['containers']) + self.dfw.client.containers.list.assert_called_once_with(all=False) - def test_get_container_all(self): + def test_get_containers_all_running(self): self.dfw = get_DockerFactsWorker({'name': [], 'action': 'get_containers'}) running_containers = get_containers(self.fake_data['containers']) @@ -177,6 +181,21 @@ class TestContainerFacts(base.BaseTestCase): self.assertIn('my_container', self.dfw.result['containers']) self.assertNotIn('my_container', self.dfw.result) self.assertNotIn('exited_container', self.dfw.result['containers']) + self.dfw.client.containers.list.assert_called_once_with(all=False) + + def test_get_containers_all_including_stopped(self): + self.dfw = get_DockerFactsWorker({'name': [], + 'action': 'get_containers', + 'args': { + 'get_all_containers': True}}) + all_containers = get_containers(self.fake_data['containers'], all=True) + self.dfw.client.containers.list.return_value = all_containers + self.dfw.get_containers() + + self.assertFalse(self.dfw.result['changed']) + self.assertIn('my_container', self.dfw.result['containers']) + self.assertIn('exited_container', self.dfw.result['containers']) + self.dfw.client.containers.list.assert_called_once_with(all=True) def test_get_containers_env(self): fake_env = dict(KOLLA_BASE_DISTRO='ubuntu', |
