Skip to content

Commit 109056b

Browse files
committed
Allow bmh cleanup and move cleanup to role
- Allow cleanup of baremetal hosts (BMH) in OpenStack deployments. (required due to bug/change in OCP 4.18) - Move cleanup tasks to a dedicated Ansible role for better organization and reusability. - reduce cleanup time by deataching BMHs from the cluster before removing them.
1 parent 002a16f commit 109056b

7 files changed

Lines changed: 197 additions & 19 deletions

File tree

clean_openstack_deployment.yaml

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
- name: Clean OpenStack deployment
22
hosts: "{{ target_host | default('localhost') }}"
33
tasks:
4-
- name: Clean up testing resources
4+
- name: Cleanup openstack deployment
55
ansible.builtin.include_role:
6-
name: test_operator
7-
tasks_from: cleanup
8-
9-
- name: Clean up OpenStack operators
10-
vars:
11-
cifmw_kustomize_deploy_keep_generated_crs: false
12-
ansible.builtin.include_role:
13-
name: kustomize_deploy
14-
tasks_from: cleanup
15-
16-
- name: Remove logs and tests directories
17-
ansible.builtin.file:
18-
path: "{{ item }}"
19-
state: absent
20-
loop:
21-
- "/home/zuul/ci-framework-data/logs"
22-
- "/home/zuul/ci-framework-data/tests"
23-
become: true
6+
name: cleanup_openstack

docs/dictionary/en-custom.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ ddthh
131131
deepscrub
132132
delorean
133133
deployer
134+
deprovision
134135
deps
135136
dest
136137
dev

