Skip to content

Commit 83afa3d

Browse files
committed
[cleanup_openstack] enhance cleanup for infrastructure reuse
Enhance the cleanup_openstack role to support infrastructure reuse by cleaning up OpenStack resources while preserving the OpenShift cluster infrastructure. This enables faster test cycles by avoiding full infrastructure reprovisioning. Changes: - Add cleanup-openstack-for-reuse.yml playbook for direct use - Add cleanup_crs_direct.yaml to delete OpenStack CRs directly from cluster - Add cleanup_openstack_api.yaml to delete OpenStack API resources (servers, networks, volumes, etc.) before CR deletion to prevent orphaned resources - Add cleanup_storage.yaml to clean up PVCs, secrets, ConfigMaps, and PVs - Add cleanup_namespaces.yaml to optionally delete empty namespaces - Add common.yaml to eliminate code duplication (kubeconfig/auth params) - Refactor CR deletion patterns to use loops, reducing code duplication - Fix execution order: API resources are deleted first while control plane is still running, then CRs are deleted - Preserve infrastructure operators (NMState, MetalLB, OLM) for cluster reuse - Add configurable variables for granular cleanup control - Update README with comprehensive documentation Related: OSPRH-21759 Signed-off-by: Roberto Alfieri <[email protected]>
1 parent ff8eb72 commit 83afa3d

File tree

12 files changed

+752
-14
lines changed

12 files changed

+752
-14
lines changed

cleanup-openstack-for-reuse.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
# Copyright Red Hat, Inc.
3+
# All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
# This playbook cleans up OpenStack resources while preserving the OpenShift
18+
# cluster infrastructure for reuse. It removes:
19+
# - All OpenStack CRs (ControlPlane, DataPlane, etc.)
20+
# - Storage resources (PVCs, secrets, ConfigMaps)
21+
# - Optionally: OpenStack API resources (servers, networks, volumes, etc.)
22+
#
23+
# Usage examples:
24+
#
25+
# Basic cleanup (removes OpenStack CRs and storage, keeps cluster):
26+
# ansible-playbook -i inventory.yml cleanup-openstack-for-reuse.yml
27+
#
28+
# Skip API resource cleanup (if needed):
29+
# ansible-playbook -i inventory.yml cleanup-openstack-for-reuse.yml \
30+
# -e cleanup_api_resources=false
31+
#
32+
# Aggressive cleanup (removes everything including namespaces):
33+
# ansible-playbook -i inventory.yml cleanup-openstack-for-reuse.yml \
34+
# -e cleanup_api_resources=true \
35+
# -e cleanup_namespaces=true \
36+
# -e force_remove_finalizers=true
37+
38+
- name: Clean OpenStack deployment for infrastructure reuse
39+
hosts: "{{ target_host | default('localhost') }}"
40+
gather_facts: true
41+
vars:
42+
# By default, clean OpenStack CRs, storage, and API resources but keep OpenShift cluster
43+
# Set to false to skip OpenStack API resource cleanup
44+
cifmw_cleanup_openstack_delete_api_resources: "{{ cleanup_api_resources | default(true) }}"
45+
# Set to true to delete namespaces (use with caution)
46+
cifmw_cleanup_openstack_delete_namespaces: "{{ cleanup_namespaces | default(false) }}"
47+
# Set to true to force remove finalizers from stuck CRs
48+
cifmw_cleanup_openstack_force_remove_finalizers: "{{ force_remove_finalizers | default(false) }}"
49+
tasks:
50+
- name: Cleanup OpenStack deployment
51+
ansible.builtin.include_role:
52+
name: cleanup_openstack
53+
54+
- name: Display cleanup summary
55+
ansible.builtin.debug:
56+
msg: >-
57+
OpenStack cleanup completed. The OpenShift cluster is now ready for reuse.
58+
59+
Cleaned resources:
60+
- OpenStack CRs (ControlPlane, DataPlane, etc.)
61+
- Storage resources (PVCs, secrets, ConfigMaps)
62+
- OpenStack API resources (servers, networks, volumes, etc.)
63+
- Artifacts and logs
64+
{% if cifmw_cleanup_openstack_delete_namespaces %}
65+
- OpenStack namespaces (if empty)
66+
{% endif %}
67+
68+
The cluster infrastructure is preserved and ready for a new deployment.

