From 5c39810de27063992a45df57b49c2cd7332912d0 Mon Sep 17 00:00:00 2001
From: Mark Goddard <mark@stackhpc.com>
Date: Wed, 3 Feb 2021 09:15:44 +0000
Subject: [PATCH] Apply Zun configuration for Docker based on inventory

If docker_configure_for_zun is set to true, then Zun-specific
configuration for Docker is applied to all nodes. It should only be
applied based on the relevant inventory groups. In some cases this can
cause Docker to fail to start. See
https://storyboard.openstack.org/#!/story/2008544 for details.

This change applies the configuration based on the zun-compute and
zun-cni-daemon groups. It also modifies the expression to not assume
that these groups exist in the inventory.

Change-Id: I0141abf0dd83e3a567ea6dcca945f86db129becf
Closes-Bug: #1914378
Story: 2008544
Task: 41645
Co-Authored-By: Buddhika Sanjeewa <bsanjeewa@kln.ac.lk>
---
 ansible/roles/baremetal/tasks/bootstrap-servers.yml        | 5 +++--
 ansible/roles/baremetal/tasks/post-install.yml             | 7 ++++---
 .../roles/baremetal/templates/docker_systemd_service.j2    | 2 +-
 .../notes/bootstrap-without-zun-67d6ee5d84fcec22.yaml      | 6 ++++++
 4 files changed, 14 insertions(+), 6 deletions(-)
 create mode 100644 releasenotes/notes/bootstrap-without-zun-67d6ee5d84fcec22.yaml

diff --git a/ansible/roles/baremetal/tasks/bootstrap-servers.yml b/ansible/roles/baremetal/tasks/bootstrap-servers.yml
index 03b564a86e..46f2ae103e 100644
--- a/ansible/roles/baremetal/tasks/bootstrap-servers.yml
+++ b/ansible/roles/baremetal/tasks/bootstrap-servers.yml
@@ -6,5 +6,6 @@
 - import_tasks: post-install.yml
 
 - include_tasks: configure-containerd-for-zun.yml
-  when: containerd_configure_for_zun|bool and
-        inventory_hostname in groups['zun-cni-daemon']
+  when:
+    - containerd_configure_for_zun|bool
+    - "'zun-cni-daemon' in group_names"
diff --git a/ansible/roles/baremetal/tasks/post-install.yml b/ansible/roles/baremetal/tasks/post-install.yml
index 5d1be104db..d646c0139c 100644
--- a/ansible/roles/baremetal/tasks/post-install.yml
+++ b/ansible/roles/baremetal/tasks/post-install.yml
@@ -66,6 +66,7 @@
     docker_config: "{{ docker_config | combine(docker_zun_config) }}"
   when:
     - docker_configure_for_zun | bool
+    - "'zun-compute' in group_names"
 
 - name: Warn about deprecations
   debug:
@@ -136,7 +137,7 @@
     state: absent
   when:
     - not docker_custom_option
-    - not docker_configure_for_zun | bool
+    - not docker_configure_for_zun | bool or 'zun-compute' not in group_names
     - not docker_http_proxy
     - not docker_https_proxy
     - not docker_no_proxy
@@ -149,7 +150,7 @@
     recurse: yes
   when: >
     docker_custom_option | length > 0 or
-    docker_configure_for_zun | bool or
+    (docker_configure_for_zun | bool and 'zun-compute' in group_names) or
     docker_http_proxy | length > 0 or
     docker_https_proxy | length > 0 or
     docker_no_proxy | length > 0
@@ -161,7 +162,7 @@
     dest: /etc/systemd/system/docker.service.d/kolla.conf
   when: >
     docker_custom_option | length > 0 or
-    docker_configure_for_zun | bool or
+    (docker_configure_for_zun | bool and 'zun-compute' in group_names) or
     docker_http_proxy | length > 0 or
     docker_https_proxy | length > 0 or
     docker_no_proxy | length > 0
diff --git a/ansible/roles/baremetal/templates/docker_systemd_service.j2 b/ansible/roles/baremetal/templates/docker_systemd_service.j2
index 4f15d60cec..f10beaf82d 100644
--- a/ansible/roles/baremetal/templates/docker_systemd_service.j2
+++ b/ansible/roles/baremetal/templates/docker_systemd_service.j2
@@ -10,4 +10,4 @@ Environment="NO_PROXY={{ docker_no_proxy }}"
 {% endif %}
 ExecStart=
 # ExecStart commandline copied from 'docker-ce' package. Same on CentOS/Debian/Ubuntu systems.
-ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock{% if docker_custom_option %} {{ docker_custom_option }}{% endif %}{% if docker_configure_for_zun|bool %} {{ docker_zun_options }}{% endif %}
+ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock{% if docker_custom_option %} {{ docker_custom_option }}{% endif %}{% if docker_configure_for_zun|bool and 'zun-compute' in group_names %} {{ docker_zun_options }}{% endif %}
diff --git a/releasenotes/notes/bootstrap-without-zun-67d6ee5d84fcec22.yaml b/releasenotes/notes/bootstrap-without-zun-67d6ee5d84fcec22.yaml
new file mode 100644
index 0000000000..b995f2cf2e
--- /dev/null
+++ b/releasenotes/notes/bootstrap-without-zun-67d6ee5d84fcec22.yaml
@@ -0,0 +1,6 @@
+---
+fixes:
+  - |
+    Fixes an issue with ``kolla-ansible bootstrap-servers`` if Zun is enabled
+    where Zun-specific configuration for Docker was applied to all nodes.
+    `LP#1914378 <https://bugs.launchpad.net/kolla-ansible/+bug/1914378>`__