How can I automate the AutomationControllerBackup using AAP operator?
Environment
- Operator-based deployment of AAP2.x
- AAP2.x on OpenShift Platform 4.x
Issue
- Red Hat Ansible Automation Platform deployments in Red Hat OpenShift Container Platform require manual backup every time. How can we automate it?
Resolution
- Create a configmap that generates a AutomationControllerBackup CR through a script with variables that point out to the AutomationController deployment.
The following code was tested in the lab for testing purposes only. Please replace the variables as per your environment information.
apiVersion: v1
kind: ConfigMap
metadata:
name: controller-instance-backup-configmap
namespace: ansible-automation-dev
data:
script: |
#!/bin/bash
export KEEP_DAYS=5
export BACKUPS_DIR="/backups"
export DEPLOYMENT_NAME="aap-crtl-dev"
export BACKUP_PVC="aap-crtl-dev-backup"
export NAMESPACE=ansible-automation-dev
#export STORAGE_CLASS=ocs-storagecluster-cephfs
#export STORAGE_SIZE=8Gi
cat <<EOF | oc apply -f -
apiVersion: automationcontroller.ansible.com/v1beta1
kind: AutomationControllerBackup
metadata:
name: $DEPLOYMENT_NAME-backup-$(date +'%Y-%m-%d-%H%M')
namespace: $NAMESPACE
labels:
app.kubernetes.io/component: automationcontroller
app.kubernetes.io/managed-by: automationcontroller-operator
app.kubernetes.io/name: $DEPLOYMENT_NAME-backup-$(date +'%Y-%m-%d-%H%M')
app.kubernetes.io/operator-version: ""
app.kubernetes.io/part-of: $DEPLOYMENT_NAME-backup-$(date +'%Y-%m-%d-%H%M')
spec:
backup_pvc: aap-crtl-dev-backup
deployment_name: $DEPLOYMENT_NAME
# postgres_image: postgres
# postgres_image_version: "12"
# backup_pvc_namespace: $NAMESPACE
# backup_storage_class: $STORAGE_CLASS
# backup_storage_requirements: $STORAGE_SIZE
EOF
oc get AutomationControllerBackup -n $NAMESPACE -o go-template --template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' \
| sed -e 's/\(^.*\) \(....-..-..\)T.*$/\1 \2/g' \
| awk '$2 <= "'$(date -d "$(echo $KEEP_DAYS) day ago" +'%Y-%m-%d')'" { print $1 }' \
| xargs --no-run-if-empty oc delete AutomationControllerBackup -n $NAMESPACE
- Create a CronJob object that will be running the script created at the configmap object inside an image.
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: controller-instance-backup-cron
namespace: ansible-automation-dev
spec:
schedule: "0 0 * * *"
successfulJobsHistoryLimit: 0
failedJobsHistoryLimit: 1
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
metadata:
name: controller-instance-backup-exec
spec:
containers:
# https://catalog.redhat.com/software/containers/openshift4/ose-cli/5cd9ba3f5a13467289f4d51d
- name: controller-instance-backup-exec
args:
- "/bin/bash"
- "/apps/create_AutomationControllerBackup.sh"
#- "/bin/sleep"
#- "7200"
imagePullPolicy: IfNotPresent
image: openshift4/ose-cli
volumeMounts:
- name: controller-backup
mountPath: /backups
- name: controller-instance-backup-configmap
readOnly: true
mountPath: /apps/create_AutomationControllerBackup.sh
subPath: create_AutomationControllerBackup.sh
serviceAccountName: automation-controller-operator-controller-manager
volumes:
- name: controller-instance-backup-configmap
configMap:
name: controller-instance-backup-configmap
items:
- key: script
path: create_AutomationControllerBackup.sh
defaultMode: 0775
- name: controller-backup
persistentVolumeClaim:
claimName: aap-crtl-dev-backup
restartPolicy: "Never"
Note: This solution is tested and works successfully in a lab environment. Please be aware that this is not an officially supported Red Hat configuration. Red Hat Global Support Services will not provide any support or assistance with the implementation or troubleshooting of any issues arising from the use of this guide.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments