From 878fe80ca3f0565ffc08ac071ebdcb76b899e41e Mon Sep 17 00:00:00 2001
From: Hans Feldt <2808287+hafe@users.noreply.github.com>
Date: Sat, 5 Dec 2020 18:43:25 +0100
Subject: [PATCH] add and use common crictl role (#6978)

---
 .../containerd/tasks/crictl.yml               | 31 -------------------
 .../containerd/tasks/main.yml                 |  3 +-
 roles/container-engine/cri-o/tasks/main.yaml  |  3 +-
 .../container-engine/crictl/handlers/main.yml | 12 +++++++
 .../{cri-o => crictl}/tasks/crictl.yml        | 18 +++++------
 roles/container-engine/crictl/tasks/main.yml  |  4 +++
 .../templates/crictl.yaml.j2                  |  0
 roles/download/tasks/main.yml                 |  8 ++---
 8 files changed, 31 insertions(+), 48 deletions(-)
 delete mode 100644 roles/container-engine/containerd/tasks/crictl.yml
 create mode 100644 roles/container-engine/crictl/handlers/main.yml
 rename roles/container-engine/{cri-o => crictl}/tasks/crictl.yml (54%)
 create mode 100644 roles/container-engine/crictl/tasks/main.yml
 rename roles/container-engine/{containerd => crictl}/templates/crictl.yaml.j2 (100%)

diff --git a/roles/container-engine/containerd/tasks/crictl.yml b/roles/container-engine/containerd/tasks/crictl.yml
deleted file mode 100644
index 3a9c074e6..000000000
--- a/roles/container-engine/containerd/tasks/crictl.yml
+++ /dev/null
@@ -1,31 +0,0 @@
----
-- name: crictl | Download crictl
-  include_tasks: "../../../download/tasks/download_file.yml"
-  vars:
-    download: "{{ download_defaults | combine(downloads.crictl) }}"
-
-- name: Install crictl config
-  template:
-    src: ../templates/crictl.yaml.j2  # noqa 404 not in role_path when task is included from download role
-    dest: /etc/crictl.yaml
-    owner: bin
-    mode: 0644
-
-- name: Copy crictl binary from download dir
-  copy:
-    src: "{{ local_release_dir }}/crictl"
-    dest: "{{ bin_dir }}/crictl"
-    mode: 0755
-    remote_src: true
-
-- name: Get crictl completion
-  command: "{{ bin_dir }}/crictl completion"
-  changed_when: False
-  register: cri_completion
-  check_mode: False
-
-- name: Install crictl completion
-  copy:
-    dest: /etc/bash_completion.d/crictl
-    content: "{{ cri_completion.stdout }}"
-  become: True
diff --git a/roles/container-engine/containerd/tasks/main.yml b/roles/container-engine/containerd/tasks/main.yml
index c2369daad..9524e1097 100644
--- a/roles/container-engine/containerd/tasks/main.yml
+++ b/roles/container-engine/containerd/tasks/main.yml
@@ -138,4 +138,5 @@
     - ansible_distribution_major_version == "8"
   notify: restart containerd
 
-- include_tasks: crictl.yml
+- include_role:
+    name: container-engine/crictl
diff --git a/roles/container-engine/cri-o/tasks/main.yaml b/roles/container-engine/cri-o/tasks/main.yaml
index 83165f2a7..09425f670 100644
--- a/roles/container-engine/cri-o/tasks/main.yaml
+++ b/roles/container-engine/cri-o/tasks/main.yaml
@@ -43,7 +43,8 @@
   import_tasks: "crio_repo.yml"
   when: crio_add_repos
 
-- import_tasks: "crictl.yml"
+- include_role:
+    name: container-engine/crictl
 
 - name: Build a list of crio runtimes with Katacontainers runtimes
   set_fact:
diff --git a/roles/container-engine/crictl/handlers/main.yml b/roles/container-engine/crictl/handlers/main.yml
new file mode 100644
index 000000000..53195869f
--- /dev/null
+++ b/roles/container-engine/crictl/handlers/main.yml
@@ -0,0 +1,12 @@
+---
+- name: Get crictl completion
+  command: "{{ bin_dir }}/crictl completion"
+  changed_when: False
+  register: cri_completion
+  check_mode: false
+
+- name: Install crictl completion
+  copy:
+    dest: /etc/bash_completion.d/crictl
+    content: "{{ cri_completion.stdout }}"
+    mode: 0644
diff --git a/roles/container-engine/cri-o/tasks/crictl.yml b/roles/container-engine/crictl/tasks/crictl.yml
similarity index 54%
rename from roles/container-engine/cri-o/tasks/crictl.yml
rename to roles/container-engine/crictl/tasks/crictl.yml
index d7cc1e665..b98875e5d 100644
--- a/roles/container-engine/cri-o/tasks/crictl.yml
+++ b/roles/container-engine/crictl/tasks/crictl.yml
@@ -6,7 +6,7 @@
 
 - name: Install crictl config
   template:
-    src: ../templates/crictl.yaml.j2  # noqa 404 not in role_path when task is included from download role
+    src: crictl.yaml.j2
     dest: /etc/crictl.yaml
     owner: bin
     mode: 0644
@@ -17,14 +17,10 @@
     dest: "{{ bin_dir }}/crictl"
     mode: 0755
     remote_src: true
+  notify:
+    - Get crictl completion
+    - Install crictl completion
 
-- name: Get crictl completion
-  command: "{{ bin_dir }}/crictl completion"
-  changed_when: False
-  register: cri_completion
-
-- name: Install crictl completion
-  copy:
-    dest: /etc/bash_completion.d/crictl
-    content: "{{ cri_completion.stdout }}"
-  become: True
+- name: Set fact crictl_installed
+  set_fact:
+    crictl_installed: true
diff --git a/roles/container-engine/crictl/tasks/main.yml b/roles/container-engine/crictl/tasks/main.yml
new file mode 100644
index 000000000..30f1004c2
--- /dev/null
+++ b/roles/container-engine/crictl/tasks/main.yml
@@ -0,0 +1,4 @@
+---
+- name: install crictĺ
+  include_tasks: crictl.yml
+  when: not crictl_installed | default(false)
diff --git a/roles/container-engine/containerd/templates/crictl.yaml.j2 b/roles/container-engine/crictl/templates/crictl.yaml.j2
similarity index 100%
rename from roles/container-engine/containerd/templates/crictl.yaml.j2
rename to roles/container-engine/crictl/templates/crictl.yaml.j2
diff --git a/roles/download/tasks/main.yml b/roles/download/tasks/main.yml
index 8f811b02e..b5b3ef08c 100644
--- a/roles/download/tasks/main.yml
+++ b/roles/download/tasks/main.yml
@@ -7,10 +7,10 @@
     - download
     - upload
 
-- include_tasks: ../../container-engine/containerd/tasks/crictl.yml
-  when:
-    - not skip_downloads|default(false)
-    - container_manager in ['containerd', 'crio']
+- name: install crictl
+  import_role:
+    name: container-engine/crictl
+  when: not skip_downloads|default(false)
 
 - name: download | Get kubeadm binary and list of required images
   include_tasks: prep_kubeadm_images.yml
-- 
GitLab