docs/dictionary/en-custom.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ buildah
5555
buildpkgs
5656
cacert
5757
cacheable
58+
certmanager
5859
catalogsource
5960
cci
6061
ccitredhat
@@ -138,6 +139,7 @@ deepscrub
138139
delorean
139140
deployer
140141
deprovision
142+
deprovisioned
141143
deps
142144
dest
143145
dev
@@ -185,6 +187,7 @@ extraRPMs
185187
ezzmy
186188
favorit
187189
fbqufbqkfbzxrja
190+
finalizers
188191
fci
189192
fdp
190193
fedoraproject
@@ -299,6 +302,7 @@ kvm
299302
lacp
300303
lajly
301304
LDAP
305+
Lifecycle
302306
ldp
303307
libguestfs
304308
libvirt
@@ -415,8 +419,12 @@ openstack
415419
openstackclient
416420
openstackcontrolplane
417421
openstackdataplane
422+
openstackdataplanedeployment
423+
OpenStackDataPlaneDeployment
418424
openstackdataplanenodeset
419425
openstackdataplanenodesets
426+
openstackdataplaneservice
427+
OpenStackDataPlaneService
420428
openstackprovisioner
421429
openstacksdk
422430
openstackversion
@@ -443,6 +451,8 @@ passwd
443451
passwordless
444452
pastebin
445453
pem
454+
persistentvolumes
455+
PersistentVolumes
446456
pkgs
447457
pki
448458
png
@@ -468,6 +478,7 @@ pubkey
468478
publicdomain
469479
pullsecret
470480
pvs
481+
PVCs
471482
pwd
472483
pxe
473484
py
@@ -491,6 +502,7 @@ readmes
491502
readthedocs
492503
reauthenticate
493504
rebaser
505+
reusability
494506
redfish
495507
redhat
496508
refspec

roles/cleanup_openstack/README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# cleanup_openstack
22

3-
Cleans up openstack resources created by CIFMW by deleting CRs
3+
Cleans up OpenStack resources created by CIFMW while preserving the OpenShift cluster infrastructure for reuse. This role removes OpenStack-specific resources (CRs, API resources, storage) but keeps infrastructure operators and cluster components intact.
44

55
## Privilege escalation
66
None
@@ -9,3 +9,68 @@ None
99
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
1010

