diff --git a/.gitlab-ci/packet.yml b/.gitlab-ci/packet.yml index edf8ebcdbc2..b6246b6fa49 100644 --- a/.gitlab-ci/packet.yml +++ b/.gitlab-ci/packet.yml @@ -23,6 +23,14 @@ allow_failure: true extends: .packet +packet_cleanup_old: + stage: deploy-part1 + extends: .packet_periodic + script: + - cd tests + - make cleanup-packet + after_script: [] + # The ubuntu20-calico-aio jobs are meant as early stages to prevent running the full CI if something is horribly broken packet_ubuntu20-calico-aio: stage: deploy-part1 diff --git a/tests/Makefile b/tests/Makefile index 7c816d5d4b3..c9f561eee18 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -77,6 +77,10 @@ delete-packet: -e pipeline_id="$(CI_PIPELINE_ID)" \ -e inventory_path=$(INVENTORY) +cleanup-packet: + ansible-playbook cloud_playbooks/cleanup-packet.yml -c local \ + $(ANSIBLE_LOG_LEVEL) + create-vagrant: vagrant up find / -name vagrant_ansible_inventory diff --git a/tests/cloud_playbooks/cleanup-packet.yml b/tests/cloud_playbooks/cleanup-packet.yml new file mode 100644 index 00000000000..b709d6d0db6 --- /dev/null +++ b/tests/cloud_playbooks/cleanup-packet.yml @@ -0,0 +1,7 @@ +--- + +- hosts: localhost + gather_facts: no + become: true + roles: + - { role: cleanup-packet-ci } diff --git a/tests/cloud_playbooks/roles/cleanup-packet-ci/tasks/main.yml b/tests/cloud_playbooks/roles/cleanup-packet-ci/tasks/main.yml new file mode 100644 index 00000000000..9ea7c6b7c9e --- /dev/null +++ b/tests/cloud_playbooks/roles/cleanup-packet-ci/tasks/main.yml @@ -0,0 +1,15 @@ +--- + +- name: Fetch a list of namespaces + kubernetes.core.k8s_info: + api_version: v1 + kind: Namespace + label_selectors: + - cijobs = true + register: namespaces + +- name: Delete stale namespaces for more than 2 hours + command: "kubectl delete namespace {{ item.metadata.name }}" + loop: "{{ namespaces.resources }}" + when: + - (now() - (item.metadata.creationTimestamp | to_datetime("%Y-%m-%dT%H:%M:%SZ"))).total_seconds() >= 7200