github.skymusic.top/operator-framework/operator-sdk@v0.8.2/doc/ansible/dev/dependent_watches.md (about)

     1  # Dependent Watches
     2  This document describes the `watchDependentResources` option in [`watches.yaml`](#Example) file. It delves into what dependent resources are, why the option is required, how it is achieved and finally gives an example.
     3  
     4  ### What are dependent resources?
     5  In most cases, an operator creates a bunch of Kubernetes resources in the cluster, that helps deploy and manage the application. For instance, the [etcd-operator](https://github.com/coreos/etcd-operator/blob/master/doc/gif/demo.gif) creates two services and a number of pods for a single `EtcdCluster` CR. In this case, all the Kubernetes resources created by the operator for a CR is defined as dependent resources.
     6  
     7  ### Why the `watchDependentResources` option?
     8  Often, an operator needs to watch dependent resources. To achieve this, a developer would set the field, `watchDependentResources` to `True` in the `watches.yaml` file. If enabled, a change in a dependent resource will trigger the reconciliation loop causing Ansible code to run.
     9  
    10  For example, since the _etcd-operator_ needs to ensure that all the pods are up and running, it needs to know when a pod changes. Enabling the dependent watches option would trigger the reconciliation loop to run. The Ansible logic needs to handle these cases and make sure that all the dependent resources are in the desired state as declared by the `CR spec`
    11  
    12  `Note: By default it is enabled when using ansible-operator`
    13  
    14  ### How is this achieved?
    15  The `ansible-operator` base image achieves this by leveraging the concept of [owner-references](https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/). Whenever a Kubernetes resource is created by Ansible code, the `ansible-operator`'s `proxy` module injects `owner-references` into the resource being created. The `owner-references` means the resource is owned by the CR for which reconciliation is taking place.
    16  
    17  Whenever the `watchDependentResources` field is enabled, the `ansible-operator` will watch all the resources owned by the CR, registering callbacks to their change events. Upon a change, the callback will enqueue a `ReconcileRequest` for the CR. The enqueued reconciliation request will trigger the `Reconcile` function of the controller which will execute the ansible logic for reconciliation.
    18  
    19  ### Example
    20  
    21  This is an example of a watches file with the `watchDependentResources` field set to `True`
    22  ```yaml
    23  
    24  - version: v1alpha1
    25    group: app.example.com
    26    kind: AppService
    27    playbook: /opt/ansible/playbook.yml
    28    maxRunnerArtifacts: 30
    29    reconcilePeriod: 5s
    30    manageStatus: False
    31    watchDependentResources: True
    32  
    33  ```