1111
* `cifmw_cleanup_openstack_detach_bmh`: (Boolean) Detach BMH when cleaning flag, this is used to avoid deprovision when is not required. Default: `true`
12+
13+
* `cifmw_cleanup_openstack_delete_crs_direct`: (Boolean) Delete OpenStack CRs directly from cluster (not just from files). This ensures all OpenStackControlPlane, OpenStackDataPlaneDeployment, OpenStackDataPlaneNodeSet, and other CRs are removed. Default: `true`
14+
15+
* `cifmw_cleanup_openstack_delete_api_resources`: (Boolean) Delete OpenStack API resources (servers, networks, volumes, flavors, security groups, etc.) using the OpenStack client. This requires either an openstackclient pod in the cluster or openstackclient installed locally. Default: `true`
16+
17+
* `cifmw_cleanup_openstack_delete_storage`: (Boolean) Delete PVCs, secrets, ConfigMaps, and release PersistentVolumes. Default: `true`
18+
19+
* `cifmw_cleanup_openstack_delete_namespaces`: (Boolean) Delete OpenStack namespaces if they are empty. Use with caution as this will remove the namespace entirely. Default: `false`
20+
21+
* `cifmw_cleanup_openstack_force_remove_finalizers`: (Boolean) Force remove finalizers from stuck OpenStackControlPlane CRs. Use only if CRs are stuck in terminating state. Default: `false`
22+
23+
* `cifmw_cleanup_openstack_cloud_name`: (String) OpenStack cloud name to use for API cleanup. Default: `default`
24+
25+
## What gets cleaned up
26+
27+
### Always cleaned (when enabled):
28+
- OpenStack CRs (OpenStackControlPlane, OpenStackDataPlaneDeployment, OpenStackDataPlaneNodeSet, OpenStackDataPlaneService, OpenStackClient, OpenStackVersion)
29+
- Bare Metal Hosts (detached, not deprovisioned)
30+
- OpenStack deployment CRs from kustomize files
31+
- OpenStack API resources (servers, networks, volumes, flavors, security groups, etc.)
32+
- PVCs, secrets, ConfigMaps in OpenStack namespace
33+
- PersistentVolumes in Released state
34+
- Certificates and Issuers (cert-manager)
35+
- Artifacts, logs, and test directories
36+
37+
### Optionally cleaned:
38+
- Namespaces (if empty)
39+
40+
## What is preserved
41+
42+
The following infrastructure components are **NOT** deleted to preserve cluster reusability:
43+
- NMState operator (network management)
44+
- MetalLB operator (load balancing)
45+
- OLM (Operator Lifecycle Manager)
46+
- OpenShift cluster operators
47+
- Cluster-level infrastructure resources
48+
49+
## Usage
50+
51+
Basic cleanup (removes OpenStack CRs and storage, keeps OpenShift cluster):
52+
```yaml
53+
- name: Cleanup OpenStack
54+
include_role:
55+
name: cleanup_openstack
56+
```
57+
58+
Disable API resource cleanup (if needed):
59+
```yaml
60+
- name: Cleanup OpenStack without API resources
61+
include_role:
62+
name: cleanup_openstack
63+
vars:
64+
cifmw_cleanup_openstack_delete_api_resources: false
65+
```
66+
67+
Aggressive cleanup (removes everything including namespaces):
68+
```yaml
69+
- name: Aggressive cleanup
70+
include_role:
71+
name: cleanup_openstack
72+
vars:
73+
cifmw_cleanup_openstack_delete_api_resources: true
74+
cifmw_cleanup_openstack_delete_namespaces: true
75+
cifmw_cleanup_openstack_force_remove_finalizers: true
76+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
cifmw_cleanup_openstack_detach_bmh: true
2+
# Delete OpenStack CRs directly from cluster (not just from files)
3+
cifmw_cleanup_openstack_delete_crs_direct: true
4+
# Delete OpenStack API resources (servers, networks, volumes, etc.)
5+
cifmw_cleanup_openstack_delete_api_resources: true
6+
# Delete PVCs, secrets, and storage resources
7+
cifmw_cleanup_openstack_delete_storage: true
8+
# Delete namespaces if empty (use with caution)
9+
cifmw_cleanup_openstack_delete_namespaces: false
10+
# Force remove finalizers from stuck CRs
11+
cifmw_cleanup_openstack_force_remove_finalizers: false
12+
# OpenStack cloud name for API cleanup
13+
cifmw_cleanup_openstack_cloud_name: default

roles/cleanup_openstack/tasks/cleanup_crs.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
- name: Cleaning operators resources
99
kubernetes.core.k8s:
10-
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
11-
api_key: "{{ cifmw_openshift_token | default(omit) }}"
12-
context: "{{ cifmw_openshift_context | default(omit) }}"
10+
kubeconfig: "{{ _k8s_kubeconfig }}"
11+
api_key: "{{ _k8s_api_key }}"
12+
context: "{{ _k8s_context }}"
1313
state: absent
1414
src: "{{ item.stat.path }}"
1515
wait: true
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
# Copyright Red Hat, Inc.
3+
# All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
- name: Delete OpenStackControlPlane CRs
18+
kubernetes.core.k8s:
19+
kubeconfig: "{{ _k8s_kubeconfig }}"
20+
api_key: "{{ _k8s_api_key }}"
21+
context: "{{ _k8s_context }}"
22+
api_version: core.openstack.org/v1beta1
23+
kind: OpenStackControlPlane
24+
namespace: "{{ _openstack_namespace }}"
25+
state: absent
26+
wait: true
27+
wait_timeout: 600
28+
ignore_errors: true
29+
register: _delete_controlplane_result
30+
until: _delete_controlplane_result is succeeded or (_delete_controlplane_result.failed and 'not found' in (_delete_controlplane_result.msg | default('')))
31+
retries: 3
32+
delay: 30
33+
34+
- name: Wait for control plane pods to terminate
35+
kubernetes.core.k8s_info:
36+
kubeconfig: "{{ _k8s_kubeconfig }}"
37+
api_key: "{{ _k8s_api_key }}"
38+
context: "{{ _k8s_context }}"
39+
namespace: "{{ _openstack_namespace }}"
40+
kind: Pod
41+
register: _remaining_pods
42+
until: _remaining_pods.resources | length == 0 or (_remaining_pods.resources | selectattr('metadata.name', 'match', '.*(rabbitmq|galera|openstack).*') | list | length == 0)
43+
retries: 60
44+
delay: 10
45+
when: _delete_controlplane_result is succeeded
46+
47+
- name: Delete OpenStack CRs by kind
48+
kubernetes.core.k8s:
49+
kubeconfig: "{{ _k8s_kubeconfig }}"
50+
api_key: "{{ _k8s_api_key }}"
51+
context: "{{ _k8s_context }}"
52+
api_version: "{{ item.api_version }}"
53+
kind: "{{ item.kind }}"
54+
namespace: "{{ _openstack_namespace }}"
55+
state: absent
56+
wait: true
57+
wait_timeout: "{{ item.wait_timeout | default(300) }}"
58+
ignore_errors: true
59+
loop:
60+
- api_version: dataplane.openstack.org/v1beta1
61+
kind: OpenStackDataPlaneDeployment
62+
wait_timeout: 600
63+
- api_version: dataplane.openstack.org/v1beta1
64+
kind: OpenStackDataPlaneNodeSet
65+
wait_timeout: 600
66+
- api_version: dataplane.openstack.org/v1beta1
67+
kind: OpenStackDataPlaneService
68+
wait_timeout: 300
69+
- api_version: dataplane.openstack.org/v1beta1
70+
kind: OpenStackDataPlaneNode
71+
wait_timeout: 300
72+
- api_version: client.openstack.org/v1beta1
73+
kind: OpenStackClient
74+
wait_timeout: 300
75+
- api_version: core.openstack.org/v1beta1
76+
kind: OpenStackVersion
77+
wait_timeout: 300
78+
- api_version: openstack.org/v1beta1
79+
kind: OpenStack
80+
wait_timeout: 300
81+
loop_control:
82+
label: "{{ item.kind }}"
83+
84+
- name: Remove finalizers from stuck OpenStackControlPlane CRs
85+
kubernetes.core.k8s:
86+
kubeconfig: "{{ _k8s_kubeconfig }}"
87+
api_key: "{{ _k8s_api_key }}"
88+
context: "{{ _k8s_context }}"
89+
api_version: core.openstack.org/v1beta1
90+
kind: OpenStackControlPlane
91+
namespace: "{{ _openstack_namespace }}"
92+
state: patched
93+
definition:
94+
metadata:
95+
finalizers: []
96+
ignore_errors: true
97+
when: cifmw_cleanup_openstack_force_remove_finalizers | default(false)

0 commit comments

Comments
 (0)