From 80c8d87f71c4eac4531a0892f7988e9af192452f Mon Sep 17 00:00:00 2001 From: "Christian M. Adams" Date: Wed, 31 Mar 2021 15:21:42 -0400 Subject: [PATCH] Create an event when pvc is not set to alert the user --- ansible/templates/awxbackup_crd.yml.j2 | 24 +++++++++++++++++++++++- deploy/awx-operator.yaml | 24 +++++++++++++++++++++++- deploy/crds/awxbackup_v1beta1_crd.yaml | 24 +++++++++++++++++++++++- roles/backup/handlers/main.yml | 17 ----------------- roles/backup/tasks/error_handling.yml | 16 ++++++++++++++++ roles/backup/tasks/init.yml | 24 +++++++++++++++--------- roles/backup/templates/event.yml.j2 | 17 +++++++++++++++++ watches.yaml | 1 - 8 files changed, 117 insertions(+), 30 deletions(-) delete mode 100644 roles/backup/handlers/main.yml create mode 100644 roles/backup/tasks/error_handling.yml create mode 100644 roles/backup/templates/event.yml.j2 diff --git a/ansible/templates/awxbackup_crd.yml.j2 b/ansible/templates/awxbackup_crd.yml.j2 index 5e9f9deb..d0ad49a9 100644 --- a/ansible/templates/awxbackup_crd.yml.j2 +++ b/ansible/templates/awxbackup_crd.yml.j2 @@ -22,7 +22,6 @@ spec: type: object x-kubernetes-preserve-unknown-fields: true description: Schema validation for the AWXBackup CRD - # TODO: Figure out how to require the tower_name field properties: spec: type: object @@ -30,3 +29,26 @@ spec: tower_name: description: Name of the deployment to be backed up type: string + tower_backup_pvc: + description: Name of the PVC to be used for storing the backup + type: string + tower_backup_size: + description: Size of PVC + type: string + tower_backup_storage_class: + description: Storage class to use when creating PVC for backup + type: string + tower_secret_key_secret: + description: Custom secret_key secret name + type: string + tower_admin_password_secret: + description: Custom admin_password secret name + type: string + tower_broadcast_websocket_secret: + description: Custom broadcast_websocket secret name + type: string + tower_postgres_configuration_secret: + description: Custom postgres_configuration secret name + type: string + oneOf: + - required: ["tower_name"] diff --git a/deploy/awx-operator.yaml b/deploy/awx-operator.yaml index dc4252da..8b80f713 100644 --- a/deploy/awx-operator.yaml +++ b/deploy/awx-operator.yaml @@ -550,7 +550,6 @@ spec: type: object x-kubernetes-preserve-unknown-fields: true description: Schema validation for the AWXBackup CRD - # TODO: Figure out how to require the tower_name field properties: spec: type: object @@ -558,3 +557,26 @@ spec: tower_name: description: Name of the deployment to be backed up type: string + tower_backup_pvc: + description: Name of the PVC to be used for storing the backup + type: string + tower_backup_size: + description: Size of PVC + type: string + tower_backup_storage_class: + description: Storage class to use when creating PVC for backup + type: string + tower_secret_key_secret: + description: Custom secret_key secret name + type: string + tower_admin_password_secret: + description: Custom admin_password secret name + type: string + tower_broadcast_websocket_secret: + description: Custom broadcast_websocket secret name + type: string + tower_postgres_configuration_secret: + description: Custom postgres_configuration secret name + type: string + oneOf: + - required: ["tower_name"] diff --git a/deploy/crds/awxbackup_v1beta1_crd.yaml b/deploy/crds/awxbackup_v1beta1_crd.yaml index 5e9f9deb..d0ad49a9 100644 --- a/deploy/crds/awxbackup_v1beta1_crd.yaml +++ b/deploy/crds/awxbackup_v1beta1_crd.yaml @@ -22,7 +22,6 @@ spec: type: object x-kubernetes-preserve-unknown-fields: true description: Schema validation for the AWXBackup CRD - # TODO: Figure out how to require the tower_name field properties: spec: type: object @@ -30,3 +29,26 @@ spec: tower_name: description: Name of the deployment to be backed up type: string + tower_backup_pvc: + description: Name of the PVC to be used for storing the backup + type: string + tower_backup_size: + description: Size of PVC + type: string + tower_backup_storage_class: + description: Storage class to use when creating PVC for backup + type: string + tower_secret_key_secret: + description: Custom secret_key secret name + type: string + tower_admin_password_secret: + description: Custom admin_password secret name + type: string + tower_broadcast_websocket_secret: + description: Custom broadcast_websocket secret name + type: string + tower_postgres_configuration_secret: + description: Custom postgres_configuration secret name + type: string + oneOf: + - required: ["tower_name"] diff --git a/roles/backup/handlers/main.yml b/roles/backup/handlers/main.yml deleted file mode 100644 index e6ffb379..00000000 --- a/roles/backup/handlers/main.yml +++ /dev/null @@ -1,17 +0,0 @@ ---- - -- name: Update awxbackup status - block: - - name: Set apiVersion and kind variables - set_fact: - api_version: '{{ hostvars["localhost"]["inventory_file"].split("/")[4:6] | join("/") }}' - kind: '{{ hostvars["localhost"]["inventory_file"].split("/")[6] }}' - - - name: Update error status - operator_sdk.util.k8s_status: - api_version: '{{ api_version }}' - kind: "{{ kind }}" - name: "{{ meta.name }}" - namespace: "{{ meta.namespace }}" - status: - error: "{{ error_msg }}" diff --git a/roles/backup/tasks/error_handling.yml b/roles/backup/tasks/error_handling.yml new file mode 100644 index 00000000..f3361d6c --- /dev/null +++ b/roles/backup/tasks/error_handling.yml @@ -0,0 +1,16 @@ +--- + +- name: Set apiVersion and kind variables + set_fact: + api_version: '{{ hostvars["localhost"]["inventory_file"].split("/")[4:6] | join("/") }}' + kind: '{{ hostvars["localhost"]["inventory_file"].split("/")[6] }}' + +- name: Determine the timestamp + set_fact: + now: '{{ lookup("pipe", "date +%FT%TZ") }}' + +- name: Emit ocp event with error + community.kubernetes.k8s: + kind: Event + namespace: "{{ meta.namespace }}" + template: "event.yml.j2" diff --git a/roles/backup/tasks/init.yml b/roles/backup/tasks/init.yml index 85c056fd..68fb305c 100644 --- a/roles/backup/tasks/init.yml +++ b/roles/backup/tasks/init.yml @@ -17,17 +17,23 @@ namespace: "{{ meta.namespace }}" register: provided_pvc when: - - tower_backup_pvc != '' or tower_backup_pvc is defined + - tower_backup_pvc != '' -- name: Update status - set_fact: - error_msg: "{{ tower_backup_pvc }} does not exist, please create this pvc first." - notify: "Update awxbackup status" +- name: Surface error to user + block: + - name: Set error message + set_fact: + error_msg: "{{ tower_backup_pvc }} does not exist, please create this pvc first." -- name: Fail early if pvc is defined but does not exist - fail: - msg: "{{ tower_backup_pvc }} does not exist, please create this pvc first." - when: provided_pvc.resources | length == 0 + - name: Handle error + import_tasks: error_handling.yml + + - name: Fail early if pvc is defined but does not exist + fail: + msg: "{{ tower_backup_pvc }} does not exist, please create this pvc first." + when: + - tower_backup_pvc != '' + - provided_pvc.resources | length == 0 # If tower_backup_pvc is defined, use in management-pod.yml.j2 - name: Set default pvc name diff --git a/roles/backup/templates/event.yml.j2 b/roles/backup/templates/event.yml.j2 new file mode 100644 index 00000000..ead6aea4 --- /dev/null +++ b/roles/backup/templates/event.yml.j2 @@ -0,0 +1,17 @@ +--- +apiVersion: v1 +kind: Event +metadata: + name: backup-error.{{ now }} + namespace: {{ meta.namespace }} +involvedObject: + apiVersion: awx.ansible.com/v1beta1 + kind: {{ kind }} + name: {{ meta.name }} + namespace: {{ meta.namespace }} +message: {{ error_msg }} +reason: BackupFailed +type: Warning +firstTimestamp: {{ now }} +lastTimestamp: {{ now }} +count: 1 diff --git a/watches.yaml b/watches.yaml index 3d59e606..1b294dc3 100644 --- a/watches.yaml +++ b/watches.yaml @@ -11,4 +11,3 @@ group: awx.ansible.com kind: AWXBackup role: backup - reconcilePeriod: 360m