|
| 1 | +#!/usr/bin/env ansible-playbook |
| 2 | +--- |
| 3 | +- name: Determine if we have PVC clean-up to do |
| 4 | + become: false |
| 5 | + connection: local |
| 6 | + hosts: localhost |
| 7 | + gather_facts: false |
| 8 | + vars: |
| 9 | + kubeconfig: "{{ lookup('env', 'KUBECONFIG') }}" |
| 10 | + pvc_cleanup: false |
| 11 | + image_cleanup_namespace: "openshift-virtualization-os-images" |
| 12 | + dv_namespace: edge-gitops-vms |
| 13 | + dv_remove_timeout: 1800 |
| 14 | + dv_remove_status: ["Pending"] |
| 15 | + ts_fmt: '%Y-%m-%dT%H:%M:%SZ' |
| 16 | + tasks: |
| 17 | + - name: Find default storageclass |
| 18 | + ansible.builtin.shell: | |
| 19 | + set -o pipefail |
| 20 | + oc get storageclass -o json | jq -r '.items[] | select(.metadata.annotations."storageclass.kubernetes.io/is-default-class")' |
| 21 | + register: default_sc_output |
| 22 | + changed_when: false |
| 23 | + |
| 24 | + - name: Find virtualization default storageclass |
| 25 | + ansible.builtin.shell: | |
| 26 | + set -o pipefail |
| 27 | + oc get storageclass -o json | jq -r '.items[] | select(.metadata.annotations."storageclass.kubevirt.io/is-default-virt-class")' |
| 28 | + register: default_virt_sc_output |
| 29 | + changed_when: false |
| 30 | + |
| 31 | + - name: Compare default virtualization storageclass and default storageclass to determine whether to clean PVCs |
| 32 | + block: |
| 33 | + - name: Parse results |
| 34 | + ansible.builtin.set_fact: |
| 35 | + default_sc: '{{ default_sc_output.stdout | from_json }}' |
| 36 | + default_virt_sc: '{{ default_virt_sc_output.stdout | from_json }}' |
| 37 | + |
| 38 | + - name: Commit to PVC cleanup |
| 39 | + ansible.builtin.set_fact: |
| 40 | + pvc_cleanup: true |
| 41 | + when: |
| 42 | + - default_virt_sc.metadata.name == "ocs-storagecluster-ceph-rbd-virtualization" |
| 43 | + - default_sc.metadata.name != default_virt_sc.metadata.name |
| 44 | + rescue: |
| 45 | + - name: Note that we exited |
| 46 | + ansible.builtin.debug: |
| 47 | + msg: "Caught an error before we could determine to clean up PVCs, exiting" |
| 48 | + |
| 49 | + - name: Cleanup incorrect datasourceimport images (PVCs) |
| 50 | + when: |
| 51 | + - pvc_cleanup |
| 52 | + block: |
| 53 | + - name: Find PVCs |
| 54 | + kubernetes.core.k8s_info: |
| 55 | + kind: pvc |
| 56 | + namespace: '{{ image_cleanup_namespace }}' |
| 57 | + register: pvc_cleanup_list |
| 58 | + |
| 59 | + - name: Remove stray datasource PVCs |
| 60 | + kubernetes.core.k8s: |
| 61 | + kind: pvc |
| 62 | + namespace: '{{ image_cleanup_namespace }}' |
| 63 | + name: '{{ item.metadata.name }}' |
| 64 | + state: absent |
| 65 | + loop: "{{ pvc_cleanup_list.resources }}" |
| 66 | + when: |
| 67 | + - item.spec.storageClassName != default_virt_sc.metadata.name |
| 68 | + |
| 69 | + - name: Check for stuck datavolumes |
| 70 | + kubernetes.core.k8s_info: |
| 71 | + namespace: '{{ dv_namespace }}' |
| 72 | + kind: DataVolume |
| 73 | + api_version: cdi.kubevirt.io/v1beta1 |
| 74 | + register: vm_ds |
| 75 | + |
| 76 | + - name: Remove stuck datavolume if needed |
| 77 | + kubernetes.core.k8s: |
| 78 | + name: "{{ item.metadata.name }}" |
| 79 | + namespace: "{{ item.metadata.namespace }}" |
| 80 | + kind: "{{ item.kind }}" |
| 81 | + api_version: "{{ item.apiVersion }}" |
| 82 | + state: absent |
| 83 | + when: |
| 84 | + - item.status.phase in dv_remove_status |
| 85 | + - (now(utc=true) - (item.metadata.creationTimestamp|to_datetime(ts_fmt))).seconds >= dv_remove_timeout |
| 86 | + loop: '{{ vm_ds.resources }}' |
| 87 | + |
| 88 | + rescue: |
| 89 | + - name: Note that we exited |
| 90 | + ansible.builtin.debug: |
| 91 | + msg: "Caught an error while cleaning up PVCs, exiting" |
0 commit comments