molecule-plugins icon indicating copy to clipboard operation
molecule-plugins copied to clipboard

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

Open ollie1 opened this issue 1 year ago • 0 comments

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?

ollie1 avatar Feb 28 '24 16:02 ollie1