Skip to content

Commit 2d72948

Browse files
MoteHuesd109
andauthored
Fix idempotency when deleting CAPI clusters (#992)
* Fix idempotency when deleting CAPI clusters There is no need to run the deletion when the cluster does not exist. * Apply suggestion from @sd109 Co-authored-by: Scott Davidson <49713135+sd109@users.noreply.github.com> * Updates from review * Update checks --------- Co-authored-by: Scott Davidson <49713135+sd109@users.noreply.github.com>
1 parent 6adf198 commit 2d72948

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

roles/capi_cluster/tasks/main.yml

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,37 @@
104104
dest: "{{ capi_cluster_kubeconfig_path }}"
105105
mode: u=rw,g=,o=
106106

107-
- name: Including token-based kubeconfig tasks to generate token-based kubeconfig
108-
when: capi_cluster_service_account_enabled
109-
environment:
110-
KUBECONFIG: "{{ capi_cluster_kubeconfig_path }}"
111-
block:
112-
- name: Include token-based kubeconfig task file
113-
ansible.builtin.include_tasks: capi-cluster-service-account.yml
107+
- name: Including token-based kubeconfig tasks to generate token-based kubeconfig
108+
when: capi_cluster_service_account_enabled
109+
environment:
110+
KUBECONFIG: "{{ capi_cluster_kubeconfig_path }}"
111+
block:
112+
- name: Include token-based kubeconfig task file
113+
ansible.builtin.include_tasks: capi-cluster-service-account.yml
114114

115+
- name: Check that the cluster exists
116+
ansible.builtin.command: >-
117+
kubectl get
118+
openstackclusters.infrastructure.cluster.x-k8s.io
119+
-n {{ capi_cluster_release_namespace }}
120+
{{ capi_cluster_release_name }}
121+
--ignore-not-found true
122+
-o json
123+
changed_when: false
124+
register: cluster_check
125+
when: capi_cluster_release_state == 'absent'
115126

116-
- name: Delete CAPI cluster
127+
- name: Set fact from cluster check
128+
ansible.builtin.set_fact:
129+
cluster_details: "{{ cluster_check.stdout | from_json if cluster_check.stdout != '' else {} }}"
130+
changed_when: false
117131
when: capi_cluster_release_state == 'absent'
132+
133+
- name: Delete CAPI cluster
134+
when:
135+
- capi_cluster_release_state == 'absent'
136+
- cluster_details != {}
137+
- cluster_details['items'] | length == 1
118138
block:
119139
# Before deleting the cluster, update the annotation that indicates if volumes should be kept
120140
- name: Set volumes policy annotation

0 commit comments

Comments
 (0)