Skip to content

Commit

Permalink
tests: cleanup stale packet namespace automatically
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
MrFreezeex committed Jun 22, 2023
1 parent a1521dc commit 8047626
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ create-packet: init-packet
$(ANSIBLE_LOG_LEVEL) \
-e @"files/${CI_JOB_NAME}.yml" \
-e test_id=$(TEST_ID) \
-e branch="$(CI_COMMIT_BRANCH)" \
-e pipeline_id="$(CI_PIPELINE_ID)" \
-e inventory_path=$(INVENTORY)

delete-packet:
ansible-playbook cloud_playbooks/delete-packet.yml -c local \
$(ANSIBLE_LOG_LEVEL) \
-e @"files/${CI_JOB_NAME}.yml" \
-e test_id=$(TEST_ID) \
-e branch="$(CI_COMMIT_BRANCH)" \
-e pipeline_id="$(CI_PIPELINE_ID)" \
-e inventory_path=$(INVENTORY)

create-vagrant:
Expand Down
28 changes: 28 additions & 0 deletions tests/cloud_playbooks/roles/packet-ci/tasks/cleanup-old-vms.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---

- name: Fetch current namespace
kubernetes.core.k8s_info:
api_version: v1
kind: Namespace
name: "{{ test_name }}"
label_selectors:
- cijobs = true
- branch = {{ branch }}
register: current_namespace

- name: Fetch a list of namespaces
kubernetes.core.k8s_info:
api_version: v1
kind: Namespace
label_selectors:
- cijobs = true
- branch = {{ branch }}
register: namespaces

- name: Delete older namespaces
command: "kubectl delete namespace {{ item.metadata.name }}"
loop: "{{ namespaces.resources }}"
when:
- |-
(item.metadata.labels.pipeline_id | int)
< (current_namespace.resources[0].metadata.labels.pipeline_id | int)
4 changes: 3 additions & 1 deletion tests/cloud_playbooks/roles/packet-ci/tasks/create-vms.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
---

- name: "Create CI namespace {{ test_name }} for test vms"
command: "kubectl create namespace {{ test_name }}"
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"
Expand Down
2 changes: 2 additions & 0 deletions tests/cloud_playbooks/roles/packet-ci/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
when:
- not vm_cleanup

- import_tasks: cleanup-old-vms.yml

- import_tasks: delete-vms.yml
when:
- vm_cleanup | default(false)

0 comments on commit 8047626

Please sign in to comment.