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

Docker plugin tries to create / delete the same network multiple times #245

Open
ollie1 opened this issue Feb 28, 2024 · 0 comments
Open

Comments

@ollie1
Copy link

ollie1 commented Feb 28, 2024

When using the docker plugin, if multiple platforms are specified with the same network, the create and destroy playbooks loop through the same network multiple times, trying to create / delete the docker network. This doesn't cause any major problem, as the create / delete network operation is idempotent, but it's inefficient, especially when there are lots of platforms in the same molecule scenario, all sharing the same network.

A minimal example of the molecule.yml to produce this is below:

---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: test1.local
    image: alpine
    pre_build_image: true
    command: ${MOLECULE_DOCKER_COMMAND:-""}
    networks:
      - name: "my-test-network"
    network_mode: "my-test-network"
  - name: test2.local
    image: alpine
    pre_build_image: true
    command: ${MOLECULE_DOCKER_COMMAND:-""}
    networks:
      - name: "my-test-network"
    network_mode: "my-test-network"
provisioner:
  name: ansible
verifier:
  name: ansible

When molecule create is run with the above, it gives the following output (other output removed for clarity) - note that it calls the "create_network.yml" twice:

...
TASK [Create docker network(s)] ************************************************
included: /usr/local/lib/python3.9/site-packages/molecule_plugins/docker/playbooks/tasks/create_network.yml for localhost => (item=my-test-network)
included: /usr/local/lib/python3.9/site-packages/molecule_plugins/docker/playbooks/tasks/create_network.yml for localhost => (item=my-test-network)

TASK [Check if network exist] **************************************************
ok: [localhost]

TASK [Create docker network(s)] ************************************************
changed: [localhost]

TASK [Check if network exist] **************************************************
ok: [localhost]

TASK [Create docker network(s)] ************************************************
skipping: [localhost]
...

This could be easily solved by adding a unique filter to the with_items statements in create.yml and destroy.yml playbooks - i.e. in https://github.com/ansible-community/molecule-plugins/blob/main/src/molecule_plugins/docker/playbooks/create.yml#L90 , change:

      with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks(molecule_labels) }}"

to

      with_items: "{{ molecule_yml.platforms | molecule_get_docker_networks(molecule_labels) | unique }}"

and in https://github.com/ansible-community/molecule-plugins/blob/main/src/molecule_plugins/docker/playbooks/destroy.yml#L49 , change

      loop: "{{ molecule_yml.platforms | molecule_get_docker_networks() }}"

to

      loop: "{{ molecule_yml.platforms | molecule_get_docker_networks() | unique }}"

Happy to submit a PR for this if helpful?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant