From 0379a52f03c7e5c0ed84d4ad96e56ff120f062d8 Mon Sep 17 00:00:00 2001
From: Stephen Schmidt <Z3r0Sum@users.noreply.github.com>
Date: Tue, 24 Mar 2020 11:12:47 -0400
Subject: [PATCH] Fix etcd install with docker and etcd_kubeadm_enabled (#5777)

- This solves issue #5721 & #5713 (dupes)
  - Provide a cleaner default usage pattern for the download role
    around etcd that supports 'host' and 'docker' properly
  - Extract the 'etcdctl' as a separate task install piece and reuse it where
    appropriate
  - Update the kubeadm-etcd task to reflect the above change
---
 roles/download/defaults/main.yml                      |  8 ++++----
 roles/etcd/tasks/install_docker.yml                   | 11 +----------
 roles/etcd/tasks/install_etcdctl_docker.yml           | 11 +++++++++++
 roles/kubernetes/master/tasks/kubeadm-etcd.yml        |  9 +++++++++
 .../preinstall/tasks/0020-verify-settings.yml         |  6 ++++++
 roles/kubernetes/preinstall/tasks/0040-set_facts.yml  |  1 -
 6 files changed, 31 insertions(+), 15 deletions(-)
 create mode 100644 roles/etcd/tasks/install_etcdctl_docker.yml

diff --git a/roles/download/defaults/main.yml b/roles/download/defaults/main.yml
index c784c69ec..854ff2ea8 100644
--- a/roles/download/defaults/main.yml
+++ b/roles/download/defaults/main.yml
@@ -540,17 +540,17 @@ downloads:
 
   etcd:
     container: "{{ etcd_deployment_type != 'host' }}"
-    file: "{{ etcd_deployment_type == 'host' or etcd_kubeadm_enabled }}"
+    file: "{{ etcd_deployment_type == 'host' }}"
     enabled: true
     version: "{{ etcd_version }}"
-    dest: "{{local_release_dir}}/etcd-{{ etcd_version }}-linux-amd64.tar.gz"
+    dest: "{{ local_release_dir }}/etcd-{{ etcd_version }}-linux-amd64.tar.gz"
     repo: "{{ etcd_image_repo }}"
     tag: "{{ etcd_image_tag }}"
     sha256: >-
-     {{ etcd_binary_checksum if (etcd_deployment_type == 'host' or etcd_kubeadm_enabled)
+     {{ etcd_binary_checksum if (etcd_deployment_type == 'host')
      else etcd_digest_checksum|d(None) }}
     url: "{{ etcd_download_url }}"
-    unarchive: true
+    unarchive: "{{ etcd_deployment_type == 'host' }}"
     owner: "root"
     mode: "0755"
     groups:
diff --git a/roles/etcd/tasks/install_docker.yml b/roles/etcd/tasks/install_docker.yml
index 6c38ad9f3..793946fab 100644
--- a/roles/etcd/tasks/install_docker.yml
+++ b/roles/etcd/tasks/install_docker.yml
@@ -1,14 +1,5 @@
 ---
-- name: Install | Copy etcdctl binary from docker container
-  command: sh -c "{{ docker_bin_dir }}/docker rm -f etcdctl-binarycopy;
-           {{ docker_bin_dir }}/docker create --name etcdctl-binarycopy {{ etcd_image_repo }}:{{ etcd_image_tag }} &&
-           {{ docker_bin_dir }}/docker cp etcdctl-binarycopy:/usr/local/bin/etcdctl {{ bin_dir }}/etcdctl &&
-           {{ docker_bin_dir }}/docker rm -f etcdctl-binarycopy"
-  register: etcd_task_result
-  until: etcd_task_result.rc == 0
-  retries: "{{ etcd_retries }}"
-  delay: "{{ retry_stagger | random + 3 }}"
-  changed_when: false
+- import_tasks: install_etcdctl_docker.yml
   when: etcd_cluster_setup
 
 - name: Install etcd launch script
diff --git a/roles/etcd/tasks/install_etcdctl_docker.yml b/roles/etcd/tasks/install_etcdctl_docker.yml
new file mode 100644
index 000000000..1d87ccc8e
--- /dev/null
+++ b/roles/etcd/tasks/install_etcdctl_docker.yml
@@ -0,0 +1,11 @@
+---
+- name: Install | Copy etcdctl binary from docker container
+  command: sh -c "{{ docker_bin_dir }}/docker rm -f etcdctl-binarycopy;
+           {{ docker_bin_dir }}/docker create --name etcdctl-binarycopy {{ etcd_image_repo }}:{{ etcd_image_tag }} &&
+           {{ docker_bin_dir }}/docker cp etcdctl-binarycopy:/usr/local/bin/etcdctl {{ bin_dir }}/etcdctl &&
+           {{ docker_bin_dir }}/docker rm -f etcdctl-binarycopy"
+  register: etcdctl_install_result
+  until: etcdctl_install_result.rc == 0
+  retries: "{{ etcd_retries }}"
+  delay: "{{ retry_stagger | random + 3 }}"
+  changed_when: false
diff --git a/roles/kubernetes/master/tasks/kubeadm-etcd.yml b/roles/kubernetes/master/tasks/kubeadm-etcd.yml
index bfcb88d2b..2433fdb9f 100644
--- a/roles/kubernetes/master/tasks/kubeadm-etcd.yml
+++ b/roles/kubernetes/master/tasks/kubeadm-etcd.yml
@@ -16,3 +16,12 @@
   include_tasks: "{{ role_path }}/../../etcd/tasks/install_host.yml"
   vars:
     etcd_cluster_setup: true
+  when: etcd_deployment_type == "host"
+
+- name: Ensure etcdctl binary is installed
+  include_tasks: "{{ role_path }}/../../etcd/tasks/install_etcdctl_docker.yml"
+  vars:
+    etcd_cluster_setup: true
+    etcd_retries: 4
+  when:
+    - etcd_deployment_type == "docker"
diff --git a/roles/kubernetes/preinstall/tasks/0020-verify-settings.yml b/roles/kubernetes/preinstall/tasks/0020-verify-settings.yml
index b3e995926..d558fff7a 100644
--- a/roles/kubernetes/preinstall/tasks/0020-verify-settings.yml
+++ b/roles/kubernetes/preinstall/tasks/0020-verify-settings.yml
@@ -260,6 +260,12 @@
     msg: "kubeadm etcd mode requires experimental control plane"
   when: etcd_kubeadm_enabled
 
+- name: Stop if etcd deployment type is not host or docker
+  assert:
+    that: etcd_deployment_type in ['host', 'docker']
+    msg: "The etcd deployment type, 'etcd_deployment_type', must be host or docker"
+  run_once: true
+
 - name: Stop if download_localhost is enabled but download_run_once is not
   assert:
     that: download_run_once
diff --git a/roles/kubernetes/preinstall/tasks/0040-set_facts.yml b/roles/kubernetes/preinstall/tasks/0040-set_facts.yml
index a5c632aa0..e0f1995d4 100644
--- a/roles/kubernetes/preinstall/tasks/0040-set_facts.yml
+++ b/roles/kubernetes/preinstall/tasks/0040-set_facts.yml
@@ -185,7 +185,6 @@
     kube_etcd_cacert_file: "etcd/ca.crt"
     kube_etcd_cert_file: "apiserver-etcd-client.crt"
     kube_etcd_key_file: "apiserver-etcd-client.key"
-    etcd_deployment_type: host
   when:
     - etcd_kubeadm_enabled
 
-- 
GitLab