Selaa lähdekoodia

add namespace option for namespaced resources in pre_cleanup; add name/label attrs for removal of crds; properly implement pre_cleanup with wait_for_gone and wait_for_sec

Grega Bremec 22 tuntia sitten
vanhempi
commit
598c76ba61

+ 6 - 26
playbooks/inventory.yml

@@ -34,35 +34,15 @@ all:
         csv_name: kubevirt-hyperconverged-operator.v4.16.1
         nsp_kill: yes
         pre_cleanup:
-          - kind: kubevirt
-            apiv: kubevirt.io/v1
           - kind: hyperconverged
             apiv: hco.kubevirt.io/v1beta1
+            nspc: openshift-cnv
+            wait_for_gone: yes
+            wait_for_sec: 600
         add_cleanup:
-          - aaqs.aaq.kubevirt.io
-          - cdis.cdi.kubevirt.io
-          - hostpathprovisioners.hostpathprovisioner.kubevirt.io
-          - hyperconvergeds.hco.kubevirt.io
-          - kubevirts.kubevirt.io
-          - mtqs.mtq.kubevirt.io
-          - networkaddonsconfigs.networkaddonsoperator.network.kubevirt.io
-          - ssps.ssp.kubevirt.io
-          - migrationpolicies.migrations.kubevirt.io
-          - virtualmachineclones.clone.kubevirt.io
-          - virtualmachineclusterinstancetypes.instancetype.kubevirt.io
-          - virtualmachineclusterpreferences.instancetype.kubevirt.io
-          - virtualmachineexports.export.kubevirt.io
-          - virtualmachineinstancemigrations.kubevirt.io
-          - virtualmachineinstancepresets.kubevirt.io
-          - virtualmachineinstancereplicasets.kubevirt.io
-          - virtualmachineinstances.kubevirt.io
-          - virtualmachineinstancetypes.instancetype.kubevirt.io
-          - virtualmachinepools.pool.kubevirt.io
-          - virtualmachinepreferences.instancetype.kubevirt.io
-          - virtualmachinerestores.snapshot.kubevirt.io
-          - virtualmachines.kubevirt.io
-          - virtualmachinesnapshotcontents.snapshot.kubevirt.io
-          - virtualmachinesnapshots.snapshot.kubevirt.io
+          - name: aaqs.aaq.kubevirt.io
+          - name: cdis.cdi.kubevirt.io
+          - label: operators.coreos.com/kubevirt-hyperconverged.openshift-cnv
 
     # The list of OpenShift clusters check-env will try to connect to.
     clusters:

+ 26 - 24
playbooks/roles/remove-operators/tasks/main.yml

@@ -8,9 +8,14 @@
 #       sub_name          subscription name
 #       csv_name          CSV name to check for
 #       pre_cleanup       pre-cleanup tasks, a list of rtypes to remove (ALL)
-#         - apiv
-#           kind
-#       add_cleanup       additional CRDs to remove post-uninstall, a list
+#         - apiv            api version
+#           kind            resource kind
+#           nspc            namespace (required for namespaced resources)
+#           wait_for_gone   whether to wait for the resource to disappear
+#           wait_for_sec    how long (in seconds) to wait for gone
+#       add_cleanup       additional CRDs to remove post-uninstall, a list of
+#         - name            crd name, OR
+#           label           crd label, in case both are defined, both are used
 #
 # This role must then be applied as:
 #
@@ -23,17 +28,12 @@
 # What this means is that each item of removed_operators is expected to be
 # placed in the "role" variable prior to iterating over this role.
 #
-- name: Remove any of the resources found
-  k8s:
-    kubeconfig: tmp/kubeconfig-ocp4
-    validate_certs: no
-    api_version: "{{ item.apiv }}"
-    kind: "{{ item.kind }}"
-    delete_all: true
-    state: absent
-  ignore_errors: yes
+# Must include tasks because you can't loop over a block.
+- name: Remove any of the resources found from pre_cleanup
+  include_tasks: tasks/pre-cleanup.yml
   loop: "{{ role.pre_cleanup }}"
-  register: removed
+  loop_control:
+    label: "{{ item.kind }}.{{ item.apiv }}"
 
 - name: Remove the subscription
   k8s:
@@ -46,17 +46,6 @@
     state: absent
   ignore_errors: yes
 
-- name: Do post-cleanup
-  k8s:
-    kubeconfig: tmp/kubeconfig-ocp4
-    validate_certs: no
-    api_version: apiextensions.k8s.io/v1
-    kind: customresourcedefinition
-    name: "{{ item }}"
-    state: absent
-  ignore_errors: yes
-  loop: "{{ role.add_cleanup }}"
-
 - name: Remove the CSV as well, if so required
   k8s:
     kubeconfig: tmp/kubeconfig-ocp4
@@ -69,6 +58,19 @@
   ignore_errors: yes
   when: role.csv_kill
 
+- name: Do post-cleanup
+  k8s:
+    kubeconfig: tmp/kubeconfig-ocp4
+    validate_certs: no
+    api_version: apiextensions.k8s.io/v1
+    kind: customresourcedefinition
+    name: "{{ item.name | default(omit) }}"
+    label_selectors:
+      - "{{ item.label | default(omit) }}"
+    state: absent
+  ignore_errors: yes
+  loop: "{{ role.add_cleanup }}"
+
 - name: Lastly, remove the namespace, if instructed
   k8s:
     kubeconfig: tmp/kubeconfig-ocp4

+ 47 - 0
playbooks/roles/remove-operators/tasks/pre-cleanup.yml

@@ -0,0 +1,47 @@
+---
+- name: Tell what is being done.
+  ansible.builtin.pause:
+    prompt: |
+      ********************************************************************
+      
+      Removing all instances of {{ item.kind }}.{{ item.apiv }}
+      
+      ********************************************************************
+    seconds: 0
+
+- name: Remove the resources
+  k8s:
+    kubeconfig: tmp/kubeconfig-ocp4
+    validate_certs: no
+    api_version: "{{ item.apiv }}"
+    kind: "{{ item.kind }}"
+    namespace: "{{ item.nspc | default(omit) }}"
+    delete_all: true
+    state: absent
+  ignore_errors: yes
+  register: removed_rsrc
+
+- debug:
+    verbosity: 2
+    var: removed_rsrc
+
+- name: Wait for the resource(s) to be gone if so requested
+  k8s_info:
+    kubeconfig: tmp/kubeconfig-ocp4
+    validate_certs: no
+    api_version: "{{ item.apiv }}"
+    kind: "{{ item.kind }}"
+    namespace: "{{ item.nspc | default(omit) }}"
+  ignore_errors: yes
+  register: remaining_rsrc
+  until: (remaining_rsrc.resources | length) == 0
+  retries: "{{ (item.wait_for_sec | default(6)) / 5 }}"
+  delay: 5
+  when:
+    - item.wait_for_gone is defined
+    - item.wait_for_gone
+
+- debug:
+    verbosity: 2
+    var: remaining_rsrc
+...