Custom resource status management

About custom resource status in Ansible-based Operators

Ansible-based Operators automatically update custom resource (CR) status subresources with generic information about the previous Ansible run. This includes the number of successful and failed tasks and relevant error messages as shown:

  1. status:
  2. conditions:
  3. - ansibleResult:
  4. changed: 3
  5. completion: 2018-12-03T13:45:57.13329
  6. failures: 1
  7. ok: 6
  8. skipped: 0
  9. lastTransitionTime: 2018-12-03T13:45:57Z
  10. message: 'Status code was -1 and not [200]: Request failed: <urlopen error [Errno
  11. 113] No route to host>'
  12. reason: Failed
  13. status: "True"
  14. type: Failure
  15. - lastTransitionTime: 2018-12-03T13:46:13Z
  16. message: Running reconciliation
  17. reason: Running
  18. status: "True"
  19. type: Running

Ansible-based Operators also allow Operator authors to supply custom status values with the k8s_status Ansible module, which is included in the operator_sdk.util collection. This allows the author to update the status from within Ansible with any key-value pair as desired.

By default, Ansible-based Operators always include the generic Ansible run output as shown above. If you would prefer your application did not update the status with Ansible output, you can track the status manually from your application.

Tracking custom resource status manually

You can use the operator_sdk.util collection to modify your Ansible-based Operator to track custom resource (CR) status manually from your application.

Prerequisites

  • Ansible-based Operator project created by using the Operator SDK

Procedure

  1. Update the watches.yaml file with a manageStatus field set to false:

    1. - version: v1
    2. group: api.example.com
    3. kind: <kind>
    4. role: <role>
    5. manageStatus: false
  2. Use the operator_sdk.util.k8s_status Ansible module to update the subresource. For example, to update with key test and value data, operator_sdk.util can be used as shown:

    1. - operator_sdk.util.k8s_status:
    2. api_version: app.example.com/v1
    3. kind: <kind>
    4. name: "{{ ansible_operator_meta.name }}"
    5. namespace: "{{ ansible_operator_meta.namespace }}"
    6. status:
    7. test: data
  3. You can declare collections in the meta/main.yml file for the role, which is included for scaffolded Ansible-based Operators:

    1. collections:
    2. - operator_sdk.util
  4. After declaring collections in the role meta, you can invoke the k8s_status module directly:

    1. k8s_status:
    2. ...
    3. status:
    4. key1: value1