---

- name: Update the MeteringConfig status
  k8s_status:
    api_version: metering.openshift.io/v1
    kind: MeteringConfig
    name: "{{ meta.name }}"
    namespace: "{{ meta.namespace }}"
    conditions: "{{ [ current_conditions ] }}"
  when: current_conditions is defined

- block:
  - name: Log event for role failure
    k8s_event:
      state: present
      name: metering-operator-failing-role-event
      namespace: "{{ meta.namespace }}"
      message: "Error reconciling: {{ ansible_failed_result.msg }}"
      reason: FailedRole
      type: Warning
      source:
        component: metering-ansible-operator
      involvedObject:
        apiVersion: metering.openshift.io
        kind: MeteringConfig
        name: "{{ meta.name }}"
        namespace: "{{ meta.namespace }}"
    when: ansible_failed_result is defined and ansible_failed_result.msg | length > 0

  - name: Fail role execution after encountering an error
    fail:
      msg: "Failing role execution after updating the MeteringConfig.Status"
  when: end_play_after_updating_status is defined and end_play_after_updating_status