roles/cleanup_openstack/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# cleanup_openstack
2+
3+
Cleans up openstack resources created by CIFMW by deleting CRs
4+
5+
## Privilege escalation
6+
None
7+
8+
## Parameters
9+
As this role is for cleanup it utilizes default vars from other roles which can be referenced at their role readme page: kustomize_deploy, deploy_bmh
10+
11+
* `cifmw_cleanup_openstack_detach_bmh`: (Boolean) Detach BMH when cleaning flag, this is used to avoid deprovision when is not required. Default: `true`
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cifmw_cleanup_openstack_detach_bmh: true
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
- name: Ensure that kustomization files are present
3+
ansible.builtin.stat:
4+
path: "{{ item }}"
5+
loop: "{{ _crs_to_delete }}"
6+
register: _crs_to_delete_files
7+
8+
- name: Cleaning operators resources
9+
kubernetes.core.k8s:
10+
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
11+
api_key: "{{ cifmw_openshift_token | default(omit) }}"
12+
context: "{{ cifmw_openshift_context | default(omit) }}"
13+
state: absent
14+
src: "{{ item.stat.path }}"
15+
wait: true
16+
wait_timeout: 600
17+
loop: "{{ _crs_to_delete_files.results }}"
18+
register: _cleanup_results
19+
until: "_cleanup_results is success"
20+
retries: 3
21+
delay: 120
22+
when:
23+
- item.stat.exists
24+
25+
- name: Cleanup generated CRs if requested
26+
ansible.builtin.file:
27+
path: "{{ item.stat.path }}"
28+
state: absent
29+
loop: "{{ _crs_to_delete_files.results }}"
30+
when:
31+
- item.stat.exists
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This task file detaches the BMH (Bare Metal Host) resources to prevent deprovisioning them
2+
---
3+
- name: Skip deprovision for BMH
4+
when: cifmw_deploy_bmh_bm_hosts_list | length > 0
5+
block:
6+
- name: Patch bmh with detached
7+
kubernetes.core.k8s:
8+
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
9+
api_key: "{{ cifmw_openshift_token | default(omit)}}"
10+
context: "{{ cifmw_openshift_context | default(omit)}}"
11+
state: patched
12+
wait: true
13+
wait_timeout: 600
14+
api_version: metal3.io/v1alpha1
15+
kind: BareMetalHost
16+
namespace: "{{ cifmw_deploy_bmh_namespace }}"
17+
name: "{{ item }}"
18+
definition:
19+
metadata:
20+
annotations:
21+
baremetalhost.metal3.io/detached: ""
22+
loop: "{{ cifmw_deploy_bmh_bm_hosts_list }}"
23+
loop_control:
24+
label: "{{ item }}"
25+
26+
- name: Wait for operationalStatus to become detached
27+
kubernetes.core.k8s_info:
28+
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
29+
api_key: "{{ cifmw_openshift_token | default(omit)}}"
30+
context: "{{ cifmw_openshift_context | default(omit)}}"
31+
namespace: "{{ cifmw_deploy_bmh_namespace }}"
32+
kind: BareMetalHost
33+
api_version: metal3.io/v1alpha1
34+
name: "{{ item }}"
35+
retries: 60
36+
delay: 10
37+
until:
38+
- bmh_status.resources | length == 0 or bmh_status.resources[0].status.operationalStatus == 'detached'
39+
register: bmh_status
40+
loop: "{{ cifmw_deploy_bmh_bm_hosts_list }}"
41+
loop_control:
42+
label: "{{ item }}"
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
- name: Include required vars
3+
ansible.builtin.include_vars:
4+
file: "{{ item }}"
5+
loop:
6+
- roles/kustomize_deploy/defaults/main.yml
7+
- roles/deploy_bmh/defaults/main.yml
8+
9+
- name: Load architecture automation file
10+
register: _automation
11+
ansible.builtin.slurp:
12+
path: "{{ cifmw_architecture_automation_file }}"
13+
14+
- name: Prepare automation data
15+
vars:
16+
_parsed: "{{ _automation.content | b64decode | from_yaml }}"
17+
ansible.builtin.set_fact:
18+
cifmw_deploy_architecture_steps: >-
19+
{{ _parsed['vas'][cifmw_architecture_scenario] }}
20+
21+
- name: Clean up testing resources
22+
ansible.builtin.include_role:
23+
name: test_operator
24+
tasks_from: cleanup
25+
26+
- name: Set baremetal hosts facts
27+
vars:
28+
_cifmw_deploy_bmh_bm_hosts: >-
29+
{{
30+
cifmw_baremetal_hosts | default({}) | dict2items |
31+
rejectattr('key', 'in', ['crc', 'controller', 'ocp']) |
32+
items2dict
33+
}}
34+
ansible.builtin.set_fact:
35+
cifmw_deploy_bmh_bm_hosts_list: "{{ _cifmw_deploy_bmh_bm_hosts.keys() | list | default([]) }}"
36+
37+
- name: Get bmh crs
38+
ansible.builtin.find:
39+
path: "{{ cifmw_deploy_bmh_dest_dir }}"
40+
patterns: "*.yml"
41+
excludes: "bmh-secret*"
42+
register: bmh_crs
43+
44+
- name: Get bmh secrets crs
45+
ansible.builtin.find:
46+
path: "{{ cifmw_deploy_bmh_dest_dir }}"
47+
patterns: "bmh-secret*"
48+
register: bmh_secrets_crs
49+
50+
- name: Detach bmh to skip deprovisioning
51+
ansible.builtin.import_tasks: detach_bmh.yaml
52+
when: cifmw_cleanup_openstack_detach_bmh
53+
54+
- name: Delete deployment CRs
55+
vars:
56+
_stages_crs: >-
57+
{{
58+
cifmw_deploy_architecture_steps['stages'] |
59+
reverse |
60+
selectattr('build_output', 'defined') |
61+
map(attribute='build_output') |
62+
map('basename') |
63+
list
64+
}}
65+
_stages_crs_path: >-
66+
{{
67+
[cifmw_kustomize_deploy_kustomizations_dest_dir]
68+
| product(_stages_crs)
69+
| map('join', '/')
70+
| unique
71+
}}
72+
_external_dns_crs:
73+
- /home/zuul/ci-framework-data/artifacts/manifests/cifmw_external_dns/ceph-local-dns.yml
74+
- /home/zuul/ci-framework-data/artifacts/manifests/cifmw_external_dns/ceph-local-cert.yml
75+
_operators_crs:
76+
- "{{ cifmw_kustomize_deploy_nmstate_dest_file }}"
77+
- "{{ cifmw_kustomize_deploy_metallb_dest_file }}"
78+
- "{{ cifmw_kustomize_deploy_kustomizations_dest_dir }}/openstack.yaml"
79+
- "{{ cifmw_kustomize_deploy_olm_dest_file }}"
80+
_bmh_crs: >-
81+
{{
82+
bmh_crs.files |
83+
map(attribute='path') |
84+
list
85+
}}
86+
_bmh_secrets_crs: >-
87+
{{
88+
bmh_secrets_crs.files |
89+
map(attribute='path') |
90+
list
91+
}}
92+
_crs_to_delete: >-
93+
{{
94+
_external_dns_crs +
95+
_stages_crs_path +
96+
_bmh_crs +
97+
_bmh_secrets_crs +
98+
_operators_crs
99+
}}
100+
ansible.builtin.import_tasks: cleanup_crs.yaml
101+
102+
- name: Remove logs and tests directories
103+
ansible.builtin.file:
104+
path: "{{ item }}"
105+
state: absent
106+
loop:
107+
- "/home/zuul/ci-framework-data/logs"
108+
- "/home/zuul/ci-framework-data/tests"
109+
become: true

0 commit comments

Comments
 (0)