Skip to content

Commit

Permalink
Pre-create event table partitions before db backup in operator (#1443)
Browse files Browse the repository at this point in the history
Signed-off-by: jessicamack <[email protected]>
<[email protected]>
  • Loading branch information
jessicamack authored Sep 26, 2023
1 parent aee1b46 commit 31e1914
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/crd/bases/awx.ansible.com_awxbackups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ spec:
postgres_image_version:
description: PostgreSQL container image version to use
type: string
precreate_partition_hours:
description: Number of hours worth of events table partitions to precreate before backup to avoid pg_dump locks.
type: string
image_pull_policy:
description: The image pull policy
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ spec:
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:text
- urn:alm:descriptor:com.tectonic.ui:advanced
- displayName: Precreate Partition Hours
path: precreate_partition_hours
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:text
- urn:alm:descriptor:com.tectonic.ui:advanced
- urn:alm:descriptor:com.tectonic.ui:hidden
- displayName: Database Backup Label Selector
path: postgres_label_selector
x-descriptors:
Expand Down
3 changes: 3 additions & 0 deletions roles/backup/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ additional_labels: []

# Maintain some of the recommended `app.kubernetes.io/*` labels on the resource (self)
set_self_labels: true

# Number of whole hours worth of events table partitions to precreate before starting backup to avoid pg_dump locks.
precreate_partition_hours: 3
...
35 changes: 35 additions & 0 deletions roles/backup/tasks/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,41 @@
resolvable_db_host: '{{ (awx_postgres_type == "managed") | ternary(awx_postgres_host + "." + ansible_operator_meta.namespace + ".svc.cluster.local", awx_postgres_host) }}' # yamllint disable-line rule:line-length
no_log: "{{ no_log }}"

- name: Get the current resource task pod information.
k8s_info:
api_version: v1
kind: Pod
namespace: '{{ ansible_operator_meta.namespace }}'
label_selectors:
- "app.kubernetes.io/name={{ ansible_operator_meta.name }}-task"
- "app.kubernetes.io/managed-by={{ deployment_type }}-operator"
- "app.kubernetes.io/component={{ deployment_type }}"
field_selectors:
- status.phase=Running
register: awx_task_pod

- name: Set the resource pod as a variable.
set_fact:
awx_task_pod: >-
{{ awx_task_pod['resources']
| rejectattr('metadata.deletionTimestamp', 'defined')
| sort(attribute='metadata.creationTimestamp')
| first | default({}) }}
- name: Set the resource pod name as a variable.
set_fact:
awx_task_pod_name: "{{ awx_task_pod['metadata']['name'] | default('') }}"

- name: Precreate database partitions
k8s_exec:
namespace: "{{ ansible_operator_meta.namespace }}"
pod: "{{ awx_task_pod_name }}"
container: "{{ deployment_name }}-task"
command: awx-manage precreate_partitions --count='{{ precreate_partition_hours }}'
when: precreate_partition_hours > 0
register: result
changed_when: "'Created partitions for' in result.stdout"

- name: Set pg_dump command
set_fact:
pgdump: >-
Expand Down

0 comments on commit 31e1914

Please sign in to comment.