Skip to content

Commit 5220f3d

Browse files
stuggiclaude
andcommitted
[b/r] Extract GaleraRestore CR creation into template
Move inline GaleraRestore CR creation from shell to a Jinja2 template (06a-galerarestore.yaml.j2), consistent with all other restore steps. The rendered YAML is shown to the user before applying, and can be used for manual restore with oc apply -f. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7268ba4 commit 5220f3d

2 files changed

Lines changed: 45 additions & 27 deletions

File tree

docs/dev/backup-restore/restore/restore-openstack.yaml

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -497,57 +497,59 @@
497497
register: galerabackup_names
498498
changed_when: false
499499

500+
- name: Set GaleraBackup list
501+
ansible.builtin.set_fact:
502+
galerabackup_list: "{{ galerabackup_names.stdout.split() if galerabackup_names.stdout != '' else [] }}"
503+
500504
- name: Print GaleraBackup CRs found
501505
ansible.builtin.debug:
502-
msg: "GaleraBackup CRs: {{ galerabackup_names.stdout.split() if galerabackup_names.stdout != '' else 'none' }}"
506+
msg: "GaleraBackup CRs: {{ galerabackup_list if galerabackup_list | length > 0 else 'none' }}"
507+
508+
- name: Render GaleraRestore CRs
509+
ansible.builtin.template:
510+
src: "{{ playbook_dir }}/templates/06a-galerarestore.yaml.j2"
511+
dest: "{{ rendered_dir.path }}/06a-galerarestore.yaml"
512+
when: galerabackup_list | length > 0
513+
514+
- name: "Next: Create GaleraRestore CRs"
515+
ansible.builtin.debug:
516+
msg: "{{ lookup('file', rendered_dir.path + '/06a-galerarestore.yaml').splitlines() }}"
517+
when: not (auto_ack | bool) and galerabackup_list | length > 0
503518

504-
- name: Create GaleraRestore CRs
519+
- name: Apply GaleraRestore CRs
505520
ansible.builtin.shell: |
506-
BACKUP_NAME="{{ item }}"
507-
RESTORE_NAME="${BACKUP_NAME}restore"
508-
cat <<EOF | oc apply -f -
509-
apiVersion: mariadb.openstack.org/v1beta1
510-
kind: GaleraRestore
511-
metadata:
512-
name: ${RESTORE_NAME}
513-
namespace: {{ openstack_namespace }}
514-
spec:
515-
backupSource: ${BACKUP_NAME}
516-
EOF
517-
echo ${RESTORE_NAME}
518-
loop: "{{ galerabackup_names.stdout.split() }}"
519-
register: galerarestore_names
521+
oc apply -f {{ rendered_dir.path }}/06a-galerarestore.yaml
522+
register: galerarestore_apply
520523
changed_when: true
521-
when: galerabackup_names.stdout != ""
524+
when: galerabackup_list | length > 0
522525

523526
- name: Wait for GaleraRestore pods to be ready
524527
ansible.builtin.shell: |
525-
RESTORE_NAME="{{ item.stdout_lines[-1] }}"
526-
BACKUP_SOURCE=$(oc get galerarestore ${RESTORE_NAME} -n {{ openstack_namespace }} \
527-
-o jsonpath='{.spec.backupSource}')
528+
RESTORE_NAME="{{ item }}restore"
529+
BACKUP_SOURCE="{{ item }}"
528530
POD_NAME="${BACKUP_SOURCE}-restore-${RESTORE_NAME}"
529531
oc wait --for=condition=Ready pod/${POD_NAME} -n {{ openstack_namespace }} --timeout=120s
530-
loop: "{{ galerarestore_names.results }}"
532+
loop: "{{ galerabackup_list }}"
531533
changed_when: false
532-
when: galerabackup_names.stdout != ""
534+
when: galerabackup_list | length > 0
533535

534536
- name: Execute database restore for each GaleraRestore
535537
ansible.builtin.shell: |
536-
RESTORE_NAME="{{ item.stdout_lines[-1] }}"
538+
RESTORE_NAME="{{ item }}restore"
537539
{{ playbook_dir }}/../scripts/restore-galera.sh ${RESTORE_NAME} {{ backup_timestamp }} {{ openstack_namespace }}
538-
loop: "{{ galerarestore_names.results }}"
540+
loop: "{{ galerabackup_list }}"
539541
changed_when: true
540-
when: galerabackup_names.stdout != ""
542+
when: galerabackup_list | length > 0
541543

542544
- name: Print Step 7 result
543545
ansible.builtin.debug:
544546
msg: "Database restore completed"
545-
when: galerabackup_names.stdout != ""
547+
when: galerabackup_list | length > 0
546548

547549
- name: Print Step 7 skip
548550
ansible.builtin.debug:
549551
msg: "No GaleraBackup CRs found - skipping database restore"
550-
when: galerabackup_names.stdout == ""
552+
when: galerabackup_list | length == 0
551553

552554
# ========================================
553555
# Step 8: Restore RabbitMQ Credentials
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
# Step 7: Database Restore - GaleraRestore CRs
3+
# Creates a GaleraRestore CR for each GaleraBackup found in the namespace.
4+
# The mariadb-operator will create a restore pod that mounts the backup PVC.
5+
{% for backup_name in galerabackup_list %}
6+
apiVersion: mariadb.openstack.org/v1beta1
7+
kind: GaleraRestore
8+
metadata:
9+
name: {{ backup_name }}restore
10+
namespace: {{ openstack_namespace }}
11+
spec:
12+
backupSource: {{ backup_name }}
13+
{% if not loop.last %}
14+
---
15+
{% endif %}
16+
{% endfor %}

0 commit comments

Comments
 (0)