Skip to content

Commit 6c6a85a

Browse files
committed
Enable cleanup for test operator
This patch is meant to allow independent cleanup of resources created by the test operator. - Use files to manage CRs - Add cleanup tasks to remove CRs and resources created by test operator - Reduce redundant evaluations in test_operator role
1 parent a9203ec commit 6c6a85a

7 files changed

Lines changed: 260 additions & 207 deletions

File tree

clean_openstack_deployment.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
- name: Clean OpenStack deployment
22
hosts: "{{ target_host | default('localhost') }}"
33
tasks:
4+
- name: Clean up testing resources
5+
ansible.builtin.include_role:
6+
name: test_operator
7+
tasks_from: cleanup
8+
49
- name: Clean up OpenStack operators
510
vars:
611
cifmw_kustomize_deploy_keep_generated_crs: false

roles/test_operator/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ Execute tests via the [test-operator](https://openstack-k8s-operators.github.io/
1313
* `cifmw_test_operator_concurrency`: (Integer) Tempest concurrency value. NOTE: This parameter is deprecated, please use `cifmw_test_operator_tempest_concurrency` instead. Default value: `8`
1414
* `cifmw_test_operator_cleanup`: (Bool) Delete all resources created by the role at the end of the testing. Default value: `false`
1515
* `cifmw_test_operator_tempest_cleanup`: (Bool) Run tempest cleanup after test execution (tempest run) to delete any resources created by tempest that may have been left out.
16+
* `cifmw_test_operator_crs_path`: (String) The path into which the tests CRs file will be created in. Default value: `{{ cifmw_basedir | default(ansible_user_dir ~ '/ci-framework-data') }}/artifacts/test-operator-crs`
17+
* `cifmw_test_operator_log_pod_definition`: (Object) The CR definition template for creating the test log pod. Default value:
18+
```
19+
apiVersion: v1
20+
kind: Pod
21+
metadata:
22+
name: "test-operator-logs-pod-{{ run_test_fw }}-{{ test_operator_instance_name }}"
23+
namespace: "{{ cifmw_test_operator_namespace }}"
24+
spec:
25+
containers:
26+
- name: test-operator-logs-container
27+
image: "{{ cifmw_test_operator_logs_image }}"
28+
command: ["sleep"]
29+
args: ["infinity"]
30+
volumeMounts: "{{ volume_mounts }}"
31+
volumes: "{{ volumes }}"
32+
tolerations: "{{ cifmw_test_operator_tolerations | default(omit) }}"
33+
```
1634
* `cifmw_test_operator_default_groups`: (List) List of groups in the include list to search for tests to be executed. Default value: `[ 'default' ]`
1735
* `cifmw_test_operator_default_jobs`: (List) List of jobs in the exclude list to search for tests to be excluded. Default value: `[ 'default' ]`
1836
* `cifmw_test_operator_dry_run`: (Boolean) Whether test-operator should run or not. Default value: `false`

roles/test_operator/defaults/main.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ cifmw_test_operator_storage_class: "{{ cifmw_test_operator_storage_class_prefix
4242
cifmw_test_operator_delete_logs_pod: false
4343
cifmw_test_operator_privileged: true
4444
cifmw_test_operator_selinux_level: "s0:c478,c978"
45+
cifmw_test_operator_crs_path: "{{ cifmw_basedir | default(ansible_user_dir ~ '/ci-framework-data') }}/artifacts/test-operator-crs"
46+
cifmw_test_operator_log_pod_definition:
47+
apiVersion: v1
48+
kind: Pod
49+
metadata:
50+
name: "test-operator-logs-pod-{{ run_test_fw }}-{{ test_operator_instance_name }}"
51+
namespace: "{{ cifmw_test_operator_namespace }}"
52+
spec:
53+
containers:
54+
- name: test-operator-logs-container
55+
image: "{{ cifmw_test_operator_logs_image }}"
56+
command: ["sleep"]
57+
args: ["infinity"]
58+
volumeMounts: "{{ volume_mounts }}"
59+
volumes: "{{ volumes }}"
60+
tolerations: "{{ cifmw_test_operator_tolerations | default(omit) }}"
4561
# default test framework registry, namespace and tag can be overridden per test framework (tempest, tobiko, horizontest and ansibletest)
4662
cifmw_test_operator_default_registry: quay.io
4763
cifmw_test_operator_default_namespace: podified-antelope-centos9
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
- name: Delete {{ run_test_fw }}
2+
kubernetes.core.k8s:
3+
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
4+
api_key: "{{ cifmw_openshift_token | default(omit)}}"
5+
context: "{{ cifmw_openshift_context | default(omit)}}"
6+
state: absent
7+
src: "{{ cifmw_test_operator_crs_path }}/{{ test_operator_instance_name }}.yaml"
8+
wait: true
9+
wait_timeout: 600
10+
11+
- name: Delete CRD for {{ run_test_fw }}
12+
kubernetes.core.k8s:
13+
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
14+
api_key: "{{ cifmw_openshift_token | default(omit)}}"
15+
context: "{{ cifmw_openshift_context | default(omit)}}"
16+
kind: CustomResourceDefinition
17+
state: absent
18+
api_version: v1
19+
name: "{{ test_operator_crd_name }}"
20+
namespace: "{{ cifmw_test_operator_namespace }}"
21+
wait: true
22+
wait_timeout: 600
23+
24+
- name: Delete test-operator-logs-pod
25+
kubernetes.core.k8s:
26+
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
27+
api_key: "{{ cifmw_openshift_token | default(omit)}}"
28+
context: "{{ cifmw_openshift_context | default(omit)}}"
29+
state: absent
30+
api_version: v1
31+
src: "{{ cifmw_test_operator_crs_path }}/{{ test_operator_instance_name }}-log-pod.yaml"
32+
wait: true
33+
wait_timeout: 600
34+
when:
35+
- cifmw_test_operator_delete_logs_pod | bool or cifmw_test_operator_cleanup | bool
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
- name: List all CR files in the test operator CRs path
3+
ansible.builtin.find:
4+
paths: "{{ cifmw_test_operator_crs_path }}"
5+
patterns: "*.yaml"
6+
register: test_operator_cr_files
7+
8+
- name: Delete all CRs in OCP
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.path }}"
15+
wait: true
16+
wait_timeout: 600
17+
loop: "{{ test_operator_cr_files.files }}"
18+
failed_when: false
19+
20+
- name: Delete test operator CRs files
21+
ansible.builtin.file:
22+
path: "{{ item.path }}"
23+
state: absent
24+
loop: "{{ test_operator_cr_files.files }}"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
- name: Reset volumes and volume_mounts to an empty list
2+
ansible.builtin.set_fact:
3+
volumes: []
4+
volume_mounts: []
5+
6+
- name: Get information about PVCs that store the logs
7+
kubernetes.core.k8s_info:
8+
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
9+
api_key: "{{ cifmw_openshift_token | default(omit)}}"
10+
context: "{{ cifmw_openshift_context | default(omit)}}"
11+
namespace: "{{ cifmw_test_operator_namespace }}"
12+
kind: PersistentVolumeClaim
13+
label_selectors:
14+
- "instanceName={{ test_operator_instance_name }}"
15+
register: logsPVCs
16+
17+
- name: Set up volume mounts and volumes for all PVCs
18+
ansible.builtin.set_fact:
19+
volume_mounts: >
20+
{{
21+
(volume_mounts | default([])) + [{
22+
'name': "logs-volume-" ~ index,
23+
'mountPath': "/mnt/logs-{{ test_operator_instance_name }}-step-" ~ index
24+
}]
25+
}}
26+
volumes: >
27+
{{
28+
(volumes | default([])) + [{
29+
'name': "logs-volume-" ~ index,
30+
'persistentVolumeClaim': {
31+
'claimName': pvc.metadata.name
32+
}
33+
}]
34+
}}
35+
loop: "{{ logsPVCs.resources }}"
36+
loop_control:
37+
loop_var: pvc
38+
index_var: index
39+
40+
- name: Write log pod definition to file
41+
ansible.builtin.copy:
42+
content: "{{ cifmw_test_operator_log_pod_definition }}"
43+
dest: "{{ cifmw_test_operator_crs_path }}/{{ test_operator_instance_name }}-log-pod.yaml"
44+
mode: '0644'
45+
46+
- name: Start test-operator-logs-pod
47+
kubernetes.core.k8s:
48+
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
49+
api_key: "{{ cifmw_openshift_token | default(omit)}}"
50+
context: "{{ cifmw_openshift_context | default(omit)}}"
51+
state: present
52+
wait: true
53+
src: "{{ cifmw_test_operator_crs_path }}/{{ test_operator_instance_name }}-log-pod.yaml"
54+
55+
- name: Ensure that the test-operator-logs-pod is Running
56+
kubernetes.core.k8s_info:
57+
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
58+
api_key: "{{ cifmw_openshift_token | default(omit) }}"
59+
context: "{{ cifmw_openshift_context | default(omit) }}"
60+
namespace: "{{ cifmw_test_operator_namespace }}"
61+
kind: Pod
62+
name: "test-operator-logs-pod-{{ run_test_fw }}-{{ test_operator_instance_name }}"
63+
wait: true
64+
register: logs_pod
65+
until: logs_pod.resources[0].status.phase == "Running"
66+
delay: 10
67+
retries: 20
68+
69+
- name: Get logs from test-operator-logs-pod
70+
environment:
71+
KUBECONFIG: "{{ cifmw_openshift_kubeconfig }}"
72+
PATH: "{{ cifmw_path }}"
73+
vars:
74+
pod_path: mnt/logs-{{ test_operator_instance_name }}-step-{{ index }}
75+
ansible.builtin.shell: >
76+
oc cp -n {{ cifmw_test_operator_namespace }}
77+
test-operator-logs-pod-{{ run_test_fw }}-{{ test_operator_instance_name }}:{{ pod_path }}
78+
{{ cifmw_test_operator_artifacts_basedir }}
79+
loop: "{{ logsPVCs.resources }}"
80+
loop_control:
81+
index_var: index

0 commit comments

Comments
 (0)