Skip to content

Commit 568ea02

Browse files
authored
Add ownerReference to child resource on update (netbox-community#369)
A gap discovered in a backup and restore test was that the NetBox Operator child resources do not get the ownerReference set on Update. The goal of this PR is to make sure the IpAddressClaim, PrefixClaim and IpRangeClaim controllers add the ownerReference to the IpAddress, Prefix and IpRange CR when it reconciles/updates them.
1 parent 764997e commit 568ea02

File tree

87 files changed

+586
-344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+586
-344
lines changed

internal/controller/ipaddressclaim_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
192192
ipAddress.Spec.Comments = updatedIpAddressSpec.Comments
193193
ipAddress.Spec.Description = updatedIpAddressSpec.Description
194194
ipAddress.Spec.PreserveInNetbox = updatedIpAddressSpec.PreserveInNetbox
195+
err = controllerutil.SetControllerReference(o, ipAddress, r.Scheme)
196+
if err != nil {
197+
return err
198+
}
195199
return nil
196200
})
197201
if err != nil {

internal/controller/iprangeclaim_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ func (r *IpRangeClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request
147147
// update spec of IpRange object
148148
logger.V(4).Info("update iprange resource")
149149
ipRange.Spec = generateIpRangeSpec(o, ipRange.Spec.StartAddress, ipRange.Spec.EndAddress, logger)
150+
err = controllerutil.SetControllerReference(o, ipRange, r.Scheme)
151+
if err != nil {
152+
return ctrl.Result{}, err
153+
}
154+
150155
err = r.Client.Update(ctx, ipRange)
151156
if err != nil {
152157
return ctrl.Result{}, err

internal/controller/prefixclaim_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ func (r *PrefixClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request)
318318
prefix.Spec.Description = updatedPrefixSpec.Description
319319
prefix.Spec.Comments = updatedPrefixSpec.Comments
320320
prefix.Spec.PreserveInNetbox = updatedPrefixSpec.PreserveInNetbox
321+
err = controllerutil.SetControllerReference(prefixClaim, prefix, r.Scheme)
322+
if err != nil {
323+
return err
324+
}
321325
return nil
322326
}); err != nil {
323327
return ctrl.Result{}, err

kind/load-local-data-job/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ class Prefix:
350350
),
351351
Prefix(
352352
prefix="3.0.5.0/24",
353-
description="",
353+
description="chainsaw test prefixclaim-ipv4-update-ownerreference",
354354
site=None,
355355
tenant={
356356
"name": "MY_TENANT",
@@ -525,7 +525,7 @@ class Prefix:
525525
),
526526
Prefix(
527527
prefix="3.1.3.0/24",
528-
description="",
528+
description="chainsaw test ipaddressclaim-ipv4-update-ownerreference",
529529
site=None,
530530
tenant={
531531
"name": "MY_TENANT",
@@ -766,7 +766,7 @@ class Prefix:
766766
),
767767
Prefix(
768768
prefix="3.2.4.0/24",
769-
description="",
769+
description="chainsaw test iprangeclaim-ipv4-update-ownerreference",
770770
site=None,
771771
tenant={
772772
"name": "MY_TENANT",

tests/e2e/Prefix/IPv4/prefixclaim-ipv4-invalid-parentprefixselector/chainsaw-test.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
apiVersion: chainsaw.kyverno.io/v1alpha1
32
kind: Test
43
metadata:
@@ -54,4 +53,4 @@ spec:
5453
cleanup:
5554
- script:
5655
content: |
57-
kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-invalid-parentprefixselector -n $NAMESPACE
56+
kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-invalid-parentprefixselector -n $NAMESPACE

tests/e2e/Prefix/IPv4/prefixclaim-ipv4-invalid-parentprefixselector/netbox_v1_prefixclaim.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
apiVersion: netbox.dev/v1
32
kind: PrefixClaim
43
metadata:

tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/chainsaw-test.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
apiVersion: chainsaw.kyverno.io/v1alpha1
32
kind: Test
43
metadata:
@@ -84,4 +83,4 @@ spec:
8483
cleanup:
8584
- script:
8685
content: |
87-
kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-apply-update -n $NAMESPACE
86+
kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-apply-update -n $NAMESPACE

tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/netbox_v1_prefixclaim-update.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
apiVersion: netbox.dev/v1
32
kind: PrefixClaim
43
metadata:

tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-apply-update/netbox_v1_prefixclaim.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
apiVersion: netbox.dev/v1
32
kind: PrefixClaim
43
metadata:

tests/e2e/Prefix/IPv4/prefixclaim-ipv4-parentprefix-restore/chainsaw-test.yaml

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
apiVersion: chainsaw.kyverno.io/v1alpha1
32
kind: Test
43
metadata:
@@ -133,46 +132,46 @@ spec:
133132
- name: Set preserveInNetbox to false
134133
description: Set preserveInNetbox to false to clean up the NetBox test instance
135134
try:
136-
- patch:
137-
resource:
138-
apiVersion: netbox.dev/v1
139-
kind: PrefixClaim
140-
metadata:
141-
name: prefixclaim-ipv4-parentprefix-restore-1
142-
spec:
143-
preserveInNetbox: false
144-
- patch:
145-
resource:
146-
apiVersion: netbox.dev/v1
147-
kind: PrefixClaim
148-
metadata:
149-
name: prefixclaim-ipv4-parentprefix-restore-2
150-
spec:
151-
preserveInNetbox: false
152-
- assert:
153-
resource:
154-
apiVersion: netbox.dev/v1
155-
kind: Prefix
156-
metadata:
157-
name: prefixclaim-ipv4-parentprefix-restore-1
158-
status:
159-
(conditions[?type == 'Ready']):
160-
- observedGeneration: 2
161-
status: 'True'
162-
- assert:
163-
resource:
164-
apiVersion: netbox.dev/v1
165-
kind: Prefix
166-
metadata:
167-
name: prefixclaim-ipv4-parentprefix-restore-2
168-
status:
169-
(conditions[?type == 'Ready']):
170-
- observedGeneration: 2
171-
status: 'True'
135+
- patch:
136+
resource:
137+
apiVersion: netbox.dev/v1
138+
kind: PrefixClaim
139+
metadata:
140+
name: prefixclaim-ipv4-parentprefix-restore-1
141+
spec:
142+
preserveInNetbox: false
143+
- patch:
144+
resource:
145+
apiVersion: netbox.dev/v1
146+
kind: PrefixClaim
147+
metadata:
148+
name: prefixclaim-ipv4-parentprefix-restore-2
149+
spec:
150+
preserveInNetbox: false
151+
- assert:
152+
resource:
153+
apiVersion: netbox.dev/v1
154+
kind: Prefix
155+
metadata:
156+
name: prefixclaim-ipv4-parentprefix-restore-1
157+
status:
158+
(conditions[?type == 'Ready']):
159+
- observedGeneration: 2
160+
status: 'True'
161+
- assert:
162+
resource:
163+
apiVersion: netbox.dev/v1
164+
kind: Prefix
165+
metadata:
166+
name: prefixclaim-ipv4-parentprefix-restore-2
167+
status:
168+
(conditions[?type == 'Ready']):
169+
- observedGeneration: 2
170+
status: 'True'
172171
- name: Cleanup events
173172
description: Events cleanup required to fix issues with failing tests that assert the wrong Error resource
174173
cleanup:
175174
- script:
176175
content: |
177-
kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-restore-1 -n $NAMESPACE
178-
kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-restore-2 -n $NAMESPACE
176+
kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-restore-1 -n $NAMESPACE
177+
kubectl delete events --field-selector involvedObject.name=prefixclaim-ipv4-parentprefix-restore-2 -n $NAMESPACE

0 commit comments

Comments
 (0)