Skip to content

Commit 0fe97ea

Browse files
committed
Update for PR comments
1 parent 45ac3f8 commit 0fe97ea

File tree

1 file changed

+56
-24
lines changed

1 file changed

+56
-24
lines changed

doc/ansible/dev/retroactively-owned-resources.md

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1-
# Owner References for Existing Resources
1+
# Adding Owner References for Existing Resources
22

3-
Owner references are automatically injected *only during creation of
4-
resources*. Enabling owner reference injection *will not update objects*
3+
Owner references are automatically injected `only during creation of
4+
resources`. Enabling owner reference injection `will not update objects`
55
created while [owner reference injection is
66
disabled](./advanced_options.md#turning-off-dependent-watches-and-owner-reference-injection)
7-
.
87

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

12-
### Owner References and Annotations
11+
A GET request to the owning resource will provide the necessary data to
12+
construct an `ownerReference` or an `annotation`.
13+
14+
`$ kubectl get memcacheds.cache.example.com -o yaml`
15+
16+
**Example Response (Abbreviated):**
17+
18+
```yaml
19+
apiVersion: cache.example.com/v1alpha1
20+
kind: Memcached
21+
metadata:
22+
name: example-memcached
23+
namespace: default
24+
uid: 2a94ff2b-84e0-40ce-8b5e-2b7e4d2bc0e2
25+
```
1326
14-
Dependent resources *within the same namespace as the owning CR* are
27+
`kubectl edit` can be used to update the resources by hand. See below
28+
for example `ownerReference` and `annotations`.
29+
30+
## For objects in the same namespace as the Owner (CRD)
31+
32+
Dependent resources `within the same namespace as the owning CR` are
1533
tracked with the `ownerReference` field.
1634

1735
`ownerReference` structure:
@@ -20,10 +38,9 @@ tracked with the `ownerReference` field.
2038
* name: {metadata.name}
2139
* uid: {metadata.uid}
2240

23-
**Example:**
41+
**Example ownerReference:**
2442

2543
```yaml
26-
2744
metadata:
2845
...(snip)
2946
ownerReferences:
@@ -33,38 +50,50 @@ metadata:
3350
uid: ad834522-d9a5-4841-beac-991ff3798c00
3451
```
3552

53+
## For objects which are NOT in the same namespace as the Owner (CRD)
54+
3655
An `annotation` is used instead of an `ownerReference` if the dependent
37-
resource is in a different namespace than the CR or the dependent
56+
resource is in a different namespace than the CR, or the dependent
3857
resource is a cluster level resource.
3958

4059
`annotation` structure:
41-
* operator-sdk/primary-resource: {metadata.namepace}/{metadata.name}
60+
* operator-sdk/primary-resource: {metadata.namespace}/{metadata.name}
4261
* operator-sdk/primary-resource-type: {kind}.{group}
4362

44-
*Note: <group> must be determined by splitting the apiVersion field at the "/"*
63+
**NOTE**: The {group} can be found by splitting the `apiVersion`
64+
metadata of the CR, into `group` and `version`. As an example,
65+
[this apiVersion field](https://github.com/operator-framework/operator-sdk-samples/blob/master/ansible/memcached-operator/deploy/crds/cache_v1alpha1_memcached_cr.yaml#L1)
66+
gives us the group `cache.example.com`.
67+
68+
**Example Annotation:**
4569

4670
```yaml
4771
metadata:
72+
...(snip)
4873
annotations:
4974
operator-sdk/primary-resource: default/example-memcached
5075
operator-sdk/primary-resource-type: Memcached.cache.example.com
5176
```
5277

53-
A GET request to the owning resource will provide the necessary data to
54-
construct an `ownerReference` or an `annotation`.
78+
## Migration using Ansible assets
5579

56-
`$ kubectl get memcacheds.cache.example.com -o yaml`
80+
If you have many resources to update, it may be easier to use the
81+
following Ansible assets, which **should be considered an example rather
82+
than an officially supported workflow**.
5783

58-
`kubectl edit` can be used to update the resources by hand.
84+
To use these assets, create a `vars.yml` as specified below and copy
85+
`playbook.yml` and `each_resource.yml` into the same directory. Execute
86+
the playbook with:
5987

60-
### Migration Playbook
88+
``` bash
89+
$ ansible-playbook -i localhost playbook.yml
90+
```
6191

62-
If you have many resources to update, it may be easier to use the
63-
following (unsupported) playbook.
92+
### vars.yml
6493

65-
#### vars.yml
94+
This file should be created by the user to configure the playbook, and
95+
needs to contain:
6696

67-
Users will configure the playbook by providing a `vars.yml` file which will specify:
6897
* owning_resource
6998
* apiVersion
7099
* kind
@@ -76,8 +105,6 @@ Users will configure the playbook by providing a `vars.yml` file which will spec
76105
* apiVersion
77106
* kind
78107

79-
**Example File:**
80-
81108
```yaml
82109
owning_resource:
83110
apiVersion: cache.example.com/v1alpha1
@@ -94,7 +121,10 @@ resources_to_own:
94121
apiVersion: v1
95122
kind: Namespace
96123
```
97-
#### playbook.yml
124+
125+
### playbook.yml
126+
127+
This file can be used as-is without user adjustments.
98128

99129
```yaml
100130
- hosts: localhost
@@ -127,7 +157,9 @@ resources_to_own:
127157
uid: "{{ extra_owner_data.resources[0].metadata.uid }}"
128158
```
129159

130-
#### `each_resource.yml`
160+
### `each_resource.yml`
161+
162+
This file can be used as-is without user adjustments.
131163

132164
``` yaml
133165
- name: Patch resource with owner reference

0 commit comments

Comments
 (0)