|
| 1 | +#!/usr/bin/env ansible-playbook |
| 2 | +--- |
| 3 | +# This workaround was implemented to fix a problem where openshift-cnv would |
| 4 | +# not recognize a default virt storage class change and change the format of |
| 5 | +# datasources. The issue was fixed in OpenShift Virtualization 4.16.4. |
| 6 | +- name: Determine if we have PVC clean-up to do |
| 7 | + become: false |
| 8 | + connection: local |
| 9 | + hosts: localhost |
| 10 | + gather_facts: false |
| 11 | + vars: |
| 12 | + kubeconfig: "{{ lookup('env', 'KUBECONFIG') }}" |
| 13 | + dataimportcron_cleanup: false |
| 14 | + image_cleanup_namespace: "openshift-virtualization-os-images" |
| 15 | + cluster_version: "{{ global['clusterVersion'] | default('UNSET') }}" |
| 16 | + tasks: |
| 17 | + - name: Check cluster version |
| 18 | + ansible.builtin.debug: |
| 19 | + var: cluster_version |
| 20 | + |
| 21 | + - name: Exit if normal version check is not right |
| 22 | + ansible.builtin.meta: end_play |
| 23 | + when: cluster_version not in [ '4.17', '4.16', 'UNSET' ] |
| 24 | + |
| 25 | + - name: Find default storageclass |
| 26 | + ansible.builtin.shell: | |
| 27 | + set -o pipefail |
| 28 | + oc get storageclass -o json | jq -r '.items[] | select(.metadata.annotations."storageclass.kubernetes.io/is-default-class")' |
| 29 | + register: default_sc_output |
| 30 | + changed_when: false |
| 31 | + |
| 32 | + - name: Find virtualization default storageclass |
| 33 | + ansible.builtin.shell: | |
| 34 | + set -o pipefail |
| 35 | + oc get storageclass -o json | jq -r '.items[] | select(.metadata.annotations."storageclass.kubevirt.io/is-default-virt-class")' |
| 36 | + register: default_virt_sc_output |
| 37 | + changed_when: false |
| 38 | + |
| 39 | + - name: Compare default virtualization storageclass and default storageclass to determine whether to clean PVCs |
| 40 | + block: |
| 41 | + - name: Parse results |
| 42 | + ansible.builtin.set_fact: |
| 43 | + default_sc: '{{ default_sc_output.stdout | from_json }}' |
| 44 | + default_virt_sc: '{{ default_virt_sc_output.stdout | from_json }}' |
| 45 | + |
| 46 | + - name: Commit to dataimportcron cleanup |
| 47 | + ansible.builtin.set_fact: |
| 48 | + dataimportcron_cleanup: true |
| 49 | + when: |
| 50 | + - default_virt_sc.metadata.name == "ocs-storagecluster-ceph-rbd-virtualization" |
| 51 | + - default_sc.metadata.name != default_virt_sc.metadata.name |
| 52 | + rescue: |
| 53 | + - name: Note that we exited |
| 54 | + ansible.builtin.debug: |
| 55 | + msg: "Caught an error before we could determine to clean up dataimportcrons, exiting" |
| 56 | + |
| 57 | + - name: End play (successfully) |
| 58 | + ansible.builtin.meta: end_play |
| 59 | + |
| 60 | + - name: Cleanup incorrect datasourceimport images (PVCs) |
| 61 | + when: |
| 62 | + - dataimportcron_cleanup |
| 63 | + block: |
| 64 | + - name: Find dataimportcrons |
| 65 | + kubernetes.core.k8s_info: |
| 66 | + kind: dataimportcron |
| 67 | + namespace: '{{ image_cleanup_namespace }}' |
| 68 | + api_version: cdi.kubevirt.io/v1beta1 |
| 69 | + register: dic_list |
| 70 | + |
| 71 | + - name: Extract dic names |
| 72 | + ansible.builtin.set_fact: |
| 73 | + dic_names: "{{ dic_names | default([]) + [ item.metadata.name ] }}" |
| 74 | + loop: "{{ dic_list.resources }}" |
| 75 | + |
| 76 | + - name: Show names |
| 77 | + ansible.builtin.debug: |
| 78 | + var: dic_names |
| 79 | + |
| 80 | + - name: Find datasources to cleanup |
| 81 | + kubernetes.core.k8s_info: |
| 82 | + kind: datasource |
| 83 | + namespace: '{{ image_cleanup_namespace }}' |
| 84 | + api_version: cdi.kubevirt.io/v1beta1 |
| 85 | + register: ds_cleanup_list |
| 86 | + |
| 87 | + - name: Keep track of objects to remove |
| 88 | + ansible.builtin.set_fact: |
| 89 | + cron_cleanups: [] |
| 90 | + ds_cleanups: [] |
| 91 | + |
| 92 | + - name: Record datasources that need cleanup |
| 93 | + ansible.builtin.set_fact: |
| 94 | + cron_cleanups: "{{ cron_cleanups + [ item.metadata.labels['cdi.kubevirt.io/dataImportCron'] ] }}" |
| 95 | + ds_cleanups: "{{ ds_cleanups + [ item.metadata.name ] }}" |
| 96 | + loop: "{{ ds_cleanup_list.resources }}" |
| 97 | + when: |
| 98 | + - item['metadata']['labels']['cdi.kubevirt.io/dataImportCron'] is defined |
| 99 | + - item['metadata']['labels']['cdi.kubevirt.io/dataImportCron'] in dic_names |
| 100 | + - item.status.conditions[0].message != "DataSource is ready to be consumed" |
| 101 | + |
| 102 | + - name: Check on removables |
| 103 | + ansible.builtin.debug: |
| 104 | + msg: |
| 105 | + - "cron_cleanups: {{ cron_cleanups }}" |
| 106 | + - "ds_cleanups: {{ ds_cleanups }}" |
| 107 | + |
| 108 | + - name: Delete datasources in cleanup list |
| 109 | + kubernetes.core.k8s: |
| 110 | + kind: datasource |
| 111 | + namespace: '{{ image_cleanup_namespace }}' |
| 112 | + api_version: cdi.kubevirt.io/v1beta1 |
| 113 | + name: "{{ item }}" |
| 114 | + state: absent |
| 115 | + loop: "{{ ds_cleanups }}" |
| 116 | + |
| 117 | + - name: Delete datavolumes in cleanup list |
| 118 | + kubernetes.core.k8s: |
| 119 | + kind: datavolume |
| 120 | + namespace: '{{ image_cleanup_namespace }}' |
| 121 | + api_version: cdi.kubevirt.io/v1beta1 |
| 122 | + label_selectors: |
| 123 | + - 'cdi.kubevirt.io/dataImportCron={{ item }}' |
| 124 | + state: absent |
| 125 | + loop: "{{ cron_cleanups }}" |
| 126 | + |
| 127 | + - name: Delete dataimportcrons in cleanup list |
| 128 | + kubernetes.core.k8s: |
| 129 | + kind: dataimportcron |
| 130 | + namespace: '{{ image_cleanup_namespace }}' |
| 131 | + api_version: cdi.kubevirt.io/v1beta1 |
| 132 | + name: "{{ item }}" |
| 133 | + state: absent |
| 134 | + loop: "{{ cron_cleanups }}" |
| 135 | + rescue: |
| 136 | + - name: Note that we exited |
| 137 | + ansible.builtin.debug: |
| 138 | + msg: "Caught an error while cleaning up dataimportcrons, exiting" |
0 commit comments