From 31e1914db6fc98720a47e7e282a9f8624f0af7aa Mon Sep 17 00:00:00 2001 From: jessicamack Date: Tue, 26 Sep 2023 10:20:12 -0400 Subject: [PATCH] Pre-create event table partitions before db backup in operator (#1443) Signed-off-by: jessicamack <44379968+TheRealHaoLiu@users.noreply.github.com> --- .../crd/bases/awx.ansible.com_awxbackups.yaml | 3 ++ .../awx-operator.clusterserviceversion.yaml | 6 ++++ roles/backup/defaults/main.yml | 3 ++ roles/backup/tasks/postgres.yml | 35 +++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/config/crd/bases/awx.ansible.com_awxbackups.yaml b/config/crd/bases/awx.ansible.com_awxbackups.yaml index 1473a0d85..0fc00a2fa 100644 --- a/config/crd/bases/awx.ansible.com_awxbackups.yaml +++ b/config/crd/bases/awx.ansible.com_awxbackups.yaml @@ -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 diff --git a/config/manifests/bases/awx-operator.clusterserviceversion.yaml b/config/manifests/bases/awx-operator.clusterserviceversion.yaml index ea56ab0db..70affaaf2 100644 --- a/config/manifests/bases/awx-operator.clusterserviceversion.yaml +++ b/config/manifests/bases/awx-operator.clusterserviceversion.yaml @@ -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: diff --git a/roles/backup/defaults/main.yml b/roles/backup/defaults/main.yml index 437e60a10..255a1f011 100644 --- a/roles/backup/defaults/main.yml +++ b/roles/backup/defaults/main.yml @@ -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 ... diff --git a/roles/backup/tasks/postgres.yml b/roles/backup/tasks/postgres.yml index a4aef2ceb..0639c62a1 100644 --- a/roles/backup/tasks/postgres.yml +++ b/roles/backup/tasks/postgres.yml @@ -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: >-