From 65c67c5c51c2c606ec0d9baed4f71f7eef2069e7 Mon Sep 17 00:00:00 2001 From: Max Gautier Date: Fri, 13 Sep 2024 11:25:08 +0200 Subject: [PATCH] CI: use Kubernetes GC to delete kubevirt vms This leverage the Kubernetes GC to delete kubevirt VMs, by using ownerReferences, with the CI pod running the playbook as the owner. This concretely means that the control plane in our CI cluster will delete the kubevirt VMs associated with a particular ci job as soon as that pod job is deleted, which usually happens when the job terminates, (barring errors, which will be addressed in the cluster directly) Upgrade to kubevirt.io/v1 for the VirtualMachine manifests, since the alpha version is deprecated. --- .../roles/packet-ci/templates/vm.yml.j2 | 14 +++++++++++--- .../cloud_playbooks/roles/packet-ci/vars/main.yml | 8 ++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/cloud_playbooks/roles/packet-ci/templates/vm.yml.j2 b/tests/cloud_playbooks/roles/packet-ci/templates/vm.yml.j2 index 371656b73..f5f7cc02a 100644 --- a/tests/cloud_playbooks/roles/packet-ci/templates/vm.yml.j2 +++ b/tests/cloud_playbooks/roles/packet-ci/templates/vm.yml.j2 @@ -1,13 +1,21 @@ --- -apiVersion: kubevirt.io/v1alpha3 +apiVersion: kubevirt.io/v1 kind: VirtualMachine metadata: - name: "instance-{{ vm_id }}" - namespace: "{{ test_name }}" + generateName: test-vm- + namespace: {{ pod_namespace }} annotations: kubespray.com/ci.template-path: "tests/cloud_playbooks/roles/packet-ci/templates/vm.yml.j2" labels: kubevirt.io/os: {{ cloud_image }} + ci_job_id: "{{ ci_job_id }}" + ci_job_name: "{{ ci_job_name }}" + # leverage the Kubernetes GC for resources cleanup + ownerReferences: + - apiVersion: v1 + kind: Pod + name: "{{ pod_name }}" + uid: "{{ pod_uid }}" spec: running: true template: diff --git a/tests/cloud_playbooks/roles/packet-ci/vars/main.yml b/tests/cloud_playbooks/roles/packet-ci/vars/main.yml index ba448587d..103da858b 100644 --- a/tests/cloud_playbooks/roles/packet-ci/vars/main.yml +++ b/tests/cloud_playbooks/roles/packet-ci/vars/main.yml @@ -9,3 +9,11 @@ _vm_count_dict: default: 2 vm_count: "{{ _vm_count_dict[mode | d('default')] }}" + +# Get pod metadata / CI vars from environment + +ci_job_id: "{{ lookup('ansible.builtin.env', 'CI_JOB_ID', default=undefined) }}" +ci_job_name: "{{ lookup('ansible.builtin.env', 'CI_JOB_NAME', default=undefined) }}" +pod_name: "{{ lookup('ansible.builtin.env', 'POD_NAME', default=undefined) }}" +pod_uid: "{{ lookup('ansible.builtin.env', 'POD_UID', default=undefined) }}" +pod_namespace: "{{ lookup('ansible.builtin.env', 'POD_NAMESPACE', default=undefined) }}"