Adding Owner References for Existing Resources

    This guide will demonstrate how to retroactively set owner references for existing resources.

    A GET request to the owning resource will provide the necessary data to construct an ownerReference or an annotation.

    $ kubectl get memcacheds.cache.example.com -o yaml

    Example Response (Abbreviated):

    kubectl edit can be used to update the resources by hand. See below for example ownerReference and annotations.

    ownerReference structure:

    • apiVersion: {group}/{version}
    • kind: {kind}
    • name: {metadata.name}
    • uid: {metadata.uid}

    Example ownerReference:

    1. metadata:
    2. ...(snip)
    3. ownerReferences:
    4. - apiVersion: cache.example.com/v1alpha1
    5. kind: Memcached
    6. name: example-memcached
    7. uid: ad834522-d9a5-4841-beac-991ff3798c00

    An annotation is used instead of an ownerReference if the dependent resource is in a different namespace than the CR, or the dependent resource is a cluster level resource.

    structure:

    • operator-sdk/primary-resource: {metadata.namespace}/{metadata.name}

    NOTE: The {group} can be found by splitting the apiVersion metadata of the CR, into group and version. As an example, apiVersion: cache.example.com/v1alpha1 in the config/samples directory gives us the group cache.example.com.

    If you have many resources to update, it may be easier to use the following Ansible assets, which should be considered an example rather than an officially supported workflow.

    To use these assets, create a vars.yml as specified below and copy playbook.yml and each_resource.yml into the same directory. Execute the playbook with:

    1. $ ansible-playbook -i localhost playbook.yml

    This file should be created by the user to configure the playbook, and needs to contain:

    • owning_resource
      • apiVersion
      • kind
      • name
      • namespace
    • resources_to_own (list): For each resource, specify:
      • name
      • namespace (if applicable)
      • apiVersion
      • kind

    This file can be used as-is without user adjustments.

    1. - hosts: localhost
    2. tasks:
    3. - name: Import user variables
    4. include_vars: vars.yml
    5. - name: Retrieve owning resource
    6. community.kubernetes.k8s_info:
    7. name: "{{ owning_resource.name }}"
    8. namespace: "{{ owning_resource.namespace }}"
    9. register: extra_owner_data
    10. - name: Ensure resources are owned
    11. include_tasks: each_resource.yml
    12. loop: "{{ resources_to_own }}"
    13. vars:
    14. to_be_owned: '{{ q("community.kubernetes.k8s",
    15. api_version=item.apiVersion,
    16. kind=item.kind,
    17. resource_name=item.name,
    18. namespace=item.namespace
    19. ).0 }}'
    20. owner_reference:
    21. apiVersion: "{{ owning_resource.apiVersion }}"
    22. kind: "{{ owning_resource.kind }}"
    23. uid: "{{ extra_owner_data.resources[0].metadata.uid }}"

    This file can be used as-is without user adjustments.