Skip to content

Commit

Permalink
fix: Shutdown now successful if namespace does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Christie committed Dec 17, 2024
1 parent 70119c5 commit 99fd57c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 75 deletions.
65 changes: 65 additions & 0 deletions roles/fragalysis-stack/tasks/shutdown-stack.yaml
Original file line number Diff line number Diff line change
@@ -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
79 changes: 4 additions & 75 deletions roles/fragalysis-stack/tasks/shutdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 99fd57c

Please sign in to comment.