From 99fd57ce9df74e25c9b53ec13dd436a8bc10bfbc Mon Sep 17 00:00:00 2001 From: Alan Christie Date: Tue, 17 Dec 2024 07:53:29 +0000 Subject: [PATCH] fix: Shutdown now successful if namespace does not exist --- .../tasks/shutdown-stack.yaml | 65 +++++++++++++++ roles/fragalysis-stack/tasks/shutdown.yaml | 79 +------------------ 2 files changed, 69 insertions(+), 75 deletions(-) create mode 100644 roles/fragalysis-stack/tasks/shutdown-stack.yaml diff --git a/roles/fragalysis-stack/tasks/shutdown-stack.yaml b/roles/fragalysis-stack/tasks/shutdown-stack.yaml new file mode 100644 index 00000000..1b1f42cf --- /dev/null +++ b/roles/fragalysis-stack/tasks/shutdown-stack.yaml @@ -0,0 +1,65 @@ +--- + +# To 'shutdown' we remove: - +# - Database +# - Redis +# - Stack +# And then, delete... +# - Database volume +# - Media volume (optional) + +- name: Shutdown stack, database, and redis + k8s: + definition: "{{ lookup('template', '{{ item }}.yaml.j2') }}" + state: absent + wait: yes + loop: + - statefulset-stack + - statefulset-worker + - deployment-beat + - statefulset-database + - deployment-redis + +- name: Wait for database to disappear + k8s_info: + kind: Pod + name: database-0 + namespace: "{{ stack_namespace }}" + register: db_result + until: db_result.resources|length == 0 + delay: 5 + retries: "{{ (wait_timeout|int / 5)|int }}" + +- name: Wait for stack to disappear + k8s_info: + kind: Pod + name: stack-0 + namespace: "{{ stack_namespace }}" + register: stack_result + until: stack_result.resources|length == 0 + delay: 5 + retries: "{{ (wait_timeout|int / 5)|int }}" + +- name: Wait for worker to disappear + k8s_info: + kind: Pod + name: worker-0 + namespace: "{{ stack_namespace }}" + register: worker_result + until: worker_result.resources|length == 0 + delay: 5 + retries: "{{ (wait_timeout|int / 5)|int }}" + +- name: Remove database volume + k8s: + definition: "{{ lookup('template', 'pvc-database.yaml.j2') }}" + state: absent + wait: yes + when: stack_shutdown_remove_database_volume|bool + +- name: Remove media volume + k8s: + definition: "{{ lookup('template', 'pvc-media.yaml.j2') }}" + state: absent + wait: yes + when: stack_shutdown_remove_media_volume|bool diff --git a/roles/fragalysis-stack/tasks/shutdown.yaml b/roles/fragalysis-stack/tasks/shutdown.yaml index 8177c06d..fbd84f25 100644 --- a/roles/fragalysis-stack/tasks/shutdown.yaml +++ b/roles/fragalysis-stack/tasks/shutdown.yaml @@ -41,86 +41,15 @@ include_vars: file: sensitive.vault - # Assert namespace, a stateful set and database secrets exist... - - name: Get expected namespace ({{ stack_namespace }}) k8s_info: kind: Namespace name: "{{ stack_namespace }}" - register: result - - - name: Assert namespace exists - assert: - that: result.resources|length > 0 - fail_msg: Namespace '{{ stack_namespace }}' does not exist - - - name: Display ansible_date_time.iso8601_micro - debug: - var: ansible_date_time.iso8601_micro - - # To 'shutdown' we remove: - - # - Database - # - Redis - # - Stack - # And then, delete... - # - Database volume - # - Media volume (optional) - - - name: Shutdown stack, database, and redis - k8s: - definition: "{{ lookup('template', '{{ item }}.yaml.j2') }}" - state: absent - wait: yes - loop: - - statefulset-stack - - statefulset-worker - - deployment-beat - - statefulset-database - - deployment-redis - - - name: Wait for database to disappear - k8s_info: - kind: Pod - name: database-0 - namespace: "{{ stack_namespace }}" - register: db_result - until: db_result.resources|length == 0 - delay: 5 - retries: "{{ (wait_timeout|int / 5)|int }}" - - - name: Wait for stack to disappear - k8s_info: - kind: Pod - name: stack-0 - namespace: "{{ stack_namespace }}" - register: stack_result - until: stack_result.resources|length == 0 - delay: 5 - retries: "{{ (wait_timeout|int / 5)|int }}" - - - name: Wait for worker to disappear - k8s_info: - kind: Pod - name: worker-0 - namespace: "{{ stack_namespace }}" - register: worker_result - until: worker_result.resources|length == 0 - delay: 5 - retries: "{{ (wait_timeout|int / 5)|int }}" - - - name: Remove database volume - k8s: - definition: "{{ lookup('template', 'pvc-database.yaml.j2') }}" - state: absent - wait: yes - when: stack_shutdown_remove_database_volume|bool + register: ns_result - - name: Remove media volume - k8s: - definition: "{{ lookup('template', 'pvc-media.yaml.j2') }}" - state: absent - wait: yes - when: stack_shutdown_remove_media_volume|bool + - name: Shutdown (if namespace exists) + include_tasks: shutdown-stack.yaml + when: ns_result.resources | length > 0 # Done.