Project: Operator Framework (App Definition and Development / Application Definition & Image Build)
File: retroactively-owned-resources.md

title: Adding Owner References for Existing Resources linkTitle: Retroactively Owned Resources weight: 20 Owner references are automatically injected `only during creation of resources`. Enabling owner reference injection `will not update objects` created while [owner reference injection is disabled](../advanced_options#owner-reference-injection) 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): ```yaml apiVersion: cache.example.com/v1alpha1 kind: Memcached metadata: name: example-memcached namespace: default uid: 2a94ff2b-84e0-40ce-8b5e-2b7e4d2bc0e2 ``` `kubectl edit` can be used to update the resources by hand. See below for example `ownerReference` and `annotations`. Dependent resources within the same namespace as the owning CR are tracked with the `ownerReference` field. `ownerReference` structure: apiVersion: {group}/{version} kind: {kind} name: {metadata.name} uid: {metadata.uid} Example ownerReference: ```yaml metadata: ...(snip) ownerReferences: apiVersion: cache.example.com/v1alpha1 kind: Memcached name: example-memcached 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. `annotation` structure: operator-sdk/primary-resource: {metadata.namespace}/{metadata.name} operator-sdk/primary-resource-type: {kind}.{group} 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`. Example Annotation: ```yaml metadata: ...(snip) annotations: operator-sdk/primary-resource: default/example-memcached operator-sdk/primary-resource-type: Memcached.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: ``` bash $ 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 resourcestoown (list): For each resource, specify: name namespace (if applicable) apiVersion kind ```yaml owning_resource: apiVersion: cache.example.com/v1alpha1 kind: Memcached name: example-memcached namespace: default resourcestoown: name: example-memcached-memcached namespace: default apiVersion: apps/v1 kind: Deployment name: example-memcached apiVersion: v1 kind: Namespace ``` This file can be used as-is without user adjustments. ```yaml hosts: localhost tasks: name: Import user variables include_vars: vars.yml name: Retrieve owning resource kubernetes.core.k8s_info: apiversion: "{{ owningresource.apiVersion }}" kind: "{{ owning_resource.kind }}" name: "{{ owning_resource.name }}" namespace: "{{ owning_resource.namespace }}" register: extraownerdata name: Ensure resources are owned includetasks: eachresource.yml loop: "{{ resourcestoown }}" vars: tobeowned: '{{ q("kubernetes.core.k8s", api_version=item.apiVersion, kind=item.kind, resource_name=item.name, namespace=item.namespace ).0 }}' owner_reference: apiVersion: "{{ owning_resource.apiVersion }}" kind: "{{ owning_resource.kind }}" name: "{{ owning_resource.name }}" uid: "{{ extraownerdata.resources[0].metadata.uid }}" ``` This file can be used as-is without user adjustments. ``` yaml name: Patch resource with owner reference when: tobeowned.metadata.namespace is defined tobeowned.metadata.namespace == owning_resource.namespace (tobeowned.metadata.ownerReferences is not defined) or (ownerreference not in tobe_owned.metadata.ownerReferences) kubernetes.core.k8s: state: present resource_definition: apiVersion: "{{ tobeowned.apiVersion }}" kind: "{{ tobeowned.kind }}" metadata: name: "{{ tobeowned.metadata.name }}" namespace: "{{ tobeowned.metadata.namespace }}" ownerReferences: "{{ (tobeowned.metadata.ownerReferences | default([])) + [owner_reference] }}" name: Patch resource with owner annotation when: tobeowned.metadata.namespace is not defined or tobeowned.metadata.namespace != owning_resource.namespace kubernetes.core.k8s: state: present resource_definition: apiVersion: "{{ tobeowned.apiVersion }}" kind: "{{ tobeowned.kind }}" metadata: name: "{{ tobeowned.metadata.name }}" namespace: "{{ tobeowned.metadata.namespace | default(omit)}}" annotations: operator-sdk/primary-resource: "{{ owningresource.namespace }}/{{ owningresource.name }}" operator-sdk/primary-resource-type: "{{ owningresource.kind }}.{{ owningresource.apiVersion.split('/')[0] }}" ```