Files
kubespray/tests/cloud_playbooks/roles/packet-ci/tasks/create-vms.yml
Arthur Outhenin-Chalandre 3311e0a296 tests: cleanup stale packet namespace automatically (#10245)
* tests: cleanup stale packet namespace automatically

Cancelled job on Gitlab can produce stale VMs as the delete playbook
will never be executed. This commits allow removing old vms by getting
all the namespace created from the same branch with an older pipeline
id.

Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@proton.ch>

* tests: cleanup stale packet namespace after 2 hours

This ensure that we don't have any packet namespace remaining for more
than 2 hours. All the jobs complete usually within 30min-1hour so 2
hours is enough to detect a stale namespace.

Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@proton.ch>

* tests: ignore vm cleanup failure

Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@proton.ch>

* tests: use pipeline_id var instead of fetching namespace for cleanup packet vm

Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@proton.ch>

---------

Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@proton.ch>
2023-06-26 00:57:08 -07:00

51 lines
1.4 KiB
YAML

---
- name: "Create CI namespace {{ test_name }} for test vms"
shell: |-
kubectl create namespace {{ test_name }} &&
kubectl label namespace {{ test_name }} cijobs=true branch="{{ branch }}" pipeline_id="{{ pipeline_id }}"
changed_when: false
- name: "Create temp dir /tmp/{{ test_name }} for CI files"
file:
path: "/tmp/{{ test_name }}"
state: directory
mode: 0755
- name: Template vm files for CI job
set_fact:
vms_files: "{{ vms_files + [lookup('ansible.builtin.template', 'vm.yml.j2') | from_yaml] }}"
vars:
vms_files: []
loop: "{{ range(1, vm_count|int + 1, 1) | list }}"
loop_control:
index_var: vm_id
- name: Start vms for CI job
kubernetes.core.k8s:
definition: "{{ item }}"
changed_when: false
loop: "{{ vms_files }}"
- name: Wait for vms to have ipaddress assigned
shell: "set -o pipefail && kubectl get vmis -n {{ test_name }} instance-{{ vm_id }} -o json | jq '.status.interfaces[].ipAddress' | tr -d '\"'"
args:
executable: /bin/bash
changed_when: false
register: vm_ips
loop: "{{ range(1, vm_count|int + 1, 1) | list }}"
loop_control:
index_var: vm_id
retries: 20
delay: 15
until:
- vm_ips.stdout | ipaddr
- name: "Create inventory for CI test in file /tmp/{{ test_name }}/inventory"
template:
src: "inventory.j2"
dest: "{{ inventory_path }}"
mode: 0644
vars:
vms: "{{ vm_ips }}"